|
| 1 | +import type { Auth } from '../core/auth'; |
| 2 | +import type { |
| 3 | + Client as CoreClient, |
| 4 | + Config as CoreConfig, |
| 5 | +} from '../core/types'; |
| 6 | +import type { Middleware } from './utils'; |
| 7 | + |
| 8 | +export type ResponseStyle = 'data' | 'fields'; |
| 9 | + |
| 10 | +export interface Config<T extends ClientOptions = ClientOptions> |
| 11 | + extends Omit<RequestInit, 'body' | 'headers' | 'method'>, |
| 12 | + CoreConfig { |
| 13 | + /** |
| 14 | + * Base URL for all requests made by this client. |
| 15 | + */ |
| 16 | + baseUrl?: T['baseUrl']; |
| 17 | + /** |
| 18 | + * Fetch API implementation. You can use this option to provide a custom |
| 19 | + * fetch instance. |
| 20 | + * |
| 21 | + * @default globalThis.fetch |
| 22 | + */ |
| 23 | + fetch?: (request: Request) => ReturnType<typeof fetch>; |
| 24 | + /** |
| 25 | + * Please don't use the Fetch client for Next.js applications. The `next` |
| 26 | + * options won't have any effect. |
| 27 | + * |
| 28 | + * Install {@link https://www.npmjs.com/package/@hey-api/client-next `@hey-api/client-next`} instead. |
| 29 | + */ |
| 30 | + next?: never; |
| 31 | + /** |
| 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. |
| 36 | + * |
| 37 | + * @default 'auto' |
| 38 | + */ |
| 39 | + parseAs?: |
| 40 | + | 'arrayBuffer' |
| 41 | + | 'auto' |
| 42 | + | 'blob' |
| 43 | + | 'formData' |
| 44 | + | 'json' |
| 45 | + | 'stream' |
| 46 | + | 'text'; |
| 47 | + /** |
| 48 | + * Should we return only data or multiple fields (data, error, response, etc.)? |
| 49 | + * |
| 50 | + * @default 'fields' |
| 51 | + */ |
| 52 | + responseStyle?: ResponseStyle; |
| 53 | + /** |
| 54 | + * Throw an error instead of returning it in the response? |
| 55 | + * |
| 56 | + * @default false |
| 57 | + */ |
| 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 | + }> { |
| 69 | + /** |
| 70 | + * Any body that you want to add to your request. |
| 71 | + * |
| 72 | + * {@link https://developer.mozilla.org/docs/Web/API/fetch#body} |
| 73 | + */ |
| 74 | + body?: unknown; |
| 75 | + path?: Record<string, unknown>; |
| 76 | + query?: Record<string, unknown>; |
| 77 | + /** |
| 78 | + * Security mechanism(s) to use for the request. |
| 79 | + */ |
| 80 | + security?: ReadonlyArray<Auth>; |
| 81 | + url: Url; |
| 82 | +} |
| 83 | + |
| 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> |
| 120 | + ? TError[keyof TError] |
| 121 | + : TError; |
| 122 | + } |
| 123 | + ) & { |
| 124 | + request: Request; |
| 125 | + response: Response; |
| 126 | + } |
| 127 | + >; |
| 128 | + |
| 129 | +export interface ClientOptions { |
| 130 | + baseUrl?: string; |
| 131 | + responseStyle?: ResponseStyle; |
| 132 | + throwOnError?: boolean; |
| 133 | +} |
| 134 | + |
| 135 | +type MethodFn = < |
| 136 | + TData = unknown, |
| 137 | + TError = unknown, |
| 138 | + ThrowOnError extends boolean = false, |
| 139 | + TResponseStyle extends ResponseStyle = 'fields', |
| 140 | +>( |
| 141 | + options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'>, |
| 142 | +) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>; |
| 143 | + |
| 144 | +type RequestFn = < |
| 145 | + TData = unknown, |
| 146 | + TError = unknown, |
| 147 | + ThrowOnError extends boolean = false, |
| 148 | + TResponseStyle extends ResponseStyle = 'fields', |
| 149 | +>( |
| 150 | + options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'> & |
| 151 | + Pick<Required<RequestOptions<TResponseStyle, ThrowOnError>>, 'method'>, |
| 152 | +) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>; |
| 153 | + |
| 154 | +type BuildUrlFn = < |
| 155 | + TData extends { |
| 156 | + body?: unknown; |
| 157 | + path?: Record<string, unknown>; |
| 158 | + query?: Record<string, unknown>; |
| 159 | + url: string; |
| 160 | + }, |
| 161 | +>( |
| 162 | + options: Pick<TData, 'url'> & Options<TData>, |
| 163 | +) => string; |
| 164 | + |
| 165 | +export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & { |
| 166 | + interceptors: Middleware<Request, Response, unknown, RequestOptions>; |
| 167 | +}; |
| 168 | + |
| 169 | +/** |
| 170 | + * The `createClientConfig()` function will be called on client initialization |
| 171 | + * and the returned object will become the client's initial configuration. |
| 172 | + * |
| 173 | + * You may want to initialize your client this way instead of calling |
| 174 | + * `setConfig()`. This is useful for example if you're using Next.js |
| 175 | + * to ensure your client always has the correct values. |
| 176 | + */ |
| 177 | +export type CreateClientConfig<T extends ClientOptions = ClientOptions> = ( |
| 178 | + override?: Config<ClientOptions & T>, |
| 179 | +) => Config<Required<ClientOptions> & T>; |
| 180 | + |
| 181 | +export interface TDataShape { |
| 182 | + body?: unknown; |
| 183 | + headers?: unknown; |
| 184 | + path?: unknown; |
| 185 | + query?: unknown; |
| 186 | + url: string; |
| 187 | +} |
| 188 | + |
| 189 | +type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>; |
| 190 | + |
| 191 | +export type Options< |
| 192 | + TData extends TDataShape = TDataShape, |
| 193 | + ThrowOnError extends boolean = boolean, |
| 194 | + TResponseStyle extends ResponseStyle = 'fields', |
| 195 | +> = OmitKeys< |
| 196 | + RequestOptions<TResponseStyle, ThrowOnError>, |
| 197 | + 'body' | 'path' | 'query' | 'url' |
| 198 | +> & |
| 199 | + Omit<TData, 'url'>; |
| 200 | + |
| 201 | +export type OptionsLegacyParser< |
| 202 | + TData = unknown, |
| 203 | + ThrowOnError extends boolean = boolean, |
| 204 | + TResponseStyle extends ResponseStyle = 'fields', |
| 205 | +> = TData extends { body?: any } |
| 206 | + ? TData extends { headers?: any } |
| 207 | + ? OmitKeys< |
| 208 | + RequestOptions<TResponseStyle, ThrowOnError>, |
| 209 | + 'body' | 'headers' | 'url' |
| 210 | + > & |
| 211 | + TData |
| 212 | + : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'body' | 'url'> & |
| 213 | + TData & |
| 214 | + Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'headers'> |
| 215 | + : TData extends { headers?: any } |
| 216 | + ? OmitKeys< |
| 217 | + RequestOptions<TResponseStyle, ThrowOnError>, |
| 218 | + 'headers' | 'url' |
| 219 | + > & |
| 220 | + TData & |
| 221 | + Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'body'> |
| 222 | + : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'url'> & TData; |
0 commit comments