docs: multipart/form-data uploads, bodyDuplicates, maxParts
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+63
No files matched your search
@@ -307,6 +307,68 @@ to the WebSocket handshake (101/upgrade) response.
|
|||||||
| `cors` | off (same-origin only) |
|
| `cors` | off (same-origin only) |
|
||||||
| `security` | ON (HSTS only when the connection is secure) |
|
| `security` | ON (HSTS only when the connection is secure) |
|
||||||
|
|
||||||
|
## 8. File uploads / multipart
|
||||||
|
|
||||||
|
A request with `Content-Type: multipart/form-data` makes `@body()` resolve to `{ fields, files }`
|
||||||
|
instead of a plain object:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Route, Post, body } from '@green-tea/core';
|
||||||
|
import type { MultipartBody } from '@green-tea/core';
|
||||||
|
|
||||||
|
@Route('/profile')
|
||||||
|
class ProfileController {
|
||||||
|
@Post('/avatar')
|
||||||
|
upload(@body() form: MultipartBody) {
|
||||||
|
const name = form.fields.name; // string | string[]
|
||||||
|
const avatar = form.files.avatar; // UploadedFile | UploadedFile[]
|
||||||
|
return { name, size: Array.isArray(avatar) ? undefined : avatar?.size };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
`UploadedFile` is `{ filename, contentType, data: Buffer, size }` — the whole file is buffered
|
||||||
|
in memory, no temp files.
|
||||||
|
|
||||||
|
> **Access note (asymmetry).** For multipart, `@body('title')` returns `undefined` — the value
|
||||||
|
> lives at `body.fields.title`, not `body.title`, unlike JSON/urlencoded where `@body('title')`
|
||||||
|
> picks the value directly. Use `@body()` and read `.fields`/`.files`, or key into the envelope
|
||||||
|
> itself with `@body('fields')` / `@body('files')`.
|
||||||
|
|
||||||
|
### Repeated fields — `bodyDuplicates`
|
||||||
|
|
||||||
|
By default, a repeated field name keeps the **last** value (`'last'`, matching urlencoded).
|
||||||
|
Set `bodyDuplicates: 'array'` on `createApp` to accumulate repeats into a `string[]` instead —
|
||||||
|
this applies to **both** urlencoded and multipart text fields:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const app = createApp({ modules: [ApiModule], bodyDuplicates: 'array' });
|
||||||
|
```
|
||||||
|
|
||||||
|
Files under a repeated field name always become an array (`UploadedFile[]`), regardless of
|
||||||
|
`bodyDuplicates`.
|
||||||
|
|
||||||
|
### Per-endpoint override
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
@Post('/upload', { duplicates: 'array' })
|
||||||
|
upload(@body() form: MultipartBody) { /* ... */ }
|
||||||
|
```
|
||||||
|
|
||||||
|
Precedence: route `duplicates` → app `bodyDuplicates` → `'last'`.
|
||||||
|
|
||||||
|
### Limits
|
||||||
|
|
||||||
|
Uploaded files are held in memory, so they're bounded by the same `maxBodyBytes` limit as any
|
||||||
|
other request body (over the limit → 413). `maxParts` (default `1000`) caps the number of
|
||||||
|
multipart parts per request:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const app = createApp({ modules: [ApiModule], limits: { maxParts: 500 } });
|
||||||
|
```
|
||||||
|
|
||||||
|
A malformed multipart body (bad boundary, missing headers, truncated part) → 400.
|
||||||
|
|
||||||
## Inspect & test the graph
|
## Inspect & test the graph
|
||||||
|
|
||||||
### Visualize with Mermaid
|
### Visualize with Mermaid
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ export type { StreamEncoder } from './encoders';
|
|||||||
export type { MeshConfig } from './app';
|
export type { MeshConfig } from './app';
|
||||||
export type { RequestLimits } from './http';
|
export type { RequestLimits } from './http';
|
||||||
export type { TlsOptions, CorsOptions, SecurityOptions } from './security';
|
export type { TlsOptions, CorsOptions, SecurityOptions } from './security';
|
||||||
|
export type { UploadedFile, MultipartBody } from './multipart';
|
||||||
export { connectLink } from './mesh/link';
|
export { connectLink } from './mesh/link';
|
||||||
export { buildRemote, envelopeFrom } from './mesh/teacup';
|
export { buildRemote, envelopeFrom } from './mesh/teacup';
|
||||||
export { buildManifest, createMeshControl, MESH_CONTROL_PATH } from './mesh/teapot';
|
export { buildManifest, createMeshControl, MESH_CONTROL_PATH } from './mesh/teapot';
|
||||||
|
|||||||
Reference in new issue
Block a user