@hey-api/openapi-ts 0.80.6 → 0.80.8

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.
@@ -0,0 +1,162 @@
1
+ import type { HttpResponse } from '@angular/common/http';
2
+ import { HttpClient, HttpEventType, HttpRequest } from '@angular/common/http';
3
+ import {
4
+ assertInInjectionContext,
5
+ inject,
6
+ provideAppInitializer,
7
+ } from '@angular/core';
8
+ import { firstValueFrom } from 'rxjs';
9
+ import { filter } from 'rxjs/operators';
10
+
11
+ import type { Client, Config, ResolvedRequestOptions } from './types';
12
+ import {
13
+ buildUrl,
14
+ createConfig,
15
+ createInterceptors,
16
+ mergeConfigs,
17
+ mergeHeaders,
18
+ setAuthParams,
19
+ } from './utils';
20
+
21
+ export function provideHeyApiClient(client: Client) {
22
+ return provideAppInitializer(() => {
23
+ const httpClient = inject(HttpClient);
24
+ client.setConfig({ httpClient });
25
+ });
26
+ }
27
+
28
+ export const createClient = (config: Config = {}): Client => {
29
+ let _config = mergeConfigs(createConfig(), config);
30
+
31
+ const getConfig = (): Config => ({ ..._config });
32
+
33
+ const setConfig = (config: Config): Config => {
34
+ _config = mergeConfigs(_config, config);
35
+ return getConfig();
36
+ };
37
+
38
+ const interceptors = createInterceptors<
39
+ HttpRequest<unknown>,
40
+ HttpResponse<unknown>,
41
+ unknown,
42
+ ResolvedRequestOptions
43
+ >();
44
+
45
+ const request: Client['request'] = async (options) => {
46
+ const opts = {
47
+ ..._config,
48
+ ...options,
49
+ headers: mergeHeaders(_config.headers, options.headers),
50
+ httpClient: options.httpClient ?? _config.httpClient,
51
+ serializedBody: undefined,
52
+ };
53
+
54
+ if (!opts.httpClient) {
55
+ assertInInjectionContext(request);
56
+ opts.httpClient = inject(HttpClient);
57
+ }
58
+
59
+ if (opts.security) {
60
+ await setAuthParams({
61
+ ...opts,
62
+ security: opts.security,
63
+ });
64
+ }
65
+
66
+ if (opts.requestValidator) {
67
+ await opts.requestValidator(opts);
68
+ }
69
+
70
+ if (opts.body && opts.bodySerializer) {
71
+ opts.serializedBody = opts.bodySerializer(opts.body);
72
+ }
73
+
74
+ // remove Content-Type header if body is empty to avoid sending invalid requests
75
+ if (opts.serializedBody === undefined || opts.serializedBody === '') {
76
+ opts.headers.delete('Content-Type');
77
+ }
78
+
79
+ const url = buildUrl(opts);
80
+
81
+ let req = new HttpRequest<unknown>(opts.method, url, {
82
+ redirect: 'follow',
83
+ ...opts,
84
+ body: opts.serializedBody,
85
+ });
86
+
87
+ for (const fn of interceptors.request._fns) {
88
+ if (fn) {
89
+ req = await fn(req, opts);
90
+ }
91
+ }
92
+
93
+ let response;
94
+ const result = {
95
+ request: req,
96
+ response,
97
+ };
98
+
99
+ try {
100
+ response = await firstValueFrom(
101
+ opts.httpClient
102
+ .request(req)
103
+ .pipe(filter((event) => event.type === HttpEventType.Response)),
104
+ );
105
+
106
+ for (const fn of interceptors.response._fns) {
107
+ if (fn) {
108
+ response = await fn(response, req, opts);
109
+ }
110
+ }
111
+
112
+ let bodyResponse = response.body as Record<string, unknown>;
113
+
114
+ if (opts.responseValidator) {
115
+ await opts.responseValidator(bodyResponse);
116
+ }
117
+
118
+ if (opts.responseTransformer) {
119
+ bodyResponse = (await opts.responseTransformer(bodyResponse)) as Record<
120
+ string,
121
+ unknown
122
+ >;
123
+ }
124
+
125
+ return (
126
+ opts.responseStyle === 'data'
127
+ ? bodyResponse
128
+ : { data: bodyResponse, ...result }
129
+ ) as any;
130
+ } catch (error) {
131
+ for (const fn of interceptors.error._fns) {
132
+ if (fn) {
133
+ (await fn(error, response!, req, opts)) as string;
134
+ }
135
+ }
136
+
137
+ return opts.responseStyle === 'data'
138
+ ? undefined
139
+ : {
140
+ error,
141
+ ...result,
142
+ };
143
+ }
144
+ };
145
+
146
+ return {
147
+ buildUrl,
148
+ connect: (options) => request({ ...options, method: 'CONNECT' }),
149
+ delete: (options) => request({ ...options, method: 'DELETE' }),
150
+ get: (options) => request({ ...options, method: 'GET' }),
151
+ getConfig,
152
+ head: (options) => request({ ...options, method: 'HEAD' }),
153
+ interceptors,
154
+ options: (options) => request({ ...options, method: 'OPTIONS' }),
155
+ patch: (options) => request({ ...options, method: 'PATCH' }),
156
+ post: (options) => request({ ...options, method: 'POST' }),
157
+ put: (options) => request({ ...options, method: 'PUT' }),
158
+ request,
159
+ setConfig,
160
+ trace: (options) => request({ ...options, method: 'TRACE' }),
161
+ };
162
+ };
@@ -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 { createClient } from './client';
10
+ export type {
11
+ Client,
12
+ ClientOptions,
13
+ Config,
14
+ CreateClientConfig,
15
+ Options,
16
+ OptionsLegacyParser,
17
+ RequestOptions,
18
+ RequestResult,
19
+ ResolvedRequestOptions,
20
+ ResponseStyle,
21
+ TDataShape,
22
+ } from './types';
23
+ export { createConfig, mergeHeaders } from './utils';
@@ -0,0 +1,209 @@
1
+ import type {
2
+ HttpClient,
3
+ HttpRequest,
4
+ HttpResponse,
5
+ } from '@angular/common/http';
6
+
7
+ import type { Auth } from '../core/auth';
8
+ import type {
9
+ Client as CoreClient,
10
+ Config as CoreConfig,
11
+ } from '../core/types';
12
+ import type { Middleware } from './utils';
13
+
14
+ export type ResponseStyle = 'data' | 'fields';
15
+
16
+ export interface Config<T extends ClientOptions = ClientOptions>
17
+ extends Omit<RequestInit, 'body' | 'headers' | 'method'>,
18
+ CoreConfig {
19
+ /**
20
+ * Base URL for all requests made by this client.
21
+ */
22
+ baseUrl?: T['baseUrl'];
23
+ /**
24
+ * The HTTP client to use for making requests.
25
+ */
26
+ httpClient?: HttpClient;
27
+ /**
28
+ * Should we return only data or multiple fields (data, error, response, etc.)?
29
+ *
30
+ * @default 'fields'
31
+ */
32
+ responseStyle?: ResponseStyle;
33
+ }
34
+
35
+ export interface RequestOptions<
36
+ TResponseStyle extends ResponseStyle = 'fields',
37
+ ThrowOnError extends boolean = boolean,
38
+ Url extends string = string,
39
+ > extends Config<{
40
+ responseStyle: TResponseStyle;
41
+ throwOnError: ThrowOnError;
42
+ }> {
43
+ /**
44
+ * Any body that you want to add to your request.
45
+ *
46
+ * {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
47
+ */
48
+ body?: unknown;
49
+ path?: Record<string, unknown>;
50
+ query?: Record<string, unknown>;
51
+ /**
52
+ * Security mechanism(s) to use for the request.
53
+ */
54
+ security?: ReadonlyArray<Auth>;
55
+ url: Url;
56
+ }
57
+
58
+ export interface ResolvedRequestOptions<
59
+ TResponseStyle extends ResponseStyle = 'fields',
60
+ ThrowOnError extends boolean = boolean,
61
+ Url extends string = string,
62
+ > extends RequestOptions<TResponseStyle, ThrowOnError, Url> {
63
+ serializedBody?: string;
64
+ }
65
+
66
+ export type RequestResult<
67
+ TData = unknown,
68
+ TError = unknown,
69
+ ThrowOnError extends boolean = boolean,
70
+ TResponseStyle extends ResponseStyle = 'fields',
71
+ > = ThrowOnError extends true
72
+ ? Promise<
73
+ TResponseStyle extends 'data'
74
+ ? TData extends Record<string, unknown>
75
+ ? TData[keyof TData]
76
+ : TData
77
+ : {
78
+ data: TData extends Record<string, unknown>
79
+ ? TData[keyof TData]
80
+ : TData;
81
+ request: Request;
82
+ response: Response;
83
+ }
84
+ >
85
+ : Promise<
86
+ TResponseStyle extends 'data'
87
+ ?
88
+ | (TData extends Record<string, unknown>
89
+ ? TData[keyof TData]
90
+ : TData)
91
+ | undefined
92
+ : (
93
+ | {
94
+ data: TData extends Record<string, unknown>
95
+ ? TData[keyof TData]
96
+ : TData;
97
+ error: undefined;
98
+ }
99
+ | {
100
+ data: undefined;
101
+ error: TError extends Record<string, unknown>
102
+ ? TError[keyof TError]
103
+ : TError;
104
+ }
105
+ ) & {
106
+ request: Request;
107
+ response: Response;
108
+ }
109
+ >;
110
+
111
+ export interface ClientOptions {
112
+ baseUrl?: string;
113
+ responseStyle?: ResponseStyle;
114
+ throwOnError?: boolean;
115
+ }
116
+
117
+ type MethodFn = <
118
+ TData = unknown,
119
+ TError = unknown,
120
+ ThrowOnError extends boolean = false,
121
+ TResponseStyle extends ResponseStyle = 'fields',
122
+ >(
123
+ options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'>,
124
+ ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
125
+
126
+ type RequestFn = <
127
+ TData = unknown,
128
+ TError = unknown,
129
+ ThrowOnError extends boolean = false,
130
+ TResponseStyle extends ResponseStyle = 'fields',
131
+ >(
132
+ options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'> &
133
+ Pick<Required<RequestOptions<TResponseStyle, ThrowOnError>>, 'method'>,
134
+ ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
135
+
136
+ type BuildUrlFn = <
137
+ TData extends {
138
+ body?: unknown;
139
+ path?: Record<string, unknown>;
140
+ query?: Record<string, unknown>;
141
+ url: string;
142
+ },
143
+ >(
144
+ options: Pick<TData, 'url'> & Options<TData>,
145
+ ) => string;
146
+
147
+ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
148
+ interceptors: Middleware<
149
+ HttpRequest<unknown>,
150
+ HttpResponse<unknown>,
151
+ unknown,
152
+ ResolvedRequestOptions
153
+ >;
154
+ };
155
+
156
+ /**
157
+ * The `createClientConfig()` function will be called on client initialization
158
+ * and the returned object will become the client's initial configuration.
159
+ *
160
+ * You may want to initialize your client this way instead of calling
161
+ * `setConfig()`. This is useful for example if you're using Next.js
162
+ * to ensure your client always has the correct values.
163
+ */
164
+ export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
165
+ override?: Config<ClientOptions & T>,
166
+ ) => Config<Required<ClientOptions> & T>;
167
+
168
+ export interface TDataShape {
169
+ body?: unknown;
170
+ headers?: unknown;
171
+ path?: unknown;
172
+ query?: unknown;
173
+ url: string;
174
+ }
175
+
176
+ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
177
+
178
+ export type Options<
179
+ TData extends TDataShape = TDataShape,
180
+ ThrowOnError extends boolean = boolean,
181
+ TResponseStyle extends ResponseStyle = 'fields',
182
+ > = OmitKeys<
183
+ RequestOptions<TResponseStyle, ThrowOnError>,
184
+ 'body' | 'path' | 'query' | 'url'
185
+ > &
186
+ Omit<TData, 'url'>;
187
+
188
+ export type OptionsLegacyParser<
189
+ TData = unknown,
190
+ ThrowOnError extends boolean = boolean,
191
+ TResponseStyle extends ResponseStyle = 'fields',
192
+ > = TData extends { body?: any }
193
+ ? TData extends { headers?: any }
194
+ ? OmitKeys<
195
+ RequestOptions<TResponseStyle, ThrowOnError>,
196
+ 'body' | 'headers' | 'url'
197
+ > &
198
+ TData
199
+ : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'body' | 'url'> &
200
+ TData &
201
+ Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'headers'>
202
+ : TData extends { headers?: any }
203
+ ? OmitKeys<
204
+ RequestOptions<TResponseStyle, ThrowOnError>,
205
+ 'headers' | 'url'
206
+ > &
207
+ TData &
208
+ Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'body'>
209
+ : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'url'> & TData;