Skip to content

Commit 8ebc78d

Browse files
committed
chore: update stale example
1 parent c832fb0 commit 8ebc78d

3 files changed

Lines changed: 42 additions & 69 deletions

File tree

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<

0 commit comments

Comments
 (0)