Skip to content

Commit c8708e2

Browse files
duncanbeeversdrwpowopenapi-ts-botgithub-actions[bot]
authored
Fix colliding enumValues exports (#2051)
* Add schema to postTransform options (#2049) * Add schema to postTransform options Closes #2013 * fixup! Add schema to postTransform options Closes #2013 * fixup! Add schema to postTransform options Closes #2013 * fixup! Add schema to postTransform options * Add Netlify badge (#2065) * Only check GitHub token for docs build when update needed * Add Netlify badge * Use link * [ci] release (#2084) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> * Fix issue templates (#2088) * Add feat issues to projects (#2090) * Use const arrays for OpenAPI enum types Preserve capitalization of named, exported enum values. Type exported const values as const, instead of their location within the operations or components schemas. Derive and export types for enum values from concrete values in const arrays. Use derived enum value types in operations and components schemas. Use non-conflicting variable names for composed OpenAPI enums (anyOf: [enum1, enum2]) * Export type-predicates along when enumValues is true --------- Co-authored-by: Drew Powers <[email protected]> Co-authored-by: openapi-ts-bot <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 6a08b34 commit c8708e2

31 files changed

+802
-309
lines changed

.changeset/shaggy-impalas-develop.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": major
3+
---
4+
5+
Export enumValues as const arrays. Derive schema types from literal values.

.github/ISSUE_TEMPLATE/01_openapi-typescript-bug.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ labels:
66
- triage
77
body:
88
- type: input
9+
id: version
910
attributes:
1011
label: openapi-typescript version
1112
placeholder: x.x.x
1213
validations:
1314
required: true
1415
- type: input
16+
id: node
1517
attributes:
1618
label: Node.js version
1719
placeholder: 20.x.x
1820
validations:
1921
required: true
2022
- type: input
23+
id: os
2124
attributes:
2225
label: OS + version
2326
placeholder: macOS 15.1.1
@@ -45,14 +48,14 @@ body:
4548
required: true
4649
- type: checkboxes
4750
id: required
48-
label: Required
4951
attributes:
52+
label: Required
5053
options:
5154
- label: My OpenAPI schema is valid and passes the [Redocly validator](https://redocly.com/docs/cli/commands/lint/) (`npx @redocly/cli@latest lint`)
5255
required: true
5356
- type: checkboxes
5457
id: extra
55-
label: Extra
5658
attributes:
59+
label: Extra
5760
options:
5861
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/CONTRIBUTING.md))

.github/ISSUE_TEMPLATE/01_openapi-typescript-feat.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name: "openapi-typescript: Feature request"
22
description: Propose new functionality or a breaking change
3-
title: ""
43
labels:
54
- openapi-ts
65
- enhancement
6+
projects:
7+
- openapi-ts/2
78
body:
89
- type: textarea
910
id: description
@@ -21,7 +22,7 @@ body:
2122
required: true
2223
- type: checkboxes
2324
id: extra
24-
label: Extra
2525
attributes:
26+
label: Extra
2627
options:
2728
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/CONTRIBUTING.md))

.github/ISSUE_TEMPLATE/02_openapi-fetch-bug.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ labels:
66
- triage
77
body:
88
- type: input
9+
id: version
910
attributes:
10-
label: Version
11+
label: openapi-fetch version
1112
placeholder: x.x.x
1213
validations:
1314
required: true
@@ -33,7 +34,7 @@ body:
3334
required: true
3435
- type: checkboxes
3536
id: extra
36-
label: Extra
3737
attributes:
38+
label: Extra
3839
options:
3940
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/CONTRIBUTING.md))

