Skip to content

Commit 7190899

Browse files
authored
Merge branch 'drwpow:main' into main
2 parents b6dcf46 + a7dbe90 commit 7190899

File tree

432 files changed

+228968
-265446
lines changed

Some content is hidden

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

432 files changed

+228968
-265446
lines changed

.github/workflows/ci.yml

+12-16
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,26 @@ jobs:
1717
- uses: actions/checkout@v4
1818
- uses: actions/setup-node@v4
1919
with:
20-
node-version: 20
21-
- uses: pnpm/action-setup@v2
20+
node-version: 22
21+
- uses: pnpm/action-setup@v4
2222
with:
2323
version: latest
24-
- run: pnpm i
24+
run_install: true
2525
- run: pnpm run lint
2626
test-node-versions:
2727
runs-on: ubuntu-latest
2828
strategy:
2929
matrix:
30-
node-version: [18.x, 20.x, 21.x]
30+
node-version: [18, 20, 22]
3131
steps:
3232
- uses: actions/checkout@v4
3333
- uses: actions/setup-node@v4
3434
with:
3535
node-version: ${{ matrix.node-version }}
36-
- uses: pnpm/action-setup@v2
36+
- uses: pnpm/action-setup@v4
3737
with:
3838
version: latest
39-
- run: pnpm i
39+
run_install: true
4040
- run: pnpm run build
4141
- run: pnpm test
4242
test-macos:
@@ -45,27 +45,23 @@ jobs:
4545
- uses: actions/checkout@v4
4646
- uses: actions/setup-node@v4
4747
with:
48-
node-version: 20
49-
- uses: pnpm/action-setup@v2
48+
node-version: 22
49+
- uses: pnpm/action-setup@v4
5050
with:
5151
version: latest
52-
- run: pnpm i
52+
run_install: true
5353
- run: pnpm run build
5454
- run: pnpm test
55-
env:
56-
CI_ENV: macos
5755
test-windows:
5856
runs-on: windows-latest
5957
steps:
6058
- uses: actions/checkout@v4
6159
- uses: actions/setup-node@v4
6260
with:
63-
node-version: 20
64-
- uses: pnpm/action-setup@v2
61+
node-version: 22
62+
- uses: pnpm/action-setup@v4
6563
with:
6664
version: latest
67-
- run: pnpm i
65+
run_install: true
6866
- run: pnpm run build
6967
- run: pnpm test
70-
env:
71-
CI_ENV: windows

.github/workflows/release.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ jobs:
1212
- uses: actions/checkout@v4
1313
- uses: actions/setup-node@v4
1414
with:
15-
node-version: 20
16-
- uses: pnpm/action-setup@v2
15+
node-version: 22
16+
- uses: pnpm/action-setup@v4
1717
with:
1818
version: latest
19-
- run: pnpm i
19+
run_install: true
2020
- run: pnpm run build
2121
- uses: changesets/action@v1
2222
with:

docs/.vitepress/shared.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ export const shared = defineConfig({
1515
{
1616
defer: "",
1717
src: "https://static.cloudflareinsights.com/beacon.min.js",
18-
"data-cf-beacon": '{"token": "f5713e86f9aa43278151f2763d6b59ae"}',
18+
"data-cf-beacon": '{"token": "ea4189cb68024071a5aff8663b3b63f1"}',
1919
},
2020
],
2121
],
22-
2322
sitemap: {
2423
hostname: HOSTNAME,
2524
},
@@ -35,8 +34,6 @@ export const shared = defineConfig({
3534
locales: { ...zhSearch },
3635
},
3736
},
38-
socialLinks: [
39-
{ icon: "github", link: "https://github.com/drwpow/openapi-typescript" },
40-
],
37+
socialLinks: [{ icon: "github", link: "https://github.com/drwpow/openapi-typescript" }],
4138
},
4239
});

docs/6.x/advanced.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ And the magic that produces this would live in a `test/utils.ts` file that can b
8585
<details>
8686
<summary>📄 <strong>test/utils.ts</strong></summary>
8787

88-
::: code-group [test/utils.ts]
88+
::: code-group
8989

90-
```ts
90+
```ts [test/utils.ts]
9191
import type { paths } from "./api/v1"; // generated by openapi-typescript
9292

