Request
Everything the spongReq object gives you: params, query, headers and the request body.
Every handler and middleware receives spongReq. It wraps the standard Fetch Request and adds what you need most.
| Property | Type | Description |
|---|---|---|
request | Request | The original Fetch request. |
method | string | HTTP method, for example GET. |
url | URL | The full parsed URL. |
path | string | The pathname, without the query string. |
params | Record<string, string> | Route parameters, already decoded. |
query | URLSearchParams | The query string. |
headers | Headers | Request headers. |
user | { id: string; [key: string]: unknown } | Set by AuthMiddleware after a valid token. It holds whatever your verifier returned. |
auth | { token: string } | Set together with user. |
Reading the body
| Method | Returns | Notes |
|---|---|---|
json<T>() | Promise<T> | The Content-Type must be application/json or a +json type. |
formData() | Promise<FormData> | Needs a Content-Type header. Uploaded files arrive as File objects. |
text() | Promise<string> | The raw body as text. |
TypeScript
bob.spongPost('/users', async (spongReq, spongRes) => { const body = await spongReq.json<{ name: string }>() return spongRes.json({ created: body.name })})What can go wrong
Body problems are reported as HttpError, so the client always gets a clear JSON answer.
| Situation | Response |
|---|---|
json() on a request that is not JSON | 415 Unsupported Media Type |
| The body is not valid JSON | 400 Invalid JSON body |
formData() without a Content-Type | 400 Missing Content-Type |
| The form data cannot be parsed | 400 Invalid form data |
| The body is larger than 10 MiB | 413 Content Too Large |
An invalid Content-Length header | 400 Invalid Content-Length |