.github/ISSUE_TEMPLATE/02_openapi-fetch-feat.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
name: "openapi-fetch: Feature request"
22
description: Propose new functionality or a breaking change
3-
title: ""
43
labels:
54
- openapi-fetch
65
- enhancement
6+
projects:
7+
- openapi-ts/3
78
body:
89
- type: textarea
910
id: description
@@ -21,7 +22,7 @@ body:
2122
required: true
2223
- type: checkboxes
2324
id: extra
24-
label: Extra
2525
attributes:
26+
label: Extra
2627
options:
2728
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-fetch/CONTRIBUTING.md))

.github/ISSUE_TEMPLATE/03_openapi-react-query-bug.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ labels:
66
- triage
77
body:
88
- type: input
9+
id: version
910
attributes:
10-
label: Version
11+
label: openapi-react-query version
1112
placeholder: x.x.x
1213
validations:
1314
required: true
@@ -26,14 +27,15 @@ body:
2627
validations:
2728
required: true
2829
- type: textarea
30+
id: expected
2931
attributes:
3032
label: Expected result
3133
description: (In case it’s not obvious)
3234
validations:
3335
required: true
3436
- type: checkboxes
3537
id: extra
36-
label: Extra
3738
attributes:
39+
label: Extra
3840
options:
3941
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-react-query/CONTRIBUTING.md))

.github/ISSUE_TEMPLATE/03_openapi-react-query-feat.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
name: "openapi-react-query: Feature request"
22
description: Propose new functionality or a breaking change
3-
title: ""
43
labels:
54
- openapi-react-query
65
- enhancement
@@ -21,7 +20,7 @@ body:
2120
required: true
2221
- type: checkboxes
2322
id: extra
24-
label: Extra
2523
attributes:
24+
label: Extra
2625
options:
2726
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-react-query/CONTRIBUTING.md))

.github/ISSUE_TEMPLATE/04_swr-openapi-bug.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ labels:
55
- bug
66
body:
77
- type: input
8+
id: version
89
attributes:
9-
label: Version
10+
label: swr-openapi version
1011
placeholder: x.x.x
1112
validations:
1213
required: true
@@ -25,14 +26,15 @@ body:
2526
validations:
2627
required: true
2728
- type: textarea
29+
id: expected
2830
attributes:
2931
label: Expected result
3032
description: (In case it’s not obvious)
3133
validations:
3234
required: true
3335
- type: checkboxes
3436
id: extra
35-
label: Extra
3637
attributes:
38+
label: Extra
3739
options:
3840
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/swr-openapi/CONTRIBUTING.md))

.github/ISSUE_TEMPLATE/04_swr-openapi-feat.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
name: "swr-openapi: Feature request"
22
description: Propose new functionality or a breaking change
3-
title: ""
43
labels:
54
- swr-openapi
65
- enhancement
@@ -21,7 +20,7 @@ body:
2120
required: true
2221
- type: checkboxes
2322
id: extra
24-
label: Extra
2523
attributes:
24+
label: Extra
2625
options:
2726
- label: I’m willing to open a PR (see [CONTRIBUTING.md](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/swr-openapi/CONTRIBUTING.md))

docs/.vitepress/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import zh from "./zh";
44
import ja from "./ja";
55
import shared from "./shared";
66
import { transformerTwoslash } from "@shikijs/vitepress-twoslash";
7-
import { ModuleKind, ModuleResolutionKind } from "typescript";
7+
import { ModuleResolutionKind } from "typescript";
88

99
// https://vitepress.dev/reference/site-config
1010
export default defineConfig({

docs/.vitepress/en.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export default defineConfig({
139139
],
140140
footer: {
141141
message:
142-
'Released under the <a href="https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE">MIT License</a>.',
142+
'Released under the <a href="https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE">MIT License</a>. Powered by <a href="https://netlify.com">Netlify</a>.',
143143
},
144144
},
145145
});

