Skip to content

Commit 4c0c7fc

Browse files
authored
Fix TS errors (#1580)
1 parent 60516f8 commit 4c0c7fc

File tree

16 files changed

+389
-269
lines changed

16 files changed

+389
-269
lines changed

.changeset/unlucky-pumpkins-march.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-fetch": patch
3+
---
4+
5+
Fix type errors

packages/openapi-fetch/examples/nextjs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"react-dom": "18.2.0"
1313
},
1414
"devDependencies": {
15-
"@types/node": "20.11.19",
15+
"@types/node": "20.11.24",
1616
"@types/react": "18.2.20",
1717
"@types/react-dom": "18.2.7",
1818
"openapi-typescript": "workspace:^",

packages/openapi-fetch/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"openapi-typescript-fetch": "^1.1.3",
7575
"superagent": "^8.1.2",
7676
"typescript": "^5.3.3",
77-
"vitest": "^1.2.2",
77+
"vitest": "^1.3.1",
7878
"vitest-fetch-mock": "^0.2.2"
7979
}
8080
}

packages/openapi-fetch/src/index.d.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import type {
22
ErrorResponse,
3-
SuccessResponse,
43
FilterKeys,
4+
HasRequiredKeys,
5+
HttpMethod,
56
MediaType,
7+
OperationRequestBodyContent,
68
PathsWithMethod,
79
ResponseObjectMap,
8-
OperationRequestBodyContent,
9-
HasRequiredKeys,
10+
SuccessResponse,
1011
} from "openapi-typescript-helpers";
1112

