@kubb/plugin-axios 5.13.0 → 5.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +63 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +63 -10
- package/dist/index.js.map +1 -1
- package/docs/guides/serialization.md +12 -0
- package/package.json +2 -2
- package/templates/client.ts +79 -4
|
@@ -61,6 +61,18 @@ stays a string, and a binary type becomes a `Blob`. The negotiated media type is
|
|
|
61
61
|
`contentType`, so a `switch (result.contentType)` narrows `data` for an operation that returns
|
|
62
62
|
more than one.
|
|
63
63
|
|
|
64
|
+
When that success response declares several content types that do not share one parser, the
|
|
65
|
+
generated call sets `responseType` to a map from content type to parser, for example
|
|
66
|
+
`{ 'audio/mpeg': 'blob', 'text/event-stream': 'stream' }`. Axios reads the raw body, then the
|
|
67
|
+
runtime matches the response `Content-Type` against the map, including a `type/*` wildcard,
|
|
68
|
+
before the media-type heuristic. A schema with `format: binary` is a blob even when the media
|
|
69
|
+
type is not a well-known binary type.
|
|
70
|
+
|
|
71
|
+
Axios response interceptors see the decoded body: parsed JSON, a string, or a `Blob`. A map with
|
|
72
|
+
a `'stream'` entry reads the body as a stream, the same way as `responseType: 'stream'`, so an
|
|
73
|
+
event stream arrives as it is sent. Interceptors then see the stream, and the runtime reads any
|
|
74
|
+
other content type from it afterwards.
|
|
75
|
+
|
|
64
76
|
To decode a media type the runtime does not handle, register a codec's `deserialize` for it.
|
|
65
77
|
`deserialize` receives the raw body and the content type and returns the parsed value. It runs
|
|
66
78
|
before validation, so the schema checks the parsed value:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-axios",
|
|
3
|
-
"version": "5.13.
|
|
3
|
+
"version": "5.13.1",
|
|
4
4
|
"description": "Generate a slim, type-safe HTTP client with Kubb based on axios.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-client",
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"@kubb/kit": "^5.16.0",
|
|
84
|
-
"@kubb/plugin-client": "^5.
|
|
84
|
+
"@kubb/plugin-client": "^5.11.0",
|
|
85
85
|
"@kubb/plugin-ts": "^5.4.1",
|
|
86
86
|
"@kubb/plugin-zod": "^5.8.2",
|
|
87
87
|
"@kubb/renderer-jsx": "^5.16.0"
|
package/templates/client.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import axios from 'axios'
|
|
2
2
|
import type { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
|
3
|
-
import { getResponseContentType, isStreamed, type CallResult, type Transport } from './core.ts'
|
|
3
|
+
import { getResponseContentType, isStreamed, resolveResponseType, type CallResult, type ResponseType, type ResponseTypeMap, type Transport } from './core.ts'
|
|
4
4
|
import { isJsonMediaType } from './serializers.ts'
|
|
5
5
|
import * as core from './core.ts'
|
|
6
6
|
|
|
@@ -111,6 +111,65 @@ async function readStreamedBody({ data, contentType, status }: { data: unknown;
|
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
async function readStreamedBytes(data: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>): Promise<Uint8Array> {
|
|
115
|
+
if (data instanceof ReadableStream) return new Uint8Array(await new Response(data).arrayBuffer())
|
|
116
|
+
const chunks: Array<Uint8Array> = []
|
|
117
|
+
for await (const chunk of data) chunks.push(typeof chunk === 'string' ? new TextEncoder().encode(chunk) : chunk)
|
|
118
|
+
return new Uint8Array(await new Blob(chunks as Array<BlobPart>).arrayBuffer())
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function toBytes(data: unknown): Uint8Array | undefined {
|
|
122
|
+
if (data instanceof ArrayBuffer) return new Uint8Array(data)
|
|
123
|
+
if (ArrayBuffer.isView(data)) return new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
|
|
124
|
+
return undefined
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Decodes raw bytes with the parser a `responseType` map picked for the response content type. */
|
|
128
|
+
function decodeBytes(bytes: Uint8Array, responseType: ResponseType | undefined, contentType: string | undefined): unknown {
|
|
129
|
+
switch (responseType) {
|
|
130
|
+
case 'blob':
|
|
131
|
+
return new Blob([bytes as BlobPart], { type: contentType ?? '' })
|
|
132
|
+
case 'arraybuffer':
|
|
133
|
+
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength)
|
|
134
|
+
case 'stream':
|
|
135
|
+
return new Blob([bytes as BlobPart]).stream()
|
|
136
|
+
case 'text':
|
|
137
|
+
case 'document':
|
|
138
|
+
return new TextDecoder().decode(bytes)
|
|
139
|
+
}
|
|
140
|
+
const text = new TextDecoder().decode(bytes)
|
|
141
|
+
if (!text) return undefined
|
|
142
|
+
try {
|
|
143
|
+
return JSON.parse(text)
|
|
144
|
+
} catch {
|
|
145
|
+
return text
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Decodes a body read for a `responseType` map. Bytes are decoded by the mapped parser. A streamed body stays a stream
|
|
151
|
+
* when the map picks `'stream'` for a success, and is read and decoded otherwise. Anything else was already decoded.
|
|
152
|
+
*/
|
|
153
|
+
async function decodeMappedBody({
|
|
154
|
+
data,
|
|
155
|
+
responseTypeMap,
|
|
156
|
+
contentType,
|
|
157
|
+
status,
|
|
158
|
+
}: {
|
|
159
|
+
data: unknown
|
|
160
|
+
responseTypeMap: ResponseTypeMap
|
|
161
|
+
contentType: string | undefined
|
|
162
|
+
status: number
|
|
163
|
+
}): Promise<unknown> {
|
|
164
|
+
const responseType = resolveResponseType(responseTypeMap, contentType)
|
|
165
|
+
if (isStreamed(data)) {
|
|
166
|
+
if (responseType === 'stream' && status < 300) return data
|
|
167
|
+
return decodeBytes(await readStreamedBytes(data), responseType, contentType)
|
|
168
|
+
}
|
|
169
|
+
const bytes = toBytes(data)
|
|
170
|
+
return bytes ? decodeBytes(bytes, responseType, contentType) : data
|
|
171
|
+
}
|
|
172
|
+
|
|
114
173
|
/**
|
|
115
174
|
* Turns an axios instance into a transport: each resolved request becomes one `instance.request` with the instance's
|
|
116
175
|
* defaults, adapter, and axios interceptors. Every status resolves, because the core splits success from error itself.
|
|
@@ -126,21 +185,37 @@ export function createAxiosTransport(instance: AxiosInstance): Transport {
|
|
|
126
185
|
// Kubb already serialized the body; only URLSearchParams needs a string, since axios's Node adapter rejects it.
|
|
127
186
|
transformRequest: (data) => (data instanceof URLSearchParams ? data.toString() : data),
|
|
128
187
|
signal: request.signal,
|
|
129
|
-
responseType: request.responseType,
|
|
188
|
+
responseType: typeof request.responseType === 'object' ? undefined : request.responseType,
|
|
130
189
|
validateStatus: () => true,
|
|
131
190
|
}
|
|
132
191
|
|
|
192
|
+
// Axios picks how to read the body before the Content-Type is known, so a map reads raw bytes and decodes them once it
|
|
193
|
+
// is. A map with a stream entry reads a stream, so an event stream still arrives incrementally. Bytes are decoded in
|
|
194
|
+
// `transformResponse`, before the axios interceptors run, so they see the parsed body.
|
|
195
|
+
const responseTypeMap = typeof request.responseType === 'object' ? request.responseType : undefined
|
|
196
|
+
if (responseTypeMap) {
|
|
197
|
+
config.responseType = Object.values(responseTypeMap).includes('stream') ? 'stream' : 'arraybuffer'
|
|
198
|
+
config.transformResponse = (data, headers) => {
|
|
199
|
+
const bytes = toBytes(data)
|
|
200
|
+
if (!bytes) return data
|
|
201
|
+
const contentType = getResponseContentType(toHeaders(headers))
|
|
202
|
+
return decodeBytes(bytes, resolveResponseType(responseTypeMap, contentType), contentType)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
133
206
|
// In the browser the default adapters pick XHR, which buffers the whole body, so a stream switches to fetch. An adapter set
|
|
134
207
|
// on the call or the instance, and the Node http adapter with its proxy and agent, are kept.
|
|
135
208
|
const defaultAdapter = !instance.defaults?.adapter || String(instance.defaults.adapter) === String(axios.defaults.adapter)
|
|
136
|
-
if (
|
|
209
|
+
if (config.responseType === 'stream' && !config.adapter && defaultAdapter && typeof XMLHttpRequest !== 'undefined') config.adapter = 'fetch'
|
|
137
210
|
|
|
138
211
|
const response = await instance.request<unknown>(config)
|
|
139
212
|
const headers = toHeaders(response.headers)
|
|
140
213
|
const contentType = getResponseContentType(headers)
|
|
141
214
|
|
|
142
215
|
return {
|
|
143
|
-
data:
|
|
216
|
+
data: responseTypeMap
|
|
217
|
+
? await decodeMappedBody({ data: response.data, responseTypeMap, contentType, status: response.status })
|
|
218
|
+
: await readStreamedBody({ data: response.data, contentType, status: response.status }),
|
|
144
219
|
status: response.status,
|
|
145
220
|
statusText: response.statusText,
|
|
146
221
|
headers,
|