Skip to content

Commit c815d6a

Browse files
committed
fix(enumeration): correct enumeration naming
BREAKING CHANGE: The naming conventions for enumerations within the package have been updated to avoid conflicts in most cases. Previously, only the field name was used for the enumeration included in a path specification (components were fine). Now it includes all elements of the path, other than any "v(\d+)" prefix and other than the word "parameters." At some point this should be improved to just exclude the real "parameters" component of the schema.
1 parent a760de7 commit c815d6a

File tree

6 files changed

+133
-127
lines changed

6 files changed

+133
-127
lines changed

README.md

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

33
This module adds a transformer to openapi-typescript that generates real enums in addition to the typed unions.
4+
You can use it as a straight replacement for `npx openapi-typescript` or `yarn openapi-typescript` as we essentially
5+
copied the CLI code. In the future we'd prefer this was just a transform that was invoked from the openapi-typescript
6+
CLI.

__tests__/__snapshots__/index.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ export interface operations {
665665
};
666666
}
667667
668-
export enum FindByStatusStatusEnum {
668+
export enum PetFindByStatusStatusEnum {
669669
\\"available\\" = \\"available\\",
670670
\\"pending\\" = \\"pending\\",
671671
\\"sold\\" = \\"sold\\"

package.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@
4343
"devDependencies": {
4444
"@commitlint/cli": "^17.6.7",
4545
"@commitlint/config-conventional": "^17.6.7",
46-
"@openapi-typescript-infra/coconfig": "^3.3.3",
46+
"@openapi-typescript-infra/coconfig": "^3.3.6",
4747
"@semantic-release/changelog": "^6.0.3",
4848
"@semantic-release/exec": "^6.0.3",
4949
"@semantic-release/git": "^10.0.1",
5050
"@types/yargs-parser": "^21.0.0",
51-
"@typescript-eslint/eslint-plugin": "^6.2.0",
52-
"@typescript-eslint/parser": "^6.2.0",
51+
"@typescript-eslint/eslint-plugin": "^6.2.1",
52+
"@typescript-eslint/parser": "^6.2.1",
5353
"coconfig": "^0.13.3",
5454
"eslint": "^8.46.0",
55-
"eslint-config-prettier": "^8.9.0",
55+
"eslint-config-prettier": "^8.10.0",
5656
"eslint-plugin-import": "^2.28.0",
57-
"prettier": "^3.0.0",
57+
"prettier": "^3.0.1",
5858
"ts-node": "^10.9.1",
5959
"typescript": "^5.1.6",
60-
"vitest": "^0.33.0"
60+
"vitest": "^0.34.1"
6161
},
6262
"scripts": {
6363
"test": "tsc -p tsconfig.test.json --noEmit && vitest",
@@ -71,7 +71,7 @@
7171
},
7272
"dependencies": {
7373
"ansi-colors": "^4.1.3",
74-
"openapi-typescript": "^6.3.9",
74+
"openapi-typescript": "6.3.9",
7575
"yargs-parser": "^21.1.1"
7676
}
7777
}

src/bin/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function error(msg: string) {
1818
}
1919

2020
const HELP = `Usage
21-
$ openapi-typescript [input] [options]
21+
$ openapi-typescript-enum [input] [options]
2222
2323
Options
2424
--help Display this

src/index.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
// @ts-expect-error ESM is a threat to humanity
1+
// @ts-expect-error ESM is a threat to humanity. Once OATS exposes a working CJS, use it
22
import type { OpenAPITSOptions } from 'openapi-typescript';
33

44
function getEnumTitle(path: string) {
5-
const parts =
6-
path
7-
.replace(/[^A-Za-z0-9/]/g, '/')
8-
.match(/\/([^/]+)(?=\/parameters\/|$)/g)
9-
?.map((p) => p.slice(1)) ?? [];
5+
const parts = path
6+
.substring('#/paths//'.length)
7+
.split(/\/parameters\//)
8+
.join('/')
9+
.split('/');
10+
if (parts?.[0].match(/v[0-9]+/)) {
11+
parts.shift();
12+
}
1013
return `${parts.map((p) => p.charAt(0).toUpperCase() + p.slice(1)).join('')}`;
1114
}
1215

0 commit comments

Comments
 (0)