Stream Server-Sent Events
Typed async iterables for text/event-stream responses
When an operation's only success content type is text/event-stream, the Fetch client generates a function that streams the typed events instead of awaiting the whole body.
OpenAPI Schema
The response schema describes the data of one event. On OpenAPI 3.2, itemSchema takes precedence over schema.
paths:
/games/{id}/state:
get:
operationId: getGameState
parameters:
- name: id
in: path
required: true
schema:
type: string
responses:
'200':
description: Game state updates
content:
text/event-stream:
schema:
$ref: '#/components/schemas/GameState'
components:
schemas:
GameState:
type: object
required: [turn, updatedAt]
properties:
turn:
type: number
updatedAt:
type: string
format: date-timeUsage
const controller = new AbortController();
const { stream } = await getGameState('g1', {
signal: controller.signal,
retryDelay: 1000,
maxRetryDelay: 30000,
});
for await (const state of stream) {
// state: GameState
}With includeHttpResponseReturnType: false, the function resolves to the AsyncIterable itself.
- Each event's
datais parsed as JSON, or yielded as text when the event schema is a string. - A dropped connection reconnects. The delay starts at
retryDelay(default 3000 ms), honours the server'sretry:field, and doubles up tomaxRetryDelay(default 30000 ms). Each reconnect sends the last received event ID asLast-Event-ID. - A dropped
POSTorPATCHstream is not replayed unless you passreconnect: true, because resending the request can repeat a non-idempotent action.reconnect: falseturns reconnects off for any method. - Aborting
signal, or leaving thefor awaitloop early, closes the stream and cancels any pending reconnect.
The query clients (React Query, Vue Query, Svelte Query, Solid Query, Angular Query and SWR) don't wrap streaming operations in a query hook. Call the plain function from an effect instead.
Dates and formatType
With useDatesTransform, each event's data is decoded like a JSON response body: date-time fields become Date objects, and formatType values go through their decode function.
This needs a single event schema. A bodyless success status, such as a 204 declared next to the stream, doesn't prevent decoding. If several success statuses each declare a stream with its own event schema, events are left as parsed JSON.
for await (const state of stream) {
state.updatedAt.getTime(); // a Date, not a string
}A decode error ends the stream with that error, and the stream does not reconnect.
Custom Mutator
An SSE operation still goes through your custom mutator. The mutator must resolve to the raw Response without reading its body. Orval parses the stream itself and calls the mutator again for every reconnect, so your base URL, headers and credentials apply to each connection.
The request the mutator receives has an Accept: text/event-stream header, which you can check to return the response as is:
export const customFetch = async <T>(
url: string,
options: RequestInit,
): Promise<T> => {
const response = await fetch(`https://api.example.com${url}`, {
...options,
headers: { ...options.headers, Authorization: `Bearer ${getToken()}` },
});
if (new Headers(options.headers).get('accept') === 'text/event-stream') {
return response as T; // let Orval read the stream
}
const data = await response.json();
return { status: response.status, data, headers: response.headers } as T;
};The same applies to a hook mutator and to the mutator of the shared runtime. If the mutator resolves to anything other than a Response, the call rejects with a TypeError. An inferred mutator keeps the plain request, because Orval can't know its return type.
SSE streaming is only supported with the Fetch client.