|
| 1 | +import Tabs from '@theme/Tabs'; |
| 2 | +import TabItem from '@theme/TabItem'; |
| 3 | + |
| 4 | +# multipart/form-data |
| 5 | + |
| 6 | +ts-rest supports multipart/form-data requests, this is useful for uploading files or working with FormData from a form. |
| 7 | + |
| 8 | +## Contract |
| 9 | + |
| 10 | +The contract implementation is the same as any other mutation, however, `contentType` must be set to `multipart/form-data` and the `body` must be a `FormData-compatible` object (one level deep, no weird nested structures!). |
| 11 | + |
| 12 | +```ts |
| 13 | +const c = initContract(); |
| 14 | + |
| 15 | +export const postsApi = c.router({ |
| 16 | + updatePostThumbnail: { |
| 17 | + method: 'POST', |
| 18 | + path: '/posts/:id/thumbnail', |
| 19 | + contentType: 'multipart/form-data', // <- Only difference |
| 20 | + body: c.body<{ thumbnail: File }>(), // <- Use File type in here |
| 21 | + responses: { |
| 22 | + 200: z.object({ |
| 23 | + uploadedFile: z.object({ |
| 24 | + name: z.string(), |
| 25 | + size: z.number(), |
| 26 | + type: z.string(), |
| 27 | + }), |
| 28 | + }), |
| 29 | + 400: z.object({ |
| 30 | + message: z.string(), |
| 31 | + }), |
| 32 | + }, |
| 33 | + }, |
| 34 | +}); |
| 35 | +``` |
| 36 | + |
| 37 | +## Client |
| 38 | + |
| 39 | +If your query utilizes multipart/form-data, ts-rest allows you to choose from FormData or a type safe object, the latter is recommended in most cases, just make sure you don't make a nested object. |
| 40 | + |
| 41 | +```ts |
| 42 | +// client.ts |
| 43 | + |
| 44 | +const App = () => { |
| 45 | + const [thumbnail, setThumbnail] = React.useState<File | null>(null); |
| 46 | + |
| 47 | + return ( |
| 48 | + <div> |
| 49 | + <input |
| 50 | + multiple={false} |
| 51 | + type="file" |
| 52 | + onChange={(e) => setThumbnail(e.target.files?.[0] || null)} |
| 53 | + /> |
| 54 | + <button |
| 55 | + onClick={() => { |
| 56 | + if (file) { |
| 57 | + apiClient.uploadFile({ |
| 58 | + body: { |
| 59 | + thumbnail: file, // <- typed body with "File" type |
| 60 | + }, |
| 61 | + }); |
| 62 | + } |
| 63 | + }} |
| 64 | + > |
| 65 | + Upload |
| 66 | + </button> |
| 67 | + </div> |
| 68 | + ); |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +## Server - Express |
| 73 | + |
| 74 | +With Express it is recommend to use the `multer` package to handle the multipart/form-data requests. |
| 75 | + |
| 76 | +ts-rest offers some nice types to help with this, however, we're leaving this up to you to implement with middleware outside of ts-rest. |
| 77 | + |
| 78 | +- `file` is typed as `unknown` <- BYO middleware |
| 79 | +- `files` is typed as `unknown` <- BYO middleware |
| 80 | +- `body` has had any `File` types removed (so other types are still there) |
| 81 | + |
| 82 | +```ts |
| 83 | +const s = initServer(); |
| 84 | + |
| 85 | +const postsRouter = s.router(postsApi, { |
| 86 | + updatePostThumbnail: async ({ file, files, body }) => { |
| 87 | + const thumbnail = file as Express.Multer.File; |
| 88 | + |
| 89 | + return { |
| 90 | + status: 200, |
| 91 | + body: { |
| 92 | + message: `File ${thumbnail.originalname} successfully!`, |
| 93 | + }, |
| 94 | + }; |
| 95 | + }, |
| 96 | +}); |
| 97 | + |
| 98 | +const app = express(); |
| 99 | + |
| 100 | +app.use(cors()); |
| 101 | + |
| 102 | +// File upload |
| 103 | +app.post(postsApi.updatePostThumbnail.path, upload.single('thumbnail')); |
| 104 | +``` |
| 105 | + |
| 106 | +## Server - Nest |
| 107 | + |
| 108 | +With Nest this is a pretty simple implementation, due to the extensible Decorator driven approach of Nest, you're able to utilize you're favourite multipart/form-data middleware, in this case we're following <a href="https://docs.nestjs.com/techniques/file-upload">https://docs.nestjs.com/techniques/file-upload</a> from Nest. |
| 109 | + |
| 110 | +- `body` has had any `File` types removed (so other types are still there) |
| 111 | + |
| 112 | +```ts |
| 113 | +// nest |
| 114 | +@Controller() |
| 115 | +export class AppController implements ControllerShape { |
| 116 | + @Api(s.route.updateUserAvatar) |
| 117 | + @UseInterceptors(FileInterceptor('avatar')) |
| 118 | + async updateUserAvatar( |
| 119 | + @ApiDecorator() { params: { id } }: RouteShape['updateUserAvatar'], |
| 120 | + @UploadedFile() avatar: Express.Multer.File |
| 121 | + ) { |
| 122 | + return { |
| 123 | + status: 200 as const, |
| 124 | + body: { |
| 125 | + message: `Updated user ${id}'s avatar with ${avatar.originalname}`, |
| 126 | + }, |
| 127 | + }; |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
0 commit comments