Skip to content

Commit b2c3fc7

Browse files
authored
Merge branch 'master' into non-ascii-identifiers
2 parents 308a180 + a9c6e48 commit b2c3fc7

32 files changed

+1103
-214
lines changed

.github/workflows/unittest.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: unittest
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout
10+
uses: actions/[email protected]
11+
12+
- name: Setup Node environment
13+
uses: actions/[email protected]
14+
with:
15+
node-version: 20
16+
17+
- name: Cache Modules
18+
uses: actions/cache@v4
19+
with:
20+
path: "**/node_modules"
21+
key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }}
22+
23+
- name: Install dependencies
24+
run: npm install
25+
26+
- name: Build library
27+
run: npm run release
28+
29+
- name: Run unit tests
30+
run: npm run test
31+
32+
# - name: Run e2e tests
33+
# run: npm run test:e2e
34+
35+
# - name: Submit to Codecov
36+
# run: npm run codecov
37+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ test/e2e/generated
1515
samples/generated
1616
samples/swagger-codegen-cli-v2.jar
1717
samples/swagger-codegen-cli-v3.jar
18+
.env

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ $ openapi --help
4343
--useOptions Use options instead of arguments
4444
--useUnionTypes Use union types instead of enums
4545
--exportCore <value> Write core files to disk (default: true)
46-
--exportServices <value> Write services to disk (default: true)
47-
--exportModels <value> Write models to disk (default: true)
46+
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
47+
--exportModels <value> Write models to disk [true, false, regexp] (default: true)
4848
--exportSchemas <value> Write schemas to disk (default: false)
4949
--indent <value> Indentation options [4, 2, tab] (default: "4")
5050
--postfixServices Service name postfix (default: "Service")

bin/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const params = program
1616
.option('--name <value>', 'Custom client class name')
1717
.option('--useOptions', 'Use options instead of arguments')
1818
.option('--useUnionTypes', 'Use union types instead of enums')
19+
.option('--autoformat', 'Process generated files with autoformatter', false)
1920
.option('--exportCore <value>', 'Write core files to disk', true)
2021
.option('--exportServices <value>', 'Write services to disk', true)
2122
.option('--exportModels <value>', 'Write models to disk', true)
@@ -29,6 +30,14 @@ const params = program
2930

3031
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
3132

