Skip to content

Commit 9424a7b

Browse files
committed
- Updated PR with master branch
2 parents 975d44e + 8673f06 commit 9424a7b

File tree

206 files changed

+3002
-2031
lines changed

Some content is hidden

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

206 files changed

+3002
-2031
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ trim_trailing_whitespace = true
77
insert_final_newline = true
88
indent_style = space
99
indent_size = 4
10+
11+
[*.hbs]
12+
indent_style = tab

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist
2+
samples
23
test/generated
34
test/e2e/generated
45
node_modules

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
## [0.16.0] - 2021-01-25
5+
### Added
6+
- Added option to set the indentation (spaces and tabs)
7+
- Added option to export separate client file that allows usage for multiple backends
8+
### Fixed
9+
- Decoupled OpenAPI object from requests
10+
- Updated dependencies
11+
12+
## [0.15.0] - 2021-01-24
13+
### Added
14+
- Added change log and releases on GitHub
15+
16+
## [0.14.0] - 2021-01-24
17+
### Fixed
18+
- Added missing `postfix` options to typedef
19+
- Updated escaping of comments and descriptions
20+
- Better handling of services without tags
21+
- Updated dependencies

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@ $ openapi --help
4141
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
4242
-o, --output <value> Output directory (required)
4343
-c, --client <value> HTTP client to generate [fetch, xhr, axios, node] (default: "fetch")
44+
--name <value> Custom client class name
4445
--useOptions Use options instead of arguments
4546
--useUnionTypes Use union types instead of enums
4647
--exportCore <value> Write core files to disk (default: true)
4748
--exportServices <value> Write services to disk (default: true)
4849
--exportModels <value> Write models to disk (default: true)
4950
--exportSchemas <value> Write schemas to disk (default: false)
50-
--postfix <value> Service name postfix (default: "Service")
51+
--indent <value> Service name postfix (default: "Service")
52+
--postfix <value> Indentation options [4, 2, tab] (default: "5")
5153
--request <value> Path to custom request file
5254
-h, --help display help for command
5355
@@ -95,6 +97,33 @@ OpenAPI.generate({
9597

9698
## Features
9799

100+
101+
### Generate client instance with `--name` option
102+
The OpenAPI generator allows creation of client instances to support the multiple backend services use case.
103+
The generated client uses an instance of the server configuration and not the global `OpenAPI` constant.
104+
To generate a client instance, set a custom name to the client class, use `--name` option.
105+
106+
```
107+
openapi --input ./spec.json --output ./dist ---name AppClient
108+
```
109+
110+
The generated client will be exported from the `index` file and can be used as shown below:
111+
112+
```typescript
113+
// Create the client instance with server and authentication details
114+
const appClient = new AppClient({
115+
BASE: 'http://server-host.com',
116+
TOKEN: '1234'
117+
});
118+
119+
// Use the client instance to make the API call
120+
const res = await appClient.organizations.createOrganization({
121+
name: 'OrgName',
122+
description: 'OrgDescription',
123+
});
124+
```
125+
126+
98127
### Argument style vs. Object style `--useOptions`
99128
There's no [named parameter](https://en.wikipedia.org/wiki/Named_parameter) in JavaScript or TypeScript, because of
100129
that, we offer the flag `--useOptions` to generate code in two different styles.

bin/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ const params = program
1313
.requiredOption('-i, --input <value>', 'OpenAPI specification, can be a path, url or string content (required)')
1414
.requiredOption('-o, --output <value>', 'Output directory (required)')
1515
.option('-c, --client <value>', 'HTTP client to generate [fetch, xhr, node, axios, angular]', 'fetch')
16+
.option('--name <value>', 'Custom client class name')
1617
.option('--useOptions', 'Use options instead of arguments')
1718
.option('--useUnionTypes', 'Use union types instead of enums')
1819
.option('--exportCore <value>', 'Write core files to disk', true)
1920
.option('--exportServices <value>', 'Write services to disk', true)
2021
.option('--exportModels <value>', 'Write models to disk', true)
2122
.option('--exportSchemas <value>', 'Write schemas to disk', false)
23+
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
2224
.option('--postfix <value>', 'Service name postfix', 'Service')
2325
.option('--request <value>', 'Path to custom request file')
2426
.parse(process.argv)
@@ -31,12 +33,14 @@ if (OpenAPI) {
3133
input: params.input,
3234
output: params.output,
3335
httpClient: params.client,
36+
clientName: params.name,
3437
useOptions: params.useOptions,
3538
useUnionTypes: params.useUnionTypes,
3639
exportCore: JSON.parse(params.exportCore) === true,
3740
exportServices: JSON.parse(params.exportServices) === true,
3841
exportModels: JSON.parse(params.exportModels) === true,
3942
exportSchemas: JSON.parse(params.exportSchemas) === true,
43+
indent: params.indent,
4044
postfix: params.postfix,
4145
request: params.request,
4246
})

jest.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const config: Config.InitialOptions = {
2525
'<rootDir>/test/e2e/v3.node.spec.ts',
2626
'<rootDir>/test/e2e/v3.axios.spec.ts',
2727
'<rootDir>/test/e2e/v3.babel.spec.ts',
28+
'<rootDir>/test/e2e/client.fetch.spec.ts',
29+
'<rootDir>/test/e2e/client.xhr.spec.ts',
30+
'<rootDir>/test/e2e/client.node.spec.ts',
31+
'<rootDir>/test/e2e/client.axios.spec.ts',
32+
'<rootDir>/test/e2e/client.babel.spec.ts',
2833
],
2934
},
3035
],

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.13.0",
3+
"version": "0.16.0",
44
"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,7 +77,7 @@
7777
"@types/express": "4.17.13",
7878
"@types/glob": "7.2.0",
7979
"@types/jest": "27.4.0",
80-
"@types/node": "17.0.10",
80+
"@types/node": "17.0.12",
8181
"@types/node-fetch": "^2.5.12",
8282
"@types/qs": "6.9.7",
8383
"@typescript-eslint/eslint-plugin": "5.10.1",
@@ -96,10 +96,10 @@
9696
"jest-cli": "27.4.7",
9797
"node-fetch": "^2.6.6",
9898
"prettier": "2.5.1",
99-
"puppeteer": "13.1.1",
99+
"puppeteer": "13.1.2",
100100
"qs": "6.10.3",
101101
"rimraf": "^3.0.2",
102-
"rollup": "2.66.0",
102+
"rollup": "2.66.1",
103103
"rollup-plugin-node-externals": "3.1.2",
104104
"rollup-plugin-terser": "7.0.2",
105105
"rxjs": "7.5.2",

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const handlebarsPlugin = () => ({
3636
enumerator: true,
3737
escapeComment: true,
3838
escapeDescription: true,
39+
camelCase: true,
3940
},
4041
});
4142
return `export default ${templateSpec};`;