1213
// Note: though "any" is considered bad practice in general, this library relies
@@ -80,7 +81,7 @@ type BodyType<T = unknown> = {
8081
stream: Response["body"];
8182
};
8283
export type ParseAs = keyof BodyType;
83-
export type ParseAsResponse<T, O extends FetchOptions> = O extends {
84+
export type ParseAsResponse<T, O> = O extends {
8485
parseAs: ParseAs;
8586
}
8687
? BodyType<T>[O["parseAs"]]
@@ -110,13 +111,7 @@ export type RequestBodyOption<T> =
110111
export type FetchOptions<T> = RequestOptions<T> &
111112
Omit<RequestInit, "body" | "headers">;
112113

113-
/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
114-
export type MaybeOptionalInit<P extends {}, M extends keyof P> =
115-
HasRequiredKeys<FetchOptions<FilterKeys<P, M>>> extends never
116-
? [(FetchOptions<FilterKeys<P, M>> | undefined)?]
117-
: [FetchOptions<FilterKeys<P, M>>];
118-
119-
export type FetchResponse<T, O extends FetchOptions> =
114+
export type FetchResponse<T, O> =
120115
| {
121116
data: ParseAsResponse<
122117
FilterKeys<SuccessResponse<ResponseObjectMap<T>>, MediaType>,
@@ -174,7 +169,19 @@ export interface Middleware {
174169
onResponse?: typeof onResponse;
175170
}
176171

177-
export type ClientMethod<Paths extends {}, M> = <
172+
/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
173+
export type MaybeOptionalInit<
174+
P extends Record<HttpMethod, {}>,
175+
M extends keyof P,
176+
> =
177+
HasRequiredKeys<FetchOptions<FilterKeys<P, M>>> extends never
178+
? [(FetchOptions<FilterKeys<P, M>> | undefined)?]
179+
: [FetchOptions<FilterKeys<P, M>>];
180+
181+
export type ClientMethod<
182+
Paths extends Record<string, Record<HttpMethod, {}>>,
183+
M extends HttpMethod,
184+
> = <
178185
P extends PathsWithMethod<Paths, M>,
179186
I extends MaybeOptionalInit<Paths[P], M>,
180187
>(

packages/openapi-fetch/test/index.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
21
// @ts-expect-error
32
import createFetchMock from "vitest-fetch-mock";
43
import createClient, {

packages/openapi-fetch/test/v7-beta.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
21
// @ts-expect-error
32
import createFetchMock from "vitest-fetch-mock";
43
import createClient, {

packages/openapi-fetch/tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
"moduleResolution": "NodeNext",
1010
"noUncheckedIndexedAccess": true,
1111
"outDir": "dist",
12-
"skipLibCheck": true,
12+
"skipLibCheck": false,
1313
"strict": true,
1414
"target": "ESNext",
1515
"types": ["vitest/globals"]
1616
},
1717
"include": ["src", "test"],
18-
"exclude": ["node_modules"]
18+
"exclude": ["examples", "node_modules"]
1919
}

packages/openapi-typescript-helpers/index.d.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,15 @@ export type OperationRequestBodyMediaContent<T> =
5858
? FilterKeys<NonNullable<OperationRequestBody<T>>, "content"> | undefined
5959
: FilterKeys<OperationRequestBody<T>, "content">;
6060
/** Return first `content` from a Request Object Mapping, allowing any media type */
61-
export type OperationRequestBodyContent<T> = FilterKeys<
62-
OperationRequestBodyMediaContent<T>,
63-
MediaType
64-
> extends never
65-
?
66-
| FilterKeys<NonNullable<OperationRequestBodyMediaContent<T>>, MediaType>
67-
| undefined
68-
: FilterKeys<OperationRequestBodyMediaContent<T>, MediaType>;
61+
export type OperationRequestBodyContent<T> =
62+
FilterKeys<OperationRequestBodyMediaContent<T>, MediaType> extends never
63+
?
64+
| FilterKeys<
65+
NonNullable<OperationRequestBodyMediaContent<T>>,
66+
MediaType
67+
>
68+
| undefined
69+
: FilterKeys<OperationRequestBodyMediaContent<T>, MediaType>;
6970
/** Return first 2XX response from a Response Object Map */
7071
export type SuccessResponse<T> = ResponseContent<FilterKeys<T, OkStatus>>;
7172
/** Return first 5XX or 4XX response (in that order) from a Response Object Map */
@@ -97,4 +98,5 @@ export type FindRequiredKeys<T, K extends keyof T> = K extends unknown
9798
? never
9899
: K
99100
: K;
101+
/** Does this object contain required keys? */
100102
export type HasRequiredKeys<T> = FindRequiredKeys<T, keyof T>;

packages/openapi-typescript/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,21 @@
6262
"typescript": "^5.x"
6363
},
6464
"dependencies": {
65-
"@redocly/openapi-core": "^1.9.0",
65+
"@redocly/openapi-core": "^1.10.3",
6666
"ansi-colors": "^4.1.3",
6767
"supports-color": "^9.4.0",
6868
"yargs-parser": "^21.1.1"
6969
},
7070
"devDependencies": {
7171
"@types/degit": "^2.8.6",
7272
"@types/js-yaml": "^4.0.9",
73-
"@types/node": "^20.11.19",
73+
"@types/node": "^20.11.24",
7474
"degit": "^2.8.4",
7575
"del-cli": "^5.1.0",
76-
"esbuild": "^0.20.0",
76+
"esbuild": "^0.20.1",
7777
"execa": "^7.2.0",
7878
"typescript": "^5.3.3",
79-
"vite-node": "^1.2.2",
80-
"vitest": "^1.2.2"
79+
"vite-node": "^1.3.1",
80+
"vitest": "^1.3.1"
8181
}
8282
}

packages/openapi-typescript/test/index.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { fileURLToPath } from "node:url";
2+
import { beforeAll, describe, expect, test, vi } from "vitest";
23
import openapiTS, { astToString } from "../src/index.js";
34
import type { OpenAPI3, OpenAPITSOptions } from "../src/types.js";
45
import type { TestCase } from "./test-helpers.js";
@@ -692,7 +693,7 @@ export type operations = Record<string, never>;`,
692693
);
693694
}
694695

695-
it("does not mutate original reference", async () => {
696+
test("does not mutate original reference", async () => {
696697
const schema: OpenAPI3 = {
697698
openapi: "3.1",
698699
info: { title: "test", version: "1.0" },

packages/openapi-typescript/test/lib/ts.test.ts

+28-28
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "../../src/lib/ts.js";
1616

1717
describe("addJSDocComment", () => {
18-
it("single-line comment", () => {
18+
test("single-line comment", () => {
1919
const property = ts.factory.createPropertySignature(
2020
undefined,
2121
"comment",
@@ -30,7 +30,7 @@ describe("addJSDocComment", () => {
3030
}`);
3131
});
3232

