Skip to content

Commit c99380f

Browse files
committed
make it an option
1 parent 7022183 commit c99380f

File tree

5 files changed

+84
-95
lines changed

5 files changed

+84
-95
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# OpenAPI Typescript Codegen
22

33
Fork ([diff](https://github.com/ferdikoomen/openapi-typescript-codegen/compare/master...mb21:openapi-typescript-codegen:generate-custom))
4-
which exports a `generateCustom` function that can be used like:
4+
which adds a `--serviceTemplate` option.
55

6-
```js
7-
require('openapi-typescript-codegen').generateCustom('api.yaml', 'outputDir/', 'serviceTemplate.hbs')
6+
Can be used in another project by adding to `package.json`::
7+
8+
```json
9+
"openapi-typescript-codegen": "https://github.com/mb21/openapi-typescript-codegen.git#generate-custom",
810
```
911

10-
To release a new version, run `npm run release` and push everything, including the `dist/index.js` file to GitHub.
12+
To release a new version, run `npm run release` and push everything, including the `dist/index.js` file to GitHub. Then, in the project using it, delete the
13+
`openapi-typescript-codegen` entry in the `package-lock.json` and run `npm install` to install the new version.
1114

1215
---
1316

@@ -61,6 +64,7 @@ $ openapi --help
6164
--indent <value> Indentation options [4, 2, tab] (default: "4")
6265
--postfix <value> Service name postfix (default: "Service")
6366
--request <value> Path to custom request file
67+
--serviceTemplate Path to custom service handlebars template to generate the service files
6468
-h, --help display help for command
6569
6670
Examples

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const params = program
2323
.option('--indent <value>', 'Indentation options [4, 2, tabs]', '4')
2424
.option('--postfix <value>', 'Service name postfix', 'Service')
2525
.option('--request <value>', 'Path to custom request file')
26+
.option('--serviceTemplate <value>', 'Path to custom service handlebars template to generate the service files')
2627
.parse(process.argv)
2728
.opts();
2829

@@ -43,6 +44,7 @@ if (OpenAPI) {
4344
indent: params.indent,
4445
postfix: params.postfix,
4546
request: params.request,
47+
serviceTemplate: params.serviceTemplate,
4648
})
4749
.then(() => {
4850
process.exit(0);

src/generateCustom.ts

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

src/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { isString } from './utils/isString';
88
import { postProcessClient } from './utils/postProcessClient';
99
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
1010
import { writeClient } from './utils/writeClient';
11+
import { writeClientServicesCustomTemplate } from './utils/writeClientServicesCustomTemplate';
1112

12-
export { generateCustom } from './generateCustom';
1313
export { HttpClient } from './HttpClient';
1414
export { Indent } from './Indent';
1515

@@ -27,6 +27,7 @@ export type Options = {
2727
indent?: Indent;
2828
postfix?: string;
2929
request?: string;
30+
serviceTemplate?: string;
3031
write?: boolean;
3132
};
3233

@@ -63,6 +64,7 @@ export const generate = async ({
6364
indent = Indent.SPACE_4,
6465
postfix = 'Service',
6566
request,
67+
serviceTemplate,
6668
write = true,
6769
}: Options): Promise<void> => {
6870
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
@@ -73,10 +75,15 @@ export const generate = async ({
7375
useOptions,
7476
});
7577

78+
if (serviceTemplate) {
79+
exportServices = false;
80+
}
81+
82+
let clientFinal;
7683
switch (openApiVersion) {
7784
case OpenApiVersion.V2: {
7885
const client = parseV2(openApi);
79-
const clientFinal = postProcessClient(client);
86+
clientFinal = postProcessClient(client);
8087
if (!write) break;
8188
await writeClient(
8289
clientFinal,
@@ -99,7 +106,7 @@ export const generate = async ({
99106

100107
case OpenApiVersion.V3: {
101108
const client = parseV3(openApi);
102-
const clientFinal = postProcessClient(client);
109+
clientFinal = postProcessClient(client);
103110
if (!write) break;
104111
await writeClient(
105112
clientFinal,
@@ -120,6 +127,19 @@ export const generate = async ({
120127
break;
121128
}
122129
}
130+
131+
if (serviceTemplate) {
132+
await writeClientServicesCustomTemplate(
133+
clientFinal,
134+
output,
135+
httpClient,
136+
useOptions,
137+
useUnionTypes,
138+
indent,
139+
postfix,
140+
serviceTemplate
141+
);
142+
}
123143
};
124144

125145
export default {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { mkdir, readFile, remove } from 'fs-extra';
2+
import Handlebars from 'handlebars';
3+
import { resolve } from 'path';
4+
5+
import { Client } from '../client/interfaces/Client';
6+
import { HttpClient } from '../HttpClient';
7+
import { Indent } from '../Indent';
8+
import { writeFile } from './fileSystem';
9+
import { formatCode } from './formatCode';
10+
import { formatIndentation } from './formatIndentation';
11+
import { registerHandlebarTemplates } from './registerHandlebarTemplates';
12+
13+
export const writeClientServicesCustomTemplate = async (
14+
client: Client,
15+
outputPath: string,
16+
httpClient: HttpClient,
17+
useOptions: boolean,
18+
useUnionTypes: boolean,
19+
indent: Indent,
20+
postfix: string,
21+
templatePath: string
22+
) => {
23+
registerHandlebarTemplates({
24+
httpClient,
25+
useUnionTypes,
26+
useOptions,
27+
handlebars: Handlebars, // since we're not using precompiled templates, we need a different object here
28+
});
29+
Handlebars.registerHelper('capitalize', str => {
30+
return str.charAt(0).toUpperCase() + str.slice(1);
31+
});
32+
33+
const serviceTemplate = Handlebars.compile(await readFile(templatePath, 'utf8'));
34+
35+
const servicesDir = resolve(outputPath, 'services');
36+
await remove(servicesDir);
37+
await mkdir(servicesDir);
38+
39+
for (const service of client.services) {
40+
const file = resolve(outputPath, `services/${service.name}${postfix}.ts`);
41+
const templateResult = serviceTemplate({
42+
...service,
43+
serviceBaseUrl: client.server,
44+
httpClient,
45+
useUnionTypes,
46+
useOptions,
47+
postfix,
48+
});
49+
await writeFile(file, formatIndentation(formatCode(templateResult), indent));
50+
}
51+
};

0 commit comments

Comments
 (0)