33+
const parseBooleanOrString = value => {
34+
try {
35+
return JSON.parse(value) === true;
36+
} catch (error) {
37+
return value;
38+
}
39+
};
40+
3241
if (OpenAPI) {
3342
OpenAPI.generate({
3443
input: params.input,
@@ -37,9 +46,10 @@ if (OpenAPI) {
3746
clientName: params.name,
3847
useOptions: params.useOptions,
3948
useUnionTypes: params.useUnionTypes,
49+
autoformat: JSON.parse(params.autoformat) === true,
4050
exportCore: JSON.parse(params.exportCore) === true,
41-
exportServices: JSON.parse(params.exportServices) === true,
42-
exportModels: JSON.parse(params.exportModels) === true,
51+
exportServices: parseBooleanOrString(params.exportServices),
52+
exportModels: parseBooleanOrString(params.exportModels),
4353
exportSchemas: JSON.parse(params.exportSchemas) === true,
4454
indent: params.indent,
4555
postfixServices: params.postfixServices,

bin/index.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@ describe('bin', () => {
4343
expect(result.stderr.toString()).toBe('');
4444
});
4545

46+
it('it should support regexp params', async () => {
47+
const result = crossSpawn.sync('node', [
48+
'./bin/index.js',
49+
'--input',
50+
'./test/spec/v3.json',
51+
'--output',
52+
'./test/generated/bin',
53+
'--exportServices',
54+
'^(Simple|Types)',
55+
'--exportModels',
56+
'^(Simple|Types)',
57+
]);
58+
expect(result.stdout.toString()).toBe('');
59+
expect(result.stderr.toString()).toBe('');
60+
});
61+
62+
it('should autoformat with Prettier', async () => {
63+
const result = crossSpawn.sync('node', [
64+
'./bin/index.js',
65+
'--input',
66+
'./test/spec/v3.json',
67+
'--output',
68+
'./test/generated/bin',
69+
'--autoformat',
70+
]);
71+
expect(result.stdout.toString()).toBe('');
72+
expect(result.stderr.toString()).toBe('');
73+
});
74+
4675
it('it should throw error without params', async () => {
4776
const result = crossSpawn.sync('node', ['./bin/index.js']);
4877
expect(result.stdout.toString()).toBe('');

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "openapi-typescript-codegen",
3-
"version": "0.27.0",
2+
"name": "@nicolas-chaulet/openapi-typescript-codegen",
3+
"version": "0.27.6",
44
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
6-
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
6+
"homepage": "https://github.com/CanoaPBC/openapi-typescript-codegen",
77
"repository": {
88
"type": "git",
9-
"url": "git+https://github.com/ferdikoomen/openapi-typescript-codegen.git"
9+
"url": "git+https://github.com/CanoaPBC/openapi-typescript-codegen.git"
1010
},
1111
"bugs": {
1212
"url": "https://github.com/ferdikoomen/openapi-typescript-codegen/issues"

src/client/interfaces/Model.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ import type { Schema } from './Schema';
33

44
export interface Model extends Schema {
55
name: string;
6-
export: 'reference' | 'generic' | 'enum' | 'array' | 'dictionary' | 'interface' | 'one-of' | 'any-of' | 'all-of';
6+
export:
7+
| 'reference'
8+
| 'generic'
9+
| 'enum'
10+
| 'array'
11+
| 'dictionary'
12+
| 'interface'
13+
| 'one-of'
14+
| 'any-of'
15+
| 'all-of'
16+
| 'const';
717
type: string;
818
base: string;
919
template: string | null;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Model } from './Model';
22

33
export interface ModelComposition {
4-
type: 'one-of' | 'any-of' | 'all-of';
5-
imports: string[];
64
enums: Model[];
5+
export: 'one-of' | 'any-of' | 'all-of';
6+
imports: string[];
77
properties: Model[];
88
}

src/index.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ export type Options = {
1919
clientName?: string;
2020
useOptions?: boolean;
2121
useUnionTypes?: boolean;
22+
autoformat?: boolean;
2223
exportCore?: boolean;
23-
exportServices?: boolean;
24-
exportModels?: boolean;
24+
exportServices?: boolean | string;
25+
exportModels?: boolean | string;
2526
exportSchemas?: boolean;
2627
indent?: Indent;
2728
postfixServices?: string;
@@ -57,6 +58,7 @@ export const generate = async ({
5758
clientName,
5859
useOptions = false,
5960
useUnionTypes = false,
61+
autoformat = false,
6062
exportCore = true,
6163
exportServices = true,
6264
exportModels = true,
@@ -75,42 +77,32 @@ export const generate = async ({
7577
useOptions,
7678
});
7779

80+
let parser: typeof parseV2 | typeof parseV3;
81+
7882
switch (openApiVersion) {
7983
case OpenApiVersion.V2: {
80-
const client = parseV2(openApi);
81-
const clientFinal = postProcessClient(client);
82-
if (!write) break;
83-
await writeClient(
84-
clientFinal,
85-
templates,
86-
output,
87-
httpClient,
88-
useOptions,
89-
useUnionTypes,
90-
exportCore,
91-
exportServices,
92-
exportModels,
93-
exportSchemas,
94-
indent,
95-
postfixServices,
96-
postfixModels,
97-
clientName,
98-
request
99-
);
84+
parser = parseV2;
10085
break;
10186
}
10287

10388
case OpenApiVersion.V3: {
104-
const client = parseV3(openApi);
105-
const clientFinal = postProcessClient(client);
106-
if (!write) break;
89+
parser = parseV3;
90+
break;
91+
}
92+
}
93+
94+
if (parser) {
95+
const client = parser(openApi);
96+
const clientFinal = postProcessClient(client);
97+
if (write) {
10798
await writeClient(
10899
clientFinal,
109100
templates,
110101
output,
111102
httpClient,
112103
useOptions,
113104
useUnionTypes,
105+
autoformat,
114106
exportCore,
115107
exportServices,
116108
exportModels,
@@ -121,7 +113,6 @@ export const generate = async ({
121113
clientName,
122114
request
123115
);
124-
break;
125116
}
126117
}
127118
};

0 commit comments

Comments
 (0)