9393
// Settings
@@ -123,7 +123,7 @@ export function mockResponses(responses: {
123123
fetchMock.mockResponse((req) => {
124124
const mockedPath = findPath(
125125
req.url.replace(BASE_URL, ""),
126-
Object.keys(responses),
126+
Object.keys(responses)
127127
)!;
128128
// note: we get lazy with the types here, because the inference is bad anyway and this has a `void` return signature. The important bit is the parameter signature.
129129
if (!mockedPath || (!responses as any)[mockedPath])
@@ -142,11 +142,11 @@ export function mockResponses(responses: {
142142
// helper function that matches a realistic URL (/users/123) to an OpenAPI path (/users/{user_id}
143143
export function findPath(
144144
actual: string,
145-
testPaths: string[],
145+
testPaths: string[]
146146
): string | undefined {
147147
const url = new URL(
148148
actual,
149-
actual.startsWith("http") ? undefined : "http://testapi.com",
149+
actual.startsWith("http") ? undefined : "http://testapi.com"
150150
);
151151
const actualParts = url.pathname.split("/");
152152
for (const p of testPaths) {

docs/advanced.md

+50-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Note that debug messages will be suppressed if the output is `stdout`.
2929

3030
Example:
3131

32-
```yaml
32+
::: code-group
33+
34+
```yaml [my-openapi-3-schema.yaml]
3335
ErrorCode:
3436
type: integer
3537
format: int32
@@ -47,9 +49,13 @@ ErrorCode:
4749
- "Something went wrong"
4850
```
4951
52+
:::
53+
5054
Will result in:
5155
52-
```ts
56+
::: code-group
57+
58+
```ts [my-openapi-3-schema.d.ts]
5359
enum ErrorCode {
5460
// User is not authorized
5561
Unauthorized = 100
@@ -60,6 +66,8 @@ enum ErrorCode {
6066
}
6167
```
6268

69+
:::
70+
6371
Alternatively you can use `x-enumNames` and `x-enumDescriptions` ([NSwag/NJsonSchema](https://github.com/RicoSuter/NJsonSchema/wiki/Enums#enum-names-and-descriptions)).
6472

6573
## Styleguide
@@ -274,7 +282,9 @@ OpenAPI’s composition tools (`oneOf`/`anyOf`/`allOf`) are powerful tools for r
274282

275283
#### ❌ Bad
276284

277-
```yaml
285+
::: code-group
286+
287+
```yaml [my-openapi-3-schema.yaml]
278288
Pet:
279289
type: object
280290
properties:
@@ -296,19 +306,27 @@ Pet:
296306
- $ref: "#/components/schemas/Turtle"
297307
```
298308
309+
:::
310+
299311
This generates the following type which mixes both TypeScript unions and intersections. While this is valid TypeScript, it’s complex, and inference may not work as you intended. But the biggest offense is TypeScript can’t discriminate via the `type` property:
300312

301-
```ts
313+
::: code-group
314+
315+
```ts [my-openapi-3-schema.d.ts]
302316
Pet: ({
303317
/** @enum {string} */
304318
type?: "cat" | "dog" | "rabbit" | "snake" | "turtle";
305319
name?: string;
306320
}) & (components["schemas"]["Cat"] | components["schemas"]["Dog"] | components["schemas"]["Rabbit"] | components["schemas"]["Snake"] | components["schemas"]["Turtle"]);
307321
```
308322

323+
:::
324+
309325
#### ✅ Better
310326

311-
```yaml
327+
::: code-group
328+
329+
```yaml [my-openapi-3-schema.yaml]
312330
Pet:
313331
oneOf:
314332
- $ref: "#/components/schemas/Cat"
@@ -330,13 +348,19 @@ Cat:
330348
- cat
331349
```
332350

351+
:::
352+
333353
The resulting generated types are not only simpler; TypeScript can now discriminate using `type` (notice `Cat` has `type` with a single enum value of `"cat"`).
334354

335-
```ts
355+
::: code-group
356+
357+
```ts [my-openapi-3-schema.d.ts]
336358
Pet: components["schemas"]["Cat"] | components["schemas"]["Dog"] | components["schemas"]["Rabbit"] | components["schemas"]["Snake"] | components["schemas"]["Turtle"];
337359
Cat: { type?: "cat"; } & components["schemas"]["PetCommonProperties"];
338360
```
339361

362+
:::
363+
340364
_Note: you optionally could provide `discriminator.propertyName: "type"` on `Pet` ([docs](https://spec.openapis.org/oas/v3.1.0#discriminator-object)) to automatically generate the `type` key, but is less explicit._
341365

342366
While the schema permits you to use composition in any way you like, it’s good to always take a look at the generated types and see if there’s a simpler way to express your unions & intersections. Limiting the use of `oneOf` is not the only way to do that, but often yields the greatest benefits.
@@ -345,7 +369,9 @@ While the schema permits you to use composition in any way you like, it’s good
345369

346370
[JSONSchema $defs](https://json-schema.org/understanding-json-schema/structuring.html#defs) can be used to provide sub-schema definitions anywhere. However, these won’t always convert cleanly to TypeScript. For example, this works:
347371

348-
```yaml
372+
::: code-group
373+
374+
```yaml [my-openapi-3-schema.yaml]
349375
components:
350376
schemas:
351377
DefType:
@@ -360,9 +386,13 @@ components:
360386
$ref: "#/components/schemas/DefType/$defs/myDefType"
361387
```
362388
389+
:::
390+
363391
This will transform into the following TypeScript:
364392
365-
```ts
393+
::: code-group
394+
395+
```ts [my-openapi-3-schema.d.ts]
366396
export interface components {
367397
schemas: {
368398
DefType: {
@@ -377,9 +407,13 @@ export interface components {
377407
}
378408
```
379409

410+
:::
411+
380412
However, this won’t:
381413

382-
```yaml
414+
::: code-group
415+
416+
```yaml [my-openapi-3-schema.yaml]
383417
components:
384418
schemas:
385419
DefType:
@@ -393,9 +427,13 @@ components:
393427
$ref: "#/components/schemas/DefType/$defs/myDefType"
394428
```
395429
430+
:::
431+
396432
Because it will transform into:
397433
398-
```ts
434+
::: code-group
435+
436+
```ts [my-openapi-3-schema.d.ts]
399437
export interface components {
400438
schemas: {
401439
DefType: string;
@@ -406,6 +444,8 @@ export interface components {
406444
}
407445
```
408446

447+
:::
448+
409449
So be wary about where you define `$defs` as they may go missing in your final generated types.
410450

411451
::: tip

0 commit comments

Comments
 (0)