@hey-api/openapi-ts 0.81.0 → 0.82.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/README.md +6 -6
- package/dist/chunk-LKJWUBMN.js +43 -0
- package/dist/chunk-LKJWUBMN.js.map +1 -0
- package/dist/clients/angular/client.ts +27 -14
- package/dist/clients/angular/types.ts +9 -7
- package/dist/clients/axios/client.ts +27 -14
- package/dist/clients/axios/types.ts +9 -7
- package/dist/clients/core/serverSentEvents.ts +28 -1
- package/dist/clients/core/types.ts +20 -22
- package/dist/clients/fetch/client.ts +61 -21
- package/dist/clients/fetch/types.ts +10 -8
- package/dist/clients/next/client.ts +66 -20
- package/dist/clients/next/types.ts +9 -7
- package/dist/clients/nuxt/client.ts +28 -14
- package/dist/clients/nuxt/types.ts +3 -7
- package/dist/index.cjs +68 -68
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +671 -4
- package/dist/index.d.ts +671 -4
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +13 -13
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.js +1 -1
- package/dist/{types.d-CFCN8diW.d.cts → types.d-kCNGz4sv.d.cts} +3208 -2407
- package/dist/{types.d-CFCN8diW.d.ts → types.d-kCNGz4sv.d.ts} +3208 -2407
- package/package.json +2 -2
- package/dist/chunk-Z7FL5BGA.js +0 -43
- package/dist/chunk-Z7FL5BGA.js.map +0 -1
|
@@ -15,6 +15,7 @@ import { firstValueFrom } from 'rxjs';
|
|
|
15
15
|
import { filter } from 'rxjs/operators';
|
|
16
16
|
|
|
17
17
|
import { createSseClient } from '../core/serverSentEvents';
|
|
18
|
+
import type { HttpMethod } from '../core/types';
|
|
18
19
|
import type {
|
|
19
20
|
Client,
|
|
20
21
|
Config,
|
|
@@ -198,9 +199,12 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
198
199
|
}
|
|
199
200
|
};
|
|
200
201
|
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
202
|
+
const makeMethodFn =
|
|
203
|
+
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
204
|
+
request({ ...options, method });
|
|
205
|
+
|
|
206
|
+
const makeSseFn =
|
|
207
|
+
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
|
|
204
208
|
const { opts, url } = await beforeRequest(options);
|
|
205
209
|
return createSseClient({
|
|
206
210
|
...opts,
|
|
@@ -210,21 +214,19 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
210
214
|
url,
|
|
211
215
|
});
|
|
212
216
|
};
|
|
213
|
-
return fn;
|
|
214
|
-
};
|
|
215
217
|
|
|
216
218
|
return {
|
|
217
219
|
buildUrl,
|
|
218
|
-
connect:
|
|
219
|
-
delete:
|
|
220
|
-
get:
|
|
220
|
+
connect: makeMethodFn('CONNECT'),
|
|
221
|
+
delete: makeMethodFn('DELETE'),
|
|
222
|
+
get: makeMethodFn('GET'),
|
|
221
223
|
getConfig,
|
|
222
|
-
head:
|
|
224
|
+
head: makeMethodFn('HEAD'),
|
|
223
225
|
interceptors,
|
|
224
|
-
options:
|
|
225
|
-
patch:
|
|
226
|
-
post:
|
|
227
|
-
put:
|
|
226
|
+
options: makeMethodFn('OPTIONS'),
|
|
227
|
+
patch: makeMethodFn('PATCH'),
|
|
228
|
+
post: makeMethodFn('POST'),
|
|
229
|
+
put: makeMethodFn('PUT'),
|
|
228
230
|
request,
|
|
229
231
|
requestOptions: (options) => {
|
|
230
232
|
if (options.security) {
|
|
@@ -240,6 +242,17 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
240
242
|
return requestOptions(options).req;
|
|
241
243
|
},
|
|
242
244
|
setConfig,
|
|
243
|
-
|
|
245
|
+
sse: {
|
|
246
|
+
connect: makeSseFn('CONNECT'),
|
|
247
|
+
delete: makeSseFn('DELETE'),
|
|
248
|
+
get: makeSseFn('GET'),
|
|
249
|
+
head: makeSseFn('HEAD'),
|
|
250
|
+
options: makeSseFn('OPTIONS'),
|
|
251
|
+
patch: makeSseFn('PATCH'),
|
|
252
|
+
post: makeSseFn('POST'),
|
|
253
|
+
put: makeSseFn('PUT'),
|
|
254
|
+
trace: makeSseFn('TRACE'),
|
|
255
|
+
},
|
|
256
|
+
trace: makeMethodFn('TRACE'),
|
|
244
257
|
} as Client;
|
|
245
258
|
};
|
|
@@ -155,7 +155,7 @@ export interface ClientOptions {
|
|
|
155
155
|
throwOnError?: boolean;
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
type
|
|
158
|
+
type MethodFn = <
|
|
159
159
|
TData = unknown,
|
|
160
160
|
TError = unknown,
|
|
161
161
|
ThrowOnError extends boolean = false,
|
|
@@ -164,7 +164,7 @@ type MethodFnBase = <
|
|
|
164
164
|
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
|
|
165
165
|
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
|
166
166
|
|
|
167
|
-
type
|
|
167
|
+
type SseFn = <
|
|
168
168
|
TData = unknown,
|
|
169
169
|
TError = unknown,
|
|
170
170
|
ThrowOnError extends boolean = false,
|
|
@@ -173,10 +173,6 @@ type MethodFnServerSentEvents = <
|
|
|
173
173
|
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
|
|
174
174
|
) => Promise<ServerSentEventsResult<TData, TError>>;
|
|
175
175
|
|
|
176
|
-
type MethodFn = MethodFnBase & {
|
|
177
|
-
sse: MethodFnServerSentEvents;
|
|
178
|
-
};
|
|
179
|
-
|
|
180
176
|
type RequestFn = <
|
|
181
177
|
TData = unknown,
|
|
182
178
|
TError = unknown,
|
|
@@ -208,7 +204,13 @@ type BuildUrlFn = <
|
|
|
208
204
|
options: Pick<TData, 'url'> & Options<TData>,
|
|
209
205
|
) => string;
|
|
210
206
|
|
|
211
|
-
export type Client = CoreClient<
|
|
207
|
+
export type Client = CoreClient<
|
|
208
|
+
RequestFn,
|
|
209
|
+
Config,
|
|
210
|
+
MethodFn,
|
|
211
|
+
BuildUrlFn,
|
|
212
|
+
SseFn
|
|
213
|
+
> & {
|
|
212
214
|
interceptors: Middleware<
|
|
213
215
|
HttpRequest<unknown>,
|
|
214
216
|
HttpResponse<unknown>,
|
|
@@ -2,6 +2,7 @@ import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios';
|
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
|
|
4
4
|
import { createSseClient } from '../core/serverSentEvents';
|
|
5
|
+
import type { HttpMethod } from '../core/types';
|
|
5
6
|
import type { Client, Config, RequestOptions } from './types';
|
|
6
7
|
import {
|
|
7
8
|
buildUrl,
|
|
@@ -111,9 +112,12 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
111
112
|
}
|
|
112
113
|
};
|
|
113
114
|
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
const makeMethodFn =
|
|
116
|
+
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
117
|
+
request({ ...options, method });
|
|
118
|
+
|
|
119
|
+
const makeSseFn =
|
|
120
|
+
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
|
|
117
121
|
const { opts, url } = await beforeRequest(options);
|
|
118
122
|
return createSseClient({
|
|
119
123
|
...opts,
|
|
@@ -125,23 +129,32 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
125
129
|
url,
|
|
126
130
|
});
|
|
127
131
|
};
|
|
128
|
-
return fn;
|
|
129
|
-
};
|
|
130
132
|
|
|
131
133
|
return {
|
|
132
134
|
buildUrl,
|
|
133
|
-
connect:
|
|
134
|
-
delete:
|
|
135
|
-
get:
|
|
135
|
+
connect: makeMethodFn('CONNECT'),
|
|
136
|
+
delete: makeMethodFn('DELETE'),
|
|
137
|
+
get: makeMethodFn('GET'),
|
|
136
138
|
getConfig,
|
|
137
|
-
head:
|
|
139
|
+
head: makeMethodFn('HEAD'),
|
|
138
140
|
instance,
|
|
139
|
-
options:
|
|
140
|
-
patch:
|
|
141
|
-
post:
|
|
142
|
-
put:
|
|
141
|
+
options: makeMethodFn('OPTIONS'),
|
|
142
|
+
patch: makeMethodFn('PATCH'),
|
|
143
|
+
post: makeMethodFn('POST'),
|
|
144
|
+
put: makeMethodFn('PUT'),
|
|
143
145
|
request,
|
|
144
146
|
setConfig,
|
|
145
|
-
|
|
147
|
+
sse: {
|
|
148
|
+
connect: makeSseFn('CONNECT'),
|
|
149
|
+
delete: makeSseFn('DELETE'),
|
|
150
|
+
get: makeSseFn('GET'),
|
|
151
|
+
head: makeSseFn('HEAD'),
|
|
152
|
+
options: makeSseFn('OPTIONS'),
|
|
153
|
+
patch: makeSseFn('PATCH'),
|
|
154
|
+
post: makeSseFn('POST'),
|
|
155
|
+
put: makeSseFn('PUT'),
|
|
156
|
+
trace: makeSseFn('TRACE'),
|
|
157
|
+
},
|
|
158
|
+
trace: makeMethodFn('TRACE'),
|
|
146
159
|
} as Client;
|
|
147
160
|
};
|
|
@@ -116,7 +116,7 @@ export type RequestResult<
|
|
|
116
116
|
})
|
|
117
117
|
>;
|
|
118
118
|
|
|
119
|
-
type
|
|
119
|
+
type MethodFn = <
|
|
120
120
|
TData = unknown,
|
|
121
121
|
TError = unknown,
|
|
122
122
|
ThrowOnError extends boolean = false,
|
|
@@ -124,7 +124,7 @@ type MethodFnBase = <
|
|
|
124
124
|
options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
|
|
125
125
|
) => RequestResult<TData, TError, ThrowOnError>;
|
|
126
126
|
|
|
127
|
-
type
|
|
127
|
+
type SseFn = <
|
|
128
128
|
TData = unknown,
|
|
129
129
|
TError = unknown,
|
|
130
130
|
ThrowOnError extends boolean = false,
|
|
@@ -132,10 +132,6 @@ type MethodFnServerSentEvents = <
|
|
|
132
132
|
options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
|
|
133
133
|
) => Promise<ServerSentEventsResult<TData, TError>>;
|
|
134
134
|
|
|
135
|
-
type MethodFn = MethodFnBase & {
|
|
136
|
-
sse: MethodFnServerSentEvents;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
135
|
type RequestFn = <
|
|
140
136
|
TData = unknown,
|
|
141
137
|
TError = unknown,
|
|
@@ -156,7 +152,13 @@ type BuildUrlFn = <
|
|
|
156
152
|
options: Pick<TData, 'url'> & Omit<Options<TData>, 'axios'>,
|
|
157
153
|
) => string;
|
|
158
154
|
|
|
159
|
-
export type Client = CoreClient<
|
|
155
|
+
export type Client = CoreClient<
|
|
156
|
+
RequestFn,
|
|
157
|
+
Config,
|
|
158
|
+
MethodFn,
|
|
159
|
+
BuildUrlFn,
|
|
160
|
+
SseFn
|
|
161
|
+
> & {
|
|
160
162
|
instance: AxiosInstance;
|
|
161
163
|
};
|
|
162
164
|
|
|
@@ -5,6 +5,17 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
|
|
|
5
5
|
'method'
|
|
6
6
|
> &
|
|
7
7
|
Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & {
|
|
8
|
+
/**
|
|
9
|
+
* Fetch API implementation. You can use this option to provide a custom
|
|
10
|
+
* fetch instance.
|
|
11
|
+
*
|
|
12
|
+
* @default globalThis.fetch
|
|
13
|
+
*/
|
|
14
|
+
fetch?: typeof fetch;
|
|
15
|
+
/**
|
|
16
|
+
* Implementing clients can call request interceptors inside this hook.
|
|
17
|
+
*/
|
|
18
|
+
onRequest?: (url: string, init: RequestInit) => Promise<Request>;
|
|
8
19
|
/**
|
|
9
20
|
* Callback invoked when a network or parsing error occurs during streaming.
|
|
10
21
|
*
|
|
@@ -22,6 +33,7 @@ export type ServerSentEventsOptions<TData = unknown> = Omit<
|
|
|
22
33
|
* @returns Nothing (void).
|
|
23
34
|
*/
|
|
24
35
|
onSseEvent?: (event: StreamEvent<TData>) => void;
|
|
36
|
+
serializedBody?: RequestInit['body'];
|
|
25
37
|
/**
|
|
26
38
|
* Default retry delay in milliseconds.
|
|
27
39
|
*
|
|
@@ -73,6 +85,7 @@ export type ServerSentEventsResult<
|
|
|
73
85
|
};
|
|
74
86
|
|
|
75
87
|
export const createSseClient = <TData = unknown>({
|
|
88
|
+
onRequest,
|
|
76
89
|
onSseError,
|
|
77
90
|
onSseEvent,
|
|
78
91
|
responseTransformer,
|
|
@@ -110,7 +123,21 @@ export const createSseClient = <TData = unknown>({
|
|
|
110
123
|
}
|
|
111
124
|
|
|
112
125
|
try {
|
|
113
|
-
const
|
|
126
|
+
const requestInit: RequestInit = {
|
|
127
|
+
redirect: 'follow',
|
|
128
|
+
...options,
|
|
129
|
+
body: options.serializedBody,
|
|
130
|
+
headers,
|
|
131
|
+
signal,
|
|
132
|
+
};
|
|
133
|
+
let request = new Request(url, requestInit);
|
|
134
|
+
if (onRequest) {
|
|
135
|
+
request = await onRequest(url, requestInit);
|
|
136
|
+
}
|
|
137
|
+
// fetch must be assigned here, otherwise it would throw the error:
|
|
138
|
+
// TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
|
|
139
|
+
const _fetch = options.fetch ?? globalThis.fetch;
|
|
140
|
+
const response = await _fetch(request);
|
|
114
141
|
|
|
115
142
|
if (!response.ok)
|
|
116
143
|
throw new Error(
|
|
@@ -5,29 +5,36 @@ import type {
|
|
|
5
5
|
QuerySerializerOptions,
|
|
6
6
|
} from './bodySerializer';
|
|
7
7
|
|
|
8
|
-
export
|
|
8
|
+
export type HttpMethod =
|
|
9
|
+
| 'connect'
|
|
10
|
+
| 'delete'
|
|
11
|
+
| 'get'
|
|
12
|
+
| 'head'
|
|
13
|
+
| 'options'
|
|
14
|
+
| 'patch'
|
|
15
|
+
| 'post'
|
|
16
|
+
| 'put'
|
|
17
|
+
| 'trace';
|
|
18
|
+
|
|
19
|
+
export type Client<
|
|
9
20
|
RequestFn = never,
|
|
10
21
|
Config = unknown,
|
|
11
22
|
MethodFn = never,
|
|
12
23
|
BuildUrlFn = never,
|
|
13
|
-
|
|
24
|
+
SseFn = never,
|
|
25
|
+
> = {
|
|
14
26
|
/**
|
|
15
27
|
* Returns the final request URL.
|
|
16
28
|
*/
|
|
17
29
|
buildUrl: BuildUrlFn;
|
|
18
|
-
connect: MethodFn;
|
|
19
|
-
delete: MethodFn;
|
|
20
|
-
get: MethodFn;
|
|
21
30
|
getConfig: () => Config;
|
|
22
|
-
head: MethodFn;
|
|
23
|
-
options: MethodFn;
|
|
24
|
-
patch: MethodFn;
|
|
25
|
-
post: MethodFn;
|
|
26
|
-
put: MethodFn;
|
|
27
31
|
request: RequestFn;
|
|
28
32
|
setConfig: (config: Config) => Config;
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
} & {
|
|
34
|
+
[K in HttpMethod]: MethodFn;
|
|
35
|
+
} & ([SseFn] extends [never]
|
|
36
|
+
? { sse?: never }
|
|
37
|
+
: { sse: { [K in HttpMethod]: SseFn } });
|
|
31
38
|
|
|
32
39
|
export interface Config {
|
|
33
40
|
/**
|
|
@@ -63,16 +70,7 @@ export interface Config {
|
|
|
63
70
|
*
|
|
64
71
|
* {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more}
|
|
65
72
|
*/
|
|
66
|
-
method?:
|
|
67
|
-
| 'CONNECT'
|
|
68
|
-
| 'DELETE'
|
|
69
|
-
| 'GET'
|
|
70
|
-
| 'HEAD'
|
|
71
|
-
| 'OPTIONS'
|
|
72
|
-
| 'PATCH'
|
|
73
|
-
| 'POST'
|
|
74
|
-
| 'PUT'
|
|
75
|
-
| 'TRACE';
|
|
73
|
+
method?: Uppercase<HttpMethod>;
|
|
76
74
|
/**
|
|
77
75
|
* A function for serializing request query parameters. By default, arrays
|
|
78
76
|
* will be exploded in form style, objects will be exploded in deepObject
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createSseClient } from '../core/serverSentEvents';
|
|
2
|
+
import type { HttpMethod } from '../core/types';
|
|
2
3
|
import type {
|
|
3
4
|
Client,
|
|
4
5
|
Config,
|
|
@@ -105,23 +106,41 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
105
106
|
};
|
|
106
107
|
|
|
107
108
|
if (response.ok) {
|
|
109
|
+
const parseAs =
|
|
110
|
+
(opts.parseAs === 'auto'
|
|
111
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
112
|
+
: opts.parseAs) ?? 'json';
|
|
113
|
+
|
|
108
114
|
if (
|
|
109
115
|
response.status === 204 ||
|
|
110
116
|
response.headers.get('Content-Length') === '0'
|
|
111
117
|
) {
|
|
118
|
+
let emptyData: any;
|
|
119
|
+
switch (parseAs) {
|
|
120
|
+
case 'arrayBuffer':
|
|
121
|
+
case 'blob':
|
|
122
|
+
case 'text':
|
|
123
|
+
emptyData = await response[parseAs]();
|
|
124
|
+
break;
|
|
125
|
+
case 'formData':
|
|
126
|
+
emptyData = new FormData();
|
|
127
|
+
break;
|
|
128
|
+
case 'stream':
|
|
129
|
+
emptyData = response.body;
|
|
130
|
+
break;
|
|
131
|
+
case 'json':
|
|
132
|
+
default:
|
|
133
|
+
emptyData = {};
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
112
136
|
return opts.responseStyle === 'data'
|
|
113
|
-
?
|
|
137
|
+
? emptyData
|
|
114
138
|
: {
|
|
115
|
-
data:
|
|
139
|
+
data: emptyData,
|
|
116
140
|
...result,
|
|
117
141
|
};
|
|
118
142
|
}
|
|
119
143
|
|
|
120
|
-
const parseAs =
|
|
121
|
-
(opts.parseAs === 'auto'
|
|
122
|
-
? getParseAs(response.headers.get('Content-Type'))
|
|
123
|
-
: opts.parseAs) ?? 'json';
|
|
124
|
-
|
|
125
144
|
let data: any;
|
|
126
145
|
switch (parseAs) {
|
|
127
146
|
case 'arrayBuffer':
|
|
@@ -191,35 +210,56 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
191
210
|
};
|
|
192
211
|
};
|
|
193
212
|
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
213
|
+
const makeMethodFn =
|
|
214
|
+
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
215
|
+
request({ ...options, method });
|
|
216
|
+
|
|
217
|
+
const makeSseFn =
|
|
218
|
+
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
|
|
197
219
|
const { opts, url } = await beforeRequest(options);
|
|
198
220
|
return createSseClient({
|
|
199
221
|
...opts,
|
|
200
222
|
body: opts.body as BodyInit | null | undefined,
|
|
201
223
|
headers: opts.headers as unknown as Record<string, string>,
|
|
202
224
|
method,
|
|
225
|
+
onRequest: async (url, init) => {
|
|
226
|
+
let request = new Request(url, init);
|
|
227
|
+
for (const fn of interceptors.request._fns) {
|
|
228
|
+
if (fn) {
|
|
229
|
+
request = await fn(request, opts);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return request;
|
|
233
|
+
},
|
|
203
234
|
url,
|
|
204
235
|
});
|
|
205
236
|
};
|
|
206
|
-
return fn;
|
|
207
|
-
};
|
|
208
237
|
|
|
209
238
|
return {
|
|
210
239
|
buildUrl,
|
|
211
|
-
connect:
|
|
212
|
-
delete:
|
|
213
|
-
get:
|
|
240
|
+
connect: makeMethodFn('CONNECT'),
|
|
241
|
+
delete: makeMethodFn('DELETE'),
|
|
242
|
+
get: makeMethodFn('GET'),
|
|
214
243
|
getConfig,
|
|
215
|
-
head:
|
|
244
|
+
head: makeMethodFn('HEAD'),
|
|
216
245
|
interceptors,
|
|
217
|
-
options:
|
|
218
|
-
patch:
|
|
219
|
-
post:
|
|
220
|
-
put:
|
|
246
|
+
options: makeMethodFn('OPTIONS'),
|
|
247
|
+
patch: makeMethodFn('PATCH'),
|
|
248
|
+
post: makeMethodFn('POST'),
|
|
249
|
+
put: makeMethodFn('PUT'),
|
|
221
250
|
request,
|
|
222
251
|
setConfig,
|
|
223
|
-
|
|
252
|
+
sse: {
|
|
253
|
+
connect: makeSseFn('CONNECT'),
|
|
254
|
+
delete: makeSseFn('DELETE'),
|
|
255
|
+
get: makeSseFn('GET'),
|
|
256
|
+
head: makeSseFn('HEAD'),
|
|
257
|
+
options: makeSseFn('OPTIONS'),
|
|
258
|
+
patch: makeSseFn('PATCH'),
|
|
259
|
+
post: makeSseFn('POST'),
|
|
260
|
+
put: makeSseFn('PUT'),
|
|
261
|
+
trace: makeSseFn('TRACE'),
|
|
262
|
+
},
|
|
263
|
+
trace: makeMethodFn('TRACE'),
|
|
224
264
|
} as Client;
|
|
225
265
|
};
|
|
@@ -24,7 +24,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
|
|
|
24
24
|
*
|
|
25
25
|
* @default globalThis.fetch
|
|
26
26
|
*/
|
|
27
|
-
fetch?:
|
|
27
|
+
fetch?: typeof fetch;
|
|
28
28
|
/**
|
|
29
29
|
* Please don't use the Fetch client for Next.js applications. The `next`
|
|
30
30
|
* options won't have any effect.
|
|
@@ -153,7 +153,7 @@ export interface ClientOptions {
|
|
|
153
153
|
throwOnError?: boolean;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
type
|
|
156
|
+
type MethodFn = <
|
|
157
157
|
TData = unknown,
|
|
158
158
|
TError = unknown,
|
|
159
159
|
ThrowOnError extends boolean = false,
|
|
@@ -162,7 +162,7 @@ type MethodFnBase = <
|
|
|
162
162
|
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
|
|
163
163
|
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
|
164
164
|
|
|
165
|
-
type
|
|
165
|
+
type SseFn = <
|
|
166
166
|
TData = unknown,
|
|
167
167
|
TError = unknown,
|
|
168
168
|
ThrowOnError extends boolean = false,
|
|
@@ -171,10 +171,6 @@ type MethodFnServerSentEvents = <
|
|
|
171
171
|
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
|
|
172
172
|
) => Promise<ServerSentEventsResult<TData, TError>>;
|
|
173
173
|
|
|
174
|
-
type MethodFn = MethodFnBase & {
|
|
175
|
-
sse: MethodFnServerSentEvents;
|
|
176
|
-
};
|
|
177
|
-
|
|
178
174
|
type RequestFn = <
|
|
179
175
|
TData = unknown,
|
|
180
176
|
TError = unknown,
|
|
@@ -199,7 +195,13 @@ type BuildUrlFn = <
|
|
|
199
195
|
options: Pick<TData, 'url'> & Options<TData>,
|
|
200
196
|
) => string;
|
|
201
197
|
|
|
202
|
-
export type Client = CoreClient<
|
|
198
|
+
export type Client = CoreClient<
|
|
199
|
+
RequestFn,
|
|
200
|
+
Config,
|
|
201
|
+
MethodFn,
|
|
202
|
+
BuildUrlFn,
|
|
203
|
+
SseFn
|
|
204
|
+
> & {
|
|
203
205
|
interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>;
|
|
204
206
|
};
|
|
205
207
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createSseClient } from '../core/serverSentEvents';
|
|
2
|
+
import type { HttpMethod } from '../core/types';
|
|
2
3
|
import type {
|
|
3
4
|
Client,
|
|
4
5
|
Config,
|
|
@@ -100,21 +101,39 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
100
101
|
};
|
|
101
102
|
|
|
102
103
|
if (response.ok) {
|
|
104
|
+
const parseAs =
|
|
105
|
+
(opts.parseAs === 'auto'
|
|
106
|
+
? getParseAs(response.headers.get('Content-Type'))
|
|
107
|
+
: opts.parseAs) ?? 'json';
|
|
108
|
+
|
|
103
109
|
if (
|
|
104
110
|
response.status === 204 ||
|
|
105
111
|
response.headers.get('Content-Length') === '0'
|
|
106
112
|
) {
|
|
113
|
+
let emptyData: any;
|
|
114
|
+
switch (parseAs) {
|
|
115
|
+
case 'arrayBuffer':
|
|
116
|
+
case 'blob':
|
|
117
|
+
case 'text':
|
|
118
|
+
emptyData = await response[parseAs]();
|
|
119
|
+
break;
|
|
120
|
+
case 'formData':
|
|
121
|
+
emptyData = new FormData();
|
|
122
|
+
break;
|
|
123
|
+
case 'stream':
|
|
124
|
+
emptyData = response.body;
|
|
125
|
+
break;
|
|
126
|
+
case 'json':
|
|
127
|
+
default:
|
|
128
|
+
emptyData = {};
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
107
131
|
return {
|
|
108
|
-
data:
|
|
132
|
+
data: emptyData,
|
|
109
133
|
...result,
|
|
110
134
|
};
|
|
111
135
|
}
|
|
112
136
|
|
|
113
|
-
const parseAs =
|
|
114
|
-
(opts.parseAs === 'auto'
|
|
115
|
-
? getParseAs(response.headers.get('Content-Type'))
|
|
116
|
-
: opts.parseAs) ?? 'json';
|
|
117
|
-
|
|
118
137
|
let data: any;
|
|
119
138
|
switch (parseAs) {
|
|
120
139
|
case 'arrayBuffer':
|
|
@@ -177,35 +196,62 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
177
196
|
};
|
|
178
197
|
};
|
|
179
198
|
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
199
|
+
const makeMethodFn =
|
|
200
|
+
(method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
|
|
201
|
+
request({ ...options, method });
|
|
202
|
+
|
|
203
|
+
const makeSseFn =
|
|
204
|
+
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
|
|
183
205
|
const { opts, url } = await beforeRequest(options);
|
|
184
206
|
return createSseClient({
|
|
185
207
|
...opts,
|
|
186
208
|
body: opts.body as BodyInit | null | undefined,
|
|
187
209
|
headers: opts.headers as unknown as Record<string, string>,
|
|
188
210
|
method,
|
|
211
|
+
onRequest: async (url, init) => {
|
|
212
|
+
let request = new Request(url, init);
|
|
213
|
+
const requestInit = {
|
|
214
|
+
...init,
|
|
215
|
+
method: init.method as Config['method'],
|
|
216
|
+
url,
|
|
217
|
+
};
|
|
218
|
+
for (const fn of interceptors.request._fns) {
|
|
219
|
+
if (fn) {
|
|
220
|
+
await fn(requestInit);
|
|
221
|
+
request = new Request(requestInit.url, requestInit);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return request;
|
|
225
|
+
},
|
|
189
226
|
url,
|
|
190
227
|
});
|
|
191
228
|
};
|
|
192
|
-
return fn;
|
|
193
|
-
};
|
|
194
229
|
|
|
195
230
|
return {
|
|
196
231
|
buildUrl,
|
|
197
|
-
connect:
|
|
198
|
-
delete:
|
|
199
|
-
get:
|
|
232
|
+
connect: makeMethodFn('CONNECT'),
|
|
233
|
+
delete: makeMethodFn('DELETE'),
|
|
234
|
+
get: makeMethodFn('GET'),
|
|
200
235
|
getConfig,
|
|
201
|
-
head:
|
|
236
|
+
head: makeMethodFn('HEAD'),
|
|
202
237
|
interceptors,
|
|
203
|
-
options:
|
|
204
|
-
patch:
|
|
205
|
-
post:
|
|
206
|
-
put:
|
|
238
|
+
options: makeMethodFn('OPTIONS'),
|
|
239
|
+
patch: makeMethodFn('PATCH'),
|
|
240
|
+
post: makeMethodFn('POST'),
|
|
241
|
+
put: makeMethodFn('PUT'),
|
|
207
242
|
request,
|
|
208
243
|
setConfig,
|
|
209
|
-
|
|
244
|
+
sse: {
|
|
245
|
+
connect: makeSseFn('CONNECT'),
|
|
246
|
+
delete: makeSseFn('DELETE'),
|
|
247
|
+
get: makeSseFn('GET'),
|
|
248
|
+
head: makeSseFn('HEAD'),
|
|
249
|
+
options: makeSseFn('OPTIONS'),
|
|
250
|
+
patch: makeSseFn('PATCH'),
|
|
251
|
+
post: makeSseFn('POST'),
|
|
252
|
+
put: makeSseFn('PUT'),
|
|
253
|
+
trace: makeSseFn('TRACE'),
|
|
254
|
+
},
|
|
255
|
+
trace: makeMethodFn('TRACE'),
|
|
210
256
|
} as Client;
|
|
211
257
|
};
|