Skip to content

Commit 9abd347

Browse files
authored
Merge pull request #3806 from SukkaW/fix-3805
2 parents 6cf6b1a + d16cf1c commit 9abd347

41 files changed

Lines changed: 575 additions & 826 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/weak-avocados-tap.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@hey-api/openapi-ts": minor
3+
---
4+
5+
**plugin(@hey-api/client-ky)**: respect ky instance defaults

examples/openapi-ts-ky/src/client/client/client.gen.ts

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +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 {
10-
Client,
11-
Config,
12-
RequestOptions,
13-
ResolvedRequestOptions,
14-
RetryOptions,
15-
} from './types.gen';
9+
import type { Client, Config, RequestOptions, ResolvedRequestOptions } from './types.gen';
1610
import type { Middleware } from './utils.gen';
1711
import {
1812
buildUrl,
@@ -49,6 +43,11 @@ export const createClient = (config: Config = {}): Client => {
4943
...options,
5044
headers: mergeHeaders(_config.headers, options.headers),
5145
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+
},
5251
serializedBody: undefined as string | undefined,
5352
};
5453

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

131130
const kyOptions: KyOptions = {
132131
body: validBody as BodyInit,
133-
cache: opts.cache,
134-
credentials: opts.credentials,
135-
headers: opts.headers,
136-
integrity: opts.integrity,
137-
keepalive: opts.keepalive,
138-
method: opts.method as KyOptions['method'],
139-
mode: opts.mode,
140-
redirect: 'follow',
141-
referrer: opts.referrer,
142-
referrerPolicy: opts.referrerPolicy,
143-
signal: opts.signal,
132+
...(opts.cache !== undefined ? { cache: opts.cache } : {}),
133+
...(opts.credentials !== undefined ? { credentials: opts.credentials } : {}),
134+
...(opts.headers !== undefined ? { headers: opts.headers } : {}),
135+
...(opts.integrity !== undefined ? { integrity: opts.integrity } : {}),
136+
...(opts.keepalive !== undefined ? { keepalive: opts.keepalive } : {}),
137+
...(opts.method !== undefined ? { method: opts.method } : {}),
138+
...(opts.mode !== undefined ? { mode: opts.mode } : {}),
139+
redirect: opts.redirect ?? 'follow',
140+
...(opts.referrer !== undefined ? { referrer: opts.referrer } : {}),
141+
...(opts.referrerPolicy !== undefined ? { referrerPolicy: opts.referrerPolicy } : {}),
142+
...(opts.signal !== undefined ? { signal: opts.signal } : {}),
144143
throwHttpErrors: opts.throwOnError ?? false,
145-
timeout: opts.timeout,
144+
...(opts.timeout !== undefined ? { timeout: opts.timeout } : {}),
146145
...opts.kyOptions,
146+
retry: opts.retry ?? opts.kyOptions?.retry ?? 2,
147147
};
148148

