@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,102 @@
|
|
|
1
|
+
import type { Auth, AuthToken } from './auth';
|
|
2
|
+
import type { BodySerializer, QuerySerializer, QuerySerializerOptions } from './bodySerializer';
|
|
3
|
+
|
|
4
|
+
export type HttpMethod =
|
|
5
|
+
| 'connect'
|
|
6
|
+
| 'delete'
|
|
7
|
+
| 'get'
|
|
8
|
+
| 'head'
|
|
9
|
+
| 'options'
|
|
10
|
+
| 'patch'
|
|
11
|
+
| 'post'
|
|
12
|
+
| 'put'
|
|
13
|
+
| 'trace';
|
|
14
|
+
|
|
15
|
+
export type Client<
|
|
16
|
+
RequestFn = never,
|
|
17
|
+
Config = unknown,
|
|
18
|
+
MethodFn = never,
|
|
19
|
+
BuildUrlFn = never,
|
|
20
|
+
SseFn = never,
|
|
21
|
+
> = {
|
|
22
|
+
/**
|
|
23
|
+
* Returns the final request URL.
|
|
24
|
+
*/
|
|
25
|
+
buildUrl: BuildUrlFn;
|
|
26
|
+
getConfig: () => Config;
|
|
27
|
+
request: RequestFn;
|
|
28
|
+
setConfig: (config: Config) => Config;
|
|
29
|
+
} & {
|
|
30
|
+
[K in HttpMethod]: MethodFn;
|
|
31
|
+
} & ([SseFn] extends [never] ? { sse?: never } : { sse: { [K in HttpMethod]: SseFn } });
|
|
32
|
+
|
|
33
|
+
export interface Config {
|
|
34
|
+
/**
|
|
35
|
+
* Auth token or a function returning auth token. The resolved value will be
|
|
36
|
+
* added to the request payload as defined by its `security` array.
|
|
37
|
+
*/
|
|
38
|
+
auth?: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken;
|
|
39
|
+
/**
|
|
40
|
+
* A function for serializing request body parameter. By default,
|
|
41
|
+
* {@link JSON.stringify()} will be used.
|
|
42
|
+
*/
|
|
43
|
+
bodySerializer?: BodySerializer | null;
|
|
44
|
+
/**
|
|
45
|
+
* An object containing any HTTP headers that you want to pre-populate your
|
|
46
|
+
* `Headers` object with.
|
|
47
|
+
*
|
|
48
|
+
* {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more}
|
|
49
|
+
*/
|
|
50
|
+
headers?:
|
|
51
|
+
| RequestInit['headers']
|
|
52
|
+
| Record<
|
|
53
|
+
string,
|
|
54
|
+
string | number | boolean | (string | number | boolean)[] | null | undefined | unknown
|
|
55
|
+
>;
|
|
56
|
+
/**
|
|
57
|
+
* The request method.
|
|
58
|
+
*
|
|
59
|
+
* {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more}
|
|
60
|
+
*/
|
|
61
|
+
method?: Uppercase<HttpMethod>;
|
|
62
|
+
/**
|
|
63
|
+
* A function for serializing request query parameters. By default, arrays
|
|
64
|
+
* will be exploded in form style, objects will be exploded in deepObject
|
|
65
|
+
* style, and reserved characters are percent-encoded.
|
|
66
|
+
*
|
|
67
|
+
* This method will have no effect if the native `paramsSerializer()` Axios
|
|
68
|
+
* API function is used.
|
|
69
|
+
*
|
|
70
|
+
* {@link https://swagger.io/docs/specification/serialization/#query View examples}
|
|
71
|
+
*/
|
|
72
|
+
querySerializer?: QuerySerializer | QuerySerializerOptions;
|
|
73
|
+
/**
|
|
74
|
+
* A function validating request data. This is useful if you want to ensure
|
|
75
|
+
* the request conforms to the desired shape, so it can be safely sent to
|
|
76
|
+
* the server.
|
|
77
|
+
*/
|
|
78
|
+
requestValidator?: (data: unknown) => Promise<unknown>;
|
|
79
|
+
/**
|
|
80
|
+
* A function transforming response data before it's returned. This is useful
|
|
81
|
+
* for post-processing data, e.g. converting ISO strings into Date objects.
|
|
82
|
+
*/
|
|
83
|
+
responseTransformer?: (data: unknown) => Promise<unknown>;
|
|
84
|
+
/**
|
|
85
|
+
* A function validating response data. This is useful if you want to ensure
|
|
86
|
+
* the response conforms to the desired shape, so it can be safely passed to
|
|
87
|
+
* the transformers and returned to the user.
|
|
88
|
+
*/
|
|
89
|
+
responseValidator?: (data: unknown) => Promise<unknown>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
type IsExactlyNeverOrNeverUndefined<T> = [T] extends [never]
|
|
93
|
+
? true
|
|
94
|
+
: [T] extends [never | undefined]
|
|
95
|
+
? [undefined] extends [T]
|
|
96
|
+
? false
|
|
97
|
+
: true
|
|
98
|
+
: false;
|
|
99
|
+
|
|
100
|
+
export type OmitNever<T extends Record<string, unknown>> = {
|
|
101
|
+
[K in keyof T as IsExactlyNeverOrNeverUndefined<T[K]> extends true ? never : K]: T[K];
|
|
102
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { BodySerializer, QuerySerializer } from './bodySerializer';
|
|
2
|
+
import {
|
|
3
|
+
type ArraySeparatorStyle,
|
|
4
|
+
serializeArrayParam,
|
|
5
|
+
serializeObjectParam,
|
|
6
|
+
serializePrimitiveParam,
|
|
7
|
+
} from './pathSerializer';
|
|
8
|
+
|
|
9
|
+
export interface PathSerializer {
|
|
10
|
+
path: Record<string, unknown>;
|
|
11
|
+
url: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
15
|
+
|
|
16
|
+
export const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => {
|
|
17
|
+
let url = _url;
|
|
18
|
+
const matches = _url.match(PATH_PARAM_RE);
|
|
19
|
+
if (matches) {
|
|
20
|
+
for (const match of matches) {
|
|
21
|
+
let explode = false;
|
|
22
|
+
let name = match.substring(1, match.length - 1);
|
|
23
|
+
let style: ArraySeparatorStyle = 'simple';
|
|
24
|
+
|
|
25
|
+
if (name.endsWith('*')) {
|
|
26
|
+
explode = true;
|
|
27
|
+
name = name.substring(0, name.length - 1);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (name.startsWith('.')) {
|
|
31
|
+
name = name.substring(1);
|
|
32
|
+
style = 'label';
|
|
33
|
+
} else if (name.startsWith(';')) {
|
|
34
|
+
name = name.substring(1);
|
|
35
|
+
style = 'matrix';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const value = path[name];
|
|
39
|
+
|
|
40
|
+
if (value === undefined || value === null) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeof value === 'object') {
|
|
50
|
+
url = url.replace(
|
|
51
|
+
match,
|
|
52
|
+
serializeObjectParam({
|
|
53
|
+
explode,
|
|
54
|
+
name,
|
|
55
|
+
style,
|
|
56
|
+
value: value as Record<string, unknown>,
|
|
57
|
+
valueOnly: true,
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (style === 'matrix') {
|
|
64
|
+
url = url.replace(
|
|
65
|
+
match,
|
|
66
|
+
`;${serializePrimitiveParam({
|
|
67
|
+
name,
|
|
68
|
+
value: value as string,
|
|
69
|
+
})}`,
|
|
70
|
+
);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const replaceValue = encodeURIComponent(
|
|
75
|
+
style === 'label' ? `.${value as string}` : (value as string),
|
|
76
|
+
);
|
|
77
|
+
url = url.replace(match, replaceValue);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return url;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const getUrl = ({
|
|
84
|
+
baseUrl,
|
|
85
|
+
path,
|
|
86
|
+
query,
|
|
87
|
+
querySerializer,
|
|
88
|
+
url: _url,
|
|
89
|
+
}: {
|
|
90
|
+
baseUrl?: string;
|
|
91
|
+
path?: Record<string, unknown>;
|
|
92
|
+
query?: Record<string, unknown>;
|
|
93
|
+
querySerializer: QuerySerializer;
|
|
94
|
+
url: string;
|
|
95
|
+
}) => {
|
|
96
|
+
const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
|
|
97
|
+
let url = (baseUrl ?? '') + pathUrl;
|
|
98
|
+
if (path) {
|
|
99
|
+
url = defaultPathSerializer({ path, url });
|
|
100
|
+
}
|
|
101
|
+
let search = query ? querySerializer(query) : '';
|
|
102
|
+
if (search.startsWith('?')) {
|
|
103
|
+
search = search.substring(1);
|
|
104
|
+
}
|
|
105
|
+
if (search) {
|
|
106
|
+
url += `?${search}`;
|
|
107
|
+
}
|
|
108
|
+
return url;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export function getValidRequestBody(options: {
|
|
112
|
+
body?: unknown;
|
|
113
|
+
bodySerializer?: BodySerializer | null;
|
|
114
|
+
serializedBody?: unknown;
|
|
115
|
+
}) {
|
|
116
|
+
const hasBody = options.body !== undefined;
|
|
117
|
+
const isSerializedBody = hasBody && options.bodySerializer;
|
|
118
|
+
|
|
119
|
+
if (isSerializedBody) {
|
|
120
|
+
if ('serializedBody' in options) {
|
|
121
|
+
const hasSerializedBody =
|
|
122
|
+
options.serializedBody !== undefined && options.serializedBody !== '';
|
|
123
|
+
|
|
124
|
+
return hasSerializedBody ? options.serializedBody : null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// not all clients implement a serializedBody property (i.e. client-axios)
|
|
128
|
+
return options.body !== '' ? options.body : null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// plain/text body
|
|
132
|
+
if (hasBody) {
|
|
133
|
+
return options.body;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// no body was provided
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
@@ -0,0 +1,286 @@
|
|
|
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<Request, 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
|
+
const request: Client['request'] = async (options) => {
|
|
67
|
+
// @ts-expect-error
|
|
68
|
+
const { opts, url } = await beforeRequest(options);
|
|
69
|
+
const requestInit: ReqInit = {
|
|
70
|
+
redirect: 'follow',
|
|
71
|
+
...opts,
|
|
72
|
+
body: getValidRequestBody(opts),
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
let request = new Request(url, requestInit);
|
|
76
|
+
|
|
77
|
+
for (const fn of interceptors.request.fns) {
|
|
78
|
+
if (fn) {
|
|
79
|
+
request = await fn(request, opts);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
84
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
85
|
+
const _fetch = opts.fetch!;
|
|
86
|
+
let response: Response;
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
response = await _fetch(request);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
// Handle fetch exceptions (AbortError, network errors, etc.)
|
|
92
|
+
let finalError = error;
|
|
93
|
+
|
|
94
|
+
for (const fn of interceptors.error.fns) {
|
|
95
|
+
if (fn) {
|
|
96
|
+
finalError = (await fn(error, undefined as any, request, opts)) as unknown;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
finalError = finalError || ({} as unknown);
|
|
101
|
+
|
|
102
|
+
if (opts.throwOnError) {
|
|
103
|
+
throw finalError;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Return error response
|
|
107
|
+
return opts.responseStyle === 'data'
|
|
108
|
+
? undefined
|
|
109
|
+
: {
|
|
110
|
+
error: finalError,
|
|
111
|
+
request,
|
|
112
|
+
response: undefined as any,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
for (const fn of interceptors.response.fns) {
|
|
117
|
+
if (fn) {
|
|
118
|
+
response = await fn(response, request, opts);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const result = {
|
|
123
|
+
request,
|
|
124
|
+
response,
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
if (response.ok) {
|
|
128
|
+
const parseAs =
|
|
129
|
+
(opts.parseAs === 'auto'
|
|
130
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
131
|
+
: opts.parseAs) ?? 'json';
|
|
132
|
+
|
|
133
|
+
if (response.status === 204 || response.headers.get('Content-Length') === '0') {
|
|
134
|
+
let emptyData: any;
|
|
135
|
+
switch (parseAs) {
|
|
136
|
+
case 'arrayBuffer':
|
|
137
|
+
case 'blob':
|
|
138
|
+
case 'text':
|
|
139
|
+
emptyData = await response[parseAs]();
|
|
140
|
+
break;
|
|
141
|
+
case 'formData':
|
|
142
|
+
emptyData = new FormData();
|
|
143
|
+
break;
|
|
144
|
+
case 'stream':
|
|
145
|
+
emptyData = response.body;
|
|
146
|
+
break;
|
|
147
|
+
case 'json':
|
|
148
|
+
default:
|
|
149
|
+
emptyData = {};
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
return opts.responseStyle === 'data'
|
|
153
|
+
? emptyData
|
|
154
|
+
: {
|
|
155
|
+
data: emptyData,
|
|
156
|
+
...result,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let data: any;
|
|
161
|
+
switch (parseAs) {
|
|
162
|
+
case 'arrayBuffer':
|
|
163
|
+
case 'blob':
|
|
164
|
+
case 'formData':
|
|
165
|
+
case 'text':
|
|
166
|
+
data = await response[parseAs]();
|
|
167
|
+
break;
|
|
168
|
+
case 'json': {
|
|
169
|
+
// Some servers return 200 with no Content-Length and empty body.
|
|
170
|
+
// response.json() would throw; read as text and parse if non-empty.
|
|
171
|
+
const text = await response.text();
|
|
172
|
+
data = text ? JSON.parse(text) : {};
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
case 'stream':
|
|
176
|
+
return opts.responseStyle === 'data'
|
|
177
|
+
? response.body
|
|
178
|
+
: {
|
|
179
|
+
data: response.body,
|
|
180
|
+
...result,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (parseAs === 'json') {
|
|
185
|
+
if (opts.responseValidator) {
|
|
186
|
+
await opts.responseValidator(data);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (opts.responseTransformer) {
|
|
190
|
+
data = await opts.responseTransformer(data);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return opts.responseStyle === 'data'
|
|
195
|
+
? data
|
|
196
|
+
: {
|
|
197
|
+
data,
|
|
198
|
+
...result,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const textError = await response.text();
|
|
203
|
+
let jsonError: unknown;
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
jsonError = JSON.parse(textError);
|
|
207
|
+
} catch {
|
|
208
|
+
// noop
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const error = jsonError ?? textError;
|
|
212
|
+
let finalError = error;
|
|
213
|
+
|
|
214
|
+
for (const fn of interceptors.error.fns) {
|
|
215
|
+
if (fn) {
|
|
216
|
+
finalError = (await fn(error, response, request, opts)) as string;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
finalError = finalError || ({} as string);
|
|
221
|
+
|
|
222
|
+
if (opts.throwOnError) {
|
|
223
|
+
throw finalError;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// TODO: we probably want to return error and improve types
|
|
227
|
+
return opts.responseStyle === 'data'
|
|
228
|
+
? undefined
|
|
229
|
+
: {
|
|
230
|
+
error: finalError,
|
|
231
|
+
...result,
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const makeMethodFn = (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
236
|
+
request({ ...options, method });
|
|
237
|
+
|
|
238
|
+
const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
|
|
239
|
+
const { opts, url } = await beforeRequest(options);
|
|
240
|
+
return createSseClient({
|
|
241
|
+
...opts,
|
|
242
|
+
body: opts.body as BodyInit | null | undefined,
|
|
243
|
+
headers: opts.headers as unknown as Record<string, string>,
|
|
244
|
+
method,
|
|
245
|
+
onRequest: async (url, init) => {
|
|
246
|
+
let request = new Request(url, init);
|
|
247
|
+
for (const fn of interceptors.request.fns) {
|
|
248
|
+
if (fn) {
|
|
249
|
+
request = await fn(request, opts);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return request;
|
|
253
|
+
},
|
|
254
|
+
serializedBody: getValidRequestBody(opts) as BodyInit | null | undefined,
|
|
255
|
+
url,
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
return {
|
|
260
|
+
buildUrl,
|
|
261
|
+
connect: makeMethodFn('CONNECT'),
|
|
262
|
+
delete: makeMethodFn('DELETE'),
|
|
263
|
+
get: makeMethodFn('GET'),
|
|
264
|
+
getConfig,
|
|
265
|
+
head: makeMethodFn('HEAD'),
|
|
266
|
+
interceptors,
|
|
267
|
+
options: makeMethodFn('OPTIONS'),
|
|
268
|
+
patch: makeMethodFn('PATCH'),
|
|
269
|
+
post: makeMethodFn('POST'),
|
|
270
|
+
put: makeMethodFn('PUT'),
|
|
271
|
+
request,
|
|
272
|
+
setConfig,
|
|
273
|
+
sse: {
|
|
274
|
+
connect: makeSseFn('CONNECT'),
|
|
275
|
+
delete: makeSseFn('DELETE'),
|
|
276
|
+
get: makeSseFn('GET'),
|
|
277
|
+
head: makeSseFn('HEAD'),
|
|
278
|
+
options: makeSseFn('OPTIONS'),
|
|
279
|
+
patch: makeSseFn('PATCH'),
|
|
280
|
+
post: makeSseFn('POST'),
|
|
281
|
+
put: makeSseFn('PUT'),
|
|
282
|
+
trace: makeSseFn('TRACE'),
|
|
283
|
+
},
|
|
284
|
+
trace: makeMethodFn('TRACE'),
|
|
285
|
+
} as Client;
|
|
286
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
ResolvedRequestOptions,
|
|
20
|
+
ResponseStyle,
|
|
21
|
+
TDataShape,
|
|
22
|
+
} from './types';
|
|
23
|
+
export { createConfig, mergeHeaders } from './utils';
|