Skip to content

Fix colliding enumValues exports #2051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-impalas-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": major
---

Export enumValues as const arrays. Derive schema types from literal values.
7 changes: 5 additions & 2 deletions .github/ISSUE_TEMPLATE/01_openapi-typescript-bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ labels:
- triage
body:
- type: input
id: version
attributes:
label: openapi-typescript version
placeholder: x.x.x
validations:
required: true
- type: input
id: node
attributes:
label: Node.js version
placeholder: 20.x.x
validations:
required: true
- type: input
id: os
attributes:
label: OS + version
placeholder: macOS 15.1.1
Expand Down Expand Up @@ -45,14 +48,14 @@ body:
required: true
- type: checkboxes
id: required
label: Required
attributes:
label: Required
options:
- label: My OpenAPI schema is valid and passes the [Redocly validator](https://redocly.com/docs/cli/commands/lint/) (`npx @redocly/cli@latest lint`)
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
5 changes: 3 additions & 2 deletions .github/ISSUE_TEMPLATE/01_openapi-typescript-feat.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: "openapi-typescript: Feature request"
description: Propose new functionality or a breaking change
title: ""
labels:
- openapi-ts
- enhancement
projects:
- openapi-ts/2
body:
- type: textarea
id: description
Expand All @@ -21,7 +22,7 @@ body:
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
5 changes: 3 additions & 2 deletions .github/ISSUE_TEMPLATE/02_openapi-fetch-bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ labels:
- triage
body:
- type: input
id: version
attributes:
label: Version
label: openapi-fetch version
placeholder: x.x.x
validations:
required: true
Expand All @@ -33,7 +34,7 @@ body:
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
5 changes: 3 additions & 2 deletions .github/ISSUE_TEMPLATE/02_openapi-fetch-feat.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: "openapi-fetch: Feature request"
description: Propose new functionality or a breaking change
title: ""
labels:
- openapi-fetch
- enhancement
projects:
- openapi-ts/3
body:
- type: textarea
id: description
Expand All @@ -21,7 +22,7 @@ body:
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
6 changes: 4 additions & 2 deletions .github/ISSUE_TEMPLATE/03_openapi-react-query-bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ labels:
- triage
body:
- type: input
id: version
attributes:
label: Version
label: openapi-react-query version
placeholder: x.x.x
validations:
required: true
Expand All @@ -26,14 +27,15 @@ body:
validations:
required: true
- type: textarea
id: expected
attributes:
label: Expected result
description: (In case it’s not obvious)
validations:
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
3 changes: 1 addition & 2 deletions .github/ISSUE_TEMPLATE/03_openapi-react-query-feat.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name: "openapi-react-query: Feature request"
description: Propose new functionality or a breaking change
title: ""
labels:
- openapi-react-query
- enhancement
Expand All @@ -21,7 +20,7 @@ body:
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
6 changes: 4 additions & 2 deletions .github/ISSUE_TEMPLATE/04_swr-openapi-bug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ labels:
- bug
body:
- type: input
id: version
attributes:
label: Version
label: swr-openapi version
placeholder: x.x.x
validations:
required: true
Expand All @@ -25,14 +26,15 @@ body:
validations:
required: true
- type: textarea
id: expected
attributes:
label: Expected result
description: (In case it’s not obvious)
validations:
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
3 changes: 1 addition & 2 deletions .github/ISSUE_TEMPLATE/04_swr-openapi-feat.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
name: "swr-openapi: Feature request"
description: Propose new functionality or a breaking change
title: ""
labels:
- swr-openapi
- enhancement
Expand All @@ -21,7 +20,7 @@ body:
required: true
- type: checkboxes
id: extra
label: Extra
attributes:
label: Extra
options:
- 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))
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import zh from "./zh";
import ja from "./ja";
import shared from "./shared";
import { transformerTwoslash } from "@shikijs/vitepress-twoslash";
import { ModuleKind, ModuleResolutionKind } from "typescript";
import { ModuleResolutionKind } from "typescript";

