|
| 1 | +import type { Auth } from "../core/auth.gen.js"; |
| 2 | +import type { ServerSentEventsOptions, ServerSentEventsResult } from "../core/serverSentEvents.gen.js"; |
| 3 | +import type { Client as CoreClient, Config as CoreConfig } from "../core/types.gen.js"; |
| 4 | +import type { Middleware } from "./utils.gen.js"; |
| 5 | +export type ResponseStyle = "data" | "fields"; |
| 6 | +export interface Config<T extends ClientOptions = ClientOptions> extends Omit<RequestInit, "body" | "headers" | "method">, CoreConfig { |
| 7 | + /** |
| 8 | + * Base URL for all requests made by this client. |
| 9 | + */ |
| 10 | + baseUrl?: T["baseUrl"]; |
| 11 | + /** |
| 12 | + * Fetch API implementation. You can use this option to provide a custom |
| 13 | + * fetch instance. |
| 14 | + * |
| 15 | + * @default globalThis.fetch |
| 16 | + */ |
| 17 | + fetch?: (request: Request) => ReturnType<typeof fetch>; |
| 18 | + /** |
| 19 | + * Please don't use the Fetch client for Next.js applications. The `next` |
| 20 | + * options won't have any effect. |
| 21 | + * |
| 22 | + * Install {@link https://www.npmjs.com/package/@hey-api/client-next `@hey-api/client-next`} instead. |
| 23 | + */ |
| 24 | + next?: never; |
| 25 | + /** |
| 26 | + * Return the response data parsed in a specified format. By default, `auto` |
| 27 | + * will infer the appropriate method from the `Content-Type` response header. |
| 28 | + * You can override this behavior with any of the {@link Body} methods. |
| 29 | + * Select `stream` if you don't want to parse response data at all. |
| 30 | + * |
| 31 | + * @default 'auto' |
| 32 | + */ |
| 33 | + parseAs?: "arrayBuffer" | "auto" | "blob" | "formData" | "json" | "stream" | "text"; |
| 34 | + /** |
| 35 | + * Should we return only data or multiple fields (data, error, response, etc.)? |
| 36 | + * |
| 37 | + * @default 'fields' |
| 38 | + */ |
| 39 | + responseStyle?: ResponseStyle; |
| 40 | + /** |
| 41 | + * Throw an error instead of returning it in the response? |
| 42 | + * |
| 43 | + * @default false |
| 44 | + */ |
| 45 | + throwOnError?: T["throwOnError"]; |
| 46 | +} |
| 47 | +export interface RequestOptions<TData = unknown, TResponseStyle extends ResponseStyle = "fields", ThrowOnError extends boolean = boolean, Url extends string = string> extends Config<{ |
| 48 | + responseStyle: TResponseStyle; |
| 49 | + throwOnError: ThrowOnError; |
| 50 | +}>, Pick<ServerSentEventsOptions<TData>, "onSseError" | "onSseEvent" | "sseDefaultRetryDelay" | "sseMaxRetryAttempts" | "sseMaxRetryDelay"> { |
| 51 | + /** |
| 52 | + * Any body that you want to add to your request. |
| 53 | + * |
| 54 | + * {@link https://developer.mozilla.org/docs/Web/API/fetch#body} |
| 55 | + */ |
| 56 | + body?: unknown; |
| 57 | + path?: Record<string, unknown>; |
| 58 | + query?: Record<string, unknown>; |
| 59 | + /** |
| 60 | + * Security mechanism(s) to use for the request. |
| 61 | + */ |
| 62 | + security?: ReadonlyArray<Auth>; |
| 63 | + url: Url; |
| 64 | +} |
| 65 | +export interface ResolvedRequestOptions<TResponseStyle extends ResponseStyle = "fields", ThrowOnError extends boolean = boolean, Url extends string = string> extends RequestOptions<unknown, TResponseStyle, ThrowOnError, Url> { |
| 66 | + serializedBody?: string; |
| 67 | +} |
| 68 | +export type RequestResult<TData = unknown, TError = unknown, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = "fields"> = ThrowOnError extends true ? Promise<TResponseStyle extends "data" ? TData extends Record<string, unknown> ? TData[keyof TData] : TData : { |
| 69 | + data: TData extends Record<string, unknown> ? TData[keyof TData] : TData; |
| 70 | + request: Request; |
| 71 | + response: Response; |
| 72 | +}> : Promise<TResponseStyle extends "data" ? (TData extends Record<string, unknown> ? TData[keyof TData] : TData) | undefined : ({ |
| 73 | + data: TData extends Record<string, unknown> ? TData[keyof TData] : TData; |
| 74 | + error: undefined; |
| 75 | +} | { |
| 76 | + data: undefined; |
| 77 | + error: TError extends Record<string, unknown> ? TError[keyof TError] : TError; |
| 78 | +}) & { |
| 79 | + request: Request; |
| 80 | + response: Response; |
| 81 | +}>; |
| 82 | +export interface ClientOptions { |
| 83 | + baseUrl?: string; |
| 84 | + responseStyle?: ResponseStyle; |
| 85 | + throwOnError?: boolean; |
| 86 | +} |
| 87 | +type MethodFnBase = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method">) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>; |
| 88 | +type MethodFnServerSentEvents = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method">) => Promise<ServerSentEventsResult<TData, TError>>; |
| 89 | +type MethodFn = MethodFnBase & { |
| 90 | + sse: MethodFnServerSentEvents; |
| 91 | +}; |
| 92 | +type RequestFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false, TResponseStyle extends ResponseStyle = "fields">(options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, "method"> & Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, "method">) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>; |
| 93 | +type BuildUrlFn = <TData extends { |
| 94 | + body?: unknown; |
| 95 | + path?: Record<string, unknown>; |
| 96 | + query?: Record<string, unknown>; |
| 97 | + url: string; |
| 98 | +}>(options: Pick<TData, "url"> & Options<TData>) => string; |
| 99 | +export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & { |
| 100 | + interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>; |
| 101 | +}; |
| 102 | +/** |
| 103 | + * The `createClientConfig()` function will be called on client initialization |
| 104 | + * and the returned object will become the client's initial configuration. |
| 105 | + * |
| 106 | + * You may want to initialize your client this way instead of calling |
| 107 | + * `setConfig()`. This is useful for example if you're using Next.js |
| 108 | + * to ensure your client always has the correct values. |
| 109 | + */ |
| 110 | +export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (override?: Config<ClientOptions & T>) => Config<Required<ClientOptions> & T>; |
| 111 | +export interface TDataShape { |
| 112 | + body?: unknown; |
| 113 | + headers?: unknown; |
| 114 | + path?: unknown; |
| 115 | + query?: unknown; |
| 116 | + url: string; |
| 117 | +} |
| 118 | +type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>; |
| 119 | +export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean, TResponse = unknown, TResponseStyle extends ResponseStyle = "fields"> = OmitKeys<RequestOptions<TResponse, TResponseStyle, ThrowOnError>, "body" | "path" | "query" | "url"> & Omit<TData, "url">; |
| 120 | +export type OptionsLegacyParser<TData = unknown, ThrowOnError extends boolean = boolean, TResponseStyle extends ResponseStyle = "fields"> = TData extends { |
| 121 | + body?: any; |
| 122 | +} ? TData extends { |
| 123 | + headers?: any; |
| 124 | +} ? OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body" | "headers" | "url"> & TData : OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body" | "url"> & TData & Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "headers"> : TData extends { |
| 125 | + headers?: any; |
| 126 | +} ? OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "headers" | "url"> & TData & Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "body"> : OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, "url"> & TData; |
| 127 | +export {}; |
0 commit comments