@kubb/plugin-fetch 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 +6 -0
- package/package.json +2 -2
- package/templates/client.ts +13 -13
|
@@ -61,6 +61,12 @@ 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' }`. The runtime matches the response
|
|
67
|
+
`Content-Type` against the map, including a `type/*` wildcard, before the media-type heuristic.
|
|
68
|
+
A schema with `format: binary` is a blob even when the media type is not a well-known binary type.
|
|
69
|
+
|
|
64
70
|
To decode a media type the runtime does not handle, register a codec's `deserialize` for it.
|
|
65
71
|
`deserialize` receives the raw body and the content type and returns the parsed value. It runs
|
|
66
72
|
before validation, so the schema checks the parsed value:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-fetch",
|
|
3
|
-
"version": "5.13.
|
|
3
|
+
"version": "5.13.1",
|
|
4
4
|
"description": "Generate a slim, type-safe HTTP client with Kubb based on the native Fetch api.",
|
|
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,4 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createClientCore,
|
|
3
|
+
getResponseContentType,
|
|
4
|
+
resolveResponseType,
|
|
5
|
+
type ResponseType,
|
|
6
|
+
type ResponseTypeMap,
|
|
7
|
+
type Transport,
|
|
8
|
+
type TransportResult,
|
|
9
|
+
} from './core.ts'
|
|
2
10
|
import { isJsonMediaType } from './serializers.ts'
|
|
3
11
|
import * as core from './core.ts'
|
|
4
12
|
|
|
@@ -35,24 +43,16 @@ export const ResponseError = core.ResponseError as {
|
|
|
35
43
|
prototype: core.ResponseError<unknown, NativeRequest, NativeResponse>
|
|
36
44
|
}
|
|
37
45
|
|
|
38
|
-
function
|
|
39
|
-
if (!contentType) return undefined
|
|
40
|
-
if (contentType.includes('text/event-stream')) return 'stream'
|
|
41
|
-
if (contentType.includes('application/json') || contentType.includes('text/json')) return 'json'
|
|
42
|
-
if (contentType.includes('text/')) return 'text'
|
|
43
|
-
if (contentType.includes('image/') || contentType.includes('application/octet-stream')) return 'blob'
|
|
44
|
-
return undefined
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function parseResponse(response: Response, responseType?: ResponseType): Promise<unknown> {
|
|
46
|
+
async function parseResponse(response: Response, responseType?: ResponseType | ResponseTypeMap): Promise<unknown> {
|
|
48
47
|
if (response.status === 204 || response.status === 205 || response.status === 304 || !response.body) {
|
|
49
48
|
return undefined
|
|
50
49
|
}
|
|
51
50
|
|
|
52
51
|
const contentType = response.headers.get('Content-Type')
|
|
53
52
|
// A server may answer a streamed operation with JSON, or with an error body: both are decoded by their content type. Any other streamed success body stays a stream.
|
|
54
|
-
const
|
|
55
|
-
|
|
53
|
+
const resolved = resolveResponseType(responseType, contentType)
|
|
54
|
+
const detected = resolved === 'stream' && (!response.ok || isJsonMediaType(contentType)) ? resolveResponseType(undefined, contentType) : resolved
|
|
55
|
+
switch (detected) {
|
|
56
56
|
case 'text':
|
|
57
57
|
case 'document':
|
|
58
58
|
return response.text()
|