// https://vitepress.dev/reference/site-config
export default defineConfig({
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export default defineConfig({
],
footer: {
message:
'Released under the <a href="https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE">MIT License</a>.',
'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>.',
},
},
});
2 changes: 1 addition & 1 deletion docs/.vitepress/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const shared: UserConfig = {
themeConfig: {
siteTitle: false,
logo: "/assets/openapi-ts.svg",
outline: 'deep',
outline: "deep",
search: {
provider: "algolia",
options: {
Expand Down
24 changes: 23 additions & 1 deletion docs/.vitepress/theme/CustomLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const { Layout } = DefaultTheme;
</a></template
>

<!-- Silver sponsor logos -->
<!-- Sidebar sponsors -->
<template #sidebar-nav-after>
<div class="sidenav-sponsors">
<h5>Gold Sponsors</h5>
Expand All @@ -58,6 +58,9 @@ const { Layout } = DefaultTheme;
</a>
</li>
</ul>
<p class="sidebar-hosting">
Powered by <a href="https://netlify.com">Netlify</a>
</p>
</div>
</template>
</Layout>
Expand Down Expand Up @@ -147,6 +150,25 @@ const { Layout } = DefaultTheme;
height: 3rem;
width: auto;
}

.sidebar-hosting {
color: var(--vp-c-text-3);
font-size: 0.75rem;
font-weight: 500;
letter-spacing: 0.0625em;
margin-block-start: 2rem;
text-transform: uppercase;

a {
color: var(--vp-c-brand-1);
text-decoration: underline;

&:hover,
&:focus-visible {
color: var(--vp-c-brand-2);
}
}
}
</style>

<style>
Expand Down
14 changes: 12 additions & 2 deletions docs/6.x/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ Resultant diff with correctly-typed `file` property:
+ file?: Blob;
```

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.
#### transform / postTransform metadata

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).
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!

The `metadata` parameter present on both `transform` and `postTransform` has additional context that may be helpful.

| Property | Description |
|-|-|
| `metadata.path` | A [`$ref`](https://json-schema.org/understanding-json-schema/structuring#dollarref) URI string, pointing to the current schema object |
| `metadata.schema` | The schema object being transformed (only present for `postTransform`) |
| `metadata.ctx` | The GlobalContext object, containing

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).
29 changes: 12 additions & 17 deletions docs/scripts/update-contributors.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const data = JSON.parse(fs.readFileSync(CONTRIBUTORS_JSON, "utf8"));

const ONE_WEEK = 1000 * 60 * 60 * 24;

const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
if (!GITHUB_TOKEN) {
throw new Error(
'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',
);
function getGitHubToken() {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
if (!GITHUB_TOKEN) {
throw new Error(
'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',
);
}
return GITHUB_TOKEN;
}

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

async function main() {
let i = 0;
const total = Object.values(CONTRIBUTORS).reduce(
(total, next) => total + next.size,
0,
);
const total = Object.values(CONTRIBUTORS).reduce((total, next) => total + next.size, 0);
await Promise.all(
Object.entries(CONTRIBUTORS).map(async ([repo, contributors]) => {
data[repo] ??= [];
for (const username of [...contributors]) {
i++;
// skip profiles that have been updated within the past week
const { lastFetch } = data[repo].find(
(u) => u.username === username,
) ?? { lastFetch: 0 };
const { lastFetch } = data[repo].find((u) => u.username === username) ?? { lastFetch: 0 };
if (Date.now() - lastFetch < ONE_WEEK) {
// biome-ignore lint/suspicious/noConsoleLog: this is a script
console.log(`[${i}/${total}] (Skipped ${username})`);
Expand All @@ -219,10 +217,7 @@ async function main() {
upsert(data[repo], userData);
// biome-ignore lint/suspicious/noConsoleLog: this is a script
console.log(`[${i}/${total}] Updated for ${username}`);
fs.writeFileSync(
new URL("../data/contributors.json", import.meta.url),
JSON.stringify(data),
); // update file while fetching (sync happens safely in between fetches)
fs.writeFileSync(new URL("../data/contributors.json", import.meta.url), JSON.stringify(data)); // update file while fetching (sync happens safely in between fetches)
} catch (err) {
if (err instanceof UserFetchError && err.notFound) {
console.warn(`[${i}/${total}] (Skipped ${username}, not found)`);
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-typescript/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# openapi-typescript

## 7.5.1

### Patch Changes

- [#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

## 7.5.0

### Minor Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/openapi-typescript/examples/digital-ocean-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Do not make direct changes to the file.
*/

type WithRequired<T, K extends keyof T> = T & {
[P in K]-?: T[P];
};
export interface paths {
"/v2/1-clicks": {
parameters: {
Expand Down Expand Up @@ -28798,6 +28801,3 @@ export interface operations {
};
};
}
type WithRequired<T, K extends keyof T> = T & {
[P in K]-?: T[P];
};
2 changes: 1 addition & 1 deletion packages/openapi-typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default async function openapiTS(
immutable: options.immutable ?? false,
rootTypes: options.rootTypes ?? false,
rootTypesNoSchemaPrefix: options.rootTypesNoSchemaPrefix ?? false,
injectFooter: [],
injectNodes: [],
pathParamsAsTypes: options.pathParamsAsTypes ?? false,
postTransform: typeof options.postTransform === "function" ? options.postTransform : undefined,
propertiesRequiredByDefault: options.propertiesRequiredByDefault ?? false,
Expand Down
Loading
Loading