Skip to content

Commit 63fc673

Browse files
author
Zakhar Dolozhevskiy
committed
feat: custom templates implementation DAS-224
1 parent 478aa2a commit 63fc673

10 files changed

+246
-164
lines changed

.prettierrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"singleQuote": true,
44
"trailingComma": "es5",
55
"arrowParens": "avoid",
6-
"printWidth": 200,
6+
"printWidth": 120,
77
"tabWidth": 4
88
}

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ 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]', 'fetch')
16+
.option('-t --templates <value>', 'Path to custom templates directory')
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)
@@ -34,6 +35,7 @@ if (OpenAPI) {
3435
exportServices: JSON.parse(program.exportServices) === true,
3536
exportModels: JSON.parse(program.exportModels) === true,
3637
exportSchemas: JSON.parse(program.exportSchemas) === true,
38+
customTemplatesPath: program.templates,
3739
})
3840
.then(() => {
3941
process.exit(0);

package.json

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"name": "openapi-typescript-codegen",
2+
"name": "dh-api-client-codegen",
33
"version": "0.5.4",
44
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
5-
"author": "Ferdi Koomen",
6-
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",
5+
"author": "DoclerLabs",
6+
"homepage": "https://github.com/DoclerLabs/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/DoclerLabs/openapi-typescript-codegen.git"
1010
},
1111
"bugs": {
12-
"url": "https://github.com/ferdikoomen/openapi-typescript-codegen/issues"
12+
"url": "https://github.com/DoclerLabs/openapi-typescript-codegen/issues"
1313
},
1414
"license": "MIT",
1515
"keywords": [
@@ -28,8 +28,8 @@
2828
],
2929
"maintainers": [
3030
{
31-
"name": "Ferdi Koomen",
32-
"email": "info@madebyferdi.com"
31+
"name": "DoclerLabs",
32+
"email": "info@DoclerLabs.com"
3333
}
3434
],
3535
"main": "dist/index.js",
@@ -64,6 +64,7 @@
6464
"dependencies": {
6565
"camelcase": "6.1.0",
6666
"commander": "6.2.0",
67+
"fs-extra": "^9.0.1",
6768
"handlebars": "4.7.6",
6869
"js-yaml": "3.14.0",
6970
"mkdirp": "1.0.4",
@@ -98,6 +99,7 @@
9899
"prettier": "2.1.2",
99100
"puppeteer": "5.4.1",
100101
"rollup": "2.32.1",
102+
"rollup-plugin-copy": "^3.3.0",
101103
"rollup-plugin-terser": "7.0.2",
102104
"rollup-plugin-typescript2": "0.28.0",
103105
"typescript": "4.0.5"

rollup.config.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const copy = require('rollup-plugin-copy');
34
const commonjs = require('@rollup/plugin-commonjs');
45
const { nodeResolve } = require('@rollup/plugin-node-resolve');
56
const { terser } = require('rollup-plugin-terser');
@@ -23,7 +24,7 @@ const handlebarsPlugin = () => ({
2324
}
2425
return null;
2526
},
26-
load: (file) => {
27+
load: file => {
2728
if (path.extname(file) === '.hbs') {
2829
const template = fs.readFileSync(file, 'utf8').toString().trim();
2930
const templateSpec = handlebars.precompile(template, {
@@ -39,7 +40,7 @@ const handlebarsPlugin = () => ({
3940
return `export default ${templateSpec};`;
4041
}
4142
return null;
42-
}
43+
},
4344
});
4445

4546
const getPlugins = () => {
@@ -48,27 +49,20 @@ const getPlugins = () => {
4849
typescript(),
4950
nodeResolve(),
5051
commonjs(),
51-
]
52+
copy({ targets: [{ src: 'src/templates/', dest: 'dist' }] }),
53+
];
5254
if (process.env.NODE_ENV === 'development') {
5355
return plugins;
5456
}
5557
return [...plugins, terser()];
56-
}
58+
};
5759

5860
module.exports = {
5961
input: './src/index.ts',
6062
output: {
6163
file: './dist/index.js',
6264
format: 'cjs',
6365
},
64-
external: [
65-
'fs',
66-
'os',
67-
'util',
68-
'http',
69-
'https',
70-
'handlebars/runtime',
71-
...external,
72-
],
66+
external: ['fs', 'os', 'util', 'http', 'https', 'handlebars/runtime', ...external],
7367
plugins: getPlugins(),
7468
};

src/index.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface Options {
2424
exportModels?: boolean;
2525
exportSchemas?: boolean;
2626
write?: boolean;
27+
customTemplatesPath?: string;
2728
}
2829

2930
/**
@@ -40,6 +41,7 @@ export interface Options {
4041
* @param exportModels: Generate models
4142
* @param exportSchemas: Generate schemas
4243
* @param write Write the files to disk (true or false)
44+
* @param customTemplatesPath: Provide custom templates to override default set
4345
*/
4446
export async function generate({
4547
input,
@@ -52,25 +54,48 @@ export async function generate({
5254
exportModels = true,
5355
exportSchemas = false,
5456
write = true,
57+
customTemplatesPath,
5558
}: Options): Promise<void> {
5659
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
5760
const openApiVersion = getOpenApiVersion(openApi);
58-
const templates = registerHandlebarTemplates();
61+
const templates = registerHandlebarTemplates(customTemplatesPath);
5962

6063
switch (openApiVersion) {
6164
case OpenApiVersion.V2: {
6265
const client = parseV2(openApi);
6366
const clientFinal = postProcessClient(client);
6467
if (!write) break;
65-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
68+
await writeClient(
69+
clientFinal,
70+
templates,
71+
output,
72+
httpClient,
73+
useOptions,
74+
useUnionTypes,
75+
exportCore,
76+
exportServices,
77+
exportModels,
78+
exportSchemas
79+
);
6680
break;
6781
}
6882

6983
case OpenApiVersion.V3: {
7084
const client = parseV3(openApi);
7185
const clientFinal = postProcessClient(client);
7286
if (!write) break;
73-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
87+
await writeClient(
88+
clientFinal,
89+
templates,
90+
output,
91+
httpClient,
92+
useOptions,
93+
useUnionTypes,
94+
exportCore,
95+
exportServices,
96+
exportModels,
97+
exportSchemas
98+
);
7499
break;
75100
}
76101
}

src/utils/registerHandlebarHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @ts-nocheck
22

3-
import * as Handlebars from 'handlebars/runtime';
3+
import * as Handlebars from 'handlebars';
44

55
export function registerHandlebarHelpers(): void {
66
Handlebars.registerHelper('equals', function (a: string, b: string, options: Handlebars.HelperOptions): string {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import * as Handlebars from 'handlebars';
2+
import { resolveHandlebarTemplate } from './resolveHandlebarTemplate';
3+
4+
export const registerHandlebarPartials = () => {
5+
// Partials for the generations of the models, services, etc.
6+
Handlebars.registerPartial('exportEnum', resolveHandlebarTemplate('partials/exportEnum'));
7+
Handlebars.registerPartial('exportInterface', resolveHandlebarTemplate('partials/exportInterface'));
8+
Handlebars.registerPartial('exportType', resolveHandlebarTemplate('partials/exportType'));
9+
Handlebars.registerPartial('extends', resolveHandlebarTemplate('partials/extends'));
10+
Handlebars.registerPartial('header', resolveHandlebarTemplate('partials/header'));
11+
Handlebars.registerPartial('isNullable', resolveHandlebarTemplate('partials/isNullable'));
12+
Handlebars.registerPartial('isReadOnly', resolveHandlebarTemplate('partials/isReadOnly'));
13+
Handlebars.registerPartial('isRequired', resolveHandlebarTemplate('partials/isRequired'));
14+
Handlebars.registerPartial('parameters', resolveHandlebarTemplate('partials/parameters'));
15+
Handlebars.registerPartial('result', resolveHandlebarTemplate('partials/result'));
16+
Handlebars.registerPartial('schema', resolveHandlebarTemplate('partials/schema'));
17+
Handlebars.registerPartial('schemaArray', resolveHandlebarTemplate('partials/schemaArray'));
18+
Handlebars.registerPartial('schemaDictionary', resolveHandlebarTemplate('partials/schemaDictionary'));
19+
Handlebars.registerPartial('schemaEnum', resolveHandlebarTemplate('partials/schemaEnum'));
20+
Handlebars.registerPartial('schemaGeneric', resolveHandlebarTemplate('partials/schemaGeneric'));
21+
Handlebars.registerPartial('schemaInterface', resolveHandlebarTemplate('partials/schemaInterface'));
22+
Handlebars.registerPartial('type', resolveHandlebarTemplate('partials/type'));
23+
Handlebars.registerPartial('typeArray', resolveHandlebarTemplate('partials/typeArray'));
24+
Handlebars.registerPartial('typeDictionary', resolveHandlebarTemplate('partials/typeDictionary'));
25+
Handlebars.registerPartial('typeEnum', resolveHandlebarTemplate('partials/typeEnum'));
26+
Handlebars.registerPartial('typeGeneric', resolveHandlebarTemplate('partials/typeGeneric'));
27+
Handlebars.registerPartial('typeInterface', resolveHandlebarTemplate('partials/typeInterface'));
28+
Handlebars.registerPartial('typeReference', resolveHandlebarTemplate('partials/typeReference'));
29+
Handlebars.registerPartial('base', resolveHandlebarTemplate('partials/base'));
30+
31+
// Generic functions used in 'request' file @see src/templates/core/request.hbs for more info
32+
Handlebars.registerPartial('functions/catchErrors', resolveHandlebarTemplate('core/functions/catchErrors'));
33+
Handlebars.registerPartial('functions/getFormData', resolveHandlebarTemplate('core/functions/getFormData'));
34+
Handlebars.registerPartial('functions/getToken', resolveHandlebarTemplate('core/functions/getToken'));
35+
Handlebars.registerPartial('functions/getQueryString', resolveHandlebarTemplate('core/functions/getQueryString'));
36+
Handlebars.registerPartial('functions/getUrl', resolveHandlebarTemplate('core/functions/getUrl'));
37+
Handlebars.registerPartial('functions/isBinary', resolveHandlebarTemplate('core/functions/isBinary'));
38+
Handlebars.registerPartial('functions/isBlob', resolveHandlebarTemplate('core/functions/isBlob'));
39+
Handlebars.registerPartial('functions/isDefined', resolveHandlebarTemplate('core/functions/isDefined'));
40+
Handlebars.registerPartial('functions/isString', resolveHandlebarTemplate('core/functions/isString'));
41+
Handlebars.registerPartial('functions/isSuccess', resolveHandlebarTemplate('core/functions/isSuccess'));
42+
43+
// Specific files for the fetch client implementation
44+
Handlebars.registerPartial('fetch/getHeaders', resolveHandlebarTemplate('core/fetch/getHeaders'));
45+
Handlebars.registerPartial('fetch/getRequestBody', resolveHandlebarTemplate('core/fetch/getRequestBody'));
46+
Handlebars.registerPartial('fetch/getResponseBody', resolveHandlebarTemplate('core/fetch/getResponseBody'));
47+
Handlebars.registerPartial('fetch/getResponseHeader', resolveHandlebarTemplate('core/fetch/getResponseHeader'));
48+
Handlebars.registerPartial('fetch/sendRequest', resolveHandlebarTemplate('core/fetch/sendRequest'));
49+
Handlebars.registerPartial('fetch/request', resolveHandlebarTemplate('core/fetch/request'));
50+
51+
// Specific files for the xhr client implementation
52+
Handlebars.registerPartial('xhr/getHeaders', resolveHandlebarTemplate('core/xhr/getHeaders'));
53+
Handlebars.registerPartial('xhr/getRequestBody', resolveHandlebarTemplate('core/xhr/getRequestBody'));
54+
Handlebars.registerPartial('xhr/getResponseBody', resolveHandlebarTemplate('core/xhr/getResponseBody'));
55+
Handlebars.registerPartial('xhr/getResponseHeader', resolveHandlebarTemplate('core/xhr/getResponseHeader'));
56+
Handlebars.registerPartial('xhr/sendRequest', resolveHandlebarTemplate('core/xhr/sendRequest'));
57+
Handlebars.registerPartial('xhr/request', resolveHandlebarTemplate('core/xhr/request'));
58+
59+
// Specific files for the node client implementation
60+
Handlebars.registerPartial('node/getHeaders', resolveHandlebarTemplate('core/node/getHeaders'));
61+
Handlebars.registerPartial('node/getRequestBody', resolveHandlebarTemplate('core/node/getRequestBody'));
62+
Handlebars.registerPartial('node/getResponseBody', resolveHandlebarTemplate('core/node/getResponseBody'));
63+
Handlebars.registerPartial('node/getResponseHeader', resolveHandlebarTemplate('core/node/getResponseHeader'));
64+
Handlebars.registerPartial('node/sendRequest', resolveHandlebarTemplate('core/node/sendRequest'));
65+
Handlebars.registerPartial('node/request', resolveHandlebarTemplate('core/node/request'));
66+
};

0 commit comments

Comments
 (0)