-
Notifications
You must be signed in to change notification settings - Fork 3
feat: support file upload via application/octet-stream #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2cd3c4a
create dsl req().file() for upload single file api
PENEKhun 4980480
create fileField dsl sample.
PENEKhun 6086c75
create examples/express for testing file upload api
PENEKhun c6a537c
sample tests
PENEKhun 2d54323
add some validate fileField
PENEKhun 34bfb98
update
PENEKhun 6aae492
write example itdoc for octstream api
PENEKhun 8d8ddfe
revert package.json to original
PENEKhun 7f80225
refactor: fileField remove
PENEKhun 47336b1
test refactor
PENEKhun 873cada
clean up
PENEKhun 8ba0eb8
기본적인 문서 작성
PENEKhun 355fb76
review apply. thx rabbit~
PENEKhun a098d36
revert it
PENEKhun 91bca05
review apply. logic fix - thx rabbit~
PENEKhun 070ea67
logic enhance
PENEKhun b869307
review apply
PENEKhun a15c5ba
fix validate
PENEKhun 3b5fa8c
apply review : type specific
PENEKhun 5af6b83
fix: ensure header keys are case-insensitive (#251)
PENEKhun 042c3c5
revert package.json
PENEKhun 9e915a9
fix logic error
PENEKhun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| sidebar_position: 4 | ||
| --- | ||
|
|
||
| # Working with File APIs | ||
|
|
||
| > This guide explains how to test APIs that upload or download files. | ||
|
|
||
| ## Single File Upload (Binary Body) | ||
|
|
||
| `itdoc` handles binary uploads through the `req().file()` DSL. You cannot combine `req().file()` with `req().body()` in the same request. | ||
|
|
||
| **Supported signatures** | ||
|
|
||
| - `req().file("description", { path: string, filename?, contentType? })` | ||
| - `req().file("description", { buffer: Buffer, filename?, contentType? })` | ||
| - `req().file("description", { stream: Readable, filename?, contentType? })` | ||
|
|
||
| Choose exactly one of `path`, `buffer`, or `stream` to supply the file. The default `contentType` is `application/octet-stream`. | ||
|
|
||
| ```ts title="Upload via file path" | ||
| await apiDoc | ||
| .test() | ||
| .req() | ||
| .file("File to upload", { | ||
| path: path.join(__dirname, "fixtures/sample.bin"), | ||
| }) | ||
| .res() | ||
| .status(HttpStatus.CREATED) | ||
| ``` | ||
|
|
||
| ```ts title="Upload via stream" | ||
| await apiDoc | ||
| .test() | ||
| .req() | ||
| .file("File to upload", { | ||
| stream: fs.createReadStream(filePath), | ||
| filename: "sample.bin", | ||
| contentType: "application/pdf", | ||
| }) | ||
| .res() | ||
| .status(HttpStatus.CREATED) | ||
| ``` | ||
|
|
||
| ```ts title="Upload via buffer" | ||
| await apiDoc | ||
| .test() | ||
| .req() | ||
| .file("File to upload", { | ||
| buffer: fs.readFileSync(filePath), | ||
| filename: "sample.bin", | ||
| }) | ||
| .res() | ||
| .status(HttpStatus.CREATED) | ||
| ``` | ||
|
|
||
| :::tip | ||
| Calling `.file()` without a source sends an empty body with only the `Content-Type` header set. This is handy when you need to assert a failure path. | ||
|
|
||
| ```js | ||
| itDoc("fail when no file is provided", () => { | ||
| return apiDoc | ||
| .test() | ||
| .req() | ||
| .file() | ||
| .res() | ||
| .status(HttpStatus.BAD_REQUEST) | ||
| .body({ | ||
| error: field("Error message", "No file uploaded"), | ||
| }) | ||
| }) | ||
| ``` | ||
| ::: | ||
|
|
||
| ## Multipart Upload | ||
|
|
||
| > Not supported yet. |
2 changes: 1 addition & 1 deletion
2
itdoc-doc/i18n/ko/docusaurus-plugin-content-docs/current/guides/configuration.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 4 | ||
| sidebar_position: 9999 | ||
| toc_max_heading_level: 4 | ||
| --- | ||
|
|
||
|
|
||
79 changes: 79 additions & 0 deletions
79
...18n/ko/docusaurus-plugin-content-docs/current/guides/file-related-api-guide.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| --- | ||
| sidebar_position: 4 | ||
| --- | ||
|
|
||
| # 파일 관련 API 다루기 | ||
|
|
||
| > 이 가이드는 파일 업로드/다운로드와 같은 API를 테스트하는 방법을 설명합니다. | ||
|
|
||
| ## 단일 파일 업로드 (Binary Body) | ||
|
|
||
| `itdoc`는 단일 바이너리 업로드를 `req().file()`를 통해 할 수 있습니다. | ||
| 이때, JSON 본문을 설정하는 `req().body()`와 동시에 사용할 수 없습니다. | ||
|
|
||
| **지원 시그니처** | ||
|
|
||
| - `req().file("설명", { path: string, filename?, contentType? })` | ||
| - `req().file("설명", { buffer: Buffer, filename?, contentType? })` | ||
| - `req().file("설명", { stream: Readable, filename?, contentType? })` | ||
|
|
||
| `path` · `buffer` · `stream` 중 하나를 선택해 파일을 전달할 수 있습니다. | ||
| `contentType` 기본값은 `application/octet-stream`입니다. | ||
|
|
||
| ```ts title="파일 경로 업로드" | ||
| await apiDoc | ||
| .test() | ||
| .req() | ||
| .file("업로드할 파일", { | ||
| path: path.join(__dirname, "fixtures/sample.bin"), | ||
| }) | ||
| .res() | ||
| .status(HttpStatus.CREATED) | ||
| ``` | ||
|
|
||
| ```ts title="스트림 업로드" | ||
| await apiDoc | ||
| .test() | ||
| .req() | ||
| .file("업로드할 파일", { | ||
| stream: fs.createReadStream(filePath), | ||
| filename: "sample.bin", | ||
| contentType: "application/pdf", | ||
| }) | ||
| .res() | ||
| .status(HttpStatus.CREATED) | ||
| ``` | ||
|
|
||
| ```ts title="버퍼 업로드" | ||
| await apiDoc | ||
| .test() | ||
| .req() | ||
| .file("업로드할 파일", { | ||
| buffer: fs.readFileSync(filePath), | ||
| filename: "sample.bin", | ||
| }) | ||
| .res() | ||
| .status(HttpStatus.CREATED) | ||
| ``` | ||
|
|
||
| :::tip | ||
| `.file()`만 호출하고 소스를 생략하면 `Content-Type`만 설정된 채 빈 본문이 전송됩니다. 업로드 실패 케이스를 검증할 때 활용할 수 있습니다. | ||
|
|
||
| ```js | ||
| itDoc("업로드할 파일을 지정하지 않으면 400에러가 뜬다", () => { | ||
| return apiDoc | ||
| .test() | ||
| .req() | ||
| .file() | ||
| .res() | ||
| .status(HttpStatus.BAD_REQUEST) | ||
| .body({ | ||
| error: field("에러 메세지", "No file uploaded") | ||
| }) | ||
| } | ||
| ``` | ||
| ::: | ||
|
|
||
| ## Multipart 파일 업로드 | ||
|
|
||
| > 아직 지원하지 않습니다. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.