docs/.vitepress/shared.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const shared: UserConfig = {
2626
themeConfig: {
2727
siteTitle: false,
2828
logo: "/assets/openapi-ts.svg",
29-
outline: 'deep',
29+
outline: "deep",
3030
search: {
3131
provider: "algolia",
3232
options: {

docs/.vitepress/theme/CustomLayout.vue

+23-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const { Layout } = DefaultTheme;
3939
</a></template
4040
>
4141

42-
<!-- Silver sponsor logos -->
42+
<!-- Sidebar sponsors -->
4343
<template #sidebar-nav-after>
4444
<div class="sidenav-sponsors">
4545
<h5>Gold Sponsors</h5>
@@ -58,6 +58,9 @@ const { Layout } = DefaultTheme;
5858
</a>
5959
</li>
6060
</ul>
61+
<p class="sidebar-hosting">
62+
Powered by <a href="https://netlify.com">Netlify</a>
63+
</p>
6164
</div>
6265
</template>
6366
</Layout>
@@ -147,6 +150,25 @@ const { Layout } = DefaultTheme;
147150
height: 3rem;
148151
width: auto;
149152
}
153+
154+
.sidebar-hosting {
155+
color: var(--vp-c-text-3);
156+
font-size: 0.75rem;
157+
font-weight: 500;
158+
letter-spacing: 0.0625em;
159+
margin-block-start: 2rem;
160+
text-transform: uppercase;
161+
162+
a {
163+
color: var(--vp-c-brand-1);
164+
text-decoration: underline;
165+
166+
&:hover,
167+
&:focus-visible {
168+
color: var(--vp-c-brand-2);
169+
}
170+
}
171+
}
150172
</style>
151173

152174
<style>

docs/6.x/node.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ Resultant diff with correctly-typed `file` property:
122122
+ file?: Blob;
123123
```
124124
125-
Any [Schema Object](https://spec.openapis.org/oas/latest.html#schema-object) present in your schema will be run through this formatter (even remote ones!). Also be sure to check the `metadata` parameter for additional context that may be helpful.
125+
#### transform / postTransform metadata
126126
127-
There are many other uses for this besides checking `format`. Because this must return a **string** you can produce any arbitrary TypeScript code you’d like (even your own custom types).
127+
Any [Schema Object](https://spec.openapis.org/oas/latest.html#schema-object) present in your schema will be run through `transform`, prior to its conversion to a TypeScript AST node, and `postTransform` after its conversion, including remote schemas!
128+
129+
The `metadata` parameter present on both `transform` and `postTransform` has additional context that may be helpful.
130+
131+
| Property | Description |
132+
|-|-|
133+
| `metadata.path` | A [`$ref`](https://json-schema.org/understanding-json-schema/structuring#dollarref) URI string, pointing to the current schema object |
134+
| `metadata.schema` | The schema object being transformed (only present for `postTransform`) |
135+
| `metadata.ctx` | The GlobalContext object, containing
136+
137+
There are many other uses for this besides checking `format`. Because `tranform` may return a **string** you can produce any arbitrary TypeScript code you’d like (even your own custom types).

docs/scripts/update-contributors.js

+12-17
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ const data = JSON.parse(fs.readFileSync(CONTRIBUTORS_JSON, "utf8"));
77

88
const ONE_WEEK = 1000 * 60 * 60 * 24;
99

10-
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
11-
if (!GITHUB_TOKEN) {
12-
throw new Error(
13-
'GITHUB_TOKEN not set! Create a token with "read:user" scope and set as an environment variable.\nhttps://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-personal-access-token-classic',
14-
);
10+
function getGitHubToken() {
11+
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
12+
if (!GITHUB_TOKEN) {
13+
throw new Error(
14+
'GITHUB_TOKEN not set! Create a token with "read:user" scope and set as an environment variable.\nhttps://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-personal-access-token-classic',
15+
);
16+
}
17+
return GITHUB_TOKEN;
1518
}
1619

1720
class UserFetchError extends Error {
@@ -37,7 +40,7 @@ async function fetchUserInfo(username) {
3740
const res = await fetch(`https://api.github.com/users/${username}`, {
3841
headers: {
3942
Accept: "application/vnd.github+json",
40-
Authorization: `Bearer ${GITHUB_TOKEN}`,
43+
Authorization: `Bearer ${getGitHubToken()}`,
4144
"X-GitHub-Api-Version": "2022-11-28",
4245
},
4346
});
@@ -189,19 +192,14 @@ const CONTRIBUTORS = {
189192

190193
async function main() {
191194
let i = 0;
192-
const total = Object.values(CONTRIBUTORS).reduce(
193-
(total, next) => total + next.size,
194-
0,
195-
);
195+
const total = Object.values(CONTRIBUTORS).reduce((total, next) => total + next.size, 0);
196196
await Promise.all(
197197
Object.entries(CONTRIBUTORS).map(async ([repo, contributors]) => {
198198
data[repo] ??= [];
199199
for (const username of [...contributors]) {
200200
i++;
201201
// skip profiles that have been updated within the past week
202-
const { lastFetch } = data[repo].find(
203-
(u) => u.username === username,
204-
) ?? { lastFetch: 0 };
202+
const { lastFetch } = data[repo].find((u) => u.username === username) ?? { lastFetch: 0 };
205203
if (Date.now() - lastFetch < ONE_WEEK) {
206204
// biome-ignore lint/suspicious/noConsoleLog: this is a script
207205
console.log(`[${i}/${total}] (Skipped ${username})`);
@@ -219,10 +217,7 @@ async function main() {
219217
upsert(data[repo], userData);
220218
// biome-ignore lint/suspicious/noConsoleLog: this is a script
221219
console.log(`[${i}/${total}] Updated for ${username}`);
222-
fs.writeFileSync(
223-
new URL("../data/contributors.json", import.meta.url),
224-
JSON.stringify(data),
225-
); // update file while fetching (sync happens safely in between fetches)
220+
fs.writeFileSync(new URL("../data/contributors.json", import.meta.url), JSON.stringify(data)); // update file while fetching (sync happens safely in between fetches)
226221
} catch (err) {
227222
if (err instanceof UserFetchError && err.notFound) {
228223
console.warn(`[${i}/${total}] (Skipped ${username}, not found)`);

packages/openapi-typescript/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# openapi-typescript
22

3+
## 7.5.1
4+
5+
### Patch Changes
6+
7+
- [#2049](https://github.com/openapi-ts/openapi-typescript/pull/2049) [`39f9b2f`](https://github.com/openapi-ts/openapi-typescript/commit/39f9b2fb913eb13817592b22f1bbe35de1bc4c33) Thanks [@duncanbeevers](https://github.com/duncanbeevers)! - Add schema to postTransform options
8+
39
## 7.5.0
410

511
### Minor Changes

packages/openapi-typescript/examples/digital-ocean-api.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
* Do not make direct changes to the file.
44
*/
55

6+
type WithRequired<T, K extends keyof T> = T & {
7+
[P in K]-?: T[P];
8+
};
69
export interface paths {
710
"/v2/1-clicks": {
811
parameters: {
@@ -28798,6 +28801,3 @@ export interface operations {
2879828801
};
2879928802
};
2880028803
}
28801-
type WithRequired<T, K extends keyof T> = T & {
28802-
[P in K]-?: T[P];
28803-
};

packages/openapi-typescript/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default async function openapiTS(
8181
immutable: options.immutable ?? false,
8282
rootTypes: options.rootTypes ?? false,
8383
rootTypesNoSchemaPrefix: options.rootTypesNoSchemaPrefix ?? false,
84-
injectFooter: [],
84+
injectNodes: [],
8585
pathParamsAsTypes: options.pathParamsAsTypes ?? false,
8686
postTransform: typeof options.postTransform === "function" ? options.postTransform : undefined,
8787
propertiesRequiredByDefault: options.propertiesRequiredByDefault ?? false,

0 commit comments

Comments
 (0)