From 97284935aaa67c936f7432160974790f3860f041 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Mon, 6 Jul 2026 20:19:29 -0500 Subject: [PATCH] docs: multipart/form-data uploads, bodyDuplicates, maxParts Signed-off-by: Diego Resendez --- docs/quickstart.md | 62 ++++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 1 + 2 files changed, 63 insertions(+) diff --git a/docs/quickstart.md b/docs/quickstart.md index 2f0a365..31620ca 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -307,6 +307,68 @@ to the WebSocket handshake (101/upgrade) response. | `cors` | off (same-origin only) | | `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 ### Visualize with Mermaid diff --git a/src/index.ts b/src/index.ts index 28cf546..344a578 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,6 +25,7 @@ export type { StreamEncoder } from './encoders'; export type { MeshConfig } from './app'; export type { RequestLimits } from './http'; export type { TlsOptions, CorsOptions, SecurityOptions } from './security'; +export type { UploadedFile, MultipartBody } from './multipart'; export { connectLink } from './mesh/link'; export { buildRemote, envelopeFrom } from './mesh/teacup'; export { buildManifest, createMeshControl, MESH_CONTROL_PATH } from './mesh/teapot';