Skip to content

Commit 792f1c0

Browse files
committed
fix(api): prefer unknown instead of any
1 parent 45b695a commit 792f1c0

File tree

151 files changed

+1114
-1082
lines changed

Some content is hidden

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

151 files changed

+1114
-1082
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@typescript-eslint/ban-ts-ignore": 0,
1717
"@typescript-eslint/explicit-function-return-type": 0,
1818
"@typescript-eslint/explicit-module-boundary-types": 0,
19-
"@typescript-eslint/no-explicit-any": 0,
19+
"@typescript-eslint/no-explicit-any": "error",
2020
"@typescript-eslint/no-inferrable-types": 0,
2121
"@typescript-eslint/no-non-null-assertion": 0,
2222
"@typescript-eslint/no-var-requires": 0,

.github/workflows/unittest.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ jobs:
2626
- name: Build library
2727
run: npm run release
2828

29+
- name: Run linter
30+
run: npm run eslint
31+
2932
- name: Run unit tests
3033
run: npm run test
3134

bin/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const params = program
1212
.version(json.version)
1313
.requiredOption('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
1414
.requiredOption('-o, --output <value>', 'Output directory (required)')
15-
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, axios, angular]', 'fetch')
15+
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, axios, angular]')
1616
.option('--name <value>', 'Custom client class name')
1717
.option('--useOptions [value]', 'Use options instead of arguments', false)
1818
.option('--no-autoformat', 'Disable processing generated files with formatter')
@@ -44,7 +44,6 @@ if (OpenAPI) {
4444
exportModels: parseBooleanOrString(params.exportModels),
4545
exportSchemas: JSON.parse(params.exportSchemas) === true,
4646
exportServices: parseBooleanOrString(params.exportServices),
47-
httpClient: params.client,
4847
useDateType: JSON.parse(params.useDateType) === true,
4948
useOptions: JSON.parse(params.useOptions) === true,
5049
})

bin/index.spec.js

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { sync } = require('cross-spawn');
22