33-
it("multi-line comment", () => {
33+
test("multi-line comment", () => {
3434
const property = ts.factory.createPropertySignature(
3535
undefined,
3636
"comment",
@@ -57,7 +57,7 @@ describe("addJSDocComment", () => {
5757
}`);
5858
});
5959

60-
it("escapes internal comments", () => {
60+
test("escapes internal comments", () => {
6161
const property = ts.factory.createPropertySignature(
6262
undefined,
6363
"comment",
@@ -77,19 +77,19 @@ describe("addJSDocComment", () => {
7777
});
7878

7979
describe("oapiRef", () => {
80-
it("single part", () => {
80+
test("single part", () => {
8181
expect(astToString(oapiRef("#/components")).trim()).toBe(`components`);
8282
});
8383

84-
it("multiple parts", () => {
84+
test("multiple parts", () => {
8585
expect(astToString(oapiRef("#/components/schemas/User")).trim()).toBe(
8686
`components["schemas"]["User"]`,
8787
);
8888
});
8989
});
9090

9191
describe("tsEnum", () => {
92-
it("string members", () => {
92+
test("string members", () => {
9393
expect(astToString(tsEnum("-my-color-", ["green", "red", "blue"])).trim())
9494
.toBe(`enum MyColor {
9595
green = "green",
@@ -98,7 +98,7 @@ describe("tsEnum", () => {
9898
}`);
9999
});
100100

101-
it("name from path", () => {
101+
test("name from path", () => {
102102
expect(
103103
astToString(
104104
tsEnum("#/paths/url/get/parameters/query/status", [
@@ -112,7 +112,7 @@ describe("tsEnum", () => {
112112
}`);
113113
});
114114

115-
it("string members with numeric prefix", () => {
115+
test("string members with numeric prefix", () => {
116116
expect(astToString(tsEnum("/my/enum/", ["0a", "1b", "2c"])).trim())
117117
.toBe(`enum MyEnum {
118118
Value0a = "0a",
@@ -121,7 +121,7 @@ describe("tsEnum", () => {
121121
}`);
122122
});
123123

124-
it("number members", () => {
124+
test("number members", () => {
125125
expect(astToString(tsEnum(".Error.code.", [100, 101, 102])).trim())
126126
.toBe(`enum ErrorCode {
127127
Value100 = 100,
@@ -130,7 +130,7 @@ describe("tsEnum", () => {
130130
}`);
131131
});
132132

133-
it("number members with x-enum-descriptions", () => {
133+
test("number members with x-enum-descriptions", () => {
134134
expect(
135135
astToString(
136136
tsEnum(
@@ -153,7 +153,7 @@ describe("tsEnum", () => {
153153
}`);
154154
});
155155

156-
it("x-enum-varnames", () => {
156+
test("x-enum-varnames", () => {
157157
expect(
158158
astToString(
159159
tsEnum(
@@ -173,7 +173,7 @@ describe("tsEnum", () => {
173173
}`);
174174
});
175175

176-
it("x-enum-varnames with numeric prefix", () => {
176+
test("x-enum-varnames with numeric prefix", () => {
177177
expect(
178178
astToString(
179179
tsEnum(
@@ -189,7 +189,7 @@ describe("tsEnum", () => {
189189
}`);
190190
});
191191

192-
it("partial x-enum-varnames and x-enum-descriptions", () => {
192+
test("partial x-enum-varnames and x-enum-descriptions", () => {
193193
expect(
194194
astToString(
195195
tsEnum(
@@ -209,7 +209,7 @@ describe("tsEnum", () => {
209209
}`);
210210
});
211211

212-
it("x-enum-descriptions with x-enum-varnames", () => {
212+
test("x-enum-descriptions with x-enum-varnames", () => {
213213
expect(
214214
astToString(
215215
tsEnum(
@@ -237,15 +237,15 @@ describe("tsEnum", () => {
237237
});
238238

239239
describe("tsPropertyIndex", () => {
240-
it("numbers -> number literals", () => {
240+
test("numbers -> number literals", () => {
241241
expect(astToString(tsPropertyIndex(200)).trim()).toBe(`200`);
242242
expect(astToString(tsPropertyIndex(200.5)).trim()).toBe(`200.5`);
243243
expect(astToString(tsPropertyIndex(Infinity)).trim()).toBe(`Infinity`);
244244
expect(astToString(tsPropertyIndex(NaN)).trim()).toBe(`NaN`);
245245
expect(astToString(tsPropertyIndex(10e3)).trim()).toBe(`10000`);
246246
});
247247

248-
it("valid strings -> identifiers", () => {
248+
test("valid strings -> identifiers", () => {
249249
expect(astToString(tsPropertyIndex("identifier")).trim()).toBe(
250250
`identifier`,
251251
);
@@ -257,7 +257,7 @@ describe("tsPropertyIndex", () => {
257257
expect(astToString(tsPropertyIndex("10e3")).trim()).toBe(`"10e3"`);
258258
});
259259

260-
it("invalid strings -> string literals", () => {
260+
test("invalid strings -> string literals", () => {
261261
expect(astToString(tsPropertyIndex("kebab-case")).trim()).toBe(
262262
`"kebab-case"`,
263263
);
@@ -273,27 +273,27 @@ describe("tsPropertyIndex", () => {
273273
});
274274

275275
describe("tsIsPrimitive", () => {
276-
it("null", () => {
276+
test("null", () => {
277277
expect(tsIsPrimitive(NULL)).toBe(true);
278278
});
279279

280-
it("number", () => {
280+
test("number", () => {
281281
expect(tsIsPrimitive(NUMBER)).toBe(true);
282282
});
283283

284-
it("string", () => {
284+
test("string", () => {
285285
expect(tsIsPrimitive(STRING)).toBe(true);
286286
});
287287

288-
it("boolean", () => {
288+
test("boolean", () => {
289289
expect(tsIsPrimitive(BOOLEAN)).toBe(true);
290290
});
291291

292-
it("array", () => {
292+
test("array", () => {
293293
expect(tsIsPrimitive(ts.factory.createArrayTypeNode(STRING))).toBe(false);
294294
});
295295

296-
it("object", () => {
296+
test("object", () => {
297297
expect(
298298
tsIsPrimitive(
299299
ts.factory.createTypeLiteralNode([
@@ -310,29 +310,29 @@ describe("tsIsPrimitive", () => {
310310
});
311311

312312
describe("tsUnion", () => {
313-
it("none", () => {
313+
test("none", () => {
314314
expect(astToString(tsUnion([])).trim()).toBe(`never`);
315315
});
316316

317-
it("one", () => {
317+
test("one", () => {
318318
expect(astToString(tsUnion([STRING])).trim()).toBe(`string`);
319319
});
320320

321-
it("multiple (primitive)", () => {
321+
test("multiple (primitive)", () => {
322322
expect(
323323
astToString(tsUnion([STRING, STRING, NUMBER, NULL, NUMBER, NULL])).trim(),
324324
).toBe(`string | number | null`);
325325
});
326326

327-
it("multiple (const)", () => {
327+
test("multiple (const)", () => {
328328
expect(
329329
astToString(
330330
tsUnion([NULL, tsLiteral("red"), tsLiteral(42), tsLiteral(false)]),
331331
).trim(),
332332
).toBe(`null | "red" | 42 | false`);
333333
});
334334

335-
it("multiple (object types)", () => {
335+
test("multiple (object types)", () => {
336336
const obj = ts.factory.createTypeLiteralNode([
337337
ts.factory.createPropertySignature(undefined, "foo", undefined, STRING),
338338
]);

0 commit comments

Comments
 (0)