|
1 |
| -import type { Auth } from '../core/auth'; |
| 1 | +import type { Auth, AuthToken } from './auth'; |
2 | 2 | import type {
|
3 |
| - Client as CoreClient, |
4 |
| - Config as CoreConfig, |
5 |
| -} from '../core/types'; |
6 |
| -import type { Middleware } from './utils'; |
| 3 | + BodySerializer, |
| 4 | + QuerySerializer, |
| 5 | + QuerySerializerOptions, |
| 6 | +} from './bodySerializer'; |
7 | 7 |
|
8 |
| -export type ResponseStyle = 'data' | 'fields'; |
| 8 | +export interface Client< |
| 9 | + RequestFn = never, |
| 10 | + Config = unknown, |
| 11 | + MethodFn = never, |
| 12 | + BuildUrlFn = never, |
| 13 | +> { |
| 14 | + /** |
| 15 | + * Returns the final request URL. |
| 16 | + */ |
| 17 | + buildUrl: BuildUrlFn; |
| 18 | + connect: MethodFn; |
| 19 | + delete: MethodFn; |
| 20 | + get: MethodFn; |
| 21 | + getConfig: () => Config; |
| 22 | + head: MethodFn; |
| 23 | + options: MethodFn; |
| 24 | + patch: MethodFn; |
| 25 | + post: MethodFn; |
| 26 | + put: MethodFn; |
| 27 | + request: RequestFn; |
| 28 | + setConfig: (config: Config) => Config; |
| 29 | + trace: MethodFn; |
| 30 | +} |
9 | 31 |
|
10 |
| -export interface Config<T extends ClientOptions = ClientOptions> |
11 |
| - extends Omit<RequestInit, 'body' | 'headers' | 'method'>, |
12 |
| - CoreConfig { |
| 32 | +export interface Config { |
13 | 33 | /**
|
14 |
| - * Base URL for all requests made by this client. |
| 34 | + * Auth token or a function returning auth token. The resolved value will be |
| 35 | + * added to the request payload as defined by its `security` array. |
15 | 36 | */
|
16 |
| - baseUrl?: T['baseUrl']; |
| 37 | + auth?: ((auth: Auth) => Promise<AuthToken> | AuthToken) | AuthToken; |
17 | 38 | /**
|
18 |
| - * Fetch API implementation. You can use this option to provide a custom |
19 |
| - * fetch instance. |
20 |
| - * |
21 |
| - * @default globalThis.fetch |
| 39 | + * A function for serializing request body parameter. By default, |
| 40 | + * {@link JSON.stringify()} will be used. |
22 | 41 | */
|
23 |
| - fetch?: (request: Request) => ReturnType<typeof fetch>; |
| 42 | + bodySerializer?: BodySerializer | null; |
24 | 43 | /**
|
25 |
| - * Please don't use the Fetch client for Next.js applications. The `next` |
26 |
| - * options won't have any effect. |
| 44 | + * An object containing any HTTP headers that you want to pre-populate your |
| 45 | + * `Headers` object with. |
27 | 46 | *
|
28 |
| - * Install {@link https://www.npmjs.com/package/@hey-api/client-next `@hey-api/client-next`} instead. |
| 47 | + * {@link https://developer.mozilla.org/docs/Web/API/Headers/Headers#init See more} |
29 | 48 | */
|
30 |
| - next?: never; |
| 49 | + headers?: |
| 50 | + | RequestInit['headers'] |
| 51 | + | Record< |
| 52 | + string, |
| 53 | + | string |
| 54 | + | number |
| 55 | + | boolean |
| 56 | + | (string | number | boolean)[] |
| 57 | + | null |
| 58 | + | undefined |
| 59 | + | unknown |
| 60 | + >; |
31 | 61 | /**
|
32 |
| - * Return the response data parsed in a specified format. By default, `auto` |
33 |
| - * will infer the appropriate method from the `Content-Type` response header. |
34 |
| - * You can override this behavior with any of the {@link Body} methods. |
35 |
| - * Select `stream` if you don't want to parse response data at all. |
| 62 | + * The request method. |
36 | 63 | *
|
37 |
| - * @default 'auto' |
| 64 | + * {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more} |
38 | 65 | */
|
39 |
| - parseAs?: |
40 |
| - | 'arrayBuffer' |
41 |
| - | 'auto' |
42 |
| - | 'blob' |
43 |
| - | 'formData' |
44 |
| - | 'json' |
45 |
| - | 'stream' |
46 |
| - | 'text'; |
| 66 | + method?: |
| 67 | + | 'CONNECT' |
| 68 | + | 'DELETE' |
| 69 | + | 'GET' |
| 70 | + | 'HEAD' |
| 71 | + | 'OPTIONS' |
| 72 | + | 'PATCH' |
| 73 | + | 'POST' |
| 74 | + | 'PUT' |
| 75 | + | 'TRACE'; |
47 | 76 | /**
|
48 |
| - * Should we return only data or multiple fields (data, error, response, etc.)? |
| 77 | + * A function for serializing request query parameters. By default, arrays |
| 78 | + * will be exploded in form style, objects will be exploded in deepObject |
| 79 | + * style, and reserved characters are percent-encoded. |
| 80 | + * |
| 81 | + * This method will have no effect if the native `paramsSerializer()` Axios |
| 82 | + * API function is used. |
49 | 83 | *
|
50 |
| - * @default 'fields' |
| 84 | + * {@link https://swagger.io/docs/specification/serialization/#query View examples} |
51 | 85 | */
|
52 |
| - responseStyle?: ResponseStyle; |
| 86 | + querySerializer?: QuerySerializer | QuerySerializerOptions; |
53 | 87 | /**
|
54 |
| - * Throw an error instead of returning it in the response? |
55 |
| - * |
56 |
| - * @default false |
| 88 | + * A function validating request data. This is useful if you want to ensure |
| 89 | + * the request conforms to the desired shape, so it can be safely sent to |
| 90 | + * the server. |
57 | 91 | */
|
58 |
| - throwOnError?: T['throwOnError']; |
59 |
| -} |
60 |
| - |
61 |
| -export interface RequestOptions< |
62 |
| - TResponseStyle extends ResponseStyle = 'fields', |
63 |
| - ThrowOnError extends boolean = boolean, |
64 |
| - Url extends string = string, |
65 |
| -> extends Config<{ |
66 |
| - responseStyle: TResponseStyle; |
67 |
| - throwOnError: ThrowOnError; |
68 |
| - }> { |
| 92 | + requestValidator?: (data: unknown) => Promise<unknown>; |
69 | 93 | /**
|
70 |
| - * Any body that you want to add to your request. |
71 |
| - * |
72 |
| - * {@link https://developer.mozilla.org/docs/Web/API/fetch#body} |
| 94 | + * A function transforming response data before it's returned. This is useful |
| 95 | + * for post-processing data, e.g. converting ISO strings into Date objects. |
73 | 96 | */
|
74 |
| - body?: unknown; |
75 |
| - path?: Record<string, unknown>; |
76 |
| - query?: Record<string, unknown>; |
| 97 | + responseTransformer?: (data: unknown) => Promise<unknown>; |
77 | 98 | /**
|
78 |
| - * Security mechanism(s) to use for the request. |
| 99 | + * A function validating response data. This is useful if you want to ensure |
| 100 | + * the response conforms to the desired shape, so it can be safely passed to |
| 101 | + * the transformers and returned to the user. |
79 | 102 | */
|
80 |
| - security?: ReadonlyArray<Auth>; |
81 |
| - url: Url; |
| 103 | + responseValidator?: (data: unknown) => Promise<unknown>; |
82 | 104 | }
|
83 | 105 |
|
84 |
| -export type RequestResult< |
85 |
| - TData = unknown, |
86 |
| - TError = unknown, |
87 |
| - ThrowOnError extends boolean = boolean, |
88 |
| - TResponseStyle extends ResponseStyle = 'fields', |
89 |
| -> = ThrowOnError extends true |
90 |
| - ? Promise< |
91 |
| - TResponseStyle extends 'data' |
92 |
| - ? TData extends Record<string, unknown> |
93 |
| - ? TData[keyof TData] |
94 |
| - : TData |
95 |
| - : { |
96 |
| - data: TData extends Record<string, unknown> |
97 |
| - ? TData[keyof TData] |
98 |
| - : TData; |
99 |
| - request: Request; |
100 |
| - response: Response; |
101 |
| - } |
102 |
| - > |
103 |
| - : Promise< |
104 |
| - TResponseStyle extends 'data' |
105 |
| - ? |
106 |
| - | (TData extends Record<string, unknown> |
107 |
| - ? TData[keyof TData] |
108 |
| - : TData) |
109 |
| - | undefined |
110 |
| - : ( |
111 |
| - | { |
112 |
| - data: TData extends Record<string, unknown> |
113 |
| - ? TData[keyof TData] |
114 |
| - : TData; |
115 |
| - error: undefined; |
116 |
| - } |
117 |
| - | { |
118 |
| - data: undefined; |
119 |
| - error: TError extends Record<string, unknown> |
| 106 | +type IsExactlyNeverOrNeverUndefined<T> = [T] extends [never] |
| 107 | + ? true |
| 108 | + : [T] extends [never | undefined] |
| 109 | + ? [undefined] extends [T] |
| 110 | + ? false |
| 111 | + : true |
| 112 | + : false; |
| 113 | + |
| 114 | +export type OmitNever<T extends Record<string, unknown>> = { |
| 115 | + [K in keyof T as IsExactlyNeverOrNeverUndefined<T[K]> extends true |
| 116 | + ? never |
| 117 | + : K]: T[K]; |
| 118 | +}; |
| 119 | +n> |
120 | 120 | ? TError[keyof TError]
|
121 | 121 | : TError;
|
122 | 122 | }
|
|
0 commit comments