fork of hey-api/openapi-ts because I need some additional things

Merge pull request #2804 from hey-api/revert/axios-client-error

Revert/axios client error

authored by

Lubos and committed by
GitHub
fb9460b4 81dee18c

+988 -932
+5
.changeset/slick-dancers-think.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix(client-axios): revert return error when axios request fails
+6 -7
examples/openapi-ts-axios/src/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 3 import { 4 - type ClientOptions as DefaultClientOptions, 4 + type ClientOptions, 5 5 type Config, 6 6 createClient, 7 7 createConfig, 8 8 } from './client'; 9 - import type { ClientOptions } from './types.gen'; 9 + import type { ClientOptions as ClientOptions2 } from './types.gen'; 10 10 11 11 /** 12 12 * The `createClientConfig()` function will be called on client initialization ··· 16 16 * `setConfig()`. This is useful for example if you're using Next.js 17 17 * to ensure your client always has the correct values. 18 18 */ 19 - export type CreateClientConfig<T extends DefaultClientOptions = ClientOptions> = 20 - ( 21 - override?: Config<DefaultClientOptions & T>, 22 - ) => Config<Required<DefaultClientOptions> & T>; 19 + export type CreateClientConfig<T extends ClientOptions = ClientOptions2> = ( 20 + override?: Config<ClientOptions & T>, 21 + ) => Config<Required<ClientOptions> & T>; 23 22 24 23 export const client = createClient( 25 - createConfig<ClientOptions>({ 24 + createConfig<ClientOptions2>({ 26 25 baseURL: 'https://petstore3.swagger.io/api/v3', 27 26 }), 28 27 );
+53 -13
examples/openapi-ts-axios/src/client/client/client.gen.ts
··· 3 3 import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 4 import axios from 'axios'; 5 5 6 - import type { Client, Config } from './types.gen'; 6 + import { createSseClient } from '../core/serverSentEvents.gen'; 7 + import type { HttpMethod } from '../core/types.gen'; 8 + import { getValidRequestBody } from '../core/utils.gen'; 9 + import type { Client, Config, RequestOptions } from './types.gen'; 7 10 import { 8 11 buildUrl, 9 12 createConfig, ··· 38 41 return getConfig(); 39 42 }; 40 43 41 - // @ts-expect-error 42 - const request: Client['request'] = async (options) => { 44 + const beforeRequest = async (options: RequestOptions) => { 43 45 const opts = { 44 46 ..._config, 45 47 ...options, ··· 58 60 await opts.requestValidator(opts); 59 61 } 60 62 61 - if (opts.body && opts.bodySerializer) { 63 + if (opts.body !== undefined && opts.bodySerializer) { 62 64 opts.body = opts.bodySerializer(opts.body); 63 65 } 64 66 65 67 const url = buildUrl(opts); 66 68 69 + return { opts, url }; 70 + }; 71 + 72 + // @ts-expect-error 73 + const request: Client['request'] = async (options) => { 74 + // @ts-expect-error 75 + const { opts, url } = await beforeRequest(options); 67 76 try { 68 77 // assign Axios here for consistency with fetch 69 78 const _axios = opts.axios!; ··· 71 80 const { auth, ...optsWithoutAuth } = opts; 72 81 const response = await _axios({ 73 82 ...optsWithoutAuth, 74 - baseURL: opts.baseURL as string, 75 - data: opts.body, 83 + baseURL: '', // the baseURL is already included in `url` 84 + data: getValidRequestBody(opts), 76 85 headers: opts.headers as RawAxiosRequestHeaders, 77 86 // let `paramsSerializer()` handle query params if it exists 78 87 params: opts.paramsSerializer ? opts.query : undefined, ··· 106 115 } 107 116 }; 108 117 118 + const makeMethodFn = 119 + (method: Uppercase<HttpMethod>) => (options: RequestOptions) => 120 + request({ ...options, method }); 121 + 122 + const makeSseFn = 123 + (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => { 124 + const { opts, url } = await beforeRequest(options); 125 + return createSseClient({ 126 + ...opts, 127 + body: opts.body as BodyInit | null | undefined, 128 + headers: opts.headers as Record<string, string>, 129 + method, 130 + // @ts-expect-error 131 + signal: opts.signal, 132 + url, 133 + }); 134 + }; 135 + 109 136 return { 110 137 buildUrl, 111 - delete: (options) => request({ ...options, method: 'DELETE' }), 112 - get: (options) => request({ ...options, method: 'GET' }), 138 + connect: makeMethodFn('CONNECT'), 139 + delete: makeMethodFn('DELETE'), 140 + get: makeMethodFn('GET'), 113 141 getConfig, 114 - head: (options) => request({ ...options, method: 'HEAD' }), 142 + head: makeMethodFn('HEAD'), 115 143 instance, 116 - options: (options) => request({ ...options, method: 'OPTIONS' }), 117 - patch: (options) => request({ ...options, method: 'PATCH' }), 118 - post: (options) => request({ ...options, method: 'POST' }), 119 - put: (options) => request({ ...options, method: 'PUT' }), 144 + options: makeMethodFn('OPTIONS'), 145 + patch: makeMethodFn('PATCH'), 146 + post: makeMethodFn('POST'), 147 + put: makeMethodFn('PUT'), 120 148 request, 121 149 setConfig, 150 + sse: { 151 + connect: makeSseFn('CONNECT'), 152 + delete: makeSseFn('DELETE'), 153 + get: makeSseFn('GET'), 154 + head: makeSseFn('HEAD'), 155 + options: makeSseFn('OPTIONS'), 156 + patch: makeSseFn('PATCH'), 157 + post: makeSseFn('POST'), 158 + put: makeSseFn('PUT'), 159 + trace: makeSseFn('TRACE'), 160 + }, 161 + trace: makeMethodFn('TRACE'), 122 162 } as Client; 123 163 };
+1
examples/openapi-ts-axios/src/client/client/index.ts
··· 8 8 urlSearchParamsBodySerializer, 9 9 } from '../core/bodySerializer.gen'; 10 10 export { buildClientParams } from '../core/params.gen'; 11 + export { serializeQueryKeyValue } from '../core/queryKeySerializer.gen'; 11 12 export { createClient } from './client.gen'; 12 13 export type { 13 14 Client,
+54 -19
examples/openapi-ts-axios/src/client/client/types.gen.ts
··· 11 11 12 12 import type { Auth } from '../core/auth.gen'; 13 13 import type { 14 + ServerSentEventsOptions, 15 + ServerSentEventsResult, 16 + } from '../core/serverSentEvents.gen'; 17 + import type { 14 18 Client as CoreClient, 15 19 Config as CoreConfig, 16 20 } from '../core/types.gen'; ··· 56 60 } 57 61 58 62 export interface RequestOptions< 63 + TData = unknown, 59 64 ThrowOnError extends boolean = boolean, 60 65 Url extends string = string, 61 66 > extends Config<{ 62 - throwOnError: ThrowOnError; 63 - }> { 67 + throwOnError: ThrowOnError; 68 + }>, 69 + Pick< 70 + ServerSentEventsOptions<TData>, 71 + | 'onSseError' 72 + | 'onSseEvent' 73 + | 'sseDefaultRetryDelay' 74 + | 'sseMaxRetryAttempts' 75 + | 'sseMaxRetryDelay' 76 + > { 64 77 /** 65 78 * Any body that you want to add to your request. 66 79 * ··· 76 89 url: Url; 77 90 } 78 91 92 + export interface ClientOptions { 93 + baseURL?: string; 94 + throwOnError?: boolean; 95 + } 96 + 79 97 export type RequestResult< 80 98 TData = unknown, 81 99 TError = unknown, ··· 100 118 }) 101 119 >; 102 120 103 - export interface ClientOptions { 104 - baseURL?: string; 105 - throwOnError?: boolean; 106 - } 107 - 108 121 type MethodFn = < 109 122 TData = unknown, 110 123 TError = unknown, 111 124 ThrowOnError extends boolean = false, 112 125 >( 113 - options: Omit<RequestOptions<ThrowOnError>, 'method'>, 126 + options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>, 114 127 ) => RequestResult<TData, TError, ThrowOnError>; 128 + 129 + type SseFn = < 130 + TData = unknown, 131 + TError = unknown, 132 + ThrowOnError extends boolean = false, 133 + >( 134 + options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>, 135 + ) => Promise<ServerSentEventsResult<TData, TError>>; 115 136 116 137 type RequestFn = < 117 138 TData = unknown, 118 139 TError = unknown, 119 140 ThrowOnError extends boolean = false, 120 141 >( 121 - options: Omit<RequestOptions<ThrowOnError>, 'method'> & 122 - Pick<Required<RequestOptions<ThrowOnError>>, 'method'>, 142 + options: Omit<RequestOptions<TData, ThrowOnError>, 'method'> & 143 + Pick<Required<RequestOptions<TData, ThrowOnError>>, 'method'>, 123 144 ) => RequestResult<TData, TError, ThrowOnError>; 124 145 125 146 type BuildUrlFn = < ··· 130 151 url: string; 131 152 }, 132 153 >( 133 - options: Pick<TData, 'url'> & Omit<Options<TData>, 'axios'>, 154 + options: Pick<TData, 'url'> & Options<TData>, 134 155 ) => string; 135 156 136 - export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & { 157 + export type Client = CoreClient< 158 + RequestFn, 159 + Config, 160 + MethodFn, 161 + BuildUrlFn, 162 + SseFn 163 + > & { 137 164 instance: AxiosInstance; 138 165 }; 139 166 ··· 162 189 export type Options< 163 190 TData extends TDataShape = TDataShape, 164 191 ThrowOnError extends boolean = boolean, 165 - > = OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'path' | 'query' | 'url'> & 192 + TResponse = unknown, 193 + > = OmitKeys< 194 + RequestOptions<TResponse, ThrowOnError>, 195 + 'body' | 'path' | 'query' | 'url' 196 + > & 166 197 Omit<TData, 'url'>; 167 198 168 199 export type OptionsLegacyParser< ··· 170 201 ThrowOnError extends boolean = boolean, 171 202 > = TData extends { body?: any } 172 203 ? TData extends { headers?: any } 173 - ? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & TData 174 - : OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> & 204 + ? OmitKeys< 205 + RequestOptions<unknown, ThrowOnError>, 206 + 'body' | 'headers' | 'url' 207 + > & 208 + TData 209 + : OmitKeys<RequestOptions<unknown, ThrowOnError>, 'body' | 'url'> & 175 210 TData & 176 - Pick<RequestOptions<ThrowOnError>, 'headers'> 211 + Pick<RequestOptions<unknown, ThrowOnError>, 'headers'> 177 212 : TData extends { headers?: any } 178 - ? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> & 213 + ? OmitKeys<RequestOptions<unknown, ThrowOnError>, 'headers' | 'url'> & 179 214 TData & 180 - Pick<RequestOptions<ThrowOnError>, 'body'> 181 - : OmitKeys<RequestOptions<ThrowOnError>, 'url'> & TData; 215 + Pick<RequestOptions<unknown, ThrowOnError>, 'body'> 216 + : OmitKeys<RequestOptions<unknown, ThrowOnError>, 'url'> & TData;
+11 -110
examples/openapi-ts-axios/src/client/client/utils.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 3 import { getAuthToken } from '../core/auth.gen'; 4 - import type { 5 - QuerySerializer, 6 - QuerySerializerOptions, 7 - } from '../core/bodySerializer.gen'; 8 - import type { ArraySeparatorStyle } from '../core/pathSerializer.gen'; 4 + import type { QuerySerializerOptions } from '../core/bodySerializer.gen'; 9 5 import { 10 6 serializeArrayParam, 11 7 serializeObjectParam, 12 8 serializePrimitiveParam, 13 9 } from '../core/pathSerializer.gen'; 10 + import { getUrl } from '../core/utils.gen'; 14 11 import type { 15 12 Client, 16 13 ClientOptions, ··· 18 15 RequestOptions, 19 16 } from './types.gen'; 20 17 21 - interface PathSerializer { 22 - path: Record<string, unknown>; 23 - url: string; 24 - } 25 - 26 - const PATH_PARAM_RE = /\{[^{}]+\}/g; 27 - 28 - const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => { 29 - let url = _url; 30 - const matches = _url.match(PATH_PARAM_RE); 31 - if (matches) { 32 - for (const match of matches) { 33 - let explode = false; 34 - let name = match.substring(1, match.length - 1); 35 - let style: ArraySeparatorStyle = 'simple'; 36 - 37 - if (name.endsWith('*')) { 38 - explode = true; 39 - name = name.substring(0, name.length - 1); 40 - } 41 - 42 - if (name.startsWith('.')) { 43 - name = name.substring(1); 44 - style = 'label'; 45 - } else if (name.startsWith(';')) { 46 - name = name.substring(1); 47 - style = 'matrix'; 48 - } 49 - 50 - const value = path[name]; 51 - 52 - if (value === undefined || value === null) { 53 - continue; 54 - } 55 - 56 - if (Array.isArray(value)) { 57 - url = url.replace( 58 - match, 59 - serializeArrayParam({ explode, name, style, value }), 60 - ); 61 - continue; 62 - } 63 - 64 - if (typeof value === 'object') { 65 - url = url.replace( 66 - match, 67 - serializeObjectParam({ 68 - explode, 69 - name, 70 - style, 71 - value: value as Record<string, unknown>, 72 - valueOnly: true, 73 - }), 74 - ); 75 - continue; 76 - } 77 - 78 - if (style === 'matrix') { 79 - url = url.replace( 80 - match, 81 - `;${serializePrimitiveParam({ 82 - name, 83 - value: value as string, 84 - })}`, 85 - ); 86 - continue; 87 - } 88 - 89 - const replaceValue = encodeURIComponent( 90 - style === 'label' ? `.${value as string}` : (value as string), 91 - ); 92 - url = url.replace(match, replaceValue); 93 - } 94 - } 95 - return url; 96 - }; 97 - 98 18 export const createQuerySerializer = <T = unknown>({ 99 19 allowReserved, 100 20 array, ··· 211 131 }; 212 132 213 133 export const buildUrl: Client['buildUrl'] = (options) => { 214 - const url = getUrl({ 134 + const instanceBaseUrl = options.axios?.defaults?.baseURL; 135 + 136 + const baseUrl = 137 + !!options.baseURL && typeof options.baseURL === 'string' 138 + ? options.baseURL 139 + : instanceBaseUrl; 140 + 141 + return getUrl({ 142 + baseUrl: baseUrl as string, 215 143 path: options.path, 216 144 // let `paramsSerializer()` handle query params if it exists 217 145 query: !options.paramsSerializer ? options.query : undefined, ··· 221 149 : createQuerySerializer(options.querySerializer), 222 150 url: options.url, 223 151 }); 224 - return url; 225 - }; 226 - 227 - export const getUrl = ({ 228 - path, 229 - query, 230 - querySerializer, 231 - url: _url, 232 - }: { 233 - path?: Record<string, unknown>; 234 - query?: Record<string, unknown>; 235 - querySerializer: QuerySerializer; 236 - url: string; 237 - }) => { 238 - const pathUrl = _url.startsWith('/') ? _url : `/${_url}`; 239 - let url = pathUrl; 240 - if (path) { 241 - url = defaultPathSerializer({ path, url }); 242 - } 243 - let search = query ? querySerializer(query) : ''; 244 - if (search.startsWith('?')) { 245 - search = search.substring(1); 246 - } 247 - if (search) { 248 - url += `?${search}`; 249 - } 250 - return url; 251 152 }; 252 153 253 154 export const mergeConfigs = (a: Config, b: Config): Config => {
+136
examples/openapi-ts-axios/src/client/core/queryKeySerializer.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + /** 4 + * JSON-friendly union that mirrors what Pinia Colada can hash. 5 + */ 6 + export type JsonValue = 7 + | null 8 + | string 9 + | number 10 + | boolean 11 + | JsonValue[] 12 + | { [key: string]: JsonValue }; 13 + 14 + /** 15 + * Replacer that converts non-JSON values (bigint, Date, etc.) to safe substitutes. 16 + */ 17 + export const queryKeyJsonReplacer = (_key: string, value: unknown) => { 18 + if ( 19 + value === undefined || 20 + typeof value === 'function' || 21 + typeof value === 'symbol' 22 + ) { 23 + return undefined; 24 + } 25 + if (typeof value === 'bigint') { 26 + return value.toString(); 27 + } 28 + if (value instanceof Date) { 29 + return value.toISOString(); 30 + } 31 + return value; 32 + }; 33 + 34 + /** 35 + * Safely stringifies a value and parses it back into a JsonValue. 36 + */ 37 + export const stringifyToJsonValue = (input: unknown): JsonValue | undefined => { 38 + try { 39 + const json = JSON.stringify(input, queryKeyJsonReplacer); 40 + if (json === undefined) { 41 + return undefined; 42 + } 43 + return JSON.parse(json) as JsonValue; 44 + } catch { 45 + return undefined; 46 + } 47 + }; 48 + 49 + /** 50 + * Detects plain objects (including objects with a null prototype). 51 + */ 52 + const isPlainObject = (value: unknown): value is Record<string, unknown> => { 53 + if (value === null || typeof value !== 'object') { 54 + return false; 55 + } 56 + const prototype = Object.getPrototypeOf(value as object); 57 + return prototype === Object.prototype || prototype === null; 58 + }; 59 + 60 + /** 61 + * Turns URLSearchParams into a sorted JSON object for deterministic keys. 62 + */ 63 + const serializeSearchParams = (params: URLSearchParams): JsonValue => { 64 + const entries = Array.from(params.entries()).sort(([a], [b]) => 65 + a.localeCompare(b), 66 + ); 67 + const result: Record<string, JsonValue> = {}; 68 + 69 + for (const [key, value] of entries) { 70 + const existing = result[key]; 71 + if (existing === undefined) { 72 + result[key] = value; 73 + continue; 74 + } 75 + 76 + if (Array.isArray(existing)) { 77 + (existing as string[]).push(value); 78 + } else { 79 + result[key] = [existing, value]; 80 + } 81 + } 82 + 83 + return result; 84 + }; 85 + 86 + /** 87 + * Normalizes any accepted value into a JSON-friendly shape for query keys. 88 + */ 89 + export const serializeQueryKeyValue = ( 90 + value: unknown, 91 + ): JsonValue | undefined => { 92 + if (value === null) { 93 + return null; 94 + } 95 + 96 + if ( 97 + typeof value === 'string' || 98 + typeof value === 'number' || 99 + typeof value === 'boolean' 100 + ) { 101 + return value; 102 + } 103 + 104 + if ( 105 + value === undefined || 106 + typeof value === 'function' || 107 + typeof value === 'symbol' 108 + ) { 109 + return undefined; 110 + } 111 + 112 + if (typeof value === 'bigint') { 113 + return value.toString(); 114 + } 115 + 116 + if (value instanceof Date) { 117 + return value.toISOString(); 118 + } 119 + 120 + if (Array.isArray(value)) { 121 + return stringifyToJsonValue(value); 122 + } 123 + 124 + if ( 125 + typeof URLSearchParams !== 'undefined' && 126 + value instanceof URLSearchParams 127 + ) { 128 + return serializeSearchParams(value); 129 + } 130 + 131 + if (isPlainObject(value)) { 132 + return stringifyToJsonValue(value); 133 + } 134 + 135 + return undefined; 136 + };
+264
examples/openapi-ts-axios/src/client/core/serverSentEvents.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + import type { Config } from './types.gen'; 4 + 5 + export type ServerSentEventsOptions<TData = unknown> = Omit< 6 + RequestInit, 7 + 'method' 8 + > & 9 + Pick<Config, 'method' | 'responseTransformer' | 'responseValidator'> & { 10 + /** 11 + * Fetch API implementation. You can use this option to provide a custom 12 + * fetch instance. 13 + * 14 + * @default globalThis.fetch 15 + */ 16 + fetch?: typeof fetch; 17 + /** 18 + * Implementing clients can call request interceptors inside this hook. 19 + */ 20 + onRequest?: (url: string, init: RequestInit) => Promise<Request>; 21 + /** 22 + * Callback invoked when a network or parsing error occurs during streaming. 23 + * 24 + * This option applies only if the endpoint returns a stream of events. 25 + * 26 + * @param error The error that occurred. 27 + */ 28 + onSseError?: (error: unknown) => void; 29 + /** 30 + * Callback invoked when an event is streamed from the server. 31 + * 32 + * This option applies only if the endpoint returns a stream of events. 33 + * 34 + * @param event Event streamed from the server. 35 + * @returns Nothing (void). 36 + */ 37 + onSseEvent?: (event: StreamEvent<TData>) => void; 38 + serializedBody?: RequestInit['body']; 39 + /** 40 + * Default retry delay in milliseconds. 41 + * 42 + * This option applies only if the endpoint returns a stream of events. 43 + * 44 + * @default 3000 45 + */ 46 + sseDefaultRetryDelay?: number; 47 + /** 48 + * Maximum number of retry attempts before giving up. 49 + */ 50 + sseMaxRetryAttempts?: number; 51 + /** 52 + * Maximum retry delay in milliseconds. 53 + * 54 + * Applies only when exponential backoff is used. 55 + * 56 + * This option applies only if the endpoint returns a stream of events. 57 + * 58 + * @default 30000 59 + */ 60 + sseMaxRetryDelay?: number; 61 + /** 62 + * Optional sleep function for retry backoff. 63 + * 64 + * Defaults to using `setTimeout`. 65 + */ 66 + sseSleepFn?: (ms: number) => Promise<void>; 67 + url: string; 68 + }; 69 + 70 + export interface StreamEvent<TData = unknown> { 71 + data: TData; 72 + event?: string; 73 + id?: string; 74 + retry?: number; 75 + } 76 + 77 + export type ServerSentEventsResult< 78 + TData = unknown, 79 + TReturn = void, 80 + TNext = unknown, 81 + > = { 82 + stream: AsyncGenerator< 83 + TData extends Record<string, unknown> ? TData[keyof TData] : TData, 84 + TReturn, 85 + TNext 86 + >; 87 + }; 88 + 89 + export const createSseClient = <TData = unknown>({ 90 + onRequest, 91 + onSseError, 92 + onSseEvent, 93 + responseTransformer, 94 + responseValidator, 95 + sseDefaultRetryDelay, 96 + sseMaxRetryAttempts, 97 + sseMaxRetryDelay, 98 + sseSleepFn, 99 + url, 100 + ...options 101 + }: ServerSentEventsOptions): ServerSentEventsResult<TData> => { 102 + let lastEventId: string | undefined; 103 + 104 + const sleep = 105 + sseSleepFn ?? 106 + ((ms: number) => new Promise((resolve) => setTimeout(resolve, ms))); 107 + 108 + const createStream = async function* () { 109 + let retryDelay: number = sseDefaultRetryDelay ?? 3000; 110 + let attempt = 0; 111 + const signal = options.signal ?? new AbortController().signal; 112 + 113 + while (true) { 114 + if (signal.aborted) break; 115 + 116 + attempt++; 117 + 118 + const headers = 119 + options.headers instanceof Headers 120 + ? options.headers 121 + : new Headers(options.headers as Record<string, string> | undefined); 122 + 123 + if (lastEventId !== undefined) { 124 + headers.set('Last-Event-ID', lastEventId); 125 + } 126 + 127 + try { 128 + const requestInit: RequestInit = { 129 + redirect: 'follow', 130 + ...options, 131 + body: options.serializedBody, 132 + headers, 133 + signal, 134 + }; 135 + let request = new Request(url, requestInit); 136 + if (onRequest) { 137 + request = await onRequest(url, requestInit); 138 + } 139 + // fetch must be assigned here, otherwise it would throw the error: 140 + // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation 141 + const _fetch = options.fetch ?? globalThis.fetch; 142 + const response = await _fetch(request); 143 + 144 + if (!response.ok) 145 + throw new Error( 146 + `SSE failed: ${response.status} ${response.statusText}`, 147 + ); 148 + 149 + if (!response.body) throw new Error('No body in SSE response'); 150 + 151 + const reader = response.body 152 + .pipeThrough(new TextDecoderStream()) 153 + .getReader(); 154 + 155 + let buffer = ''; 156 + 157 + const abortHandler = () => { 158 + try { 159 + reader.cancel(); 160 + } catch { 161 + // noop 162 + } 163 + }; 164 + 165 + signal.addEventListener('abort', abortHandler); 166 + 167 + try { 168 + while (true) { 169 + const { done, value } = await reader.read(); 170 + if (done) break; 171 + buffer += value; 172 + 173 + const chunks = buffer.split('\n\n'); 174 + buffer = chunks.pop() ?? ''; 175 + 176 + for (const chunk of chunks) { 177 + const lines = chunk.split('\n'); 178 + const dataLines: Array<string> = []; 179 + let eventName: string | undefined; 180 + 181 + for (const line of lines) { 182 + if (line.startsWith('data:')) { 183 + dataLines.push(line.replace(/^data:\s*/, '')); 184 + } else if (line.startsWith('event:')) { 185 + eventName = line.replace(/^event:\s*/, ''); 186 + } else if (line.startsWith('id:')) { 187 + lastEventId = line.replace(/^id:\s*/, ''); 188 + } else if (line.startsWith('retry:')) { 189 + const parsed = Number.parseInt( 190 + line.replace(/^retry:\s*/, ''), 191 + 10, 192 + ); 193 + if (!Number.isNaN(parsed)) { 194 + retryDelay = parsed; 195 + } 196 + } 197 + } 198 + 199 + let data: unknown; 200 + let parsedJson = false; 201 + 202 + if (dataLines.length) { 203 + const rawData = dataLines.join('\n'); 204 + try { 205 + data = JSON.parse(rawData); 206 + parsedJson = true; 207 + } catch { 208 + data = rawData; 209 + } 210 + } 211 + 212 + if (parsedJson) { 213 + if (responseValidator) { 214 + await responseValidator(data); 215 + } 216 + 217 + if (responseTransformer) { 218 + data = await responseTransformer(data); 219 + } 220 + } 221 + 222 + onSseEvent?.({ 223 + data, 224 + event: eventName, 225 + id: lastEventId, 226 + retry: retryDelay, 227 + }); 228 + 229 + if (dataLines.length) { 230 + yield data as any; 231 + } 232 + } 233 + } 234 + } finally { 235 + signal.removeEventListener('abort', abortHandler); 236 + reader.releaseLock(); 237 + } 238 + 239 + break; // exit loop on normal completion 240 + } catch (error) { 241 + // connection failed or aborted; retry after delay 242 + onSseError?.(error); 243 + 244 + if ( 245 + sseMaxRetryAttempts !== undefined && 246 + attempt >= sseMaxRetryAttempts 247 + ) { 248 + break; // stop after firing error 249 + } 250 + 251 + // exponential backoff: double retry each attempt, cap at 30s 252 + const backoff = Math.min( 253 + retryDelay * 2 ** (attempt - 1), 254 + sseMaxRetryDelay ?? 30000, 255 + ); 256 + await sleep(backoff); 257 + } 258 + } 259 + }; 260 + 261 + const stream = createStream(); 262 + 263 + return { stream }; 264 + };
+20 -22
examples/openapi-ts-axios/src/client/core/types.gen.ts
··· 7 7 QuerySerializerOptions, 8 8 } from './bodySerializer.gen'; 9 9 10 - export interface Client< 10 + export type HttpMethod = 11 + | 'connect' 12 + | 'delete' 13 + | 'get' 14 + | 'head' 15 + | 'options' 16 + | 'patch' 17 + | 'post' 18 + | 'put' 19 + | 'trace'; 20 + 21 + export type Client< 11 22 RequestFn = never, 12 23 Config = unknown, 13 24 MethodFn = never, 14 25 BuildUrlFn = never, 15 - > { 26 + SseFn = never, 27 + > = { 16 28 /** 17 29 * Returns the final request URL. 18 30 */ 19 31 buildUrl: BuildUrlFn; 20 - connect: MethodFn; 21 - delete: MethodFn; 22 - get: MethodFn; 23 32 getConfig: () => Config; 24 - head: MethodFn; 25 - options: MethodFn; 26 - patch: MethodFn; 27 - post: MethodFn; 28 - put: MethodFn; 29 33 request: RequestFn; 30 34 setConfig: (config: Config) => Config; 31 - trace: MethodFn; 32 - } 35 + } & { 36 + [K in HttpMethod]: MethodFn; 37 + } & ([SseFn] extends [never] 38 + ? { sse?: never } 39 + : { sse: { [K in HttpMethod]: SseFn } }); 33 40 34 41 export interface Config { 35 42 /** ··· 65 72 * 66 73 * {@link https://developer.mozilla.org/docs/Web/API/fetch#method See more} 67 74 */ 68 - method?: 69 - | 'CONNECT' 70 - | 'DELETE' 71 - | 'GET' 72 - | 'HEAD' 73 - | 'OPTIONS' 74 - | 'PATCH' 75 - | 'POST' 76 - | 'PUT' 77 - | 'TRACE'; 75 + method?: Uppercase<HttpMethod>; 78 76 /** 79 77 * A function for serializing request query parameters. By default, arrays 80 78 * will be exploded in form style, objects will be exploded in deepObject
+143
examples/openapi-ts-axios/src/client/core/utils.gen.ts
··· 1 + // This file is auto-generated by @hey-api/openapi-ts 2 + 3 + import type { BodySerializer, QuerySerializer } from './bodySerializer.gen'; 4 + import { 5 + type ArraySeparatorStyle, 6 + serializeArrayParam, 7 + serializeObjectParam, 8 + serializePrimitiveParam, 9 + } from './pathSerializer.gen'; 10 + 11 + export interface PathSerializer { 12 + path: Record<string, unknown>; 13 + url: string; 14 + } 15 + 16 + export const PATH_PARAM_RE = /\{[^{}]+\}/g; 17 + 18 + export const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => { 19 + let url = _url; 20 + const matches = _url.match(PATH_PARAM_RE); 21 + if (matches) { 22 + for (const match of matches) { 23 + let explode = false; 24 + let name = match.substring(1, match.length - 1); 25 + let style: ArraySeparatorStyle = 'simple'; 26 + 27 + if (name.endsWith('*')) { 28 + explode = true; 29 + name = name.substring(0, name.length - 1); 30 + } 31 + 32 + if (name.startsWith('.')) { 33 + name = name.substring(1); 34 + style = 'label'; 35 + } else if (name.startsWith(';')) { 36 + name = name.substring(1); 37 + style = 'matrix'; 38 + } 39 + 40 + const value = path[name]; 41 + 42 + if (value === undefined || value === null) { 43 + continue; 44 + } 45 + 46 + if (Array.isArray(value)) { 47 + url = url.replace( 48 + match, 49 + serializeArrayParam({ explode, name, style, value }), 50 + ); 51 + continue; 52 + } 53 + 54 + if (typeof value === 'object') { 55 + url = url.replace( 56 + match, 57 + serializeObjectParam({ 58 + explode, 59 + name, 60 + style, 61 + value: value as Record<string, unknown>, 62 + valueOnly: true, 63 + }), 64 + ); 65 + continue; 66 + } 67 + 68 + if (style === 'matrix') { 69 + url = url.replace( 70 + match, 71 + `;${serializePrimitiveParam({ 72 + name, 73 + value: value as string, 74 + })}`, 75 + ); 76 + continue; 77 + } 78 + 79 + const replaceValue = encodeURIComponent( 80 + style === 'label' ? `.${value as string}` : (value as string), 81 + ); 82 + url = url.replace(match, replaceValue); 83 + } 84 + } 85 + return url; 86 + }; 87 + 88 + export const getUrl = ({ 89 + baseUrl, 90 + path, 91 + query, 92 + querySerializer, 93 + url: _url, 94 + }: { 95 + baseUrl?: string; 96 + path?: Record<string, unknown>; 97 + query?: Record<string, unknown>; 98 + querySerializer: QuerySerializer; 99 + url: string; 100 + }) => { 101 + const pathUrl = _url.startsWith('/') ? _url : `/${_url}`; 102 + let url = (baseUrl ?? '') + pathUrl; 103 + if (path) { 104 + url = defaultPathSerializer({ path, url }); 105 + } 106 + let search = query ? querySerializer(query) : ''; 107 + if (search.startsWith('?')) { 108 + search = search.substring(1); 109 + } 110 + if (search) { 111 + url += `?${search}`; 112 + } 113 + return url; 114 + }; 115 + 116 + export function getValidRequestBody(options: { 117 + body?: unknown; 118 + bodySerializer?: BodySerializer | null; 119 + serializedBody?: unknown; 120 + }) { 121 + const hasBody = options.body !== undefined; 122 + const isSerializedBody = hasBody && options.bodySerializer; 123 + 124 + if (isSerializedBody) { 125 + if ('serializedBody' in options) { 126 + const hasSerializedBody = 127 + options.serializedBody !== undefined && options.serializedBody !== ''; 128 + 129 + return hasSerializedBody ? options.serializedBody : null; 130 + } 131 + 132 + // not all clients implement a serializedBody property (i.e. client-axios) 133 + return options.body !== '' ? options.body : null; 134 + } 135 + 136 + // plain/text body 137 + if (hasBody) { 138 + return options.body; 139 + } 140 + 141 + // no body was provided 142 + return undefined; 143 + }
+2 -1
examples/openapi-ts-axios/src/client/index.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 + 2 3 export * from './sdk.gen'; 3 - export * from './types.gen'; 4 + export type * from './types.gen';
+41 -26
examples/openapi-ts-axios/src/client/sdk.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { Client, Options as ClientOptions, TDataShape } from './client'; 4 - import { client as _heyApiClient } from './client.gen'; 3 + import type { Client, Options as Options2, TDataShape } from './client'; 4 + import { client } from './client.gen'; 5 5 import type { 6 6 AddPetData, 7 7 AddPetErrors, ··· 65 65 export type Options< 66 66 TData extends TDataShape = TDataShape, 67 67 ThrowOnError extends boolean = boolean, 68 - > = ClientOptions<TData, ThrowOnError> & { 68 + > = Options2<TData, ThrowOnError> & { 69 69 /** 70 70 * You can provide a client instance returned by `createClient()` instead of 71 71 * individual options. This might be also useful if you want to implement a ··· 81 81 82 82 /** 83 83 * Add a new pet to the store. 84 + * 84 85 * Add a new pet to the store. 85 86 */ 86 87 export const addPet = <ThrowOnError extends boolean = false>( 87 88 options: Options<AddPetData, ThrowOnError>, 88 89 ) => 89 - (options.client ?? _heyApiClient).post< 90 - AddPetResponses, 91 - AddPetErrors, 92 - ThrowOnError 93 - >({ 90 + (options.client ?? client).post<AddPetResponses, AddPetErrors, ThrowOnError>({ 94 91 responseType: 'json', 95 92 security: [ 96 93 { ··· 108 105 109 106 /** 110 107 * Update an existing pet. 108 + * 111 109 * Update an existing pet by Id. 112 110 */ 113 111 export const updatePet = <ThrowOnError extends boolean = false>( 114 112 options: Options<UpdatePetData, ThrowOnError>, 115 113 ) => 116 - (options.client ?? _heyApiClient).put< 114 + (options.client ?? client).put< 117 115 UpdatePetResponses, 118 116 UpdatePetErrors, 119 117 ThrowOnError ··· 135 133 136 134 /** 137 135 * Finds Pets by status. 136 + * 138 137 * Multiple status values can be provided with comma separated strings. 139 138 */ 140 139 export const findPetsByStatus = <ThrowOnError extends boolean = false>( 141 140 options: Options<FindPetsByStatusData, ThrowOnError>, 142 141 ) => 143 - (options.client ?? _heyApiClient).get< 142 + (options.client ?? client).get< 144 143 FindPetsByStatusResponses, 145 144 FindPetsByStatusErrors, 146 145 ThrowOnError ··· 158 157 159 158 /** 160 159 * Finds Pets by tags. 160 + * 161 161 * Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing. 162 162 */ 163 163 export const findPetsByTags = <ThrowOnError extends boolean = false>( 164 164 options: Options<FindPetsByTagsData, ThrowOnError>, 165 165 ) => 166 - (options.client ?? _heyApiClient).get< 166 + (options.client ?? client).get< 167 167 FindPetsByTagsResponses, 168 168 FindPetsByTagsErrors, 169 169 ThrowOnError ··· 181 181 182 182 /** 183 183 * Deletes a pet. 184 + * 184 185 * Delete a pet. 185 186 */ 186 187 export const deletePet = <ThrowOnError extends boolean = false>( 187 188 options: Options<DeletePetData, ThrowOnError>, 188 189 ) => 189 - (options.client ?? _heyApiClient).delete< 190 + (options.client ?? client).delete< 190 191 DeletePetResponses, 191 192 DeletePetErrors, 192 193 ThrowOnError ··· 203 204 204 205 /** 205 206 * Find pet by ID. 207 + * 206 208 * Returns a single pet. 207 209 */ 208 210 export const getPetById = <ThrowOnError extends boolean = false>( 209 211 options: Options<GetPetByIdData, ThrowOnError>, 210 212 ) => 211 - (options.client ?? _heyApiClient).get< 213 + (options.client ?? client).get< 212 214 GetPetByIdResponses, 213 215 GetPetByIdErrors, 214 216 ThrowOnError ··· 230 232 231 233 /** 232 234 * Updates a pet in the store with form data. 235 + * 233 236 * Updates a pet resource based on the form data. 234 237 */ 235 238 export const updatePetWithForm = <ThrowOnError extends boolean = false>( 236 239 options: Options<UpdatePetWithFormData, ThrowOnError>, 237 240 ) => 238 - (options.client ?? _heyApiClient).post< 241 + (options.client ?? client).post< 239 242 UpdatePetWithFormResponses, 240 243 UpdatePetWithFormErrors, 241 244 ThrowOnError ··· 253 256 254 257 /** 255 258 * Uploads an image. 259 + * 256 260 * Upload image of the pet. 257 261 */ 258 262 export const uploadFile = <ThrowOnError extends boolean = false>( 259 263 options: Options<UploadFileData, ThrowOnError>, 260 264 ) => 261 - (options.client ?? _heyApiClient).post< 265 + (options.client ?? client).post< 262 266 UploadFileResponses, 263 267 UploadFileErrors, 264 268 ThrowOnError ··· 281 285 282 286 /** 283 287 * Returns pet inventories by status. 288 + * 284 289 * Returns a map of status codes to quantities. 285 290 */ 286 291 export const getInventory = <ThrowOnError extends boolean = false>( 287 292 options?: Options<GetInventoryData, ThrowOnError>, 288 293 ) => 289 - (options?.client ?? _heyApiClient).get< 294 + (options?.client ?? client).get< 290 295 GetInventoryResponses, 291 296 GetInventoryErrors, 292 297 ThrowOnError ··· 304 309 305 310 /** 306 311 * Place an order for a pet. 312 + * 307 313 * Place a new order in the store. 308 314 */ 309 315 export const placeOrder = <ThrowOnError extends boolean = false>( 310 316 options?: Options<PlaceOrderData, ThrowOnError>, 311 317 ) => 312 - (options?.client ?? _heyApiClient).post< 318 + (options?.client ?? client).post< 313 319 PlaceOrderResponses, 314 320 PlaceOrderErrors, 315 321 ThrowOnError ··· 325 331 326 332 /** 327 333 * Delete purchase order by identifier. 334 + * 328 335 * For valid response try integer IDs with value < 1000. Anything above 1000 or non-integers will generate API errors. 329 336 */ 330 337 export const deleteOrder = <ThrowOnError extends boolean = false>( 331 338 options: Options<DeleteOrderData, ThrowOnError>, 332 339 ) => 333 - (options.client ?? _heyApiClient).delete< 340 + (options.client ?? client).delete< 334 341 DeleteOrderResponses, 335 342 DeleteOrderErrors, 336 343 ThrowOnError ··· 341 348 342 349 /** 343 350 * Find purchase order by ID. 351 + * 344 352 * For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions. 345 353 */ 346 354 export const getOrderById = <ThrowOnError extends boolean = false>( 347 355 options: Options<GetOrderByIdData, ThrowOnError>, 348 356 ) => 349 - (options.client ?? _heyApiClient).get< 357 + (options.client ?? client).get< 350 358 GetOrderByIdResponses, 351 359 GetOrderByIdErrors, 352 360 ThrowOnError ··· 358 366 359 367 /** 360 368 * Create user. 369 + * 361 370 * This can only be done by the logged in user. 362 371 */ 363 372 export const createUser = <ThrowOnError extends boolean = false>( 364 373 options?: Options<CreateUserData, ThrowOnError>, 365 374 ) => 366 - (options?.client ?? _heyApiClient).post< 375 + (options?.client ?? client).post< 367 376 CreateUserResponses, 368 377 CreateUserErrors, 369 378 ThrowOnError ··· 379 388 380 389 /** 381 390 * Creates list of users with given input array. 391 + * 382 392 * Creates list of users with given input array. 383 393 */ 384 394 export const createUsersWithListInput = <ThrowOnError extends boolean = false>( 385 395 options?: Options<CreateUsersWithListInputData, ThrowOnError>, 386 396 ) => 387 - (options?.client ?? _heyApiClient).post< 397 + (options?.client ?? client).post< 388 398 CreateUsersWithListInputResponses, 389 399 CreateUsersWithListInputErrors, 390 400 ThrowOnError ··· 400 410 401 411 /** 402 412 * Logs user into the system. 413 + * 403 414 * Log into the system. 404 415 */ 405 416 export const loginUser = <ThrowOnError extends boolean = false>( 406 417 options?: Options<LoginUserData, ThrowOnError>, 407 418 ) => 408 - (options?.client ?? _heyApiClient).get< 419 + (options?.client ?? client).get< 409 420 LoginUserResponses, 410 421 LoginUserErrors, 411 422 ThrowOnError ··· 417 428 418 429 /** 419 430 * Logs out current logged in user session. 431 + * 420 432 * Log user out of the system. 421 433 */ 422 434 export const logoutUser = <ThrowOnError extends boolean = false>( 423 435 options?: Options<LogoutUserData, ThrowOnError>, 424 436 ) => 425 - (options?.client ?? _heyApiClient).get< 437 + (options?.client ?? client).get< 426 438 LogoutUserResponses, 427 439 LogoutUserErrors, 428 440 ThrowOnError ··· 433 445 434 446 /** 435 447 * Delete user resource. 448 + * 436 449 * This can only be done by the logged in user. 437 450 */ 438 451 export const deleteUser = <ThrowOnError extends boolean = false>( 439 452 options: Options<DeleteUserData, ThrowOnError>, 440 453 ) => 441 - (options.client ?? _heyApiClient).delete< 454 + (options.client ?? client).delete< 442 455 DeleteUserResponses, 443 456 DeleteUserErrors, 444 457 ThrowOnError ··· 449 462 450 463 /** 451 464 * Get user by user name. 465 + * 452 466 * Get user detail based on username. 453 467 */ 454 468 export const getUserByName = <ThrowOnError extends boolean = false>( 455 469 options: Options<GetUserByNameData, ThrowOnError>, 456 470 ) => 457 - (options.client ?? _heyApiClient).get< 471 + (options.client ?? client).get< 458 472 GetUserByNameResponses, 459 473 GetUserByNameErrors, 460 474 ThrowOnError ··· 466 480 467 481 /** 468 482 * Update user resource. 483 + * 469 484 * This can only be done by the logged in user. 470 485 */ 471 486 export const updateUser = <ThrowOnError extends boolean = false>( 472 487 options: Options<UpdateUserData, ThrowOnError>, 473 488 ) => 474 - (options.client ?? _heyApiClient).put< 489 + (options.client ?? client).put< 475 490 UpdateUserResponses, 476 491 UpdateUserErrors, 477 492 ThrowOnError
+4 -4
examples/openapi-ts-axios/src/client/types.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 + export type ClientOptions = { 4 + baseURL: 'https://petstore3.swagger.io/api/v3' | (string & {}); 5 + }; 6 + 3 7 export type Order = { 4 8 complete?: boolean; 5 9 id?: number; ··· 693 697 */ 694 698 200: unknown; 695 699 }; 696 - 697 - export type ClientOptions = { 698 - baseURL: 'https://petstore3.swagger.io/api/v3' | (string & {}); 699 - };
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@tanstack/angular-query-experimental/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@tanstack/react-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@tanstack/solid-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@tanstack/svelte-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/2.0.x/plugins/@tanstack/vue-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/content-types/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/parameter-explode-false-axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/plugins/@tanstack/angular-query-experimental/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/plugins/@tanstack/react-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/plugins/@tanstack/solid-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/plugins/@tanstack/svelte-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.0.x/plugins/@tanstack/vue-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/base-url-false/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/base-url-number/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/base-url-strict/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/base-url-string/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/clean-false/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/default/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/import-file-extension-ts/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen.ts'; 7 7 import type { HttpMethod } from '../core/types.gen.ts'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/sdk-client-optional/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/sdk-client-required/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-axios/tsconfig-nodenext-sdk/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen.js'; 7 7 import type { HttpMethod } from '../core/types.gen.js'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/content-types/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/parameter-explode-false-axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/plugins/@tanstack/angular-query-experimental/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/plugins/@tanstack/react-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/plugins/@tanstack/solid-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/plugins/@tanstack/svelte-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/plugins/@tanstack/vue-query/axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
+8 -17
packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/sse-axios/client/client.gen.ts
··· 1 1 // This file is auto-generated by @hey-api/openapi-ts 2 2 3 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 - import axios, { AxiosError } from 'axios'; 3 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 4 + import axios from 'axios'; 5 5 6 6 import { createSseClient } from '../core/serverSentEvents.gen'; 7 7 import type { HttpMethod } from '../core/types.gen'; ··· 69 69 return { opts, url }; 70 70 }; 71 71 72 + // @ts-expect-error 72 73 const request: Client['request'] = async (options) => { 73 74 // @ts-expect-error 74 75 const { opts, url } = await beforeRequest(options); ··· 86 87 params: opts.paramsSerializer ? opts.query : undefined, 87 88 url, 88 89 }); 89 - if (response instanceof Error) throw response; 90 90 91 91 let { data } = response; 92 92 ··· 105 105 data: data ?? {}, 106 106 }; 107 107 } catch (error) { 108 + const e = error as AxiosError; 108 109 if (opts.throwOnError) { 109 - throw error; 110 + throw e; 110 111 } 111 - 112 - if (error instanceof AxiosError) { 113 - // @ts-expect-error 114 - error.error = error.response?.data ?? {}; 115 - return error; 116 - } 117 - 118 - if (typeof error === 'object' && error !== null) { 119 - error.error = {}; 120 - return error; 121 - } 122 - 123 - return { error: {} }; 112 + // @ts-expect-error 113 + e.error = e.response?.data ?? {}; 114 + return e; 124 115 } 125 116 }; 126 117
-203
packages/openapi-ts/src/plugins/@hey-api/client-axios/__tests__/client.test.ts
··· 352 352 expect(config).toBe('https://api.example.com/users'); 353 353 }); 354 354 }); 355 - 356 - describe('error handling', () => { 357 - it('should handle 404 response when throwOnError is false', async () => { 358 - const client = createClient({ baseURL: 'https://api.example.com' }); 359 - 360 - const errorData = { error: 'Resource not found' }; 361 - const axiosError = new axios.AxiosError( 362 - 'Request failed with status code 404', 363 - 'ERR_BAD_REQUEST', 364 - {} as any, 365 - {}, 366 - { 367 - config: {} as any, 368 - data: errorData, 369 - headers: {}, 370 - status: 404, 371 - statusText: 'Not Found', 372 - }, 373 - ); 374 - 375 - const mockAxios = vi.fn().mockResolvedValue(axiosError); 376 - 377 - const result = await client.get({ 378 - axios: mockAxios as Partial<AxiosInstance> as AxiosInstance, 379 - headers: {}, 380 - throwOnError: false, 381 - url: '/users/999', 382 - }); 383 - 384 - expect(mockAxios).toHaveBeenCalledWith( 385 - expect.objectContaining({ 386 - url: 'https://api.example.com/users/999', 387 - }), 388 - ); 389 - expect(result).toBeInstanceOf(axios.AxiosError); 390 - expect(result).toMatchObject({ 391 - error: errorData, 392 - response: { 393 - data: errorData, 394 - status: 404, 395 - statusText: 'Not Found', 396 - }, 397 - }); 398 - }); 399 - 400 - it('should throw 404 error when throwOnError is true', async () => { 401 - const client = createClient({ baseURL: 'https://api.example.com' }); 402 - 403 - const errorData = { error: 'Resource not found' }; 404 - const axiosError = new axios.AxiosError( 405 - 'Request failed with status code 404', 406 - 'ERR_BAD_REQUEST', 407 - {} as any, 408 - {}, 409 - { 410 - config: {} as any, 411 - data: errorData, 412 - headers: {}, 413 - status: 404, 414 - statusText: 'Not Found', 415 - }, 416 - ); 417 - 418 - const mockAxios = vi.fn().mockResolvedValue(axiosError); 419 - 420 - await expect( 421 - client.get({ 422 - axios: mockAxios as Partial<AxiosInstance> as AxiosInstance, 423 - headers: {}, 424 - throwOnError: true, 425 - url: '/users/999', 426 - }), 427 - ).rejects.toMatchObject({ 428 - response: { 429 - data: errorData, 430 - status: 404, 431 - statusText: 'Not Found', 432 - }, 433 - }); 434 - }); 435 - 436 - it('should handle ECONNREFUSED error when throwOnError is false', async () => { 437 - const client = createClient({ baseURL: 'https://api.example.com' }); 438 - 439 - const connectionError = axios.AxiosError.from( 440 - new AggregateError( 441 - [ 442 - new Error('connect ECONNREFUSED ::1:3000'), 443 - new Error('connect ECONNREFUSED 127.0.0.1:3000'), 444 - ], 445 - 'Error', 446 - ), 447 - 'ECONNREFUSED', 448 - {} as any, 449 - {}, 450 - undefined, // no response object for connection errors 451 - ); 452 - 453 - const mockAxios = vi.fn().mockResolvedValue(connectionError); 454 - 455 - const result = await client.get({ 456 - axios: mockAxios as Partial<AxiosInstance> as AxiosInstance, 457 - headers: {}, 458 - throwOnError: false, 459 - url: '/users', 460 - }); 461 - 462 - expect(mockAxios).toHaveBeenCalledWith( 463 - expect.objectContaining({ 464 - url: 'https://api.example.com/users', 465 - }), 466 - ); 467 - expect(result).toMatchObject({ 468 - code: 'ECONNREFUSED', 469 - error: {}, // error.response?.data ?? {} 470 - message: 'Error', 471 - }); 472 - }); 473 - 474 - it('should throw ECONNREFUSED error when throwOnError is true', async () => { 475 - const client = createClient({ baseURL: 'https://api.example.com' }); 476 - 477 - const connectionError = axios.AxiosError.from( 478 - new AggregateError( 479 - [ 480 - new Error('connect ECONNREFUSED ::1:3000'), 481 - new Error('connect ECONNREFUSED 127.0.0.1:3000'), 482 - ], 483 - 'Error', 484 - ), 485 - 'ECONNREFUSED', 486 - {} as any, 487 - {}, 488 - undefined, 489 - ); 490 - 491 - const mockAxios = vi.fn().mockResolvedValue(connectionError); 492 - 493 - await expect( 494 - client.get({ 495 - axios: mockAxios as Partial<AxiosInstance> as AxiosInstance, 496 - headers: {}, 497 - throwOnError: true, 498 - url: '/users', 499 - }), 500 - ).rejects.toMatchObject({ 501 - code: 'ECONNREFUSED', 502 - message: 'Error', 503 - name: 'AggregateError', 504 - }); 505 - }); 506 - 507 - it('should handle non-object error from responseValidator when throwOnError is false', async () => { 508 - const client = createClient({ baseURL: 'https://api.example.com' }); 509 - 510 - const mockAxios = vi.fn().mockResolvedValue({ 511 - data: { id: 1, name: 'test' }, 512 - headers: {}, 513 - status: 200, 514 - statusText: 'OK', 515 - }); 516 - 517 - const result = await client.get({ 518 - axios: mockAxios as Partial<AxiosInstance> as AxiosInstance, 519 - headers: {}, 520 - responseType: 'json', 521 - responseValidator: vi.fn().mockRejectedValue('validation failed'), 522 - throwOnError: false, 523 - url: '/users/1', 524 - }); 525 - 526 - expect(mockAxios).toHaveBeenCalledWith( 527 - expect.objectContaining({ 528 - url: 'https://api.example.com/users/1', 529 - }), 530 - ); 531 - expect(result).toEqual({ 532 - error: {}, 533 - }); 534 - }); 535 - 536 - it('should throw non-object error from responseValidator when throwOnError is true', async () => { 537 - const client = createClient({ baseURL: 'https://api.example.com' }); 538 - 539 - const mockAxios = vi.fn().mockResolvedValue({ 540 - data: { id: 1, name: 'test' }, 541 - headers: {}, 542 - status: 200, 543 - statusText: 'OK', 544 - }); 545 - 546 - await expect( 547 - client.get({ 548 - axios: mockAxios as Partial<AxiosInstance> as AxiosInstance, 549 - headers: {}, 550 - responseType: 'json', 551 - responseValidator: vi.fn().mockRejectedValue('non-object error'), 552 - throwOnError: true, 553 - url: '/users/1', 554 - }), 555 - ).rejects.toBe('non-object error'); 556 - }); 557 - });
+8 -17
packages/openapi-ts/src/plugins/@hey-api/client-axios/bundle/client.ts
··· 1 - import type { AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 2 - import axios, { AxiosError } from 'axios'; 1 + import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios'; 2 + import axios from 'axios'; 3 3 4 4 import { createSseClient } from '../../client-core/bundle/serverSentEvents'; 5 5 import type { HttpMethod } from '../../client-core/bundle/types'; ··· 67 67 return { opts, url }; 68 68 }; 69 69 70 + // @ts-expect-error 70 71 const request: Client['request'] = async (options) => { 71 72 // @ts-expect-error 72 73 const { opts, url } = await beforeRequest(options); ··· 84 85 params: opts.paramsSerializer ? opts.query : undefined, 85 86 url, 86 87 }); 87 - if (response instanceof Error) throw response; 88 88 89 89 let { data } = response; 90 90 ··· 103 103 data: data ?? {}, 104 104 }; 105 105 } catch (error) { 106 + const e = error as AxiosError; 106 107 if (opts.throwOnError) { 107 - throw error; 108 + throw e; 108 109 } 109 - 110 - if (error instanceof AxiosError) { 111 - // @ts-expect-error 112 - error.error = error.response?.data ?? {}; 113 - return error; 114 - } 115 - 116 - if (typeof error === 'object' && error !== null) { 117 - error.error = {}; 118 - return error; 119 - } 120 - 121 - return { error: {} }; 110 + // @ts-expect-error 111 + e.error = e.response?.data ?? {}; 112 + return e; 122 113 } 123 114 }; 124 115