Skip to content
rest-rpc
Esc
↑↓navigate↵open⌘Jpreview
On this page

v0.4.0

core:

  • BodyValue types the chunks of streams a serializer returns, as ReadableStream<Uint8Array> or AsyncIterable<string | Uint8Array>. Other chunks already failed when the response was sent, after its headers, and are now a type error in custom codecs. (7e3354b)

core:

  • A .response() for a status the route already defines is a type error. So is a .response() status outside 200 to 299. Before, both only threw when the route was defined. (1b88943)

core:

  • An error response without a string code, such as a proxy’s error page, rejects with an Error whose cause holds the response as the client received it: its status, headers, cookies and decoded body. Before, only the status was kept, in the message. (30ef141)

core:

  • OpenAPI documents a defined request body as required. Before, requestBody.required was never set, so generated SDKs made every body optional. (1b88943)

core:

  • OpenAPI documents array query parameters as name[] with style: form and explode: true, the way the client sends them and the server reads them. (c56f8b6)

core:

  • Replace the client’s baseUrl option with origin and mountPath, which are both optional. A browser app served from the same origin as its routes needs no options, and mountPath is the same as the server’s. Outside browsers, a call without origin rejects with a TypeError. A trailing slash of origin is ignored, and an origin with a path throws a TypeError when the client is created. Migrate baseUrl: "https://api.example.com/api" to origin: "https://api.example.com", mountPath: "/api". (81f58db)

core, server:

  • errorResponse() from @rest-rpc/server and writeErrorResponse() from @rest-rpc/server/node send the response of a RouteError, which clients decode as one, such as from a rate limit interceptor.

core, server:

  • A serializedBody() without a content-type header is sent with the selected content type, on the client and the server. Before, the runtime chose one from the body, such as text/plain;charset=UTF-8 for a string, which could contradict the route’s content type. A FormData body is always sent with the multipart/form-data content type the runtime generates with its boundary, so the default multipart codec returns it as is instead of wrapping it in serializedBody(). (7e3354b)

core, server:

  • A Fetch Response a handler returns is sent as it is, after middleware, without response validation or codecs, on Fetch and Node. Middleware receives it as the body of the envelope next() resolves to, with its status, headers and cookies, and the envelope’s fields are sent. .response(raw()) declares such a route, such as in a contract, and must be the route’s only response. OpenAPI documents it as a default response. .static(new Response(...)) sends a clone for every request, and respond() rejects a Response body. (f86a7c5)

core, server:

  • A Fetch interceptor may call next() with another request for the matched route to read, or more than once, and receives the runtime’s arguments after the request, such as Cloudflare’s env, as args. A Node interceptor sets headers on the ServerResponse before calling next().

core, server:

  • Add interceptors: functions around everything an app or a client does for a request, for API key checks, logging, error reporting, metrics, retries or CORS. Pass them as interceptors to createApp, typed with interceptor() from @rest-rpc/server, or @rest-rpc/server/node for the Node createApp, and to createClient, typed with clientInterceptor(). They run first to last and see every response, error responses included. (81f58db)

core, server:

  • Body codecs receive headers instead of a content type: serialize(value, headers) gets the outgoing headers with the selected content-type, and deserialize(body, headers) the received ones, with lowercase names. Headers a codec returns with serializedBody() replace the handler’s and the call’s, content-type parameters included, so a codec can add a filename to content-disposition. Before, a handler’s headers won over them. The client chooses a request’s format from its content-type wherever it is set, the call’s over additionalHeaders over globalHeaders. Before, a content-type in either was ignored and the body sent as JSON, although globalHeaders made the route’s header optional. A Node request body can be read once, and a second read rejects as on Fetch instead of returning the same body or hanging. (011d62f)

core, server:

  • Content types are content-type headers instead of their own concept, and a body is JSON unless that header says otherwise. A route accepts the request content types its headers schema declares and answers others with 415, and the client states the content type in the call’s headers. A defined response declares its content type in its headers schema, and an inferred one sends the header passed to respond(). .body() and .response() take a schema, and a stream response takes sse(schema). Media type records, the contentType option of respond(), the contentType of handler arguments, client calls and results, and the BodyContent, KnownBodyContentType, MediaType and ResponseContentDefinition types are removed. A FormData body is always sent as multipart/form-data, and OpenAPI reads content types from the declared content-type header. (0699974)

core, server:

  • Make server-sent events usable behind proxies and with other data formats. The client parses event streams following the SSE spec: LF, CRLF and CR line endings, an optional space after the colon, multi-line data, persisted ids and digit-only retry values. Stream responses are sent with cache-control: no-cache by default, so proxies don’t cache or buffer them. sseFormat() and the sse: { format } option on the server and the client set the data format of events, JSON by default. The server’s sse option also takes keepAlive and initialComment, which respond(stream, { sse }) overrides per stream. (eb281e2)

core, server:

  • On the client, they receive the Fetch Request and the called route, before the response is decoded.

core, server:

  • On the server, they receive every request, including ones that match no route, except those a route completed with .static() serves. route is the matched route’s definition, with its path and meta, or undefined when no route matches. next() resolves with every response, RouteErrors included, and rejects only with other thrown values, such as an Error a handler throws, which the app sends as INTERNAL_SERVER_ERROR when nothing catches it. A Node app also sends it when its interceptors finish without a response.

core, server:

  • Params, query and headers schemas accept any object input, and header values passed to respond(), globalHeaders and additionalHeaders accept any value, all sent with String() as documented. Before, their inputs had to be strings, numbers or booleans, so z.coerce.number() needed <number> to compile. A promise in globalHeaders is awaited, like the result of a provider function. ClientHeaderValue and RequestScalar are removed, and ClientHeaders is Record<string, unknown>. (8f0be69)

server:

  • .static() rejects a respond() result with a promise body, which is not awaited, like it rejects a promise value. (350882a)

server:

  • Interceptors’ next() rejects with every error thrown after it, RouteErrors included, with the status, data and headers they’re sent with, and NOT_FOUND for unmatched requests. Interceptors send errors by throwing. errorResponse() and writeErrorResponse() take any thrown value and are what the app sends with, so an interceptor catching with them turns errors into responses for the ones before it. onError() creates an interceptor that reports errors, response bodies that fail while they stream included. createRouteHandlers and createCatchAllHandler take interceptors too. (30f9ad3)

server:

  • Parsed query strings are objects without a prototype, so a __proto__[] key is an ordinary key instead of replacing the object’s prototype, and fields the query doesn’t contain read as undefined instead of inherited values such as an array’s length. (1b88943)

server:

  • Remove the onError route option and the notFound option of both createApps. Report errors from an interceptor instead, where next() rejects with them, and send a custom response for unmatched requests from an interceptor, which receives no route for them. (aa81df2)

server:

  • The Node adapter streams Blob response bodies, such as from fs.openAsBlob(), instead of reading each into memory, and sends their size as content-length. Static routes with a Blob body stream it for each response too. (7e3354b)

server:

  • The Node adapter streams FormData response bodies instead of reading each into memory, files included. They are sent without a content-length. (7e3354b)

Last updated on

Was this page helpful?