@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,231 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
HttpClient,
|
|
3
|
+
HttpErrorResponse,
|
|
4
|
+
HttpHeaders,
|
|
5
|
+
HttpRequest,
|
|
6
|
+
HttpResponse,
|
|
7
|
+
} from '@angular/common/http';
|
|
8
|
+
import type { Injector } from '@angular/core';
|
|
9
|
+
|
|
10
|
+
import type { Auth } from '../core/auth';
|
|
11
|
+
import type {
|
|
12
|
+
ServerSentEventsOptions,
|
|
13
|
+
ServerSentEventsResult,
|
|
14
|
+
} from '../core/serverSentEvents';
|
|
15
|
+
import type { Client as CoreClient, Config as CoreConfig } from '../core/types';
|
|
16
|
+
import type { Middleware } from './utils';
|
|
17
|
+
|
|
18
|
+
export type ResponseStyle = 'data' | 'fields';
|
|
19
|
+
|
|
20
|
+
export interface Config<T extends ClientOptions = ClientOptions>
|
|
21
|
+
extends Omit<RequestInit, 'body' | 'headers' | 'method'>, Omit<CoreConfig, 'headers'> {
|
|
22
|
+
/**
|
|
23
|
+
* Base URL for all requests made by this client.
|
|
24
|
+
*/
|
|
25
|
+
baseUrl?: T['baseUrl'];
|
|
26
|
+
/**
|
|
27
|
+
* An object containing any HTTP headers that you want to pre-populate your
|
|
28
|
+
* `HttpHeaders` object with.
|
|
29
|
+
*
|
|
30
|
+
* {@link https://angular.dev/api/common/http/HttpHeaders#constructor See more}
|
|
31
|
+
*/
|
|
32
|
+
headers?:
|
|
33
|
+
| HttpHeaders
|
|
34
|
+
| Record<
|
|
35
|
+
string,
|
|
36
|
+
string | number | boolean | (string | number | boolean)[] | null | undefined | unknown
|
|
37
|
+
>;
|
|
38
|
+
/**
|
|
39
|
+
* The HTTP client to use for making requests.
|
|
40
|
+
*/
|
|
41
|
+
httpClient?: HttpClient;
|
|
42
|
+
/**
|
|
43
|
+
* Should we return only data or multiple fields (data, error, response, etc.)?
|
|
44
|
+
*
|
|
45
|
+
* @default 'fields'
|
|
46
|
+
*/
|
|
47
|
+
responseStyle?: ResponseStyle;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Throw an error instead of returning it in the response?
|
|
51
|
+
*
|
|
52
|
+
* @default false
|
|
53
|
+
*/
|
|
54
|
+
throwOnError?: T['throwOnError'];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface RequestOptions<
|
|
58
|
+
TData = unknown,
|
|
59
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
60
|
+
ThrowOnError extends boolean = boolean,
|
|
61
|
+
Url extends string = string,
|
|
62
|
+
>
|
|
63
|
+
extends
|
|
64
|
+
Config<{
|
|
65
|
+
responseStyle: TResponseStyle;
|
|
66
|
+
throwOnError: ThrowOnError;
|
|
67
|
+
}>,
|
|
68
|
+
Pick<
|
|
69
|
+
ServerSentEventsOptions<TData>,
|
|
70
|
+
| 'onSseError'
|
|
71
|
+
| 'onSseEvent'
|
|
72
|
+
| 'sseDefaultRetryDelay'
|
|
73
|
+
| 'sseMaxRetryAttempts'
|
|
74
|
+
| 'sseMaxRetryDelay'
|
|
75
|
+
> {
|
|
76
|
+
/**
|
|
77
|
+
* Any body that you want to add to your request.
|
|
78
|
+
*
|
|
79
|
+
* {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
|
|
80
|
+
*/
|
|
81
|
+
body?: unknown;
|
|
82
|
+
/**
|
|
83
|
+
* Optional custom injector for dependency resolution if you don't implicitly or explicitly provide one.
|
|
84
|
+
*/
|
|
85
|
+
injector?: Injector;
|
|
86
|
+
path?: Record<string, unknown>;
|
|
87
|
+
query?: Record<string, unknown>;
|
|
88
|
+
/**
|
|
89
|
+
* Security mechanism(s) to use for the request.
|
|
90
|
+
*/
|
|
91
|
+
security?: ReadonlyArray<Auth>;
|
|
92
|
+
url: Url;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface ResolvedRequestOptions<
|
|
96
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
97
|
+
ThrowOnError extends boolean = boolean,
|
|
98
|
+
Url extends string = string,
|
|
99
|
+
> extends RequestOptions<unknown, TResponseStyle, ThrowOnError, Url> {
|
|
100
|
+
serializedBody?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export type RequestResult<
|
|
104
|
+
TData = unknown,
|
|
105
|
+
TError = unknown,
|
|
106
|
+
ThrowOnError extends boolean = boolean,
|
|
107
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
108
|
+
> = Promise<
|
|
109
|
+
ThrowOnError extends true
|
|
110
|
+
? TResponseStyle extends 'data'
|
|
111
|
+
? TData extends Record<string, unknown>
|
|
112
|
+
? TData[keyof TData]
|
|
113
|
+
: TData
|
|
114
|
+
: {
|
|
115
|
+
data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
|
|
116
|
+
request: HttpRequest<unknown>;
|
|
117
|
+
response: HttpResponse<TData>;
|
|
118
|
+
}
|
|
119
|
+
: TResponseStyle extends 'data'
|
|
120
|
+
? (TData extends Record<string, unknown> ? TData[keyof TData] : TData) | undefined
|
|
121
|
+
:
|
|
122
|
+
| {
|
|
123
|
+
data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
|
|
124
|
+
error: undefined;
|
|
125
|
+
request: HttpRequest<unknown>;
|
|
126
|
+
response: HttpResponse<TData>;
|
|
127
|
+
}
|
|
128
|
+
| {
|
|
129
|
+
data: undefined;
|
|
130
|
+
error: TError[keyof TError];
|
|
131
|
+
request: HttpRequest<unknown>;
|
|
132
|
+
response: HttpErrorResponse & {
|
|
133
|
+
error: TError[keyof TError] | null;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
>;
|
|
137
|
+
|
|
138
|
+
export interface ClientOptions {
|
|
139
|
+
baseUrl?: string;
|
|
140
|
+
responseStyle?: ResponseStyle;
|
|
141
|
+
throwOnError?: boolean;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
type MethodFn = <
|
|
145
|
+
TData = unknown,
|
|
146
|
+
TError = unknown,
|
|
147
|
+
ThrowOnError extends boolean = false,
|
|
148
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
149
|
+
>(
|
|
150
|
+
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
|
|
151
|
+
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
|
152
|
+
|
|
153
|
+
type SseFn = <
|
|
154
|
+
TData = unknown,
|
|
155
|
+
TError = unknown,
|
|
156
|
+
ThrowOnError extends boolean = false,
|
|
157
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
158
|
+
>(
|
|
159
|
+
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
|
|
160
|
+
) => Promise<ServerSentEventsResult<TData, TError>>;
|
|
161
|
+
|
|
162
|
+
type RequestFn = <
|
|
163
|
+
TData = unknown,
|
|
164
|
+
TError = unknown,
|
|
165
|
+
ThrowOnError extends boolean = false,
|
|
166
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
167
|
+
>(
|
|
168
|
+
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'> &
|
|
169
|
+
Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, 'method'>,
|
|
170
|
+
) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
|
171
|
+
|
|
172
|
+
type RequestOptionsFn = <
|
|
173
|
+
ThrowOnError extends boolean = false,
|
|
174
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
175
|
+
>(
|
|
176
|
+
options: RequestOptions<unknown, TResponseStyle, ThrowOnError>,
|
|
177
|
+
) => HttpRequest<unknown>;
|
|
178
|
+
|
|
179
|
+
type BuildUrlFn = <
|
|
180
|
+
TData extends {
|
|
181
|
+
body?: unknown;
|
|
182
|
+
path?: Record<string, unknown>;
|
|
183
|
+
query?: Record<string, unknown>;
|
|
184
|
+
url: string;
|
|
185
|
+
},
|
|
186
|
+
>(
|
|
187
|
+
options: TData & Options<TData>,
|
|
188
|
+
) => string;
|
|
189
|
+
|
|
190
|
+
export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn, SseFn> & {
|
|
191
|
+
interceptors: Middleware<
|
|
192
|
+
HttpRequest<unknown>,
|
|
193
|
+
HttpResponse<unknown>,
|
|
194
|
+
unknown,
|
|
195
|
+
ResolvedRequestOptions
|
|
196
|
+
>;
|
|
197
|
+
requestOptions: RequestOptionsFn;
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The `createClientConfig()` function will be called on client initialization
|
|
202
|
+
* and the returned object will become the client's initial configuration.
|
|
203
|
+
*
|
|
204
|
+
* You may want to initialize your client this way instead of calling
|
|
205
|
+
* `setConfig()`. This is useful for example if you're using Next.js
|
|
206
|
+
* to ensure your client always has the correct values.
|
|
207
|
+
*/
|
|
208
|
+
export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
|
|
209
|
+
override?: Config<ClientOptions & T>,
|
|
210
|
+
) => Config<Required<ClientOptions> & T>;
|
|
211
|
+
|
|
212
|
+
export interface TDataShape {
|
|
213
|
+
body?: unknown;
|
|
214
|
+
headers?: unknown;
|
|
215
|
+
path?: unknown;
|
|
216
|
+
query?: unknown;
|
|
217
|
+
url: string;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
|
|
221
|
+
|
|
222
|
+
export type Options<
|
|
223
|
+
TData extends TDataShape = TDataShape,
|
|
224
|
+
ThrowOnError extends boolean = boolean,
|
|
225
|
+
TResponse = unknown,
|
|
226
|
+
TResponseStyle extends ResponseStyle = 'fields',
|
|
227
|
+
> = OmitKeys<
|
|
228
|
+
RequestOptions<TResponse, TResponseStyle, ThrowOnError>,
|
|
229
|
+
'body' | 'path' | 'query' | 'url'
|
|
230
|
+
> &
|
|
231
|
+
([TData] extends [never] ? unknown : Omit<TData, 'url'>);
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
import { HttpHeaders } from '@angular/common/http';
|
|
2
|
+
|
|
3
|
+
import { getAuthToken } from '../core/auth';
|
|
4
|
+
import type {
|
|
5
|
+
QuerySerializer,
|
|
6
|
+
QuerySerializerOptions,
|
|
7
|
+
} from '../core/bodySerializer';
|
|
8
|
+
import {
|
|
9
|
+
serializeArrayParam,
|
|
10
|
+
serializeObjectParam,
|
|
11
|
+
serializePrimitiveParam,
|
|
12
|
+
} from '../core/pathSerializer';
|
|
13
|
+
import type { Client, ClientOptions, Config, RequestOptions } from './types';
|
|
14
|
+
|
|
15
|
+
interface PathSerializer {
|
|
16
|
+
path: Record<string, unknown>;
|
|
17
|
+
url: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const PATH_PARAM_RE = /\{[^{}]+\}/g;
|
|
21
|
+
|
|
22
|
+
type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited';
|
|
23
|
+
type MatrixStyle = 'label' | 'matrix' | 'simple';
|
|
24
|
+
type ArraySeparatorStyle = ArrayStyle | MatrixStyle;
|
|
25
|
+
|
|
26
|
+
const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => {
|
|
27
|
+
let url = _url;
|
|
28
|
+
const matches = _url.match(PATH_PARAM_RE);
|
|
29
|
+
if (matches) {
|
|
30
|
+
for (const match of matches) {
|
|
31
|
+
let explode = false;
|
|
32
|
+
let name = match.substring(1, match.length - 1);
|
|
33
|
+
let style: ArraySeparatorStyle = 'simple';
|
|
34
|
+
|
|
35
|
+
if (name.endsWith('*')) {
|
|
36
|
+
explode = true;
|
|
37
|
+
name = name.substring(0, name.length - 1);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (name.startsWith('.')) {
|
|
41
|
+
name = name.substring(1);
|
|
42
|
+
style = 'label';
|
|
43
|
+
} else if (name.startsWith(';')) {
|
|
44
|
+
name = name.substring(1);
|
|
45
|
+
style = 'matrix';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const value = path[name];
|
|
49
|
+
|
|
50
|
+
if (value === undefined || value === null) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (Array.isArray(value)) {
|
|
55
|
+
url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (typeof value === 'object') {
|
|
60
|
+
url = url.replace(
|
|
61
|
+
match,
|
|
62
|
+
serializeObjectParam({
|
|
63
|
+
explode,
|
|
64
|
+
name,
|
|
65
|
+
style,
|
|
66
|
+
value: value as Record<string, unknown>,
|
|
67
|
+
valueOnly: true,
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (style === 'matrix') {
|
|
74
|
+
url = url.replace(
|
|
75
|
+
match,
|
|
76
|
+
`;${serializePrimitiveParam({
|
|
77
|
+
name,
|
|
78
|
+
value: value as string,
|
|
79
|
+
})}`,
|
|
80
|
+
);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const replaceValue = encodeURIComponent(
|
|
85
|
+
style === 'label' ? `.${value as string}` : (value as string),
|
|
86
|
+
);
|
|
87
|
+
url = url.replace(match, replaceValue);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return url;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const createQuerySerializer = <T = unknown>({
|
|
94
|
+
parameters = {},
|
|
95
|
+
...args
|
|
96
|
+
}: QuerySerializerOptions = {}) => {
|
|
97
|
+
const querySerializer = (queryParams: T) => {
|
|
98
|
+
const search: string[] = [];
|
|
99
|
+
if (queryParams && typeof queryParams === 'object') {
|
|
100
|
+
for (const name in queryParams) {
|
|
101
|
+
const value = queryParams[name];
|
|
102
|
+
|
|
103
|
+
if (value === undefined || value === null) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const options = parameters[name] || args;
|
|
108
|
+
|
|
109
|
+
if (Array.isArray(value)) {
|
|
110
|
+
const serializedArray = serializeArrayParam({
|
|
111
|
+
allowReserved: options.allowReserved,
|
|
112
|
+
explode: true,
|
|
113
|
+
name,
|
|
114
|
+
style: 'form',
|
|
115
|
+
value,
|
|
116
|
+
...options.array,
|
|
117
|
+
});
|
|
118
|
+
if (serializedArray) search.push(serializedArray);
|
|
119
|
+
} else if (typeof value === 'object') {
|
|
120
|
+
const serializedObject = serializeObjectParam({
|
|
121
|
+
allowReserved: options.allowReserved,
|
|
122
|
+
explode: true,
|
|
123
|
+
name,
|
|
124
|
+
style: 'deepObject',
|
|
125
|
+
value: value as Record<string, unknown>,
|
|
126
|
+
...options.object,
|
|
127
|
+
});
|
|
128
|
+
if (serializedObject) search.push(serializedObject);
|
|
129
|
+
} else {
|
|
130
|
+
const serializedPrimitive = serializePrimitiveParam({
|
|
131
|
+
allowReserved: options.allowReserved,
|
|
132
|
+
name,
|
|
133
|
+
value: value as string,
|
|
134
|
+
});
|
|
135
|
+
if (serializedPrimitive) search.push(serializedPrimitive);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return search.join('&');
|
|
140
|
+
};
|
|
141
|
+
return querySerializer;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Infers parseAs value from provided Content-Type header.
|
|
146
|
+
*/
|
|
147
|
+
export const getParseAs = (
|
|
148
|
+
contentType: string | null,
|
|
149
|
+
): 'blob' | 'formData' | 'json' | 'stream' | 'text' | undefined => {
|
|
150
|
+
if (!contentType) {
|
|
151
|
+
// If no Content-Type header is provided, the best we can do is return the raw response body,
|
|
152
|
+
// which is effectively the same as the 'stream' option.
|
|
153
|
+
return 'stream';
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const cleanContent = contentType.split(';')[0]?.trim();
|
|
157
|
+
|
|
158
|
+
if (!cleanContent) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (cleanContent.startsWith('application/json') || cleanContent.endsWith('+json')) {
|
|
163
|
+
return 'json';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (cleanContent === 'multipart/form-data') {
|
|
167
|
+
return 'formData';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (
|
|
171
|
+
['application/', 'audio/', 'image/', 'video/'].some((type) => cleanContent.startsWith(type))
|
|
172
|
+
) {
|
|
173
|
+
return 'blob';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (cleanContent.startsWith('text/')) {
|
|
177
|
+
return 'text';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export const setAuthParams = async (
|
|
184
|
+
options: Pick<Required<RequestOptions>, 'security'> &
|
|
185
|
+
Pick<RequestOptions, 'auth' | 'query'> & {
|
|
186
|
+
headers: HttpHeaders;
|
|
187
|
+
},
|
|
188
|
+
) => {
|
|
189
|
+
for (const auth of options.security) {
|
|
190
|
+
const token = await getAuthToken(auth, options.auth);
|
|
191
|
+
|
|
192
|
+
if (!token) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const name = auth.name ?? 'Authorization';
|
|
197
|
+
|
|
198
|
+
switch (auth.in) {
|
|
199
|
+
case 'query':
|
|
200
|
+
if (!options.query) {
|
|
201
|
+
options.query = {};
|
|
202
|
+
}
|
|
203
|
+
options.query[name] = token;
|
|
204
|
+
break;
|
|
205
|
+
case 'cookie':
|
|
206
|
+
options.headers = options.headers.append('Cookie', `${name}=${token}`);
|
|
207
|
+
break;
|
|
208
|
+
case 'header':
|
|
209
|
+
default:
|
|
210
|
+
options.headers = options.headers.set(name, token);
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export const buildUrl: Client['buildUrl'] = (options) => {
|
|
219
|
+
const url = getUrl({
|
|
220
|
+
baseUrl: options.baseUrl as string,
|
|
221
|
+
path: options.path,
|
|
222
|
+
query: options.query,
|
|
223
|
+
querySerializer:
|
|
224
|
+
typeof options.querySerializer === 'function'
|
|
225
|
+
? options.querySerializer
|
|
226
|
+
: createQuerySerializer(options.querySerializer),
|
|
227
|
+
url: options.url,
|
|
228
|
+
});
|
|
229
|
+
return url;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
export const getUrl = ({
|
|
233
|
+
baseUrl,
|
|
234
|
+
path,
|
|
235
|
+
query,
|
|
236
|
+
querySerializer,
|
|
237
|
+
url: _url,
|
|
238
|
+
}: {
|
|
239
|
+
baseUrl?: string;
|
|
240
|
+
path?: Record<string, unknown>;
|
|
241
|
+
query?: Record<string, unknown>;
|
|
242
|
+
querySerializer: QuerySerializer;
|
|
243
|
+
url: string;
|
|
244
|
+
}) => {
|
|
245
|
+
const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
|
|
246
|
+
let url = (baseUrl ?? '') + pathUrl;
|
|
247
|
+
if (path) {
|
|
248
|
+
url = defaultPathSerializer({ path, url });
|
|
249
|
+
}
|
|
250
|
+
let search = query ? querySerializer(query) : '';
|
|
251
|
+
if (search.startsWith('?')) {
|
|
252
|
+
search = search.substring(1);
|
|
253
|
+
}
|
|
254
|
+
if (search) {
|
|
255
|
+
url += `?${search}`;
|
|
256
|
+
}
|
|
257
|
+
return url;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export const mergeConfigs = (a: Config, b: Config): Config => {
|
|
261
|
+
const config = { ...a, ...b };
|
|
262
|
+
if (config.baseUrl?.endsWith('/')) {
|
|
263
|
+
config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
|
|
264
|
+
}
|
|
265
|
+
config.headers = mergeHeaders(a.headers, b.headers);
|
|
266
|
+
return config;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
export const mergeHeaders = (
|
|
270
|
+
...headers: Array<Required<Config>['headers'] | undefined>
|
|
271
|
+
): HttpHeaders => {
|
|
272
|
+
let mergedHeaders = new HttpHeaders();
|
|
273
|
+
|
|
274
|
+
for (const header of headers) {
|
|
275
|
+
if (!header || typeof header !== 'object') {
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (header instanceof HttpHeaders) {
|
|
280
|
+
// Merge HttpHeaders instance
|
|
281
|
+
header.keys().forEach((key) => {
|
|
282
|
+
const values = header.getAll(key);
|
|
283
|
+
if (values) {
|
|
284
|
+
values.forEach((value) => {
|
|
285
|
+
mergedHeaders = mergedHeaders.append(key, value);
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
} else {
|
|
290
|
+
// Merge plain object headers
|
|
291
|
+
for (const [key, value] of Object.entries(header)) {
|
|
292
|
+
if (value === null) {
|
|
293
|
+
mergedHeaders = mergedHeaders.delete(key);
|
|
294
|
+
} else if (Array.isArray(value)) {
|
|
295
|
+
for (const v of value) {
|
|
296
|
+
mergedHeaders = mergedHeaders.append(key, v as string);
|
|
297
|
+
}
|
|
298
|
+
} else if (value !== undefined) {
|
|
299
|
+
// assume object headers are meant to be JSON stringified, i.e. their
|
|
300
|
+
// content value in OpenAPI specification is 'application/json'
|
|
301
|
+
mergedHeaders = mergedHeaders.set(
|
|
302
|
+
key,
|
|
303
|
+
typeof value === 'object' ? JSON.stringify(value) : (value as string),
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return mergedHeaders;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
type ErrInterceptor<Err, Res, Req, Options> = (
|
|
314
|
+
error: Err,
|
|
315
|
+
response: Res,
|
|
316
|
+
request: Req,
|
|
317
|
+
options: Options,
|
|
318
|
+
) => Err | Promise<Err>;
|
|
319
|
+
|
|
320
|
+
type ReqInterceptor<Req, Options> = (request: Req, options: Options) => Req | Promise<Req>;
|
|
321
|
+
|
|
322
|
+
type ResInterceptor<Res, Req, Options> = (
|
|
323
|
+
response: Res,
|
|
324
|
+
request: Req,
|
|
325
|
+
options: Options,
|
|
326
|
+
) => Res | Promise<Res>;
|
|
327
|
+
|
|
328
|
+
class Interceptors<Interceptor> {
|
|
329
|
+
fns: Array<Interceptor | null> = [];
|
|
330
|
+
|
|
331
|
+
clear(): void {
|
|
332
|
+
this.fns = [];
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
eject(id: number | Interceptor): void {
|
|
336
|
+
const index = this.getInterceptorIndex(id);
|
|
337
|
+
if (this.fns[index]) {
|
|
338
|
+
this.fns[index] = null;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
exists(id: number | Interceptor): boolean {
|
|
343
|
+
const index = this.getInterceptorIndex(id);
|
|
344
|
+
return Boolean(this.fns[index]);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
getInterceptorIndex(id: number | Interceptor): number {
|
|
348
|
+
if (typeof id === 'number') {
|
|
349
|
+
return this.fns[id] ? id : -1;
|
|
350
|
+
}
|
|
351
|
+
return this.fns.indexOf(id);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
update(id: number | Interceptor, fn: Interceptor): number | Interceptor | false {
|
|
355
|
+
const index = this.getInterceptorIndex(id);
|
|
356
|
+
if (this.fns[index]) {
|
|
357
|
+
this.fns[index] = fn;
|
|
358
|
+
return id;
|
|
359
|
+
}
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
use(fn: Interceptor): number {
|
|
364
|
+
this.fns.push(fn);
|
|
365
|
+
return this.fns.length - 1;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export interface Middleware<Req, Res, Err, Options> {
|
|
370
|
+
error: Interceptors<ErrInterceptor<Err, Res, Req, Options>>;
|
|
371
|
+
request: Interceptors<ReqInterceptor<Req, Options>>;
|
|
372
|
+
response: Interceptors<ResInterceptor<Res, Req, Options>>;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export const createInterceptors = <Req, Res, Err, Options>(): Middleware<
|
|
376
|
+
Req,
|
|
377
|
+
Res,
|
|
378
|
+
Err,
|
|
379
|
+
Options
|
|
380
|
+
> => ({
|
|
381
|
+
error: new Interceptors<ErrInterceptor<Err, Res, Req, Options>>(),
|
|
382
|
+
request: new Interceptors<ReqInterceptor<Req, Options>>(),
|
|
383
|
+
response: new Interceptors<ResInterceptor<Res, Req, Options>>(),
|
|
384
|
+
});
|
|
385
|
+
|
|
386
|
+
const defaultQuerySerializer = createQuerySerializer({
|
|
387
|
+
allowReserved: false,
|
|
388
|
+
array: {
|
|
389
|
+
explode: true,
|
|
390
|
+
style: 'form',
|
|
391
|
+
},
|
|
392
|
+
object: {
|
|
393
|
+
explode: true,
|
|
394
|
+
style: 'deepObject',
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
const defaultHeaders = {
|
|
399
|
+
'Content-Type': 'application/json',
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
export const createConfig = <T extends ClientOptions = ClientOptions>(
|
|
403
|
+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
|
|
404
|
+
): Config<Omit<ClientOptions, keyof T> & T> => ({
|
|
405
|
+
headers: defaultHeaders,
|
|
406
|
+
querySerializer: defaultQuerySerializer,
|
|
407
|
+
...override,
|
|
408
|
+
});
|