@kubb/plugin-client 5.0.0-beta.56 → 5.5.2
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/README.md +8 -61
- package/dist/index.cjs +754 -1745
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +170 -349
- package/dist/index.js +733 -1733
- package/dist/index.js.map +1 -1
- package/package.json +10 -63
- package/src/generators/clientGenerator.tsx +6 -132
- package/src/index.ts +1 -9
- package/src/plugin.ts +32 -122
- package/src/types.ts +27 -249
- package/dist/chunk-Bx3C2hgW.cjs +0 -38
- package/dist/clients/axios.cjs +0 -68
- package/dist/clients/axios.cjs.map +0 -1
- package/dist/clients/axios.d.ts +0 -49
- package/dist/clients/axios.js +0 -58
- package/dist/clients/axios.js.map +0 -1
- package/dist/clients/fetch.cjs +0 -128
- package/dist/clients/fetch.cjs.map +0 -1
- package/dist/clients/fetch.d.ts +0 -50
- package/dist/clients/fetch.js +0 -120
- package/dist/clients/fetch.js.map +0 -1
- package/dist/templates/clients/axios.source.cjs +0 -7
- package/dist/templates/clients/axios.source.cjs.map +0 -1
- package/dist/templates/clients/axios.source.d.ts +0 -7
- package/dist/templates/clients/axios.source.js +0 -6
- package/dist/templates/clients/axios.source.js.map +0 -1
- package/dist/templates/clients/fetch.source.cjs +0 -7
- package/dist/templates/clients/fetch.source.cjs.map +0 -1
- package/dist/templates/clients/fetch.source.d.ts +0 -7
- package/dist/templates/clients/fetch.source.js +0 -6
- package/dist/templates/clients/fetch.source.js.map +0 -1
- package/dist/templates/config.source.cjs +0 -7
- package/dist/templates/config.source.cjs.map +0 -1
- package/dist/templates/config.source.d.ts +0 -7
- package/dist/templates/config.source.js +0 -6
- package/dist/templates/config.source.js.map +0 -1
- package/src/clients/axios.ts +0 -113
- package/src/clients/fetch.ts +0 -201
- package/src/components/ClassClient.tsx +0 -161
- package/src/components/Client.tsx +0 -281
- package/src/components/StaticClassClient.tsx +0 -153
- package/src/components/Url.tsx +0 -90
- package/src/components/WrapperClient.tsx +0 -33
- package/src/functionParams.ts +0 -118
- package/src/generators/classClientGenerator.tsx +0 -261
- package/src/generators/groupedClientGenerator.tsx +0 -82
- package/src/generators/operationsGenerator.ts +0 -47
- package/src/generators/staticClassClientGenerator.tsx +0 -240
- package/src/resolvers/resolverClient.ts +0 -46
- package/src/templates/clients/axios.source.ts +0 -4
- package/src/templates/clients/fetch.source.ts +0 -4
- package/src/templates/config.source.ts +0 -4
- package/src/utils.ts +0 -259
- /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/src/types.ts
CHANGED
|
@@ -1,270 +1,48 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Override, PluginFactoryOptions } from 'kubb/kit'
|
|
2
|
+
import type { Options as ClientOptions, ResolvedOptions as ClientResolvedOptions, ResolverClient } from '@internals/client'
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
* The concrete resolver type for `@kubb/plugin-client`.
|
|
5
|
-
* Extends the base `Resolver` with a `resolveName` helper for client function names.
|
|
6
|
-
*/
|
|
7
|
-
export type ResolverClient = Resolver & {
|
|
8
|
-
/**
|
|
9
|
-
* Resolves the function name for a given raw operation name.
|
|
10
|
-
*
|
|
11
|
-
* @example Resolving operation names
|
|
12
|
-
* `resolver.resolveName('show pet by id') // -> 'showPetById'`
|
|
13
|
-
*/
|
|
14
|
-
resolveName(this: ResolverClient, name: string): string
|
|
15
|
-
/**
|
|
16
|
-
* Resolves the output file name for a client module.
|
|
17
|
-
*/
|
|
18
|
-
resolvePathName(this: ResolverClient, name: string, type?: 'file' | 'function' | 'type' | 'const'): string
|
|
19
|
-
/**
|
|
20
|
-
* Resolves the generated class name for class-based clients.
|
|
21
|
-
*/
|
|
22
|
-
resolveClassName(this: ResolverClient, name: string): string
|
|
23
|
-
/**
|
|
24
|
-
* Resolves the generated class name for tag-based client groups. The default
|
|
25
|
-
* appends a `Client` suffix (tag `pet` becomes `PetClient`) so the class never
|
|
26
|
-
* collides with the schema model of the same name in the barrel.
|
|
27
|
-
*
|
|
28
|
-
* @example Resolving tag-group class names
|
|
29
|
-
* `resolver.resolveGroupName('pet') // -> 'PetClient'`
|
|
30
|
-
*/
|
|
31
|
-
resolveGroupName(this: ResolverClient, name: string): string
|
|
32
|
-
/**
|
|
33
|
-
* Resolves the generated SDK facade property name for a client class.
|
|
34
|
-
*/
|
|
35
|
-
resolveClientPropertyName(this: ResolverClient, name: string): string
|
|
36
|
-
/**
|
|
37
|
-
* Resolves the URL helper function name for an operation.
|
|
38
|
-
*
|
|
39
|
-
* @example Resolving URL helper names
|
|
40
|
-
* `resolver.resolveUrlName(node) // -> 'getShowPetByIdUrl'`
|
|
41
|
-
*/
|
|
42
|
-
resolveUrlName(this: ResolverClient, node: ast.OperationNode): string
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Use either a preset `client` type OR a custom `importPath`, not both.
|
|
47
|
-
* `importPath` will override the default `client` preset when both are provided.
|
|
48
|
-
* These options are mutually exclusive. `bundle` and `importPath` are also
|
|
49
|
-
* mutually exclusive since `bundle` only has effect when `importPath` is not set.
|
|
50
|
-
*/
|
|
51
|
-
export type ClientImportPath =
|
|
52
|
-
| {
|
|
53
|
-
/**
|
|
54
|
-
* HTTP client used by the generated code.
|
|
55
|
-
* - `'axios'` — imports from `@kubb/plugin-client/clients/axios`. Requires `axios` at runtime.
|
|
56
|
-
* - `'fetch'` — imports from `@kubb/plugin-client/clients/fetch`. Uses the global `fetch`.
|
|
57
|
-
*
|
|
58
|
-
* @default 'axios'
|
|
59
|
-
*/
|
|
60
|
-
client?: 'axios' | 'fetch'
|
|
61
|
-
importPath?: never
|
|
62
|
-
}
|
|
63
|
-
| {
|
|
64
|
-
client?: never
|
|
65
|
-
/**
|
|
66
|
-
* Path to a custom client module. Generated files import their HTTP runtime from here
|
|
67
|
-
* instead of `@kubb/plugin-client/clients/{client}`. Accepts both relative paths and
|
|
68
|
-
* bare module specifiers; the value is used as-is.
|
|
69
|
-
*
|
|
70
|
-
* @note When combined with a query plugin, the module must export `Client`,
|
|
71
|
-
* `RequestConfig`, and `ResponseErrorConfig` types.
|
|
72
|
-
*/
|
|
73
|
-
importPath: string
|
|
74
|
-
/**
|
|
75
|
-
* `bundle` has no effect when `importPath` is set.
|
|
76
|
-
* Use either `bundle` (with `client`) or `importPath`, not both.
|
|
77
|
-
*/
|
|
78
|
-
bundle?: never
|
|
79
|
-
}
|
|
4
|
+
export type { ResolverClient } from '@internals/client'
|
|
80
5
|
|
|
81
6
|
/**
|
|
82
|
-
*
|
|
83
|
-
*
|
|
84
|
-
* - `paramsType: 'object'` — all parameters (including path params) are merged into a single
|
|
85
|
-
* destructured object. `pathParamsType` is never reached in this code path and has no effect.
|
|
86
|
-
* - `paramsType?: 'inline'` (or omitted) — each parameter group is a separate function argument.
|
|
87
|
-
* `pathParamsType` controls whether the path-param group itself is destructured (`'object'`)
|
|
88
|
-
* or spread as individual arguments (`'inline'`).
|
|
7
|
+
* Omits keys from every member of a union, where the built-in `Omit` would collapse the union into one shape.
|
|
89
8
|
*/
|
|
90
|
-
type
|
|
91
|
-
| {
|
|
92
|
-
/**
|
|
93
|
-
* Every operation parameter (path, query, headers, body) is wrapped in a single
|
|
94
|
-
* destructured object argument.
|
|
95
|
-
*/
|
|
96
|
-
paramsType: 'object'
|
|
97
|
-
/**
|
|
98
|
-
* `pathParamsType` has no effect when `paramsType` is `'object'`.
|
|
99
|
-
* Path params already live inside the single destructured object.
|
|
100
|
-
*/
|
|
101
|
-
pathParamsType?: never
|
|
102
|
-
}
|
|
103
|
-
| {
|
|
104
|
-
/**
|
|
105
|
-
* Each parameter group is emitted as a separate positional function argument.
|
|
106
|
-
*
|
|
107
|
-
* @default 'inline'
|
|
108
|
-
*/
|
|
109
|
-
paramsType?: 'inline'
|
|
110
|
-
/**
|
|
111
|
-
* How URL path parameters are arranged inside the inline argument list.
|
|
112
|
-
* - `'object'` groups them into one destructured object: `{ petId }: PathParams`.
|
|
113
|
-
* - `'inline'` emits each path param as its own argument: `petId: string`.
|
|
114
|
-
*
|
|
115
|
-
* @default 'inline'
|
|
116
|
-
*/
|
|
117
|
-
pathParamsType?: 'object' | 'inline'
|
|
118
|
-
}
|
|
9
|
+
type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never
|
|
119
10
|
|
|
120
11
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
12
|
+
* Options for `pluginClient`. It takes the shared client options, without the ones this plugin does not use,
|
|
13
|
+
* plus the required `importPath`.
|
|
123
14
|
*
|
|
124
|
-
* @
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* pluginClient({ importPath: '../../../client' })
|
|
18
|
+
* ```
|
|
125
19
|
*/
|
|
126
|
-
export type Options =
|
|
127
|
-
/**
|
|
128
|
-
* Skip operations matching at least one entry in the list.
|
|
129
|
-
*/
|
|
130
|
-
exclude?: Array<Exclude>
|
|
131
|
-
/**
|
|
132
|
-
* Restrict generation to operations matching at least one entry in the list.
|
|
133
|
-
*/
|
|
134
|
-
include?: Array<Include>
|
|
135
|
-
/**
|
|
136
|
-
* Apply a different options object to operations matching a pattern.
|
|
137
|
-
*/
|
|
138
|
-
override?: Array<Override<ResolvedOptions>>
|
|
20
|
+
export type Options = DistributiveOmit<ClientOptions, 'sdk' | 'returnType' | 'baseURL' | 'override'> & {
|
|
139
21
|
/**
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
* @default false
|
|
22
|
+
* Applies different options to operations that match a pattern.
|
|
23
|
+
* `sdk`, `returnType`, and `baseURL` are not available, because this plugin generates standalone operations and your client module owns the base URL.
|
|
143
24
|
*/
|
|
144
|
-
|
|
25
|
+
override?: Array<Override<Omit<ResolvedOptions, 'throwOnErrorDefault' | 'sdk' | 'returnType' | 'baseURL'>>>
|
|
145
26
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* - `false` keeps them private.
|
|
149
|
-
*
|
|
150
|
-
* @default false
|
|
151
|
-
*/
|
|
152
|
-
urlType?: 'export' | false
|
|
153
|
-
/**
|
|
154
|
-
* Base URL prepended to every request. When omitted, falls back to the adapter's
|
|
155
|
-
* server URL (typically `servers[0].url`).
|
|
156
|
-
*/
|
|
157
|
-
baseURL?: string
|
|
158
|
-
/**
|
|
159
|
-
* Shape of the value returned by each generated client function.
|
|
160
|
-
* - `'data'` — only the response body.
|
|
161
|
-
* - `'full'` — the full response as a discriminated union keyed by HTTP status code.
|
|
162
|
-
* Each member is `{ status: N; data: StatusNType; statusText: string }`,
|
|
163
|
-
* so narrowing on `res.status` also narrows `res.data` to the matching response type.
|
|
164
|
-
*
|
|
165
|
-
* @default 'data'
|
|
166
|
-
*/
|
|
167
|
-
dataReturnType?: 'data' | 'full'
|
|
168
|
-
/**
|
|
169
|
-
* Rename parameter properties in the generated client (path, query, headers).
|
|
170
|
-
* The HTTP request still uses the original spec names; Kubb writes the mapping for you.
|
|
171
|
-
*
|
|
172
|
-
* @note Use the same value on `@kubb/plugin-ts` so types stay compatible.
|
|
173
|
-
*/
|
|
174
|
-
paramsCasing?: 'camelcase'
|
|
175
|
-
/**
|
|
176
|
-
* Validator applied to request and response bodies using schemas from `@kubb/plugin-zod`.
|
|
177
|
-
* - `false` (default): no validation. The response is returned as-is.
|
|
178
|
-
* - `'zod'`: validates response bodies only (backward-compatible shorthand).
|
|
179
|
-
* - `{ request?: 'zod'; response?: 'zod' }`: opt in per direction. `request` validates the
|
|
180
|
-
* request body and query parameters before the call. `response` validates the response body.
|
|
181
|
-
*
|
|
182
|
-
* @default false
|
|
183
|
-
* @example Response only (shorthand)
|
|
184
|
-
* `parser: 'zod'`
|
|
185
|
-
* @example Both directions
|
|
186
|
-
* `parser: { request: 'zod', response: 'zod' }`
|
|
187
|
-
*/
|
|
188
|
-
parser?: false | 'zod' | { request?: 'zod'; response?: 'zod' }
|
|
189
|
-
/**
|
|
190
|
-
* Shape of the generated client.
|
|
191
|
-
* - `'function'` — one standalone async function per operation.
|
|
192
|
-
* - `'class'` — one class per tag with instance methods.
|
|
193
|
-
* - `'staticClass'` — one class per tag with static methods.
|
|
194
|
-
*
|
|
195
|
-
* @default 'function'
|
|
196
|
-
* @note Only `'function'` is compatible with query plugins.
|
|
197
|
-
*/
|
|
198
|
-
clientType?: 'function' | 'class' | 'staticClass'
|
|
199
|
-
/**
|
|
200
|
-
* Copy the HTTP client runtime into the generated output so consumers do not need
|
|
201
|
-
* `@kubb/plugin-client` at runtime. When `false`, generated files import from
|
|
202
|
-
* `@kubb/plugin-client/clients/{client}`.
|
|
203
|
-
*
|
|
204
|
-
* @default false
|
|
205
|
-
*/
|
|
206
|
-
bundle?: boolean
|
|
207
|
-
/**
|
|
208
|
-
* Generate a single SDK class composing every tag-based client into one entry point.
|
|
209
|
-
* Automatically enables `clientType: 'class'`.
|
|
27
|
+
* Import specifier of your own client module, written into every generated import exactly as given.
|
|
28
|
+
* It must resolve from the generated file, so use a relative path, a package name, or an alias.
|
|
210
29
|
*
|
|
211
30
|
* @example
|
|
212
31
|
* ```ts
|
|
213
|
-
* pluginClient({
|
|
214
|
-
*
|
|
215
|
-
* })
|
|
216
|
-
* // class PetStoreSDK {
|
|
217
|
-
* // readonly petClient: PetClient
|
|
218
|
-
* // readonly storeClient: StoreClient
|
|
219
|
-
* // constructor(config = {}) { ... }
|
|
220
|
-
* // }
|
|
32
|
+
* pluginClient({ importPath: '../../../client' }) // relative to src/gen/clients/<tag>/
|
|
33
|
+
* pluginClient({ importPath: '@my-org/api-client' }) // a package
|
|
221
34
|
* ```
|
|
222
35
|
*/
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Name of the generated SDK facade class. Also the file name.
|
|
226
|
-
*/
|
|
227
|
-
className: string
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Override how names and file paths are built for the generated client.
|
|
231
|
-
* Methods you omit fall back to the default resolver. `this` is bound to the
|
|
232
|
-
* full resolver, so `this.default(name)` delegates to the built-in implementation.
|
|
233
|
-
*/
|
|
234
|
-
resolver?: Partial<ResolverClient> & ThisType<ResolverClient>
|
|
235
|
-
/**
|
|
236
|
-
* AST visitor applied to each operation node before code is printed.
|
|
237
|
-
* Return `null` or `undefined` to leave the node unchanged.
|
|
238
|
-
*/
|
|
239
|
-
transformer?: ast.Visitor
|
|
240
|
-
/**
|
|
241
|
-
* Custom generators that run alongside the built-in client generators.
|
|
242
|
-
*/
|
|
243
|
-
generators?: Array<Generator<PluginClient>>
|
|
244
|
-
} & ClientImportPath &
|
|
245
|
-
ParamsTypeOptions
|
|
246
|
-
|
|
247
|
-
type ResolvedOptions = {
|
|
248
|
-
output: Output
|
|
249
|
-
exclude: Array<Exclude>
|
|
250
|
-
include: Array<Include> | undefined
|
|
251
|
-
override: Array<Override<ResolvedOptions>>
|
|
252
|
-
group: Group | null
|
|
253
|
-
client: Options['client']
|
|
254
|
-
clientType: NonNullable<Options['clientType']>
|
|
255
|
-
bundle: NonNullable<Options['bundle']>
|
|
256
|
-
parser: NonNullable<Options['parser']>
|
|
257
|
-
urlType: NonNullable<Options['urlType']>
|
|
258
|
-
importPath: Options['importPath']
|
|
259
|
-
baseURL: Options['baseURL']
|
|
260
|
-
dataReturnType: NonNullable<Options['dataReturnType']>
|
|
261
|
-
pathParamsType: NonNullable<NonNullable<Options['pathParamsType']>>
|
|
262
|
-
paramsType: NonNullable<Options['paramsType']>
|
|
263
|
-
paramsCasing: Options['paramsCasing']
|
|
264
|
-
sdk: Options['sdk']
|
|
265
|
-
resolver: ResolverClient
|
|
36
|
+
importPath: string
|
|
266
37
|
}
|
|
267
38
|
|
|
39
|
+
/**
|
|
40
|
+
* Options after `pluginClient` applies its defaults.
|
|
41
|
+
*/
|
|
42
|
+
export type ResolvedOptions = ClientResolvedOptions & { importPath: string }
|
|
43
|
+
/**
|
|
44
|
+
* Plugin type of `@kubb/plugin-client`, registered in the Kubb plugin registry.
|
|
45
|
+
*/
|
|
268
46
|
export type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, ResolverClient>
|
|
269
47
|
|
|
270
48
|
declare global {
|
package/dist/chunk-Bx3C2hgW.cjs
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
//#region \0rolldown/runtime.js
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __name = (target, value) => __defProp(target, "name", {
|
|
5
|
-
value,
|
|
6
|
-
configurable: true
|
|
7
|
-
});
|
|
8
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
11
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
-
key = keys[i];
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
-
get: ((k) => from[k]).bind(null, key),
|
|
17
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
return to;
|
|
21
|
-
};
|
|
22
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
-
value: mod,
|
|
24
|
-
enumerable: true
|
|
25
|
-
}) : target, mod));
|
|
26
|
-
//#endregion
|
|
27
|
-
Object.defineProperty(exports, "__name", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function() {
|
|
30
|
-
return __name;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
Object.defineProperty(exports, "__toESM", {
|
|
34
|
-
enumerable: true,
|
|
35
|
-
get: function() {
|
|
36
|
-
return __toESM;
|
|
37
|
-
}
|
|
38
|
-
});
|
package/dist/clients/axios.cjs
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
Object.defineProperties(exports, {
|
|
2
|
-
__esModule: { value: true },
|
|
3
|
-
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
-
});
|
|
5
|
-
const require_chunk = require("../chunk-Bx3C2hgW.cjs");
|
|
6
|
-
let axios = require("axios");
|
|
7
|
-
axios = require_chunk.__toESM(axios, 1);
|
|
8
|
-
//#region src/clients/axios.ts
|
|
9
|
-
let _config = {
|
|
10
|
-
baseURL: typeof AXIOS_BASE !== "undefined" ? AXIOS_BASE : void 0,
|
|
11
|
-
headers: typeof AXIOS_HEADERS !== "undefined" ? JSON.parse(AXIOS_HEADERS) : void 0
|
|
12
|
-
};
|
|
13
|
-
const getConfig = () => _config;
|
|
14
|
-
const setConfig = (config) => {
|
|
15
|
-
_config = config;
|
|
16
|
-
return getConfig();
|
|
17
|
-
};
|
|
18
|
-
const mergeConfig = (...configs) => {
|
|
19
|
-
return configs.reduce((merged, config) => {
|
|
20
|
-
return {
|
|
21
|
-
...merged,
|
|
22
|
-
...config,
|
|
23
|
-
headers: {
|
|
24
|
-
...Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers,
|
|
25
|
-
...Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
}, {});
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* Serializes header values into the string form axios ultimately puts on the wire.
|
|
32
|
-
* Objects (including arrays) are JSON-stringified so spec-defined object headers like `X-Filter`
|
|
33
|
-
* are sent in their canonical JSON-string form rather than `[object Object]`.
|
|
34
|
-
*/
|
|
35
|
-
function serializeHeaders(headers) {
|
|
36
|
-
if (!headers) return {};
|
|
37
|
-
const entries = Array.isArray(headers) ? headers : Object.entries(headers);
|
|
38
|
-
const result = {};
|
|
39
|
-
for (const [key, value] of entries) {
|
|
40
|
-
if (value === void 0 || value === null) continue;
|
|
41
|
-
result[key] = typeof value === "string" ? value : typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
42
|
-
}
|
|
43
|
-
return result;
|
|
44
|
-
}
|
|
45
|
-
const axiosInstance = axios.default.create(getConfig());
|
|
46
|
-
const client = async (config, _request) => {
|
|
47
|
-
const { contentType, headers, ...axiosConfig } = mergeConfig(getConfig(), config);
|
|
48
|
-
return axiosInstance.request({
|
|
49
|
-
...axiosConfig,
|
|
50
|
-
headers: {
|
|
51
|
-
...contentType && contentType !== "multipart/form-data" ? { "Content-Type": contentType } : {},
|
|
52
|
-
...serializeHeaders(headers)
|
|
53
|
-
}
|
|
54
|
-
}).catch((e) => {
|
|
55
|
-
throw e;
|
|
56
|
-
});
|
|
57
|
-
};
|
|
58
|
-
client.getConfig = getConfig;
|
|
59
|
-
client.setConfig = setConfig;
|
|
60
|
-
//#endregion
|
|
61
|
-
exports.axiosInstance = axiosInstance;
|
|
62
|
-
exports.client = client;
|
|
63
|
-
exports.default = client;
|
|
64
|
-
exports.getConfig = getConfig;
|
|
65
|
-
exports.mergeConfig = mergeConfig;
|
|
66
|
-
exports.setConfig = setConfig;
|
|
67
|
-
|
|
68
|
-
//# sourceMappingURL=axios.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"axios.cjs","names":[],"sources":["../../src/clients/axios.ts"],"sourcesContent":["import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'\nimport axios from 'axios'\n\ndeclare const AXIOS_BASE: string\ndeclare const AXIOS_HEADERS: string\n\n/**\n * Header values may be objects (e.g. JSON-encoded headers like `X-Filter` in the Linode API).\n * Non-string values are JSON-serialized before the request is sent.\n */\nexport type HeaderValue = string | number | boolean | null | undefined | object\nexport type HeadersInit = Array<[string, HeaderValue]> | Record<string, HeaderValue>\n\n/**\n * Subset of AxiosRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'\n params?: unknown\n data?: TData | FormData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n validateStatus?: (status: number) => boolean\n headers?: HeadersInit\n paramsSerializer?: AxiosRequestConfig['paramsSerializer']\n contentType?: string\n}\n\n/**\n * Subset of AxiosResponse\n */\nexport type ResponseConfig<TData = unknown> = {\n data: TData\n status: number\n statusText: string\n headers: AxiosResponse['headers']\n}\n\nexport type ResponseErrorConfig<TError = unknown> = AxiosError<TError>\n\nexport type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(\n config: RequestConfig<TRequestData>,\n request?: unknown,\n) => Promise<ResponseConfig<TResponseData>>\n\nlet _config: Partial<RequestConfig> = {\n baseURL: typeof AXIOS_BASE !== 'undefined' ? AXIOS_BASE : undefined,\n headers: typeof AXIOS_HEADERS !== 'undefined' ? JSON.parse(AXIOS_HEADERS) : undefined,\n}\n\nexport const getConfig = () => _config\n\nexport const setConfig = (config: RequestConfig) => {\n _config = config\n return getConfig()\n}\n\nexport const mergeConfig = <T extends RequestConfig>(...configs: Array<Partial<T>>): Partial<T> => {\n return configs.reduce<Partial<T>>((merged, config) => {\n return {\n ...merged,\n ...config,\n headers: {\n ...(Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers),\n ...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),\n },\n }\n }, {})\n}\n\n/**\n * Serializes header values into the string form axios ultimately puts on the wire.\n * Objects (including arrays) are JSON-stringified so spec-defined object headers like `X-Filter`\n * are sent in their canonical JSON-string form rather than `[object Object]`.\n */\nfunction serializeHeaders(headers: HeadersInit | undefined): Record<string, string> {\n if (!headers) return {}\n const entries = Array.isArray(headers) ? headers : Object.entries(headers)\n const result: Record<string, string> = {}\n for (const [key, value] of entries) {\n if (value === undefined || value === null) continue\n result[key] = typeof value === 'string' ? value : typeof value === 'object' ? JSON.stringify(value) : String(value)\n }\n return result\n}\n\nexport const axiosInstance = axios.create(getConfig() as AxiosRequestConfig)\n\nexport const client = async <TResponseData, TError = unknown, TRequestData = unknown>(\n config: RequestConfig<TRequestData>,\n _request?: unknown,\n): Promise<ResponseConfig<TResponseData>> => {\n const requestConfig = mergeConfig(getConfig(), config)\n const { contentType, headers, ...axiosConfig } = requestConfig\n return axiosInstance\n .request<TResponseData, ResponseConfig<TResponseData>>({\n ...axiosConfig,\n headers: {\n ...(contentType && contentType !== 'multipart/form-data' ? { 'Content-Type': contentType } : {}),\n ...serializeHeaders(headers),\n },\n })\n .catch((e: AxiosError<TError>) => {\n throw e\n })\n}\n\nclient.getConfig = getConfig\nclient.setConfig = setConfig\n\nexport default client\n"],"mappings":";;;;;;;;AA+CA,IAAI,UAAkC;CACpC,SAAS,OAAO,eAAe,cAAc,aAAa,KAAA;CAC1D,SAAS,OAAO,kBAAkB,cAAc,KAAK,MAAM,aAAa,IAAI,KAAA;AAC9E;AAEA,MAAa,kBAAkB;AAE/B,MAAa,aAAa,WAA0B;CAClD,UAAU;CACV,OAAO,UAAU;AACnB;AAEA,MAAa,eAAwC,GAAG,YAA2C;CACjG,OAAO,QAAQ,QAAoB,QAAQ,WAAW;EACpD,OAAO;GACL,GAAG;GACH,GAAG;GACH,SAAS;IACP,GAAI,MAAM,QAAQ,OAAO,OAAO,IAAI,OAAO,YAAY,OAAO,OAAO,IAAI,OAAO;IAChF,GAAI,MAAM,QAAQ,OAAO,OAAO,IAAI,OAAO,YAAY,OAAO,OAAO,IAAI,OAAO;GAClF;EACF;CACF,GAAG,CAAC,CAAC;AACP;;;;;;AAOA,SAAS,iBAAiB,SAA0D;CAClF,IAAI,CAAC,SAAS,OAAO,CAAC;CACtB,MAAM,UAAU,MAAM,QAAQ,OAAO,IAAI,UAAU,OAAO,QAAQ,OAAO;CACzE,MAAM,SAAiC,CAAC;CACxC,KAAK,MAAM,CAAC,KAAK,UAAU,SAAS;EAClC,IAAI,UAAU,KAAA,KAAa,UAAU,MAAM;EAC3C,OAAO,OAAO,OAAO,UAAU,WAAW,QAAQ,OAAO,UAAU,WAAW,KAAK,UAAU,KAAK,IAAI,OAAO,KAAK;CACpH;CACA,OAAO;AACT;AAEA,MAAa,gBAAgB,MAAA,QAAM,OAAO,UAAU,CAAuB;AAE3E,MAAa,SAAS,OACpB,QACA,aAC2C;CAE3C,MAAM,EAAE,aAAa,SAAS,GAAG,gBADX,YAAY,UAAU,GAAG,MACc;CAC7D,OAAO,cACJ,QAAsD;EACrD,GAAG;EACH,SAAS;GACP,GAAI,eAAe,gBAAgB,wBAAwB,EAAE,gBAAgB,YAAY,IAAI,CAAC;GAC9F,GAAG,iBAAiB,OAAO;EAC7B;CACF,CAAC,CAAC,CACD,OAAO,MAA0B;EAChC,MAAM;CACR,CAAC;AACL;AAEA,OAAO,YAAY;AACnB,OAAO,YAAY"}
|
package/dist/clients/axios.d.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { t as __name } from "../chunk-C0LytTxp.js";
|
|
2
|
-
import { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
|
|
3
|
-
|
|
4
|
-
//#region src/clients/axios.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Header values may be objects (e.g. JSON-encoded headers like `X-Filter` in the Linode API).
|
|
7
|
-
* Non-string values are JSON-serialized before the request is sent.
|
|
8
|
-
*/
|
|
9
|
-
type HeaderValue = string | number | boolean | null | undefined | object;
|
|
10
|
-
type HeadersInit = Array<[string, HeaderValue]> | Record<string, HeaderValue>;
|
|
11
|
-
/**
|
|
12
|
-
* Subset of AxiosRequestConfig
|
|
13
|
-
*/
|
|
14
|
-
type RequestConfig<TData = unknown> = {
|
|
15
|
-
baseURL?: string;
|
|
16
|
-
url?: string;
|
|
17
|
-
method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD';
|
|
18
|
-
params?: unknown;
|
|
19
|
-
data?: TData | FormData;
|
|
20
|
-
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream';
|
|
21
|
-
signal?: AbortSignal;
|
|
22
|
-
validateStatus?: (status: number) => boolean;
|
|
23
|
-
headers?: HeadersInit;
|
|
24
|
-
paramsSerializer?: AxiosRequestConfig['paramsSerializer'];
|
|
25
|
-
contentType?: string;
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* Subset of AxiosResponse
|
|
29
|
-
*/
|
|
30
|
-
type ResponseConfig<TData = unknown> = {
|
|
31
|
-
data: TData;
|
|
32
|
-
status: number;
|
|
33
|
-
statusText: string;
|
|
34
|
-
headers: AxiosResponse['headers'];
|
|
35
|
-
};
|
|
36
|
-
type ResponseErrorConfig<TError = unknown> = AxiosError<TError>;
|
|
37
|
-
type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(config: RequestConfig<TRequestData>, request?: unknown) => Promise<ResponseConfig<TResponseData>>;
|
|
38
|
-
declare const getConfig: () => Partial<RequestConfig<unknown>>;
|
|
39
|
-
declare const setConfig: (config: RequestConfig) => Partial<RequestConfig<unknown>>;
|
|
40
|
-
declare const mergeConfig: <T extends RequestConfig>(...configs: Array<Partial<T>>) => Partial<T>;
|
|
41
|
-
declare const axiosInstance: import("axios").AxiosInstance;
|
|
42
|
-
declare const client: {
|
|
43
|
-
<TResponseData, TError = unknown, TRequestData = unknown>(config: RequestConfig<TRequestData>, _request?: unknown): Promise<ResponseConfig<TResponseData>>;
|
|
44
|
-
getConfig: () => Partial<RequestConfig<unknown>>;
|
|
45
|
-
setConfig: (config: RequestConfig) => Partial<RequestConfig<unknown>>;
|
|
46
|
-
};
|
|
47
|
-
//#endregion
|
|
48
|
-
export { Client, HeaderValue, HeadersInit, RequestConfig, ResponseConfig, ResponseErrorConfig, axiosInstance, client, client as default, getConfig, mergeConfig, setConfig };
|
|
49
|
-
//# sourceMappingURL=axios.d.ts.map
|
package/dist/clients/axios.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import "../chunk-C0LytTxp.js";
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
//#region src/clients/axios.ts
|
|
4
|
-
let _config = {
|
|
5
|
-
baseURL: typeof AXIOS_BASE !== "undefined" ? AXIOS_BASE : void 0,
|
|
6
|
-
headers: typeof AXIOS_HEADERS !== "undefined" ? JSON.parse(AXIOS_HEADERS) : void 0
|
|
7
|
-
};
|
|
8
|
-
const getConfig = () => _config;
|
|
9
|
-
const setConfig = (config) => {
|
|
10
|
-
_config = config;
|
|
11
|
-
return getConfig();
|
|
12
|
-
};
|
|
13
|
-
const mergeConfig = (...configs) => {
|
|
14
|
-
return configs.reduce((merged, config) => {
|
|
15
|
-
return {
|
|
16
|
-
...merged,
|
|
17
|
-
...config,
|
|
18
|
-
headers: {
|
|
19
|
-
...Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers,
|
|
20
|
-
...Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
}, {});
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Serializes header values into the string form axios ultimately puts on the wire.
|
|
27
|
-
* Objects (including arrays) are JSON-stringified so spec-defined object headers like `X-Filter`
|
|
28
|
-
* are sent in their canonical JSON-string form rather than `[object Object]`.
|
|
29
|
-
*/
|
|
30
|
-
function serializeHeaders(headers) {
|
|
31
|
-
if (!headers) return {};
|
|
32
|
-
const entries = Array.isArray(headers) ? headers : Object.entries(headers);
|
|
33
|
-
const result = {};
|
|
34
|
-
for (const [key, value] of entries) {
|
|
35
|
-
if (value === void 0 || value === null) continue;
|
|
36
|
-
result[key] = typeof value === "string" ? value : typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
const axiosInstance = axios.create(getConfig());
|
|
41
|
-
const client = async (config, _request) => {
|
|
42
|
-
const { contentType, headers, ...axiosConfig } = mergeConfig(getConfig(), config);
|
|
43
|
-
return axiosInstance.request({
|
|
44
|
-
...axiosConfig,
|
|
45
|
-
headers: {
|
|
46
|
-
...contentType && contentType !== "multipart/form-data" ? { "Content-Type": contentType } : {},
|
|
47
|
-
...serializeHeaders(headers)
|
|
48
|
-
}
|
|
49
|
-
}).catch((e) => {
|
|
50
|
-
throw e;
|
|
51
|
-
});
|
|
52
|
-
};
|
|
53
|
-
client.getConfig = getConfig;
|
|
54
|
-
client.setConfig = setConfig;
|
|
55
|
-
//#endregion
|
|
56
|
-
export { axiosInstance, client, client as default, getConfig, mergeConfig, setConfig };
|
|
57
|
-
|
|
58
|
-
//# sourceMappingURL=axios.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"axios.js","names":[],"sources":["../../src/clients/axios.ts"],"sourcesContent":["import type { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'\nimport axios from 'axios'\n\ndeclare const AXIOS_BASE: string\ndeclare const AXIOS_HEADERS: string\n\n/**\n * Header values may be objects (e.g. JSON-encoded headers like `X-Filter` in the Linode API).\n * Non-string values are JSON-serialized before the request is sent.\n */\nexport type HeaderValue = string | number | boolean | null | undefined | object\nexport type HeadersInit = Array<[string, HeaderValue]> | Record<string, HeaderValue>\n\n/**\n * Subset of AxiosRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'\n params?: unknown\n data?: TData | FormData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n validateStatus?: (status: number) => boolean\n headers?: HeadersInit\n paramsSerializer?: AxiosRequestConfig['paramsSerializer']\n contentType?: string\n}\n\n/**\n * Subset of AxiosResponse\n */\nexport type ResponseConfig<TData = unknown> = {\n data: TData\n status: number\n statusText: string\n headers: AxiosResponse['headers']\n}\n\nexport type ResponseErrorConfig<TError = unknown> = AxiosError<TError>\n\nexport type Client = <TResponseData, _TError = unknown, TRequestData = unknown>(\n config: RequestConfig<TRequestData>,\n request?: unknown,\n) => Promise<ResponseConfig<TResponseData>>\n\nlet _config: Partial<RequestConfig> = {\n baseURL: typeof AXIOS_BASE !== 'undefined' ? AXIOS_BASE : undefined,\n headers: typeof AXIOS_HEADERS !== 'undefined' ? JSON.parse(AXIOS_HEADERS) : undefined,\n}\n\nexport const getConfig = () => _config\n\nexport const setConfig = (config: RequestConfig) => {\n _config = config\n return getConfig()\n}\n\nexport const mergeConfig = <T extends RequestConfig>(...configs: Array<Partial<T>>): Partial<T> => {\n return configs.reduce<Partial<T>>((merged, config) => {\n return {\n ...merged,\n ...config,\n headers: {\n ...(Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers),\n ...(Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers),\n },\n }\n }, {})\n}\n\n/**\n * Serializes header values into the string form axios ultimately puts on the wire.\n * Objects (including arrays) are JSON-stringified so spec-defined object headers like `X-Filter`\n * are sent in their canonical JSON-string form rather than `[object Object]`.\n */\nfunction serializeHeaders(headers: HeadersInit | undefined): Record<string, string> {\n if (!headers) return {}\n const entries = Array.isArray(headers) ? headers : Object.entries(headers)\n const result: Record<string, string> = {}\n for (const [key, value] of entries) {\n if (value === undefined || value === null) continue\n result[key] = typeof value === 'string' ? value : typeof value === 'object' ? JSON.stringify(value) : String(value)\n }\n return result\n}\n\nexport const axiosInstance = axios.create(getConfig() as AxiosRequestConfig)\n\nexport const client = async <TResponseData, TError = unknown, TRequestData = unknown>(\n config: RequestConfig<TRequestData>,\n _request?: unknown,\n): Promise<ResponseConfig<TResponseData>> => {\n const requestConfig = mergeConfig(getConfig(), config)\n const { contentType, headers, ...axiosConfig } = requestConfig\n return axiosInstance\n .request<TResponseData, ResponseConfig<TResponseData>>({\n ...axiosConfig,\n headers: {\n ...(contentType && contentType !== 'multipart/form-data' ? { 'Content-Type': contentType } : {}),\n ...serializeHeaders(headers),\n },\n })\n .catch((e: AxiosError<TError>) => {\n throw e\n })\n}\n\nclient.getConfig = getConfig\nclient.setConfig = setConfig\n\nexport default client\n"],"mappings":";;;AA+CA,IAAI,UAAkC;CACpC,SAAS,OAAO,eAAe,cAAc,aAAa,KAAA;CAC1D,SAAS,OAAO,kBAAkB,cAAc,KAAK,MAAM,aAAa,IAAI,KAAA;AAC9E;AAEA,MAAa,kBAAkB;AAE/B,MAAa,aAAa,WAA0B;CAClD,UAAU;CACV,OAAO,UAAU;AACnB;AAEA,MAAa,eAAwC,GAAG,YAA2C;CACjG,OAAO,QAAQ,QAAoB,QAAQ,WAAW;EACpD,OAAO;GACL,GAAG;GACH,GAAG;GACH,SAAS;IACP,GAAI,MAAM,QAAQ,OAAO,OAAO,IAAI,OAAO,YAAY,OAAO,OAAO,IAAI,OAAO;IAChF,GAAI,MAAM,QAAQ,OAAO,OAAO,IAAI,OAAO,YAAY,OAAO,OAAO,IAAI,OAAO;GAClF;EACF;CACF,GAAG,CAAC,CAAC;AACP;;;;;;AAOA,SAAS,iBAAiB,SAA0D;CAClF,IAAI,CAAC,SAAS,OAAO,CAAC;CACtB,MAAM,UAAU,MAAM,QAAQ,OAAO,IAAI,UAAU,OAAO,QAAQ,OAAO;CACzE,MAAM,SAAiC,CAAC;CACxC,KAAK,MAAM,CAAC,KAAK,UAAU,SAAS;EAClC,IAAI,UAAU,KAAA,KAAa,UAAU,MAAM;EAC3C,OAAO,OAAO,OAAO,UAAU,WAAW,QAAQ,OAAO,UAAU,WAAW,KAAK,UAAU,KAAK,IAAI,OAAO,KAAK;CACpH;CACA,OAAO;AACT;AAEA,MAAa,gBAAgB,MAAM,OAAO,UAAU,CAAuB;AAE3E,MAAa,SAAS,OACpB,QACA,aAC2C;CAE3C,MAAM,EAAE,aAAa,SAAS,GAAG,gBADX,YAAY,UAAU,GAAG,MACc;CAC7D,OAAO,cACJ,QAAsD;EACrD,GAAG;EACH,SAAS;GACP,GAAI,eAAe,gBAAgB,wBAAwB,EAAE,gBAAgB,YAAY,IAAI,CAAC;GAC9F,GAAG,iBAAiB,OAAO;EAC7B;CACF,CAAC,CAAC,CACD,OAAO,MAA0B;EAChC,MAAM;CACR,CAAC;AACL;AAEA,OAAO,YAAY;AACnB,OAAO,YAAY"}
|
package/dist/clients/fetch.cjs
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
Object.defineProperties(exports, {
|
|
2
|
-
__esModule: { value: true },
|
|
3
|
-
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
-
});
|
|
5
|
-
require("../chunk-Bx3C2hgW.cjs");
|
|
6
|
-
//#region src/clients/fetch.ts
|
|
7
|
-
let _config = {};
|
|
8
|
-
const getConfig = () => _config;
|
|
9
|
-
const setConfig = (config) => {
|
|
10
|
-
_config = config;
|
|
11
|
-
return getConfig();
|
|
12
|
-
};
|
|
13
|
-
const mergeConfig = (...configs) => {
|
|
14
|
-
return configs.reduce((merged, config) => {
|
|
15
|
-
return {
|
|
16
|
-
...merged,
|
|
17
|
-
...config,
|
|
18
|
-
headers: {
|
|
19
|
-
...Array.isArray(merged.headers) ? Object.fromEntries(merged.headers) : merged.headers,
|
|
20
|
-
...Array.isArray(config.headers) ? Object.fromEntries(config.headers) : config.headers
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
}, {});
|
|
24
|
-
};
|
|
25
|
-
/**
|
|
26
|
-
* Serializes header values into the string form `fetch` expects.
|
|
27
|
-
* Objects (including arrays) are JSON-stringified so spec-defined object
|
|
28
|
-
* headers like `X-Filter` are sent in their canonical JSON-string form.
|
|
29
|
-
*/
|
|
30
|
-
function serializeHeaders(headers) {
|
|
31
|
-
if (!headers) return {};
|
|
32
|
-
const entries = Array.isArray(headers) ? headers : Object.entries(headers);
|
|
33
|
-
const result = {};
|
|
34
|
-
for (const [key, value] of entries) {
|
|
35
|
-
if (value === void 0 || value === null) continue;
|
|
36
|
-
result[key] = typeof value === "string" ? value : typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Serializes the request body into the form `fetch` expects.
|
|
42
|
-
* `FormData`, `URLSearchParams`, `Blob`, `ArrayBuffer` and string bodies are passed through
|
|
43
|
-
* untouched. Plain objects are encoded as `URLSearchParams` for `application/x-www-form-urlencoded`
|
|
44
|
-
* and JSON-serialized otherwise.
|
|
45
|
-
*/
|
|
46
|
-
function serializeBody(data, contentType) {
|
|
47
|
-
if (data === void 0 || data === null) return void 0;
|
|
48
|
-
if (data instanceof FormData || data instanceof URLSearchParams || data instanceof Blob || data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return data;
|
|
49
|
-
if (typeof data === "string") return data;
|
|
50
|
-
if (contentType?.includes("application/x-www-form-urlencoded")) return new URLSearchParams(data);
|
|
51
|
-
return JSON.stringify(data);
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Picks a `responseType` from a `Content-Type` header, or `undefined` when it is not recognized.
|
|
55
|
-
*/
|
|
56
|
-
function detectResponseType(contentType) {
|
|
57
|
-
if (!contentType) return void 0;
|
|
58
|
-
if (contentType.includes("application/json") || contentType.includes("text/json")) return "json";
|
|
59
|
-
if (contentType.includes("text/")) return "text";
|
|
60
|
-
if (contentType.includes("image/") || contentType.includes("application/octet-stream")) return "blob";
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Parses a `fetch` response body.
|
|
64
|
-
*
|
|
65
|
-
* - Empty responses (204/205/304 or no body) resolve to `{}`.
|
|
66
|
-
* - An explicit `responseType` (or one detected from the `Content-Type` header) forces the matching
|
|
67
|
-
* `Response` method.
|
|
68
|
-
* - As a last resort the body is read as text and `JSON.parse`d, falling back to the raw text.
|
|
69
|
-
*/
|
|
70
|
-
async function parseResponse(response, responseType) {
|
|
71
|
-
if ([
|
|
72
|
-
204,
|
|
73
|
-
205,
|
|
74
|
-
304
|
|
75
|
-
].includes(response.status) || !response.body) return {};
|
|
76
|
-
switch (responseType ?? detectResponseType(response.headers.get("Content-Type"))) {
|
|
77
|
-
case "text":
|
|
78
|
-
case "document": return await response.text();
|
|
79
|
-
case "blob": return await response.blob();
|
|
80
|
-
case "arraybuffer": return await response.arrayBuffer();
|
|
81
|
-
case "stream": return response.body;
|
|
82
|
-
case "json": return await response.json();
|
|
83
|
-
}
|
|
84
|
-
if (responseType) return await response.json();
|
|
85
|
-
const text = await response.text();
|
|
86
|
-
if (!text) return {};
|
|
87
|
-
try {
|
|
88
|
-
return JSON.parse(text);
|
|
89
|
-
} catch {
|
|
90
|
-
return text;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
const client = async (paramsConfig, _request) => {
|
|
94
|
-
const normalizedParams = new URLSearchParams();
|
|
95
|
-
const config = mergeConfig(getConfig(), paramsConfig);
|
|
96
|
-
Object.entries(config.params || {}).forEach(([key, value]) => {
|
|
97
|
-
if (value !== void 0) normalizedParams.append(key, value === null ? "null" : value.toString());
|
|
98
|
-
});
|
|
99
|
-
let targetUrl = [config.baseURL, config.url].filter(Boolean).join("");
|
|
100
|
-
if (config.params) targetUrl += `?${normalizedParams}`;
|
|
101
|
-
const headers = {
|
|
102
|
-
...config.contentType && config.contentType !== "multipart/form-data" ? { "Content-Type": config.contentType } : {},
|
|
103
|
-
...serializeHeaders(config.headers)
|
|
104
|
-
};
|
|
105
|
-
const response = await globalThis.fetch(targetUrl, {
|
|
106
|
-
credentials: config.credentials || "same-origin",
|
|
107
|
-
method: config.method?.toUpperCase(),
|
|
108
|
-
body: serializeBody(config.data, headers["Content-Type"] ?? headers["content-type"]),
|
|
109
|
-
signal: config.signal,
|
|
110
|
-
headers
|
|
111
|
-
});
|
|
112
|
-
return {
|
|
113
|
-
data: await parseResponse(response, config.responseType),
|
|
114
|
-
status: response.status,
|
|
115
|
-
statusText: response.statusText,
|
|
116
|
-
headers: response.headers
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
client.getConfig = getConfig;
|
|
120
|
-
client.setConfig = setConfig;
|
|
121
|
-
//#endregion
|
|
122
|
-
exports.client = client;
|
|
123
|
-
exports.default = client;
|
|
124
|
-
exports.getConfig = getConfig;
|
|
125
|
-
exports.mergeConfig = mergeConfig;
|
|
126
|
-
exports.setConfig = setConfig;
|
|
127
|
-
|
|
128
|
-
//# sourceMappingURL=fetch.cjs.map
|