@kubb/plugin-fetch 5.5.0-canary.20260924T111237 → 5.5.0
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 +58 -67
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -5
- package/dist/index.js +58 -67
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/plugin.ts +7 -34
- package/templates/fetch.ts +4 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-fetch",
|
|
3
|
-
"version": "5.5.0
|
|
3
|
+
"version": "5.5.0",
|
|
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",
|
|
@@ -65,17 +65,17 @@
|
|
|
65
65
|
"registry": "https://registry.npmjs.org/"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@kubb/plugin-ts": "^5.
|
|
69
|
-
"@kubb/plugin-zod": "^5.
|
|
68
|
+
"@kubb/plugin-ts": "^5.0.2",
|
|
69
|
+
"@kubb/plugin-zod": "^5.2.1"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@internals/client": "0.
|
|
73
|
-
"@internals/shared": "0.
|
|
74
|
-
"kubb": "^5.3.
|
|
72
|
+
"@internals/client": "0.0.0",
|
|
73
|
+
"@internals/shared": "0.0.0",
|
|
74
|
+
"kubb": "^5.3.14",
|
|
75
75
|
"typescript": "^6.0.3"
|
|
76
76
|
},
|
|
77
77
|
"peerDependencies": {
|
|
78
|
-
"kubb": "^5.3.
|
|
78
|
+
"kubb": "^5.3.14"
|
|
79
79
|
},
|
|
80
80
|
"engines": {
|
|
81
81
|
"node": ">=22"
|
package/src/plugin.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { readFileSync } from 'node:fs'
|
|
2
1
|
import path from 'node:path'
|
|
3
|
-
import { createSdkGenerator, defaultMacros, resolverClient } from '@internals/client'
|
|
2
|
+
import { createSdkGenerator, defaultMacros, resolverClient, runtimeTemplate } from '@internals/client'
|
|
4
3
|
import { createGroupConfig } from '@internals/shared'
|
|
5
|
-
import {
|
|
4
|
+
import { definePlugin, Resolver } from 'kubb/kit'
|
|
6
5
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
6
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
7
|
import { clientGenerator } from './generators/clientGenerator.tsx'
|
|
@@ -44,6 +43,7 @@ export const pluginFetch = definePlugin<PluginFetch>((options) => {
|
|
|
44
43
|
include,
|
|
45
44
|
override = [],
|
|
46
45
|
baseURL,
|
|
46
|
+
throwOnErrorDefault = true,
|
|
47
47
|
validator = false,
|
|
48
48
|
returnType = 'full',
|
|
49
49
|
group,
|
|
@@ -58,6 +58,7 @@ export const pluginFetch = definePlugin<PluginFetch>((options) => {
|
|
|
58
58
|
override,
|
|
59
59
|
group: createGroupConfig(group),
|
|
60
60
|
baseURL,
|
|
61
|
+
throwOnErrorDefault,
|
|
61
62
|
validator,
|
|
62
63
|
returnType,
|
|
63
64
|
sdk: sdk ? { mode: sdk.mode ?? 'tag', name: sdk.name } : undefined,
|
|
@@ -91,39 +92,11 @@ export const pluginFetch = definePlugin<PluginFetch>((options) => {
|
|
|
91
92
|
copy: fetchSerializersTemplatePath,
|
|
92
93
|
})
|
|
93
94
|
|
|
94
|
-
const clientPath = path.resolve(root, '.kubb/client.ts')
|
|
95
|
-
const clientSource = readFileSync(fetchClientTemplatePath, 'utf8')
|
|
96
|
-
const clientBody = clientSource.slice(clientSource.indexOf('\n\n') + 2)
|
|
97
|
-
const runtimeRoot = path.dirname(clientPath)
|
|
98
|
-
|
|
99
95
|
ctx.injectFile({
|
|
100
96
|
baseName: 'client.ts',
|
|
101
|
-
path:
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
name: ['applyHeaderStyles', 'defaultBodySerializer', 'defaultPathSerializer', 'defaultQuerySerializer', 'isDefaultJsonBody', 'serializeCookies'],
|
|
105
|
-
path: path.resolve(root, '.kubb/serializers.ts'),
|
|
106
|
-
root: runtimeRoot,
|
|
107
|
-
}),
|
|
108
|
-
ast.factory.createImport({
|
|
109
|
-
name: ['HeadersInit', 'PathParamStyle', 'PathSerializer', 'RequestBody', 'Serializers', 'Styles'],
|
|
110
|
-
path: path.resolve(root, '.kubb/serializers.ts'),
|
|
111
|
-
root: runtimeRoot,
|
|
112
|
-
isTypeOnly: true,
|
|
113
|
-
}),
|
|
114
|
-
ast.factory.createImport({
|
|
115
|
-
name: ['ParseError', 'validateStandardSchema'],
|
|
116
|
-
path: path.resolve(root, '.kubb/standardSchema.ts'),
|
|
117
|
-
root: runtimeRoot,
|
|
118
|
-
}),
|
|
119
|
-
ast.factory.createImport({
|
|
120
|
-
name: ['StandardSchemaValidator'],
|
|
121
|
-
path: path.resolve(root, '.kubb/standardSchema.ts'),
|
|
122
|
-
root: runtimeRoot,
|
|
123
|
-
isTypeOnly: true,
|
|
124
|
-
}),
|
|
125
|
-
],
|
|
126
|
-
sources: [ast.factory.createSource({ nodes: [ast.factory.createText(clientBody)] })],
|
|
97
|
+
path: path.resolve(root, '.kubb/client.ts'),
|
|
98
|
+
// Keep sources empty so the barrel exports the whole runtime module.
|
|
99
|
+
banner: runtimeTemplate(fetchClientTemplatePath, ctx.config),
|
|
127
100
|
footer: baseURLExpression ? `client.setConfig({ baseURL: ${baseURLExpression} })` : undefined,
|
|
128
101
|
})
|
|
129
102
|
|
package/templates/fetch.ts
CHANGED
|
@@ -109,21 +109,16 @@ export function withUnwrap<T extends { data: unknown; error: unknown }>(promise:
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
|
-
* The shape a generated operation returns
|
|
113
|
-
*
|
|
114
|
-
* `RequestResult` when a call sets `throwOnError: false` and still needs `error` to discriminate a
|
|
115
|
-
* failed response.
|
|
112
|
+
* The shape a generated operation returns with `returnType: 'data'`: the success body when
|
|
113
|
+
* `throwOnError` is true, or the full `RequestResult` when it is false.
|
|
116
114
|
*/
|
|
117
115
|
export type UnwrappedResult<TResponses, ThrowOnError extends boolean = true, TRequest = Request, TResponse = Response> = ThrowOnError extends true
|
|
118
116
|
? RequestResult<TResponses, true, TRequest, TResponse>['data']
|
|
119
117
|
: RequestResult<TResponses, ThrowOnError, TRequest, TResponse>
|
|
120
118
|
|
|
121
119
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* a call that sets `throwOnError: false`, since that path still needs `error` to discriminate a
|
|
125
|
-
* failed response. Backs `returnType: 'data'`, mirroring how `toEventStream` centralizes the
|
|
126
|
-
* post-processing for `text/event-stream` operations.
|
|
120
|
+
* Returns the success body when `throwOnError` is true, or the full result when it is false.
|
|
121
|
+
* Backs generated operations with `returnType: 'data'`.
|
|
127
122
|
*/
|
|
128
123
|
export function unwrapResult<T extends { data: unknown; error: unknown }>(promise: Promise<T>, throwOnError: boolean | undefined): Promise<T | T['data']> {
|
|
129
124
|
return promise.then((result) => ((throwOnError ?? true) ? result.data : result))
|