Skip to content

Commit 484ceb1

Browse files
fix(client-ky): resolve TypeScript type errors
Fix TypeScript compilation errors without using `as any`: - Use inline array type instead of accessing `KyOptions['retry']['methods']` to avoid type error when retry can be number | object - Add type assertion for headers as HeadersInit to satisfy Request constructor - Change body type from `BodyInit | null` to `unknown` for consistency with client-fetch and to allow testing with edge case values - Use `Partial<KyInstance> as KyInstance` pattern in tests (same as client-axios) to properly type mock ky instances without `as any`
1 parent 9c683e3 commit 484ceb1

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
lines changed

packages/openapi-ts/src/plugins/@hey-api/client-ky/__tests__/client.test.ts

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
import type { KyInstance } from 'ky';
12
import { HTTPError } from 'ky';
23
import { describe, expect, it, vi } from 'vitest';
34

45
import type { ResolvedRequestOptions } from '../bundle';
56
import { createClient } from '../bundle/client';
67

7-
type MockKy = ((...args: any[]) => any) & {
8-
preconnect?: any;
9-
};
10-
118
describe('buildUrl', () => {
129
const client = createClient();
1310

@@ -87,10 +84,10 @@ describe('zero-length body handling', () => {
8784
status: 200,
8885
});
8986

90-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
87+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
9188

9289
const result = await client.request({
93-
ky: mockKy,
90+
ky: mockKy as Partial<KyInstance> as KyInstance,
9491
method: 'GET',
9592
url: '/test',
9693
});
@@ -107,10 +104,10 @@ describe('zero-length body handling', () => {
107104
status: 200,
108105
});
109106

110-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
107+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
111108

112109
const result = await client.request({
113-
ky: mockKy,
110+
ky: mockKy as Partial<KyInstance> as KyInstance,
114111
method: 'GET',
115112
parseAs: 'arrayBuffer',
116113
url: '/test',
@@ -129,10 +126,10 @@ describe('zero-length body handling', () => {
129126
status: 200,
130127
});
131128

132-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
129+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
133130

134131
const result = await client.request({
135-
ky: mockKy,
132+
ky: mockKy as Partial<KyInstance> as KyInstance,
136133
method: 'GET',
137134
url: '/test',
138135
});
@@ -149,10 +146,10 @@ describe('zero-length body handling', () => {
149146
status: 200,
150147
});
151148

152-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
149+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
153150

154151
const result = await client.request({
155-
ky: mockKy,
152+
ky: mockKy as Partial<KyInstance> as KyInstance,
156153
method: 'GET',
157154
url: '/test',
158155
});
@@ -169,10 +166,10 @@ describe('zero-length body handling', () => {
169166
status: 200,
170167
});
171168

172-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
169+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
173170

174171
const result = await client.request({
175-
ky: mockKy,
172+
ky: mockKy as Partial<KyInstance> as KyInstance,
176173
method: 'GET',
177174
url: '/test',
178175
});
@@ -190,10 +187,10 @@ describe('zero-length body handling', () => {
190187
status: 200,
191188
});
192189

193-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
190+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
194191

195192
const result = await client.request({
196-
ky: mockKy,
193+
ky: mockKy as Partial<KyInstance> as KyInstance,
197194
method: 'GET',
198195
parseAs: 'stream',
199196
url: '/test',
@@ -211,10 +208,10 @@ describe('zero-length body handling', () => {
211208
status: 200,
212209
});
213210

214-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
211+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
215212

216213
const result = await client.request({
217-
ky: mockKy,
214+
ky: mockKy as Partial<KyInstance> as KyInstance,
218215
method: 'GET',
219216
url: '/test',
220217
});
@@ -244,15 +241,15 @@ describe('unserialized request body handling', () => {
244241
status: 200,
245242
});
246243

247-
const mockKy: MockKy = vi.fn().mockResolvedValueOnce(mockResponse);
244+
const mockKy = vi.fn().mockResolvedValueOnce(mockResponse);
248245

249246
const result = await client.post({
250247
body,
251248
bodySerializer: null,
252249
headers: {
253250
'Content-Type': 'text/plain',
254251
},
255-
ky: mockKy,
252+
ky: mockKy as Partial<KyInstance> as KyInstance,
256253
url: '/test',
257254
});
258255

@@ -319,15 +316,15 @@ describe('serialized request body handling', () => {
319316
status: 200,
320317
});
321318

322-
const mockKy: MockKy = vi.fn().mockResolvedValueOnce(mockResponse);
319+
const mockKy = vi.fn().mockResolvedValueOnce(mockResponse);
323320

324321
const result = await client.post({
325322
body,
326323
bodySerializer: () => serializedBody,
327324
headers: {
328325
'Content-Type': 'application/json',
329326
},
330-
ky: mockKy,
327+
ky: mockKy as Partial<KyInstance> as KyInstance,
331328
url: '/test',
332329
});
333330

@@ -376,7 +373,7 @@ describe('request interceptor', () => {
376373
status: 200,
377374
});
378375

379-
const mockKy: MockKy = vi.fn().mockResolvedValueOnce(mockResponse);
376+
const mockKy = vi.fn().mockResolvedValueOnce(mockResponse);
380377

381378
const mockRequestInterceptor = vi
382379
.fn()
@@ -399,7 +396,7 @@ describe('request interceptor', () => {
399396
headers: {
400397
'Content-Type': contentType,
401398
},
402-
ky: mockKy,
399+
ky: mockKy as Partial<KyInstance> as KyInstance,
403400
url: '/test',
404401
});
405402

@@ -421,7 +418,7 @@ describe('response interceptor', () => {
421418
status: 200,
422419
});
423420

424-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
421+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
425422

426423
const mockResponseInterceptor = vi
427424
.fn()
@@ -435,7 +432,7 @@ describe('response interceptor', () => {
435432
);
436433

437434
await client.get({
438-
ky: mockKy,
435+
ky: mockKy as Partial<KyInstance> as KyInstance,
439436
url: '/test',
440437
});
441438

@@ -459,14 +456,14 @@ describe('error handling', () => {
459456
},
460457
);
461458

462-
const mockKy: MockKy = vi.fn().mockRejectedValue(
459+
const mockKy = vi.fn().mockRejectedValue(
463460
new HTTPError(errorResponse, new Request('https://example.com/test'), {
464461
method: 'GET',
465462
} as any),
466463
);
467464

468465
const result = await client.get({
469-
ky: mockKy,
466+
ky: mockKy as Partial<KyInstance> as KyInstance,
470467
throwOnError: false,
471468
url: '/test',
472469
});
@@ -486,15 +483,15 @@ describe('error handling', () => {
486483
},
487484
);
488485

489-
const mockKy: MockKy = vi.fn().mockRejectedValue(
486+
const mockKy = vi.fn().mockRejectedValue(
490487
new HTTPError(errorResponse, new Request('https://example.com/test'), {
491488
method: 'GET',
492489
} as any),
493490
);
494491

495492
await expect(
496493
client.get({
497-
ky: mockKy,
494+
ky: mockKy as Partial<KyInstance> as KyInstance,
498495
throwOnError: true,
499496
url: '/test',
500497
}),
@@ -506,14 +503,14 @@ describe('error handling', () => {
506503
status: 500,
507504
});
508505

509-
const mockKy: MockKy = vi.fn().mockRejectedValue(
506+
const mockKy = vi.fn().mockRejectedValue(
510507
new HTTPError(errorResponse, new Request('https://example.com/test'), {
511508
method: 'GET',
512509
} as any),
513510
);
514511

515512
const result = await client.get({
516-
ky: mockKy,
513+
ky: mockKy as Partial<KyInstance> as KyInstance,
517514
throwOnError: false,
518515
url: '/test',
519516
});
@@ -537,7 +534,7 @@ describe('error interceptor', () => {
537534
},
538535
);
539536

540-
const mockKy: MockKy = vi.fn().mockRejectedValue(
537+
const mockKy = vi.fn().mockRejectedValue(
541538
new HTTPError(errorResponse, new Request('https://example.com/test'), {
542539
method: 'GET',
543540
} as any),
@@ -550,7 +547,7 @@ describe('error interceptor', () => {
550547
const interceptorId = client.interceptors.error.use(mockErrorInterceptor);
551548

552549
const result = await client.get({
553-
ky: mockKy,
550+
ky: mockKy as Partial<KyInstance> as KyInstance,
554551
throwOnError: false,
555552
url: '/test',
556553
});
@@ -573,10 +570,10 @@ describe('retry configuration', () => {
573570
status: 200,
574571
});
575572

576-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
573+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
577574

578575
await client.get({
579-
ky: mockKy,
576+
ky: mockKy as Partial<KyInstance> as KyInstance,
580577
retry: {
581578
limit: 3,
582579
methods: ['get', 'post'],
@@ -612,10 +609,10 @@ describe('responseStyle configuration', () => {
612609
status: 200,
613610
});
614611

615-
const mockKy: MockKy = vi.fn().mockResolvedValue(mockResponse);
612+
const mockKy = vi.fn().mockResolvedValue(mockResponse);
616613

617614
const result = await client.get({
618-
ky: mockKy,
615+
ky: mockKy as Partial<KyInstance> as KyInstance,
619616
url: '/test',
620617
});
621618

@@ -635,14 +632,14 @@ describe('responseStyle configuration', () => {
635632
},
636633
);
637634

638-
const mockKy: MockKy = vi.fn().mockRejectedValue(
635+
const mockKy = vi.fn().mockRejectedValue(
639636
new HTTPError(errorResponse, new Request('https://example.com/test'), {
640637
method: 'GET',
641638
} as any),
642639
);
643640

644641
const result = await client.get({
645-
ky: mockKy,
642+
ky: mockKy as Partial<KyInstance> as KyInstance,
646643
throwOnError: false,
647644
url: '/test',
648645
});

packages/openapi-ts/src/plugins/@hey-api/client-ky/bundle/client.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
Config,
1010
RequestOptions,
1111
ResolvedRequestOptions,
12+
RetryOptions,
1213
} from './types';
1314
import type { Middleware } from './utils';
1415
import {
@@ -146,16 +147,26 @@ export const createClient = (config: Config = {}): Client => {
146147
};
147148

148149
if (opts.retry && typeof opts.retry === 'object') {
150+
const retryOpts = opts.retry as RetryOptions;
149151
kyOptions.retry = {
150-
limit: opts.retry.limit ?? 2,
151-
methods: opts.retry.methods as KyOptions['retry']['methods'],
152-
statusCodes: opts.retry.statusCodes,
152+
limit: retryOpts.limit ?? 2,
153+
methods: retryOpts.methods as Array<
154+
| 'get'
155+
| 'post'
156+
| 'put'
157+
| 'patch'
158+
| 'head'
159+
| 'delete'
160+
| 'options'
161+
| 'trace'
162+
>,
163+
statusCodes: retryOpts.statusCodes,
153164
};
154165
}
155166

156167
let request = new Request(url, {
157168
body: kyOptions.body as BodyInit,
158-
headers: kyOptions.headers,
169+
headers: kyOptions.headers as HeadersInit,
159170
method: kyOptions.method,
160171
});
161172

packages/openapi-ts/src/plugins/@hey-api/client-ky/bundle/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export interface RequestOptions<
116116
*
117117
* {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
118118
*/
119-
body?: BodyInit | null;
119+
body?: unknown;
120120
path?: Record<string, unknown>;
121121
query?: Record<string, unknown>;
122122
/**

0 commit comments

Comments
 (0)