Responses
A handler returns its response. It never writes to the native response object: Dotpress builds the response, on every adapter the same way.
Return a value
The first route answers 202 with the job as JSON; the second, 204 without body.
Status, headers and cookies
ctx.res sets the metadata of the response, whatever the handler returns:
Headers and cookies are kept on error responses returned or thrown as HttpError, and dropped on unexpected errors (500).
setCookie() only applies the options you give, with path: '/' by default. For a session or any secret, set httpOnly: true (out of reach of the JavaScript of the page), secure: true (HTTPS only) and sameSite. For authentication, prefer the cookie mode of @dotpress/auth, which sets them for you.
Request cookies are parsed: ctx.req.cookies.session.
Response helpers
For anything other than plain JSON, return a helper from @dotpress/core:
Responses other than json() skip the response schema and the response filters.
Redirects are not prefixed with the base path of the app: use ctx.publicPath('/login') to redirect to a route of the app behind a proxy.
Streams
stream() sends the body as it is produced. The source is a ReadableStream, an iterable, an async iterable, or a function returning one: typically an async generator, which receives the abort signal of the request.
When the client disconnects, the signal is aborted and the generator stops: its finally blocks run, so the database cursor is released.
Server-Sent Events
sse() sends events to the browser, which reads them with EventSource. Each event is a string, or an object with data (sent as JSON when it is not a string), event, id, retry or comment:
A comment is sent every 15 seconds to keep the connection open through proxies (heartbeat option, false to disable it).
An error raised by a stream after the response has started is logged and reported to onError, and the connection is closed: the status was already sent.
Client disconnections
ctx.req.signal is aborted when the client goes away. Pass it to long operations, so that they stop instead of working for nobody:
Handlers no longer receive the Express res: return the value, and use ctx.res for the status, headers and cookies. Returning nothing now always answers 204.