33
describe('bin', () => {
4-
it('it should support minimal params', async () => {
4+
it('supports required parameters', async () => {
55
const result = sync('node', [
66
'./bin/index.js',
77
'--input',
@@ -10,11 +10,86 @@ describe('bin', () => {
1010
'./test/generated/bin',
1111
'--no-write',
1212
]);
13-
expect(result.stdout.toString()).toBe('');
13+
expect(result.stdout.toString()).not.toContain('Prettier');
14+
expect(result.stdout.toString()).toContain('Done!');
1415
expect(result.stderr.toString()).toBe('');
1516
});
1617

17-
it('it should support all params', async () => {
18+
it('generates angular client', async () => {
19+
const result = sync('node', [
20+
'./bin/index.js',
21+
'--input',
22+
'./test/spec/v3.json',
23+
'--output',
24+
'./test/generated/bin',
25+
'--client',
26+
'angular',
27+
'--no-write',
28+
]);
29+
expect(result.stdout.toString()).toContain('Angular');
30+
expect(result.stderr.toString()).toBe('');
31+
});
32+
33+
it('generates axios client', async () => {
34+
const result = sync('node', [
35+
'./bin/index.js',
36+
'--input',
37+
'./test/spec/v3.json',
38+
'--output',
39+
'./test/generated/bin',
40+
'--client',
41+
'axios',
42+
'--no-write',
43+
]);
44+
expect(result.stdout.toString()).toContain('Axios');
45+
expect(result.stderr.toString()).toBe('');
46+
});
47+
48+
it('generates fetch client', async () => {
49+
const result = sync('node', [
50+
'./bin/index.js',
51+
'--input',
52+
'./test/spec/v3.json',
53+
'--output',
54+
'./test/generated/bin',
55+
'--client',
56+
'fetch',
57+
'--no-write',
58+
]);
59+
expect(result.stdout.toString()).toContain('Fetch');
60+
expect(result.stderr.toString()).toBe('');
61+
});
62+
it('generates node client', async () => {
63+
const result = sync('node', [
64+
'./bin/index.js',
65+
'--input',
66+
'./test/spec/v3.json',
67+
'--output',
68+
'./test/generated/bin',
69+
'--client',
70+
'node',
71+
'--no-write',
72+
]);
73+
expect(result.stdout.toString()).toContain('Node.js');
74+
expect(result.stderr.toString()).toBe('');
75+
});
76+
77+
it('generates xhr client', async () => {
78+
const result = sync('node', [
79+
'./bin/index.js',
80+
'--input',
81+
'./test/spec/v3.json',
82+
'--output',
83+
'./test/generated/bin',
84+
'--client',
85+
'xhr',
86+
'--no-write',
87+
]);
88+
expect(result.stdout.toString()).toContain('XHR');
89+
expect(result.stderr.toString()).toBe('');
90+
});
91+
92+
it('supports all parameters', async () => {
1893
const result = sync('node', [
1994
'./bin/index.js',
2095
'--input',
@@ -38,11 +113,11 @@ describe('bin', () => {
38113
'Dto',
39114
'--no-write',
40115
]);
41-
expect(result.stdout.toString()).toBe('');
116+
expect(result.stdout.toString()).toContain('Done!');
42117
expect(result.stderr.toString()).toBe('');
43118
});
44119

45-
it('it should support regexp params', async () => {
120+
it('supports regexp parameters', async () => {
46121
const result = sync('node', [
47122
'./bin/index.js',
48123
'--input',
@@ -55,30 +130,29 @@ describe('bin', () => {
55130
'^(Simple|Types)',
56131
'--no-write',
57132
]);
58-
expect(result.stdout.toString()).toBe('');
133+
expect(result.stdout.toString()).toContain('Done!');
59134
expect(result.stderr.toString()).toBe('');
60135
});
61136

62-
it('should autoformat with Prettier', async () => {
137+
it('autoformats output with Prettier', async () => {
63138
const result = sync('node', [
64139
'./bin/index.js',
65140
'--input',
66141
'./test/spec/v3.json',
67142
'--output',
68143
'./test/generated/bin',
69-
'--no-write',
70144
]);
71-
expect(result.stdout.toString()).toBe('');
145+
expect(result.stdout.toString()).toContain('Prettier');
72146
expect(result.stderr.toString()).toBe('');
73147
});
74148

75-
it('it should throw error without params', async () => {
149+
it('throws error without parameters', async () => {
76150
const result = sync('node', ['./bin/index.js', '--no-write']);
77151
expect(result.stdout.toString()).toBe('');
78152
expect(result.stderr.toString()).toContain(`error: required option '-i, --input <value>' not specified`);
79153
});
80154

81-
it('it should throw error with wrong params', async () => {
155+
it('throws error with wrong parameters', async () => {
82156
const result = sync('node', [
83157
'./bin/index.js',
84158
'--input',
@@ -92,7 +166,7 @@ describe('bin', () => {
92166
expect(result.stderr.toString()).toContain(`error: unknown option '--unknown'`);
93167
});
94168

95-
it('it should display help', async () => {
169+
it('displays help', async () => {
96170
const result = sync('node', ['./bin/index.js', '--help', '--no-write']);
97171
expect(result.stdout.toString()).toContain(`Usage: openapi-ts [options]`);
98172
expect(result.stdout.toString()).toContain(`-i, --input <value>`);

src/HttpClient.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/client/interfaces/Options.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { HttpClient } from '../../HttpClient';
1+
import type { OpenApi as OpenApiV2 } from '../../openApi/v2/interfaces/OpenApi';
2+
import type { OpenApi as OpenApiV3 } from '../../openApi/v3/interfaces/OpenApi';
23

34
export type ServiceResponse = 'body' | 'generics' | 'response';
45

@@ -11,6 +12,10 @@ export interface Options {
1112
* Manually set base in OpenAPI config instead of inferring from server value
1213
*/
1314
base?: string;
15+
/**
16+
* The selected HTTP client (fetch, xhr, node or axios)
17+
*/
18+
client?: 'angular' | 'axios' | 'fetch' | 'node' | 'xhr';
1419
/**
1520
* Custom client class name
1621
*/
@@ -35,14 +40,10 @@ export interface Options {
3540
* Generate services
3641
*/
3742
exportServices?: boolean | string;
38-
/**
39-
* The selected httpClient (fetch, xhr, node or axios)
40-
*/
41-
httpClient?: HttpClient;
4243
/**
4344
* The relative location of the OpenAPI spec
4445
*/
45-
input: string | Record<string, any>;
46+
input: string | OpenApiV2 | OpenApiV3;
4647
/**
4748
* Use operation ID to generate operation names?
4849
*/

src/index.spec.ts

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,132 @@
1-
import OpenAPI from './index';
1+
import { generate, parseOpenApiSpecification } from './index';
2+
import * as parseV2 from './openApi/v2';
3+
import * as parseV3 from './openApi/v3';
24

35
describe('index', () => {
46
it('parses v2 without issues', async () => {
5-
await OpenAPI.generate({
7+
await generate({
68
input: './test/spec/v2.json',
79
output: './generated/v2/',
810
write: false,
911
});
1012
});
1113

1214
it('parses v3 without issues', async () => {
13-
await OpenAPI.generate({
15+
await generate({
1416
input: './test/spec/v3.json',
1517
output: './generated/v3/',
1618
write: false,
1719
});
1820
});
1921

2022
it('downloads and parses v2 without issues', async () => {
21-
await OpenAPI.generate({
23+
await generate({
2224
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/spec/v2.json',
2325
output: './generated/v2-downloaded/',
2426
write: false,
2527
});
2628
});
2729

2830
it('downloads and parses v3 without issues', async () => {
29-
await OpenAPI.generate({
31+
await generate({
3032
input: 'https://raw.githubusercontent.com/ferdikoomen/openapi-typescript-codegen/master/test/spec/v3.json',
3133
output: './generated/v3-downloaded/',
3234
write: false,
3335
});
3436
});
3537
});
38+
39+
describe('parseOpenApiSpecification', () => {
40+
afterEach(() => {
41+
jest.restoreAllMocks();
42+
});
43+
44+
const options: Parameters<typeof parseOpenApiSpecification>[1] = {
45+
autoformat: true,
46+
client: 'fetch',
47+
enums: true,
48+
exportCore: true,
49+
exportModels: true,
50+
exportSchemas: true,
51+
exportServices: true,
52+
input: '',
53+
operationId: true,
54+
output: '',
55+
postfixModels: '',
56+
postfixServices: '',
57+
serviceResponse: 'body',
58+
useDateType: false,
59+
useOptions: true,
60+
write: false,
61+
};
62+
63+
it('uses v2 parser', () => {
64+
const spy = jest.spyOn(parseV2, 'parse');
65+
66+
const spec: Parameters<typeof parseOpenApiSpecification>[0] = {
67+
info: {
68+
title: 'dummy',
69+
version: '1.0',
70+
},
71+
paths: {},
72+
swagger: '2',
73+
};
74+
parseOpenApiSpecification(spec, options);
75+
expect(spy).toHaveBeenCalledWith(spec, options);
76+
77+
const spec2: Parameters<typeof parseOpenApiSpecification>[0] = {
78+
info: {
79+
title: 'dummy',
80+
version: '1.0',
81+
},
82+
paths: {},
83+
swagger: '2.0',
84+
};
85+
parseOpenApiSpecification(spec2, options);
86+
expect(spy).toHaveBeenCalledWith(spec2, options);
87+
});
88+
89+
it('uses v3 parser', () => {
90+
const spy = jest.spyOn(parseV3, 'parse');
91+
92+
const spec: Parameters<typeof parseOpenApiSpecification>[0] = {
93+
info: {
94+
title: 'dummy',
95+
version: '1.0',
96+
},
97+
openapi: '3',
98+
paths: {},
99+
};
100+
parseOpenApiSpecification(spec, options);
101+
expect(spy).toHaveBeenCalledWith(spec, options);
102+
103+
const spec2: Parameters<typeof parseOpenApiSpecification>[0] = {
104+
info: {
105+
title: 'dummy',
106+
version: '1.0',
107+
},
108+
openapi: '3.0',
109+
paths: {},
110+
};
111+
parseOpenApiSpecification(spec2, options);
112+
expect(spy).toHaveBeenCalledWith(spec2, options);
113+
114+
const spec3: Parameters<typeof parseOpenApiSpecification>[0] = {
115+
info: {
116+
title: 'dummy',
117+
version: '1.0',
118+
},
119+
openapi: '3.1.0',
120+
paths: {},
121+
};
122+
parseOpenApiSpecification(spec3, options);
123+
expect(spy).toHaveBeenCalledWith(spec3, options);
124+
});
125+
126+
it('throws on unknown version', () => {
127+
// @ts-ignore
128+
expect(() => parseOpenApiSpecification({ foo: 'bar' }, options)).toThrow(
129+
`Unsupported Open API specification: ${JSON.stringify({ foo: 'bar' }, null, 2)}`
130+
);
131+
});
132+
});

0 commit comments

Comments
 (0)