149-
if (opts.retry && typeof opts.retry === 'object') {
150-
const retryOpts = opts.retry as RetryOptions;
151-
kyOptions.retry = {
152-
limit: retryOpts.limit ?? 2,
153-
methods: retryOpts.methods as Array<
154-
'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace'
155-
>,
156-
statusCodes: retryOpts.statusCodes,
157-
};
158-
}
159-
160149
let request = new Request(url, {
161150
body: kyOptions.body as BodyInit,
162151
headers: kyOptions.headers as HeadersInit,
@@ -169,14 +158,13 @@ export const createClient = (config: Config = {}): Client => {
169158
}
170159
}
171160

172-
let response: Response;
161+
let response: KyResponse;
173162

174163
try {
175164
response = await kyInstance(request, kyOptions);
176165
} catch (error) {
177-
if (error && typeof error === 'object' && 'response' in error) {
178-
const httpError = error as HTTPError;
179-
response = httpError.response;
166+
if (isHTTPError(error)) {
167+
response = error.response;
180168

181169
for (const fn of interceptors.response.fns) {
182170
if (fn) {

examples/openapi-ts-ky/src/client/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';

examples/openapi-ts-ky/src/client/client/types.gen.ts

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,22 @@ import type { Middleware } from './utils.gen';
1010

1111
export type ResponseStyle = 'data' | 'fields';
1212

13-
export interface RetryOptions {
14-
/**
15-
* Maximum number of retry attempts
16-
*
17-
* @default 2
18-
*/
19-
limit?: number;
20-
/**
21-
* HTTP methods to retry
22-
*
23-
* @default ['get', 'put', 'head', 'delete', 'options', 'trace']
24-
*/
25-
methods?: Array<'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace'>;
26-
/**
27-
* HTTP status codes to retry
28-
*
29-
* @default [408, 413, 429, 500, 502, 503, 504]
30-
*/
31-
statusCodes?: number[];
32-
}
33-
3413
export interface Config<T extends ClientOptions = ClientOptions>
3514
extends
36-
Omit<KyOptions, 'body' | 'headers' | 'method' | 'prefixUrl' | 'retry' | 'throwHttpErrors'>,
15+
Pick<
16+
KyOptions,
17+
| 'cache'
18+
| 'credentials'
19+
| 'retry'
20+
| 'signal'
21+
| 'integrity'
22+
| 'keepalive'
23+
| 'mode'
24+
| 'redirect'
25+
| 'referrer'
26+
| 'referrerPolicy'
27+
| 'timeout'
28+
>,
3729
CoreConfig {
3830
/**
3931
* Base URL for all requests made by this client.
@@ -42,6 +34,10 @@ export interface Config<T extends ClientOptions = ClientOptions>
4234
/**
4335
* Ky instance to use. You can use this option to provide a custom
4436
* ky instance.
37+
*
38+
* Note that the `prefixUrl` of your ky instance will be ignored, as we
39+
* will always build the full URL and pass it to your ky instance. You
40+
* should configure `baseUrl` instead.
4541
*/
4642
ky?: typeof ky;
4743
/**
@@ -64,22 +60,12 @@ export interface Config<T extends ClientOptions = ClientOptions>
6460
* @default 'fields'
6561
*/
6662
responseStyle?: ResponseStyle;
67-
/**
68-
* Retry configuration
69-
*/
70-
retry?: RetryOptions;
7163
/**
7264
* Throw an error instead of returning it in the response?
7365
*
7466
* @default false
7567
*/
7668
throwOnError?: T['throwOnError'];
77-
/**
78-
* Request timeout in milliseconds
79-
*
80-
* @default 10000
81-
*/
82-
timeout?: number;
8369
}
8470

8571
export interface RequestOptions<

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 !== undefined ? { cache: opts.cache } : {}),
133+
...(opts.credentials !== undefined ? { credentials: opts.credentials } : {}),
134+
...(opts.headers !== undefined ? { headers: opts.headers } : {}),
135+
...(opts.integrity !== undefined ? { integrity: opts.integrity } : {}),
136+
...(opts.keepalive !== undefined ? { keepalive: opts.keepalive } : {}),
137+
...(opts.method !== undefined ? { method: opts.method } : {}),
138+
...(opts.mode !== undefined ? { mode: opts.mode } : {}),
139+
redirect: opts.redirect ?? 'follow',
140+
...(opts.referrer !== undefined ? { referrer: opts.referrer } : {}),
141+
...(opts.referrerPolicy !== undefined ? { referrerPolicy: opts.referrerPolicy } : {}),
142+
...(opts.signal !== undefined ? { signal: opts.signal } : {}),
138143
throwHttpErrors: opts.throwOnError ?? false,
139-
timeout: opts.timeout,
144+
...(opts.timeout !== undefined ? { timeout: opts.timeout } : {}),
140145
...opts.kyOptions,
146+
retry: opts.retry ?? opts.kyOptions?.retry ?? 2,
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: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,22 @@ 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+
| 'timeout'
31+
>,
4032
CoreConfig {
4133
/**
4234
* Base URL for all requests made by this client.
@@ -45,6 +37,10 @@ export interface Config<T extends ClientOptions = ClientOptions>
4537
/**
4638
* Ky instance to use. You can use this option to provide a custom
4739
* ky instance.
40+
*
41+
* Note that the `prefixUrl` of your ky instance will be ignored, as we
42+
* will always build the full URL and pass it to your ky instance. You
43+
* should configure `baseUrl` instead.
4844
*/
4945
ky?: typeof ky;
5046
/**
@@ -67,22 +63,12 @@ export interface Config<T extends ClientOptions = ClientOptions>
6763
* @default 'fields'
6864
*/
6965
responseStyle?: ResponseStyle;
70-
/**
71-
* Retry configuration
72-
*/
73-
retry?: RetryOptions;
7466
/**
7567
* Throw an error instead of returning it in the response?
7668
*
7769
* @default false
7870
*/
7971
throwOnError?: T['throwOnError'];
80-
/**
81-
* Request timeout in milliseconds
82-
*
83-
* @default 10000
84-
*/
85-
timeout?: number;
8672
}
8773

8874
export interface RequestOptions<

0 commit comments

Comments
 (0)