Skip to content

Commit 8275e01

Browse files
Merge pull request #1 from parsable/feature/PE-2152
Feature/pe 2152
2 parents 715ddce + 9c9fae2 commit 8275e01

Some content is hidden

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

57 files changed

+7326
-185
lines changed

.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+
example

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ $ openapi --help
4747
--exportServices <value> Write services to disk (default: true)
4848
--exportModels <value> Write models to disk (default: true)
4949
--exportSchemas <value> Write schemas to disk (default: false)
50+
--exportClient <value> Generate and write client class to disk (default: false)
51+
--name <value> Custom client class name (default: "AppClient")
5052
5153
Examples
5254
$ openapi --input ./spec.json
@@ -392,6 +394,29 @@ const getToken = async () => {
392394
OpenAPI.TOKEN = getToken;
393395
```
394396

397+
### Generate client instance with `--exportClient` option
398+
399+
The OpenAPI generator allows to create client instances to support the multiple backend services use case.
400+
The generated client uses an instance of the server configuration and not the global `OpenAPI` constant.
401+
402+
To generate a client instance, use `--exportClient` option. To set a custom name to the client class, use `--name` option.
403+
404+
```
405+
openapi --input ./spec.json --output ./dist --exportClient true --name DemoAppClient
406+
```
407+
408+
The generated client will be exported from the `index` file and can be used as shown below:
409+
```typescript
410+
// create the client instance with server and authentication details
411+
const appClient = new DemoAppClient({ BASE: 'http://server-host.com', TOKEN: '1234' });
412+
413+
// use the client instance to make the API call
414+
const res: OrganizationResponse = await appClient.organizations.createOrganization({
415+
name: 'OrgName',
416+
description: 'OrgDescription',
417+
});
418+
```
419+
395420
### References
396421

397422
Local references to schema definitions (those beginning with `#/definitions/schemas/`)

bin/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ const params = 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+
.option('--exportClient <value>', 'Generate and write client class to disk', false)
2223
.option('--request <value>', 'Path to custom request file')
24+
.option('--name <value>', 'Custom client class name', 'AppClient')
2325
.parse(process.argv)
2426
.opts();
2527

@@ -36,6 +38,8 @@ if (OpenAPI) {
3638
exportServices: JSON.parse(params.exportServices) === true,
3739
exportModels: JSON.parse(params.exportModels) === true,
3840
exportSchemas: JSON.parse(params.exportSchemas) === true,
41+
exportClient: JSON.parse(params.exportClient) === true,
42+
clientName: params.name,
3943
request: params.request,
4044
})
4145
.then(() => {

jest.config.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ module.exports = {
55
{
66
displayName: 'UNIT',
77
testEnvironment: 'node',
8-
testMatch: [
9-
'<rootDir>/src/**/*.spec.ts',
10-
'<rootDir>/test/index.spec.js',
11-
],
8+
testMatch: ['<rootDir>/src/**/*.spec.ts', '<rootDir>/test/index.spec.js', '<rootDir>/test/index.client.spec.js'],
129
moduleFileExtensions: ['js', 'ts', 'd.ts'],
1310
moduleNameMapper: {
1411
'\\.hbs$': '<rootDir>/src/templates/__mocks__/index.js',
@@ -29,10 +26,5 @@ module.exports = {
2926
],
3027
},
3128
],
32-
collectCoverageFrom: [
33-
'<rootDir>/src/**/*.ts',
34-
'!<rootDir>/src/**/*.d.ts',
35-
'!<rootDir>/bin',
36-
'!<rootDir>/dist',
37-
],
29+
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!<rootDir>/src/**/*.d.ts', '!<rootDir>/bin', '!<rootDir>/dist'],
3830
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "openapi-typescript-codegen",
3-
"version": "0.9.3",
2+
"name": "@parsable/openapi-typescript-codegen",
3+
"version": "0.0.1-alpha-2",
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",

src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export type Options = {
2020
exportServices?: boolean;
2121
exportModels?: boolean;
2222
exportSchemas?: boolean;
23+
exportClient?: boolean;
2324
request?: string;
25+
clientName?: string;
2426
write?: boolean;
2527
};
2628

@@ -37,6 +39,8 @@ export type Options = {
3739
* @param exportServices: Generate services
3840
* @param exportModels: Generate models
3941
* @param exportSchemas: Generate schemas
42+
* @param exportClient: Generate client class
43+
* @param clientName: Custom client class name
4044
* @param request: Path to custom request file
4145
* @param write Write the files to disk (true or false)
4246
*/
@@ -50,6 +54,8 @@ export async function generate({
5054
exportServices = true,
5155
exportModels = true,
5256
exportSchemas = false,
57+
exportClient = false,
58+
clientName = 'AppClient',
5359
request,
5460
write = true,
5561
}: Options): Promise<void> {
@@ -64,17 +70,17 @@ export async function generate({
6470
switch (openApiVersion) {
6571
case OpenApiVersion.V2: {
6672
const client = parseV2(openApi);
67-
const clientFinal = postProcessClient(client);
73+
const clientFinal = postProcessClient(client, exportClient);
6874
if (!write) break;
69-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
75+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, exportClient, clientName, request);
7076
break;
7177
}
7278

7379
case OpenApiVersion.V3: {
7480
const client = parseV3(openApi);
75-
const clientFinal = postProcessClient(client);
81+
const clientFinal = postProcessClient(client, exportClient);
7682
if (!write) break;
77-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
83+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, exportClient, clientName, request);
7884
break;
7985
}
8086
}

src/openApi/v2/parser/getOperationPath.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { getOperationPath } from './getOperationPath';
22

33
describe('getOperationPath', () => {
44
it('should produce correct result', () => {
5-
expect(getOperationPath('/api/v{api-version}/list/{id}/{type}')).toEqual('/api/v${OpenAPI.VERSION}/list/${id}/${type}');
6-
expect(getOperationPath('/api/v{api-version}/list/{id}')).toEqual('/api/v${OpenAPI.VERSION}/list/${id}');
5+
expect(getOperationPath('/api/v{api-version}/list/{id}/{type}')).toEqual('/api/v${apiVersion}/list/${id}/${type}');
6+
expect(getOperationPath('/api/v{api-version}/list/{id}')).toEqual('/api/v${apiVersion}/list/${id}');
77
expect(getOperationPath('/api/v1/list/{id}')).toEqual('/api/v1/list/${id}');
88
expect(getOperationPath('/api/{foobar}')).toEqual('/api/${foobar}');
99
expect(getOperationPath('/api/{fooBar}')).toEqual('/api/${fooBar}');

src/openApi/v2/parser/getOperationPath.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { getOperationParameterName } from './getOperationParameterName';
88
* @param path
99
*/
1010
export function getOperationPath(path: string): string {
11-
return path
12-
.replace(/\{(.*?)\}/g, (_, w: string) => {
13-
return `\${${getOperationParameterName(w)}}`;
14-
})
15-
.replace('${apiVersion}', '${OpenAPI.VERSION}');
11+
return path.replace(/\{(.*?)\}/g, (_, w: string) => {
12+
return `\${${getOperationParameterName(w)}}`;
13+
});
1614
}

src/openApi/v3/parser/getOperationPath.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { getOperationPath } from './getOperationPath';
22

33
describe('getOperationPath', () => {
44
it('should produce correct result', () => {
5-
expect(getOperationPath('/api/v{api-version}/list/{id}/{type}')).toEqual('/api/v${OpenAPI.VERSION}/list/${id}/${type}');
6-
expect(getOperationPath('/api/v{api-version}/list/{id}')).toEqual('/api/v${OpenAPI.VERSION}/list/${id}');
5+
expect(getOperationPath('/api/v{api-version}/list/{id}/{type}')).toEqual('/api/v${apiVersion}/list/${id}/${type}');
6+
expect(getOperationPath('/api/v{api-version}/list/{id}')).toEqual('/api/v${apiVersion}/list/${id}');
77
expect(getOperationPath('/api/v1/list/{id}')).toEqual('/api/v1/list/${id}');
88
expect(getOperationPath('/api/{foobar}')).toEqual('/api/${foobar}');
99
expect(getOperationPath('/api/{fooBar}')).toEqual('/api/${fooBar}');

src/openApi/v3/parser/getOperationPath.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { getOperationParameterName } from './getOperationParameterName';
88
* @param path
99
*/
1010
export function getOperationPath(path: string): string {
11-
return path
12-
.replace(/\{(.*?)\}/g, (_, w: string) => {
13-
return `\${${getOperationParameterName(w)}}`;
14-
})
15-
.replace('${apiVersion}', '${OpenAPI.VERSION}');
11+
return path.replace(/\{(.*?)\}/g, (_, w: string) => {
12+
return `\${${getOperationParameterName(w)}}`;
13+
});
1614
}

0 commit comments

Comments
 (0)