src/Indent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum Indent {
2+
SPACE_4 = '4',
3+
SPACE_2 = '2',
4+
TAB = 'tab',
5+
}

src/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { HttpClient } from './HttpClient';
2+
import { Indent } from './Indent';
23
import { parse as parseV2 } from './openApi/v2';
34
import { parse as parseV3 } from './openApi/v3';
45
import { getOpenApiSpec } from './utils/getOpenApiSpec';
@@ -9,17 +10,20 @@ import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
910
import { writeClient } from './utils/writeClient';
1011

1112
export { HttpClient } from './HttpClient';
13+
export { Indent } from './Indent';
1214

1315
export type Options = {
1416
input: string | Record<string, any>;
1517
output: string;
1618
httpClient?: HttpClient;
19+
clientName?: string;
1720
useOptions?: boolean;
1821
useUnionTypes?: boolean;
1922
exportCore?: boolean;
2023
exportServices?: boolean;
2124
exportModels?: boolean;
2225
exportSchemas?: boolean;
26+
indent?: Indent;
2327
postfix?: string;
2428
request?: string;
2529
write?: boolean;
@@ -32,30 +36,34 @@ export type Options = {
3236
* @param input The relative location of the OpenAPI spec
3337
* @param output The relative location of the output directory
3438
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
39+
* @param clientName Custom client class name
3540
* @param useOptions Use options or arguments functions
3641
* @param useUnionTypes Use union types instead of enums
37-
* @param exportCore: Generate core client classes
38-
* @param exportServices: Generate services
39-
* @param exportModels: Generate models
40-
* @param exportSchemas: Generate schemas
41-
* @param postfix: Service name postfix
42-
* @param request: Path to custom request file
42+
* @param exportCore Generate core client classes
43+
* @param exportServices Generate services
44+
* @param exportModels Generate models
45+
* @param exportSchemas Generate schemas
46+
* @param indent Indentation options (4, 2 or tab)
47+
* @param postfix Service name postfix
48+
* @param request Path to custom request file
4349
* @param write Write the files to disk (true or false)
4450
*/
45-
export async function generate({
51+
export const generate = async ({
4652
input,
4753
output,
4854
httpClient = HttpClient.FETCH,
55+
clientName,
4956
useOptions = false,
5057
useUnionTypes = false,
5158
exportCore = true,
5259
exportServices = true,
5360
exportModels = true,
5461
exportSchemas = false,
62+
indent = Indent.SPACE_4,
5563
postfix = 'Service',
5664
request,
5765
write = true,
58-
}: Options): Promise<void> {
66+
}: Options): Promise<void> => {
5967
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
6068
const openApiVersion = getOpenApiVersion(openApi);
6169
const templates = registerHandlebarTemplates({
@@ -80,7 +88,9 @@ export async function generate({
8088
exportServices,
8189
exportModels,
8290
exportSchemas,
91+
indent,
8392
postfix,
93+
clientName,
8494
request
8595
);
8696
break;
@@ -101,13 +111,15 @@ export async function generate({
101111
exportServices,
102112
exportModels,
103113
exportSchemas,
114+
indent,
104115
postfix,
116+
clientName,
105117
request
106118
);
107119
break;
108120
}
109121
}
110-
}
122+
};
111123

112124
export default {
113125
HttpClient,

src/openApi/v2/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import { getServiceVersion } from './parser/getServiceVersion';
1010
* all the models, services and schema's we should output.
1111
* @param openApi The OpenAPI spec that we have loaded from disk.
1212
*/
13-
export function parse(openApi: OpenApi): Client {
13+
export const parse = (openApi: OpenApi): Client => {
1414
const version = getServiceVersion(openApi.info.version);
1515
const server = getServer(openApi);
1616
const models = getModels(openApi);
1717
const services = getServices(openApi);
1818

1919
return { version, server, models, services };
20-
}
20+
};

src/openApi/v2/parser/escapeName.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export function escapeName(value: string): string {
1+
export const escapeName = (value: string): string => {
22
if (value) {
33
const validName = /^[a-zA-Z_$][\w$]+$/g.test(value);
44
if (!validName) {
55
return `'${value}'`;
66
}
77
}
88
return value;
9-
}
9+
};

src/openApi/v2/parser/extendEnum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { WithEnumExtension } from '../interfaces/Extensions/WithEnumExtensi
77
* @param enumerators
88
* @param definition
99
*/
10-
export function extendEnum(enumerators: Enum[], definition: WithEnumExtension): Enum[] {
10+
export const extendEnum = (enumerators: Enum[], definition: WithEnumExtension): Enum[] => {
1111
const names = definition['x-enum-varnames'];
1212
const descriptions = definition['x-enum-descriptions'];
1313

@@ -17,4 +17,4 @@ export function extendEnum(enumerators: Enum[], definition: WithEnumExtension):
1717
value: enumerator.value,
1818
type: enumerator.type,
1919
}));
20-
}
20+
};

src/openApi/v2/parser/getEnum.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Enum } from '../../../client/interfaces/Enum';
22
import { isDefined } from '../../../utils/isDefined';
33

4-
export function getEnum(values?: (string | number)[]): Enum[] {
4+
export const getEnum = (values?: (string | number)[]): Enum[] => {
55
if (Array.isArray(values)) {
66
return values
77
.filter((value, index, arr) => {
@@ -30,4 +30,4 @@ export function getEnum(values?: (string | number)[]): Enum[] {
3030
});
3131
}
3232
return [];
33-
}
33+
};

src/openApi/v2/parser/getEnumFromDescription.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Enum } from '../../../client/interfaces/Enum';
33
/**
44
* @deprecated
55
*/
6-
export function getEnumFromDescription(description: string): Enum[] {
6+
export const getEnumFromDescription = (description: string): Enum[] => {
77
// Check if we can find this special format string:
88
// None=0,Something=1,AnotherThing=2
99
if (/^(\w+=[0-9]+)/g.test(description)) {
@@ -36,4 +36,4 @@ export function getEnumFromDescription(description: string): Enum[] {
3636
}
3737

3838
return [];
39-
}
39+
};

src/openApi/v2/parser/getMappedType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ const TYPE_MAPPINGS = new Map<string, string>([
2424
/**
2525
* Get mapped type for given type to any basic Typescript/Javascript type.
2626
*/
27-
export function getMappedType(type: string, format?: string): string | undefined {
27+
export const getMappedType = (type: string, format?: string): string | undefined => {
2828
if (format === 'binary') {
2929
return 'binary';
3030
}
3131
return TYPE_MAPPINGS.get(type);
32-
}
32+
};

src/openApi/v2/parser/getModel.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { getModelComposition } from './getModelComposition';
99
import { getModelProperties } from './getModelProperties';
1010
import { getType } from './getType';
1111

12-
export function getModel(
12+
export const getModel = (
1313
openApi: OpenApi,
1414
definition: OpenApiSchema,
1515
isDefinition: boolean = false,
1616
name: string = ''
17-
): Model {
17+
): Model => {
1818
const model: Model = {
1919
name,
2020
export: 'interface',
@@ -162,4 +162,4 @@ export function getModel(
162162
}
163163

164164
return model;
165-
}
165+
};

src/openApi/v2/parser/getModelComposition.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import { getRequiredPropertiesFromComposition } from './getRequiredPropertiesFro
99
// Fix for circular dependency
1010
export type GetModelFn = typeof getModel;
1111

12-
export function getModelComposition(
12+
export const getModelComposition = (
1313
openApi: OpenApi,
1414
definition: OpenApiSchema,
1515
definitions: OpenApiSchema[],
1616
type: 'one-of' | 'any-of' | 'all-of',
1717
getModel: GetModelFn
18-
): ModelComposition {
18+
): ModelComposition => {
1919
const composition: ModelComposition = {
2020
type,
2121
imports: [],
@@ -87,4 +87,4 @@ export function getModelComposition(
8787
}
8888

8989
return composition;
90-
}
90+
};

0 commit comments

Comments
 (0)