Axios
Officialv5.5.11@kubb/plugin-axios
Server-sent events
Operations returning text/event-stream produce a typed stream rather than a RequestResult. The client switches to the axios fetch adapter for these calls unless you set adapter yourself, because the default XHR adapter buffers the whole body.
Consume a stream
Read the EventStreamResult.stream with for await:
import { streamEvents } from './gen/clients/streamEvents'
const { stream } = await streamEvents({})
for await (const event of stream) {
console.info(event.data)
}data is typed from the operation's response schema. Each event also carries the optional SSE
fields, so you can branch on the event name or read the id:
type ServerSentEvent<TData> = {
data: TData
event?: string
id?: string
retry?: number
}The runtime parses each event's data as JSON when it is valid and keeps it as the raw string
otherwise.
Stop early
Break out of the loop to stop reading. The underlying reader is canceled when the iterator
finishes, so an early break releases the stream:
const { stream } = await streamEvents({})
for await (const event of stream) {
if (event.event === 'done') break
render(event.data)
}To stop a stream from outside the loop, pass an AbortSignal and abort it. Aborting ends the
request and the iteration:
const controller = new AbortController()
const { stream } = await streamEvents({ signal: controller.signal })
// elsewhere, for example on component unmount
controller.abort()Read the response
Read status and headers from the native response before iterating:
const { stream, response } = await streamEvents({})
console.info(response.status) // 200
console.info(response.headers.get('x-stream-id'))
for await (const event of stream) {
handle(event.data)
}Handle errors
Catch non-2xx responses around the call and iteration, and rethrow other errors such as connection failures:
import { ResponseError } from './gen/.kubb/client'
try {
const { stream } = await streamEvents({})
for await (const event of stream) {
handle(event.data)
}
} catch (error) {
if (!ResponseError.is(error)) throw error
console.error('stream rejected', error.status)
}for await loop. Track the last event's id if you need to reconnect
from where the stream stopped.See also
Configure serialization
The generated client encodes parameters using OpenAPI style and explode, serializes request bodies by content type, and decodes responses by media type. Each generated operation carries its parameter styles and content types, so most requests need no extra setup.
Use a custom transport
Set a transport at runtime to replace the network send. The client still builds URLs, serializes parameters, resolves authentication, and handles results.