Skip to content

Commit

Permalink
Merge pull request #53 from ts-spec/49-question-how-to-upload-multipl…
Browse files Browse the repository at this point in the history
…e-files

feat: support multiple file upload
  • Loading branch information
hyeonss0417 authored Jun 8, 2024
2 parents 4fe032e + 6551662 commit 4ff4b25
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 20 deletions.
6 changes: 3 additions & 3 deletions docs/.vitepress/cache/deps/_metadata.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"hash": "b15f0de5",
"browserHash": "a6d15481",
"hash": "0e3be5e7",
"browserHash": "01d499ed",
"optimized": {
"vue": {
"src": "../../../node_modules/vue/dist/vue.runtime.esm-bundler.js",
"file": "vue.js",
"fileHash": "7d5294c0",
"fileHash": "ef88558e",
"needsInterop": false
}
},
Expand Down
1 change: 1 addition & 0 deletions docs/guide/describing-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ The following utility types are provided by Tspec.
| `Tspec.PasswordString` | Password string (e.g. `password`) |
| `Tspec.ByteString` | Byte string (e.g. `dGVzdA==`) |
| `Tspec.BinaryString` | Binary string (e.g. `0x1234`) |
| `Tspec.BinaryStringArray` | Binary string array (e.g. `[0x1234, 0x5678]`) |
| `Tspec.EmailString` | Email string (e.g. `[email protected]`) |
| `Tspec.UuidString` | UUID string (e.g. `123e4567-e89b-12d3-a456-426614174000`) |
| `Tspec.UrlString` | URL string (e.g. `http://localhost`) |
Expand Down
21 changes: 21 additions & 0 deletions docs/guide/file-upload-download.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,24 @@ paths:

Then, you can download the file in Swagger UI.
![File Download](/assets/images/file-download.png)

## Multiple File Uploads

If you want to upload multiple files, you can use `Tspec.BinaryStringArray` type.
```ts{6-9}
export type FileApiSpec = Tspec.DefineApiSpec<{
paths: {
'/files/multiple-upload': {
post: {
summary: 'Upload Files',
/** @mediaType multipart/form-data */
body: {
files: Tspec.BinaryStringArray;
},
responses: { 200: { fileNames: string[] } },
},
},
},
}>;
```

57 changes: 57 additions & 0 deletions examples/file-upload-download-example/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,63 @@
}
}
}
},
"/files/multiple-upload": {
"post": {
"operationId": "FileApiSpec_post_/multiple-upload",
"tags": [
"File"
],
"summary": "Upload Multiple Files",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"files": {
"items": {
"type": "string",
"format": "binary"
},
"type": "array"
}
},
"additionalProperties": false,
"required": [
"files"
]
}
}
}
},
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"fileNames": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false,
"required": [
"fileNames"
]
}
}
}
}
}
}
}
},
"components": {
Expand Down
2 changes: 1 addition & 1 deletion examples/file-upload-download-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"author": "Hyeonss",
"license": "MIT",
"dependencies": {
"tspec": "^0.1.108"
"tspec": "^0.1.114"
}
}
10 changes: 10 additions & 0 deletions examples/file-upload-download-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export type FileApiSpec = Tspec.DefineApiSpec<{
},
},
},
'/multiple-upload': {
post: {
summary: 'Upload Multiple Files',
/** @mediaType multipart/form-data */
body: {
files: Tspec.BinaryStringArray;
},
responses: { 200: { fileNames: string[] } },
},
}
},
}>;

Expand Down
30 changes: 15 additions & 15 deletions examples/file-upload-download-example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,10 @@ ts-node@^10.9.1:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tspec@^0.1.108:
version "0.1.108"
resolved "https://registry.yarnpkg.com/tspec/-/tspec-0.1.108.tgz#0eea77a0b1a203f3f97d799df1d114449713099f"
integrity sha512-VVwugw/FeM1ENlgLPr/bBEQ3X8GFrW4lM0DwDSuEkIkJs0V0MJWIlQSdlSBo+yx7tEdbT657GJyVWS6vGiM4OQ==
tspec@^0.1.114:
version "0.1.114"
resolved "https://registry.yarnpkg.com/tspec/-/tspec-0.1.114.tgz#f74120c40eda6d710510b7e3d9f89a9a21972df1"
integrity sha512-85m8beD+E09BX/B4vsf55X88f7SqdvRsvyteYE388Ek2fcirfIDfBGWc7oKkRJGjWIOGhbDawwr8IXWD/5kXDw==
dependencies:
debug "^4.3.4"
express "^4.18.2"
Expand All @@ -818,8 +818,8 @@ tspec@^0.1.108:
json-schema-to-openapi-schema "^0.4.0"
openapi-types "^12.0.2"
swagger-ui-express "^4.6.2"
typescript "~4.9.5"
typescript-json-schema "^0.57.0"
typescript "~5.1.0"
typescript-json-schema "^0.62.0"
yargs "^17.7.1"

type-is@~1.6.18:
Expand All @@ -830,24 +830,24 @@ type-is@~1.6.18:
media-typer "0.3.0"
mime-types "~2.1.24"

typescript-json-schema@^0.57.0:
version "0.57.0"
resolved "https://registry.yarnpkg.com/typescript-json-schema/-/typescript-json-schema-0.57.0.tgz#e87e16355ecf942762a7cea224e4f5bf6f65d1c9"
integrity sha512-xirQK0s95K3OL7Knve0OOxQHErByCxUsgXr5oCHa9J0jWgiA+9/tidHWPu1UMjRv7MoX5ViP6QmOrWCi+dvRew==
typescript-json-schema@^0.62.0:
version "0.62.0"
resolved "https://registry.yarnpkg.com/typescript-json-schema/-/typescript-json-schema-0.62.0.tgz#774b06b0c9d86d7f3580ea9136363a6eafae1470"
integrity sha512-qRO6pCgyjKJ230QYdOxDRpdQrBeeino4v5p2rYmSD72Jf4rD3O+cJcROv46sQukm46CLWoeusqvBgKpynEv25g==
dependencies:
"@types/json-schema" "^7.0.9"
"@types/node" "^16.9.2"
glob "^7.1.7"
path-equal "^1.2.5"
safe-stable-stringify "^2.2.0"
ts-node "^10.9.1"
typescript "~4.9.5"
typescript "~5.1.0"
yargs "^17.1.1"

typescript@~4.9.5:
version "4.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.9.5.tgz#095979f9bcc0d09da324d58d03ce8f8374cbe65a"
integrity sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==
typescript@~5.1.0:
version "5.1.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274"
integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==

[email protected], unpipe@~1.0.0:
version "1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/tspec/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tspec",
"description": "Generate OpenAPI 3.0 spec from TypeScript code.",
"version": "0.1.113",
"version": "0.1.114",
"main": "dist/index.js",
"type": "module",
"typings": "dist/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions packages/tspec/src/types/tspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ export namespace Tspec {
*/
export type BinaryString = string;

/**
* @items.type string
* @items.format binary
*/
export type BinaryStringArray = string[];

/**
* @TJS-format email
* @examples ["[email protected]"]
Expand Down

0 comments on commit 4ff4b25

Please sign in to comment.