Skip to content

Commit c42c65c

Browse files
committed
fix(#3805)!: respect ky instance default
1 parent 6cf6b1a commit c42c65c

37 files changed

Lines changed: 516 additions & 685 deletions

File tree

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-ky/base-url-false/client/client.gen.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { HTTPError, Options as KyOptions } from 'ky';
4-
import ky from 'ky';
3+
import type { KyResponse, Options as KyOptions } from 'ky';
4+
import ky, { isHTTPError } from 'ky';
55

66
import { createSseClient } from '../core/serverSentEvents.gen';
77
import type { HttpMethod } from '../core/types.gen';
88
import { getValidRequestBody } from '../core/utils.gen';
9-
import type { Client, Config, RequestOptions, ResolvedRequestOptions, RetryOptions } from './types.gen';
9+
import type { Client, Config, RequestOptions, ResolvedRequestOptions } from './types.gen';
1010
import type { Middleware } from './utils.gen';
1111
import {
1212
buildUrl,
@@ -43,6 +43,11 @@ export const createClient = (config: Config = {}): Client => {
4343
...options,
4444
headers: mergeHeaders(_config.headers, options.headers),
4545
ky: options.ky ?? _config.ky ?? ky,
46+
// deep merge kyOptions to ensure base _config is being respected
47+
kyOptions: {
48+
..._config.kyOptions,
49+
...options.kyOptions,
50+
},
4651
serializedBody: undefined as string | undefined,
4752
};
4853

@@ -124,33 +129,23 @@ export const createClient = (config: Config = {}): Client => {
124129

125130
const kyOptions: KyOptions = {
126131
body: validBody as BodyInit,
127-
cache: opts.cache,
128-
credentials: opts.credentials,
129-
headers: opts.headers,
130-
integrity: opts.integrity,
131-
keepalive: opts.keepalive,
132-
method: opts.method as KyOptions['method'],
133-
mode: opts.mode,
134-
redirect: 'follow',
135-
referrer: opts.referrer,
136-
referrerPolicy: opts.referrerPolicy,
137-
signal: opts.signal,
132+
...(opts.cache ? { cache: opts.cache } : {}),
133+
...(opts.credentials ? { credentials: opts.credentials } : {}),
134+
...(opts.headers ? { headers: opts.headers } : {}),
135+
...(opts.integrity ? { integrity: opts.integrity } : {}),
136+
...(opts.keepalive ? { keepalive: opts.keepalive } : {}),
137+
...(opts.method ? { method: opts.method } : {}),
138+
...(opts.mode ? { mode: opts.mode } : {}),
139+
redirect: opts.redirect || 'follow',
140+
...(opts.referrer ? { referrer: opts.referrer } : {}),
141+
...(opts.referrerPolicy ? { referrerPolicy: opts.referrerPolicy } : {}),
142+
...(opts.signal ? { signal: opts.signal } : {}),
138143
throwHttpErrors: opts.throwOnError ?? false,
139-
timeout: opts.timeout,
144+
...(opts.timeout ? { timeout: opts.timeout } : {}),
145+
retry: opts.retry ?? 2,
140146
...opts.kyOptions,
141147
};
142148

143-
if (opts.retry && typeof opts.retry === 'object') {
144-
const retryOpts = opts.retry as RetryOptions;
145-
kyOptions.retry = {
146-
limit: retryOpts.limit ?? 2,
147-
methods: retryOpts.methods as Array<
148-
'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace'
149-
>,
150-
statusCodes: retryOpts.statusCodes,
151-
};
152-
}
153-
154149
let request = new Request(url, {
155150
body: kyOptions.body as BodyInit,
156151
headers: kyOptions.headers as HeadersInit,
@@ -163,14 +158,13 @@ export const createClient = (config: Config = {}): Client => {
163158
}
164159
}
165160

166-
let response: Response;
161+
let response: KyResponse;
167162

168163
try {
169164
response = await kyInstance(request, kyOptions);
170165
} catch (error) {
171-
if (error && typeof error === 'object' && 'response' in error) {
172-
const httpError = error as HTTPError;
173-
response = httpError.response;
166+
if (isHTTPError(error)) {
167+
response = error.response;
174168

175169
for (const fn of interceptors.response.fns) {
176170
if (fn) {

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-ky/base-url-false/client/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export type {
2020
RequestResult,
2121
ResolvedRequestOptions,
2222
ResponseStyle,
23-
RetryOptions,
2423
TDataShape,
2524
} from './types.gen';
2625
export { createConfig, mergeHeaders } from './utils.gen';

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-ky/base-url-false/client/types.gen.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,21 @@ import type { Middleware } from './utils.gen';
1313

1414
export type ResponseStyle = 'data' | 'fields';
1515

16-
export interface RetryOptions {
17-
/**
18-
* Maximum number of retry attempts
19-
*
20-
* @default 2
21-
*/
22-
limit?: number;
23-
/**
24-
* HTTP methods to retry
25-
*
26-
* @default ['get', 'put', 'head', 'delete', 'options', 'trace']
27-
*/
28-
methods?: Array<'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace'>;
29-
/**
30-
* HTTP status codes to retry
31-
*
32-
* @default [408, 413, 429, 500, 502, 503, 504]
33-
*/
34-
statusCodes?: number[];
35-
}
36-
3716
export interface Config<T extends ClientOptions = ClientOptions>
3817
extends
39-
Omit<KyOptions, 'body' | 'headers' | 'method' | 'prefixUrl' | 'retry' | 'throwHttpErrors'>,
18+
Pick<
19+
KyOptions,
20+
| 'cache'
21+
| 'credentials'
22+
| 'retry'
23+
| 'signal'
24+
| 'integrity'
25+
| 'keepalive'
26+
| 'mode'
27+
| 'redirect'
28+
| 'referrer'
29+
| 'referrerPolicy'
30+
>,
4031
CoreConfig {
4132
/**
4233
* Base URL for all requests made by this client.
@@ -45,6 +36,10 @@ export interface Config<T extends ClientOptions = ClientOptions>
4536
/**
4637
* Ky instance to use. You can use this option to provide a custom
4738
* ky instance.
39+
*
40+
* Note that the `prefixUrl` of your ky instance will be ignored, as we
41+
* will always build the full URL and pass it to your ky instance. You
42+
* should configure `baseUrl` instead.
4843
*/
4944
ky?: typeof ky;
5045
/**
@@ -67,10 +62,6 @@ export interface Config<T extends ClientOptions = ClientOptions>
6762
* @default 'fields'
6863
*/
6964
responseStyle?: ResponseStyle;
70-
/**
71-
* Retry configuration
72-
*/
73-
retry?: RetryOptions;
7465
/**
7566
* Throw an error instead of returning it in the response?
7667
*

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-ky/base-url-number/client/client.gen.ts

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// This file is auto-generated by @hey-api/openapi-ts
22

3-
import type { HTTPError, Options as KyOptions } from 'ky';
4-
import ky from 'ky';
3+
import type { KyResponse, Options as KyOptions } from 'ky';
4+
import ky, { isHTTPError } from 'ky';
55

66
import { createSseClient } from '../core/serverSentEvents.gen';
77
import type { HttpMethod } from '../core/types.gen';
88
import { getValidRequestBody } from '../core/utils.gen';
9-
import type { Client, Config, RequestOptions, ResolvedRequestOptions, RetryOptions } from './types.gen';
9+
import type { Client, Config, RequestOptions, ResolvedRequestOptions } from './types.gen';
1010
import type { Middleware } from './utils.gen';
1111
import {
1212
buildUrl,
@@ -43,6 +43,11 @@ export const createClient = (config: Config = {}): Client => {
4343
...options,
4444
headers: mergeHeaders(_config.headers, options.headers),
4545
ky: options.ky ?? _config.ky ?? ky,
46+
// deep merge kyOptions to ensure base _config is being respected
47+
kyOptions: {
48+
..._config.kyOptions,
49+
...options.kyOptions,
50+
},
4651
serializedBody: undefined as string | undefined,
4752
};
4853

@@ -124,33 +129,23 @@ export const createClient = (config: Config = {}): Client => {
124129

125130
const kyOptions: KyOptions = {
126131
body: validBody as BodyInit,
127-
cache: opts.cache,
128-
credentials: opts.credentials,
129-
headers: opts.headers,
130-
integrity: opts.integrity,
131-
keepalive: opts.keepalive,
132-
method: opts.method as KyOptions['method'],
133-
mode: opts.mode,
134-
redirect: 'follow',
135-
referrer: opts.referrer,
136-
referrerPolicy: opts.referrerPolicy,
137-
signal: opts.signal,
132+
...(opts.cache ? { cache: opts.cache } : {}),
133+
...(opts.credentials ? { credentials: opts.credentials } : {}),
134+
...(opts.headers ? { headers: opts.headers } : {}),
135+
...(opts.integrity ? { integrity: opts.integrity } : {}),
136+
...(opts.keepalive ? { keepalive: opts.keepalive } : {}),
137+
...(opts.method ? { method: opts.method } : {}),
138+
...(opts.mode ? { mode: opts.mode } : {}),
139+
redirect: opts.redirect || 'follow',
140+
...(opts.referrer ? { referrer: opts.referrer } : {}),
141+
...(opts.referrerPolicy ? { referrerPolicy: opts.referrerPolicy } : {}),
142+
...(opts.signal ? { signal: opts.signal } : {}),
138143
throwHttpErrors: opts.throwOnError ?? false,
139-
timeout: opts.timeout,
144+
...(opts.timeout ? { timeout: opts.timeout } : {}),
145+
retry: opts.retry ?? 2,
140146
...opts.kyOptions,
141147
};
142148

143-
if (opts.retry && typeof opts.retry === 'object') {
144-
const retryOpts = opts.retry as RetryOptions;
145-
kyOptions.retry = {
146-
limit: retryOpts.limit ?? 2,
147-
methods: retryOpts.methods as Array<
148-
'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace'
149-
>,
150-
statusCodes: retryOpts.statusCodes,
151-
};
152-
}
153-
154149
let request = new Request(url, {
155150
body: kyOptions.body as BodyInit,
156151
headers: kyOptions.headers as HeadersInit,
@@ -163,14 +158,13 @@ export const createClient = (config: Config = {}): Client => {
163158
}
164159
}
165160

166-
let response: Response;
161+
let response: KyResponse;
167162

168163
try {
169164
response = await kyInstance(request, kyOptions);
170165
} catch (error) {
171-
if (error && typeof error === 'object' && 'response' in error) {
172-
const httpError = error as HTTPError;
173-
response = httpError.response;
166+
if (isHTTPError(error)) {
167+
response = error.response;
174168

175169
for (const fn of interceptors.response.fns) {
176170
if (fn) {

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-ky/base-url-number/client/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export type {
2020
RequestResult,
2121
ResolvedRequestOptions,
2222
ResponseStyle,
23-
RetryOptions,
2423
TDataShape,
2524
} from './types.gen';
2625
export { createConfig, mergeHeaders } from './utils.gen';

packages/openapi-ts-tests/main/test/__snapshots__/3.1.x/clients/@hey-api/client-ky/base-url-number/client/types.gen.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,21 @@ import type { Middleware } from './utils.gen';
1313

1414
export type ResponseStyle = 'data' | 'fields';
1515

16-
export interface RetryOptions {
17-
/**
18-
* Maximum number of retry attempts
19-
*
20-
* @default 2
21-
*/
22-
limit?: number;
23-
/**
24-
* HTTP methods to retry
25-
*
26-
* @default ['get', 'put', 'head', 'delete', 'options', 'trace']
27-
*/
28-
methods?: Array<'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace'>;
29-
/**
30-
* HTTP status codes to retry
31-
*
32-
* @default [408, 413, 429, 500, 502, 503, 504]
33-
*/
34-
statusCodes?: number[];
35-
}
36-
3716
export interface Config<T extends ClientOptions = ClientOptions>
3817
extends
39-
Omit<KyOptions, 'body' | 'headers' | 'method' | 'prefixUrl' | 'retry' | 'throwHttpErrors'>,
18+
Pick<
19+
KyOptions,
20+
| 'cache'
21+
| 'credentials'
22+
| 'retry'
23+
| 'signal'
24+
| 'integrity'
25+
| 'keepalive'
26+
| 'mode'
27+
| 'redirect'
28+
| 'referrer'
29+
| 'referrerPolicy'
30+
>,
4031
CoreConfig {
4132
/**
4233
* Base URL for all requests made by this client.
@@ -45,6 +36,10 @@ export interface Config<T extends ClientOptions = ClientOptions>
4536
/**
4637
* Ky instance to use. You can use this option to provide a custom
4738
* ky instance.
39+
*
40+
* Note that the `prefixUrl` of your ky instance will be ignored, as we
41+
* will always build the full URL and pass it to your ky instance. You
42+
* should configure `baseUrl` instead.
4843
*/
4944
ky?: typeof ky;
5045
/**
@@ -67,10 +62,6 @@ export interface Config<T extends ClientOptions = ClientOptions>
6762
* @default 'fields'
6863
*/
6964
responseStyle?: ResponseStyle;
70-
/**
71-
* Retry configuration
72-
*/
73-
retry?: RetryOptions;
7465
/**
7566
* Throw an error instead of returning it in the response?
7667
*

0 commit comments

Comments
 (0)