@hey-api/openapi-ts 0.0.0-next-20260205083026
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/LICENSE.md +21 -0
- package/README.md +381 -0
- package/bin/run.cmd +3 -0
- package/bin/run.js +18 -0
- package/dist/clients/angular/client.ts +237 -0
- package/dist/clients/angular/index.ts +23 -0
- package/dist/clients/angular/types.ts +231 -0
- package/dist/clients/angular/utils.ts +408 -0
- package/dist/clients/axios/client.ts +154 -0
- package/dist/clients/axios/index.ts +21 -0
- package/dist/clients/axios/types.ts +158 -0
- package/dist/clients/axios/utils.ts +206 -0
- package/dist/clients/core/auth.ts +39 -0
- package/dist/clients/core/bodySerializer.ts +82 -0
- package/dist/clients/core/params.ts +167 -0
- package/dist/clients/core/pathSerializer.ts +169 -0
- package/dist/clients/core/queryKeySerializer.ts +115 -0
- package/dist/clients/core/serverSentEvents.ts +241 -0
- package/dist/clients/core/types.ts +102 -0
- package/dist/clients/core/utils.ts +138 -0
- package/dist/clients/fetch/client.ts +286 -0
- package/dist/clients/fetch/index.ts +23 -0
- package/dist/clients/fetch/types.ts +211 -0
- package/dist/clients/fetch/utils.ts +314 -0
- package/dist/clients/ky/client.ts +318 -0
- package/dist/clients/ky/index.ts +24 -0
- package/dist/clients/ky/types.ts +243 -0
- package/dist/clients/ky/utils.ts +312 -0
- package/dist/clients/next/client.ts +253 -0
- package/dist/clients/next/index.ts +21 -0
- package/dist/clients/next/types.ts +162 -0
- package/dist/clients/next/utils.ts +413 -0
- package/dist/clients/nuxt/client.ts +213 -0
- package/dist/clients/nuxt/index.ts +22 -0
- package/dist/clients/nuxt/types.ts +189 -0
- package/dist/clients/nuxt/utils.ts +384 -0
- package/dist/clients/ofetch/client.ts +259 -0
- package/dist/clients/ofetch/index.ts +23 -0
- package/dist/clients/ofetch/types.ts +275 -0
- package/dist/clients/ofetch/utils.ts +504 -0
- package/dist/index.d.mts +8634 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +4 -0
- package/dist/init-DlaW5Djq.mjs +13832 -0
- package/dist/init-DlaW5Djq.mjs.map +1 -0
- package/dist/internal.d.mts +33 -0
- package/dist/internal.d.mts.map +1 -0
- package/dist/internal.mjs +4 -0
- package/dist/run.d.mts +1 -0
- package/dist/run.mjs +60 -0
- package/dist/run.mjs.map +1 -0
- package/dist/src-BYA2YioO.mjs +225 -0
- package/dist/src-BYA2YioO.mjs.map +1 -0
- package/dist/types-Ba27ofyy.d.mts +157 -0
- package/dist/types-Ba27ofyy.d.mts.map +1 -0
- package/package.json +109 -0
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import { getAuthToken } from '../core/auth';
|
|
2
|
+
import type { QuerySerializerOptions } from '../core/bodySerializer';
|
|
3
|
+
import { jsonBodySerializer } from '../core/bodySerializer';
|
|
4
|
+
import {
|
|
5
|
+
serializeArrayParam,
|
|
6
|
+
serializeObjectParam,
|
|
7
|
+
serializePrimitiveParam,
|
|
8
|
+
} from '../core/pathSerializer';
|
|
9
|
+
import { getUrl } from '../core/utils';
|
|
10
|
+
import type { Client, ClientOptions, Config, RequestOptions } from './types';
|
|
11
|
+
|
|
12
|
+
export const createQuerySerializer = <T = unknown>({
|
|
13
|
+
parameters = {},
|
|
14
|
+
...args
|
|
15
|
+
}: QuerySerializerOptions = {}) => {
|
|
16
|
+
const querySerializer = (queryParams: T) => {
|
|
17
|
+
const search: string[] = [];
|
|
18
|
+
if (queryParams && typeof queryParams === 'object') {
|
|
19
|
+
for (const name in queryParams) {
|
|
20
|
+
const value = queryParams[name];
|
|
21
|
+
|
|
22
|
+
if (value === undefined || value === null) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const options = parameters[name] || args;
|
|
27
|
+
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
const serializedArray = serializeArrayParam({
|
|
30
|
+
allowReserved: options.allowReserved,
|
|
31
|
+
explode: true,
|
|
32
|
+
name,
|
|
33
|
+
style: 'form',
|
|
34
|
+
value,
|
|
35
|
+
...options.array,
|
|
36
|
+
});
|
|
37
|
+
if (serializedArray) search.push(serializedArray);
|
|
38
|
+
} else if (typeof value === 'object') {
|
|
39
|
+
const serializedObject = serializeObjectParam({
|
|
40
|
+
allowReserved: options.allowReserved,
|
|
41
|
+
explode: true,
|
|
42
|
+
name,
|
|
43
|
+
style: 'deepObject',
|
|
44
|
+
value: value as Record<string, unknown>,
|
|
45
|
+
...options.object,
|
|
46
|
+
});
|
|
47
|
+
if (serializedObject) search.push(serializedObject);
|
|
48
|
+
} else {
|
|
49
|
+
const serializedPrimitive = serializePrimitiveParam({
|
|
50
|
+
allowReserved: options.allowReserved,
|
|
51
|
+
name,
|
|
52
|
+
value: value as string,
|
|
53
|
+
});
|
|
54
|
+
if (serializedPrimitive) search.push(serializedPrimitive);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return search.join('&');
|
|
59
|
+
};
|
|
60
|
+
return querySerializer;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Infers parseAs value from provided Content-Type header.
|
|
65
|
+
*/
|
|
66
|
+
export const getParseAs = (contentType: string | null): Exclude<Config['parseAs'], 'auto'> => {
|
|
67
|
+
if (!contentType) {
|
|
68
|
+
return 'stream';
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const cleanContent = contentType.split(';')[0]?.trim();
|
|
72
|
+
|
|
73
|
+
if (!cleanContent) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (cleanContent.startsWith('application/json') || cleanContent.endsWith('+json')) {
|
|
78
|
+
return 'json';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (cleanContent === 'multipart/form-data') {
|
|
82
|
+
return 'formData';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (
|
|
86
|
+
['application/', 'audio/', 'image/', 'video/'].some((type) => cleanContent.startsWith(type))
|
|
87
|
+
) {
|
|
88
|
+
return 'blob';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (cleanContent.startsWith('text/')) {
|
|
92
|
+
return 'text';
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const checkForExistence = (
|
|
99
|
+
options: Pick<RequestOptions, 'auth' | 'query'> & {
|
|
100
|
+
headers: Headers;
|
|
101
|
+
},
|
|
102
|
+
name?: string,
|
|
103
|
+
): boolean => {
|
|
104
|
+
if (!name) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (
|
|
108
|
+
options.headers.has(name) ||
|
|
109
|
+
options.query?.[name] ||
|
|
110
|
+
options.headers.get('Cookie')?.includes(`${name}=`)
|
|
111
|
+
) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const setAuthParams = async ({
|
|
118
|
+
security,
|
|
119
|
+
...options
|
|
120
|
+
}: Pick<Required<RequestOptions>, 'security'> &
|
|
121
|
+
Pick<RequestOptions, 'auth' | 'query'> & {
|
|
122
|
+
headers: Headers;
|
|
123
|
+
}) => {
|
|
124
|
+
for (const auth of security) {
|
|
125
|
+
if (checkForExistence(options, auth.name)) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const token = await getAuthToken(auth, options.auth);
|
|
130
|
+
|
|
131
|
+
if (!token) {
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const name = auth.name ?? 'Authorization';
|
|
136
|
+
|
|
137
|
+
switch (auth.in) {
|
|
138
|
+
case 'query':
|
|
139
|
+
if (!options.query) {
|
|
140
|
+
options.query = {};
|
|
141
|
+
}
|
|
142
|
+
options.query[name] = token;
|
|
143
|
+
break;
|
|
144
|
+
case 'cookie':
|
|
145
|
+
options.headers.append('Cookie', `${name}=${token}`);
|
|
146
|
+
break;
|
|
147
|
+
case 'header':
|
|
148
|
+
default:
|
|
149
|
+
options.headers.set(name, token);
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const buildUrl: Client['buildUrl'] = (options) =>
|
|
156
|
+
getUrl({
|
|
157
|
+
baseUrl: options.baseUrl as string,
|
|
158
|
+
path: options.path,
|
|
159
|
+
query: options.query,
|
|
160
|
+
querySerializer:
|
|
161
|
+
typeof options.querySerializer === 'function'
|
|
162
|
+
? options.querySerializer
|
|
163
|
+
: createQuerySerializer(options.querySerializer),
|
|
164
|
+
url: options.url,
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
export const mergeConfigs = (a: Config, b: Config): Config => {
|
|
168
|
+
const config = { ...a, ...b };
|
|
169
|
+
if (config.baseUrl?.endsWith('/')) {
|
|
170
|
+
config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
|
|
171
|
+
}
|
|
172
|
+
config.headers = mergeHeaders(a.headers, b.headers);
|
|
173
|
+
return config;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const headersEntries = (headers: Headers): Array<[string, string]> => {
|
|
177
|
+
const entries: Array<[string, string]> = [];
|
|
178
|
+
headers.forEach((value, key) => {
|
|
179
|
+
entries.push([key, value]);
|
|
180
|
+
});
|
|
181
|
+
return entries;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
export const mergeHeaders = (
|
|
185
|
+
...headers: Array<Required<Config>['headers'] | undefined>
|
|
186
|
+
): Headers => {
|
|
187
|
+
const mergedHeaders = new Headers();
|
|
188
|
+
for (const header of headers) {
|
|
189
|
+
if (!header) {
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);
|
|
194
|
+
|
|
195
|
+
for (const [key, value] of iterator) {
|
|
196
|
+
if (value === null) {
|
|
197
|
+
mergedHeaders.delete(key);
|
|
198
|
+
} else if (Array.isArray(value)) {
|
|
199
|
+
for (const v of value) {
|
|
200
|
+
mergedHeaders.append(key, v as string);
|
|
201
|
+
}
|
|
202
|
+
} else if (value !== undefined) {
|
|
203
|
+
mergedHeaders.set(
|
|
204
|
+
key,
|
|
205
|
+
typeof value === 'object' ? JSON.stringify(value) : (value as string),
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return mergedHeaders;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
type ErrInterceptor<Err, Res, Req, Options> = (
|
|
214
|
+
error: Err,
|
|
215
|
+
response: Res,
|
|
216
|
+
request: Req,
|
|
217
|
+
options: Options,
|
|
218
|
+
) => Err | Promise<Err>;
|
|
219
|
+
|
|
220
|
+
type ReqInterceptor<Req, Options> = (request: Req, options: Options) => Req | Promise<Req>;
|
|
221
|
+
|
|
222
|
+
type ResInterceptor<Res, Req, Options> = (
|
|
223
|
+
response: Res,
|
|
224
|
+
request: Req,
|
|
225
|
+
options: Options,
|
|
226
|
+
) => Res | Promise<Res>;
|
|
227
|
+
|
|
228
|
+
class Interceptors<Interceptor> {
|
|
229
|
+
fns: Array<Interceptor | null> = [];
|
|
230
|
+
|
|
231
|
+
clear(): void {
|
|
232
|
+
this.fns = [];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
eject(id: number | Interceptor): void {
|
|
236
|
+
const index = this.getInterceptorIndex(id);
|
|
237
|
+
if (this.fns[index]) {
|
|
238
|
+
this.fns[index] = null;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
exists(id: number | Interceptor): boolean {
|
|
243
|
+
const index = this.getInterceptorIndex(id);
|
|
244
|
+
return Boolean(this.fns[index]);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
getInterceptorIndex(id: number | Interceptor): number {
|
|
248
|
+
if (typeof id === 'number') {
|
|
249
|
+
return this.fns[id] ? id : -1;
|
|
250
|
+
}
|
|
251
|
+
return this.fns.indexOf(id);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
update(id: number | Interceptor, fn: Interceptor): number | Interceptor | false {
|
|
255
|
+
const index = this.getInterceptorIndex(id);
|
|
256
|
+
if (this.fns[index]) {
|
|
257
|
+
this.fns[index] = fn;
|
|
258
|
+
return id;
|
|
259
|
+
}
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
use(fn: Interceptor): number {
|
|
264
|
+
this.fns.push(fn);
|
|
265
|
+
return this.fns.length - 1;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export interface Middleware<Req, Res, Err, Options> {
|
|
270
|
+
error: Interceptors<ErrInterceptor<Err, Res, Req, Options>>;
|
|
271
|
+
request: Interceptors<ReqInterceptor<Req, Options>>;
|
|
272
|
+
response: Interceptors<ResInterceptor<Res, Req, Options>>;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
export const createInterceptors = <Req, Res, Err, Options>(): Middleware<
|
|
276
|
+
Req,
|
|
277
|
+
Res,
|
|
278
|
+
Err,
|
|
279
|
+
Options
|
|
280
|
+
> => ({
|
|
281
|
+
error: new Interceptors<ErrInterceptor<Err, Res, Req, Options>>(),
|
|
282
|
+
request: new Interceptors<ReqInterceptor<Req, Options>>(),
|
|
283
|
+
response: new Interceptors<ResInterceptor<Res, Req, Options>>(),
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const defaultQuerySerializer = createQuerySerializer({
|
|
287
|
+
allowReserved: false,
|
|
288
|
+
array: {
|
|
289
|
+
explode: true,
|
|
290
|
+
style: 'form',
|
|
291
|
+
},
|
|
292
|
+
object: {
|
|
293
|
+
explode: true,
|
|
294
|
+
style: 'deepObject',
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
const defaultHeaders = {
|
|
299
|
+
'Content-Type': 'application/json',
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
export const createConfig = <T extends ClientOptions = ClientOptions>(
|
|
303
|
+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
|
|
304
|
+
): Config<Omit<ClientOptions, keyof T> & T> => ({
|
|
305
|
+
...jsonBodySerializer,
|
|
306
|
+
headers: defaultHeaders,
|
|
307
|
+
parseAs: 'auto',
|
|
308
|
+
querySerializer: defaultQuerySerializer,
|
|
309
|
+
throwOnError: false,
|
|
310
|
+
timeout: 10000,
|
|
311
|
+
...override,
|
|
312
|
+
});
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { createSseClient } from '../core/serverSentEvents';
|
|
2
|
+
import type { HttpMethod } from '../core/types';
|
|
3
|
+
import { getValidRequestBody } from '../core/utils';
|
|
4
|
+
import type { Client, Config, RequestOptions, ResolvedRequestOptions } from './types';
|
|
5
|
+
import {
|
|
6
|
+
buildUrl,
|
|
7
|
+
createConfig,
|
|
8
|
+
createInterceptors,
|
|
9
|
+
getParseAs,
|
|
10
|
+
mergeConfigs,
|
|
11
|
+
mergeHeaders,
|
|
12
|
+
setAuthParams,
|
|
13
|
+
} from './utils';
|
|
14
|
+
|
|
15
|
+
type ReqInit = Omit<RequestInit, 'body' | 'headers'> & {
|
|
16
|
+
body?: any;
|
|
17
|
+
headers: ReturnType<typeof mergeHeaders>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const createClient = (config: Config = {}): Client => {
|
|
21
|
+
let _config = mergeConfigs(createConfig(), config);
|
|
22
|
+
|
|
23
|
+
const getConfig = (): Config => ({ ..._config });
|
|
24
|
+
|
|
25
|
+
const setConfig = (config: Config): Config => {
|
|
26
|
+
_config = mergeConfigs(_config, config);
|
|
27
|
+
return getConfig();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const interceptors = createInterceptors<Response, unknown, ResolvedRequestOptions>();
|
|
31
|
+
|
|
32
|
+
const beforeRequest = async (options: RequestOptions) => {
|
|
33
|
+
const opts = {
|
|
34
|
+
..._config,
|
|
35
|
+
...options,
|
|
36
|
+
fetch: options.fetch ?? _config.fetch ?? globalThis.fetch,
|
|
37
|
+
headers: mergeHeaders(_config.headers, options.headers),
|
|
38
|
+
serializedBody: undefined,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
if (opts.security) {
|
|
42
|
+
await setAuthParams({
|
|
43
|
+
...opts,
|
|
44
|
+
security: opts.security,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (opts.requestValidator) {
|
|
49
|
+
await opts.requestValidator(opts);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (opts.body !== undefined && opts.bodySerializer) {
|
|
53
|
+
opts.serializedBody = opts.bodySerializer(opts.body);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// remove Content-Type header if body is empty to avoid sending invalid requests
|
|
57
|
+
if (opts.body === undefined || opts.serializedBody === '') {
|
|
58
|
+
opts.headers.delete('Content-Type');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const url = buildUrl(opts);
|
|
62
|
+
|
|
63
|
+
return { opts, url };
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// @ts-expect-error
|
|
67
|
+
const request: Client['request'] = async (options) => {
|
|
68
|
+
// @ts-expect-error
|
|
69
|
+
const { opts, url } = await beforeRequest(options);
|
|
70
|
+
|
|
71
|
+
for (const fn of interceptors.request.fns) {
|
|
72
|
+
if (fn) {
|
|
73
|
+
await fn(opts);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
78
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
79
|
+
const _fetch = opts.fetch!;
|
|
80
|
+
const requestInit: ReqInit = {
|
|
81
|
+
...opts,
|
|
82
|
+
body: getValidRequestBody(opts),
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
let response = await _fetch(url, requestInit);
|
|
86
|
+
|
|
87
|
+
for (const fn of interceptors.response.fns) {
|
|
88
|
+
if (fn) {
|
|
89
|
+
response = await fn(response, opts);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const result = {
|
|
94
|
+
response,
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
if (response.ok) {
|
|
98
|
+
const parseAs =
|
|
99
|
+
(opts.parseAs === 'auto'
|
|
100
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
101
|
+
: opts.parseAs) ?? 'json';
|
|
102
|
+
|
|
103
|
+
if (response.status === 204 || response.headers.get('Content-Length') === '0') {
|
|
104
|
+
let emptyData: any;
|
|
105
|
+
switch (parseAs) {
|
|
106
|
+
case 'arrayBuffer':
|
|
107
|
+
case 'blob':
|
|
108
|
+
case 'text':
|
|
109
|
+
emptyData = await response[parseAs]();
|
|
110
|
+
break;
|
|
111
|
+
case 'formData':
|
|
112
|
+
emptyData = new FormData();
|
|
113
|
+
break;
|
|
114
|
+
case 'stream':
|
|
115
|
+
emptyData = response.body;
|
|
116
|
+
break;
|
|
117
|
+
case 'json':
|
|
118
|
+
default:
|
|
119
|
+
emptyData = {};
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
data: emptyData,
|
|
124
|
+
...result,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
let data: any;
|
|
129
|
+
switch (parseAs) {
|
|
130
|
+
case 'arrayBuffer':
|
|
131
|
+
case 'blob':
|
|
132
|
+
case 'formData':
|
|
133
|
+
case 'text':
|
|
134
|
+
data = await response[parseAs]();
|
|
135
|
+
break;
|
|
136
|
+
case 'json': {
|
|
137
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
138
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
139
|
+
const text = await response.text();
|
|
140
|
+
data = text ? JSON.parse(text) : {};
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case 'stream':
|
|
144
|
+
return {
|
|
145
|
+
data: response.body,
|
|
146
|
+
...result,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (parseAs === 'json') {
|
|
151
|
+
if (opts.responseValidator) {
|
|
152
|
+
await opts.responseValidator(data);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (opts.responseTransformer) {
|
|
156
|
+
data = await opts.responseTransformer(data);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
data,
|
|
162
|
+
...result,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const textError = await response.text();
|
|
167
|
+
let jsonError: unknown;
|
|
168
|
+
|
|
169
|
+
try {
|
|
170
|
+
jsonError = JSON.parse(textError);
|
|
171
|
+
} catch {
|
|
172
|
+
// noop
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const error = jsonError ?? textError;
|
|
176
|
+
let finalError = error;
|
|
177
|
+
|
|
178
|
+
for (const fn of interceptors.error.fns) {
|
|
179
|
+
if (fn) {
|
|
180
|
+
finalError = (await fn(error, response, opts)) as string;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
finalError = finalError || ({} as string);
|
|
185
|
+
|
|
186
|
+
if (opts.throwOnError) {
|
|
187
|
+
throw finalError;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
error: finalError,
|
|
192
|
+
...result,
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const makeMethodFn = (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
197
|
+
request({ ...options, method });
|
|
198
|
+
|
|
199
|
+
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
|
|
200
|
+
const { opts, url } = await beforeRequest(options);
|
|
201
|
+
return createSseClient({
|
|
202
|
+
...opts,
|
|
203
|
+
body: opts.body as BodyInit | null | undefined,
|
|
204
|
+
headers: opts.headers as unknown as Record<string, string>,
|
|
205
|
+
method,
|
|
206
|
+
onRequest: async (url, init) => {
|
|
207
|
+
let request = new Request(url, init);
|
|
208
|
+
const requestInit = {
|
|
209
|
+
...init,
|
|
210
|
+
method: init.method as Config['method'],
|
|
211
|
+
url,
|
|
212
|
+
};
|
|
213
|
+
for (const fn of interceptors.request.fns) {
|
|
214
|
+
if (fn) {
|
|
215
|
+
await fn(requestInit);
|
|
216
|
+
request = new Request(requestInit.url, requestInit);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return request;
|
|
220
|
+
},
|
|
221
|
+
serializedBody: getValidRequestBody(opts) as BodyInit | null | undefined,
|
|
222
|
+
url,
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
return {
|
|
227
|
+
buildUrl,
|
|
228
|
+
connect: makeMethodFn('CONNECT'),
|
|
229
|
+
delete: makeMethodFn('DELETE'),
|
|
230
|
+
get: makeMethodFn('GET'),
|
|
231
|
+
getConfig,
|
|
232
|
+
head: makeMethodFn('HEAD'),
|
|
233
|
+
interceptors,
|
|
234
|
+
options: makeMethodFn('OPTIONS'),
|
|
235
|
+
patch: makeMethodFn('PATCH'),
|
|
236
|
+
post: makeMethodFn('POST'),
|
|
237
|
+
put: makeMethodFn('PUT'),
|
|
238
|
+
request,
|
|
239
|
+
setConfig,
|
|
240
|
+
sse: {
|
|
241
|
+
connect: makeSseFn('CONNECT'),
|
|
242
|
+
delete: makeSseFn('DELETE'),
|
|
243
|
+
get: makeSseFn('GET'),
|
|
244
|
+
head: makeSseFn('HEAD'),
|
|
245
|
+
options: makeSseFn('OPTIONS'),
|
|
246
|
+
patch: makeSseFn('PATCH'),
|
|
247
|
+
post: makeSseFn('POST'),
|
|
248
|
+
put: makeSseFn('PUT'),
|
|
249
|
+
trace: makeSseFn('TRACE'),
|
|
250
|
+
},
|
|
251
|
+
trace: makeMethodFn('TRACE'),
|
|
252
|
+
} as Client;
|
|
253
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type { Auth } from '../core/auth';
|
|
2
|
+
export type { QuerySerializerOptions } from '../core/bodySerializer';
|
|
3
|
+
export {
|
|
4
|
+
formDataBodySerializer,
|
|
5
|
+
jsonBodySerializer,
|
|
6
|
+
urlSearchParamsBodySerializer,
|
|
7
|
+
} from '../core/bodySerializer';
|
|
8
|
+
export { buildClientParams } from '../core/params';
|
|
9
|
+
export { serializeQueryKeyValue } from '../core/queryKeySerializer';
|
|
10
|
+
export { createClient } from './client';
|
|
11
|
+
export type {
|
|
12
|
+
Client,
|
|
13
|
+
ClientOptions,
|
|
14
|
+
Config,
|
|
15
|
+
CreateClientConfig,
|
|
16
|
+
Options,
|
|
17
|
+
RequestOptions,
|
|
18
|
+
RequestResult,
|
|
19
|
+
TDataShape,
|
|
20
|
+
} from './types';
|
|
21
|
+
export { createConfig } from './utils';
|