Skip to content

Commit f777635

Browse files
author
Hampus Joakim Borgos
committed
Merge remote-tracking branch 'upstream/master'
2 parents 12277f9 + 224e8b0 commit f777635

File tree

84 files changed

+1908
-1386
lines changed

Some content is hidden

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

84 files changed

+1908
-1386
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ The OpenAPI spec allows you to define [enums](https://swagger.io/docs/specificat
130130
data model. By default, we convert these enums definitions to [TypeScript enums](https://www.typescriptlang.org/docs/handbook/enums.html).
131131
However, these enums are merged inside the namespace of the model, this is unsupported by Babel, [see docs](https://babeljs.io/docs/en/babel-plugin-transform-typescript#impartial-namespace-support).
132132
Because we also want to support projects that use Babel [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript),
133-
we offer the flag `--useOptions` to generate [union types](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-types)
133+
we offer the flag `--useUnionTypes` to generate [union types](https://www.typescriptlang.org/docs/handbook/unions-and-intersections.html#union-types)
134134
instead of the traditional enums. The difference can be seen below:
135135

136136
**Enums:**

bin/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const path = require('path');
66
const program = require('commander');
77
const pkg = require('../package.json');
88

9-
program
9+
const params = program
1010
.name('openapi')
1111
.usage('[options]')
1212
.version(pkg.version)
@@ -19,21 +19,24 @@ program
1919
.option('--exportServices <value>', 'Write services to disk', true)
2020
.option('--exportModels <value>', 'Write models to disk', true)
2121
.option('--exportSchemas <value>', 'Write schemas to disk', false)
22-
.parse(process.argv);
22+
.option('--request <value>', 'Path to custom request file')
23+
.parse(process.argv)
24+
.opts();
2325

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

2628
if (OpenAPI) {
2729
OpenAPI.generate({
28-
input: program.input,
29-
output: program.output,
30-
httpClient: program.client,
31-
useOptions: program.useOptions,
32-
useUnionTypes: program.useUnionTypes,
33-
exportCore: JSON.parse(program.exportCore) === true,
34-
exportServices: JSON.parse(program.exportServices) === true,
35-
exportModels: JSON.parse(program.exportModels) === true,
36-
exportSchemas: JSON.parse(program.exportSchemas) === true,
30+
input: params.input,
31+
output: params.output,
32+
httpClient: params.client,
33+
useOptions: params.useOptions,
34+
useUnionTypes: params.useUnionTypes,
35+
exportCore: JSON.parse(params.exportCore) === true,
36+
exportServices: JSON.parse(params.exportServices) === true,
37+
exportModels: JSON.parse(params.exportModels) === true,
38+
exportSchemas: JSON.parse(params.exportSchemas) === true,
39+
request: params.request,
3740
})
3841
.then(() => {
3942
process.exit(0);

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
'<rootDir>/src/**/*.spec.ts',
1010
'<rootDir>/test/index.spec.js',
1111
],
12+
moduleFileExtensions: ['js', 'ts', 'd.ts'],
1213
moduleNameMapper: {
1314
'\\.hbs$': '<rootDir>/src/templates/__mocks__/index.js',
1415
},

package.json

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.7.0-beta",
4-
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
3+
"version": "0.9.0",
4+
"description": "Library that generates Typescript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
77
"repository": {
@@ -19,7 +19,6 @@
1919
"generator",
2020
"client",
2121
"typescript",
22-
"javascript",
2322
"yaml",
2423
"json",
2524
"fetch",
@@ -62,44 +61,46 @@
6261
"codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b"
6362
},
6463
"dependencies": {
65-
"camelcase": "6.2.0",
66-
"commander": "6.2.0",
67-
"handlebars": "4.7.6",
68-
"js-yaml": "3.14.0",
69-
"mkdirp": "1.0.4",
70-
"path": "0.12.7",
71-
"rimraf": "3.0.2"
64+
"camelcase": "^6.2.0",
65+
"commander": "^7.0.0",
66+
"handlebars": "^4.7.6",
67+
"js-yaml": "^4.0.0",
68+
"json-schema-ref-parser": "^9.0.7",
69+
"mkdirp": "^1.0.4",
70+
"rimraf": "^3.0.2"
7271
},
7372
"devDependencies": {
74-
"@babel/cli": "7.12.1",
75-
"@babel/core": "7.12.3",
76-
"@babel/preset-env": "7.12.1",
77-
"@babel/preset-typescript": "7.12.1",
78-
"@rollup/plugin-commonjs": "16.0.0",
79-
"@rollup/plugin-node-resolve": "10.0.0",
80-
"@types/express": "4.17.9",
81-
"@types/jest": "26.0.15",
82-
"@types/js-yaml": "3.12.5",
83-
"@types/node": "14.14.7",
84-
"@types/node-fetch": "2.5.7",
85-
"@typescript-eslint/eslint-plugin": "4.7.0",
86-
"@typescript-eslint/parser": "4.7.0",
73+
"@babel/cli": "7.12.16",
74+
"@babel/core": "7.12.17",
75+
"@babel/preset-env": "7.12.17",
76+
"@babel/preset-typescript": "7.12.17",
77+
"@rollup/plugin-commonjs": "17.1.0",
78+
"@rollup/plugin-node-resolve": "11.2.0",
79+
"@types/express": "4.17.11",
80+
"@types/jest": "26.0.20",
81+
"@types/js-yaml": "4.0.0",
82+
"@types/node": "14.14.31",
83+
"@types/node-fetch": "2.5.8",
84+
"@types/qs": "6.9.5",
85+
"@typescript-eslint/eslint-plugin": "4.15.1",
86+
"@typescript-eslint/parser": "4.15.1",
8787
"codecov": "3.8.1",
88-
"eslint": "7.13.0",
89-
"eslint-config-prettier": "6.15.0",
90-
"eslint-plugin-prettier": "3.1.4",
91-
"eslint-plugin-simple-import-sort": "6.0.0",
88+
"eslint": "7.20.0",
89+
"eslint-config-prettier": "8.0.0",
90+
"eslint-plugin-prettier": "3.3.1",
91+
"eslint-plugin-simple-import-sort": "7.0.0",
9292
"express": "4.17.1",
93-
"form-data": "3.0.0",
93+
"form-data": "4.0.0",
9494
"glob": "7.1.6",
9595
"jest": "26.6.3",
9696
"jest-cli": "26.6.3",
9797
"node-fetch": "2.6.1",
98-
"prettier": "2.1.2",
99-
"puppeteer": "5.4.1",
100-
"rollup": "2.33.2",
98+
"prettier": "2.2.1",
99+
"puppeteer": "7.1.0",
100+
"qs": "6.9.6",
101+
"rollup": "2.39.0",
101102
"rollup-plugin-terser": "7.0.2",
102-
"rollup-plugin-typescript2": "0.29.0",
103-
"typescript": "4.0.5"
103+
"rollup-plugin-typescript2": "0.30.0",
104+
"typescript": "4.1.5"
104105
}
105106
}

rollup.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ const handlebarsPlugin = () => ({
3434
knownHelpers: {
3535
equals: true,
3636
notEquals: true,
37+
containsSpaces: true,
38+
union: true,
39+
intersection: true,
40+
enumerator: true,
3741
},
3842
});
3943
return `export default ${templateSpec};`;
@@ -65,6 +69,7 @@ module.exports = {
6569
'fs',
6670
'os',
6771
'util',
72+
'path',
6873
'http',
6974
'https',
7075
'handlebars/runtime',

samples/spec/v2.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,13 @@
978978
"Order": {
979979
"type": "object",
980980
"properties": {
981+
"bool": {
982+
"description": "Simple boolean enum",
983+
"type": "boolean",
984+
"enum": [
985+
true
986+
]
987+
},
981988
"id": {
982989
"type": "integer",
983990
"format": "int64"

src/HttpClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum HttpClient {
2+
FETCH = 'fetch',
3+
XHR = 'xhr',
4+
NODE = 'node',
5+
}

src/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HttpClient } from './HttpClient';
12
import { parse as parseV2 } from './openApi/v2';
23
import { parse as parseV3 } from './openApi/v3';
34
import { getOpenApiSpec } from './utils/getOpenApiSpec';
@@ -7,11 +8,7 @@ import { postProcessClient } from './utils/postProcessClient';
78
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
89
import { writeClient } from './utils/writeClient';
910

10-
export enum HttpClient {
11-
FETCH = 'fetch',
12-
XHR = 'xhr',
13-
NODE = 'node',
14-
}
11+
export { HttpClient } from './HttpClient';
1512

1613
export type Options = {
1714
input: string | Record<string, any>;
@@ -23,6 +20,7 @@ export type Options = {
2320
exportServices?: boolean;
2421
exportModels?: boolean;
2522
exportSchemas?: boolean;
23+
request?: string;
2624
write?: boolean;
2725
};
2826

@@ -39,6 +37,7 @@ export type Options = {
3937
* @param exportServices: Generate services
4038
* @param exportModels: Generate models
4139
* @param exportSchemas: Generate schemas
40+
* @param request: Path to custom request file
4241
* @param write Write the files to disk (true or false)
4342
*/
4443
export async function generate({
@@ -51,26 +50,31 @@ export async function generate({
5150
exportServices = true,
5251
exportModels = true,
5352
exportSchemas = false,
53+
request,
5454
write = true,
5555
}: Options): Promise<void> {
5656
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
5757
const openApiVersion = getOpenApiVersion(openApi);
58-
const templates = registerHandlebarTemplates();
58+
const templates = registerHandlebarTemplates({
59+
httpClient,
60+
useUnionTypes,
61+
useOptions,
62+
});
5963

6064
switch (openApiVersion) {
6165
case OpenApiVersion.V2: {
6266
const client = parseV2(openApi);
6367
const clientFinal = postProcessClient(client);
6468
if (!write) break;
65-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
69+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
6670
break;
6771
}
6872

6973
case OpenApiVersion.V3: {
7074
const client = parseV3(openApi);
7175
const clientFinal = postProcessClient(client);
7276
if (!write) break;
73-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
77+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
7478
break;
7579
}
7680
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { escapeDescription } from './escapeDescription';
2+
3+
describe('escapeDescription', () => {
4+
it('should escape', () => {
5+
expect(escapeDescription('foo `test` bar')).toEqual('foo \\`test\\` bar');
6+
});
7+
8+
it('should not escape', () => {
9+
expect(escapeDescription('')).toEqual('');
10+
expect(escapeDescription('fooBar')).toEqual('fooBar');
11+
expect(escapeDescription('foo \\`test\\` bar')).toEqual('foo \\`test\\` bar');
12+
});
13+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function escapeDescription(value: string): string {
2+
return value.replace(/([^\\])`/g, '$1\\`');
3+
}

0 commit comments

Comments
 (0)