Skip to content

Commit 7305dfd

Browse files
mb21alexjennings
authored andcommitted
make it an option
1 parent f402eac commit 7305dfd

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

@@ -62,6 +65,7 @@ $ openapi --help
6265
--postfixServices Service name postfix (default: "Service")
6366
--postfixModels Model name postfix
6467
--request <value> Path to custom request file
68+
--serviceTemplate Path to custom service handlebars template to generate the service files
6569
-h, --help display help for command
6670
6771
Examples

bin/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const params = program
2525
.option('--postfixServices <value>', 'Service name postfix', 'Service')
2626
.option('--postfixModels <value>', 'Model name postfix')
2727
.option('--request <value>', 'Path to custom request file')
28+
.option('--serviceTemplate <value>', 'Path to custom service handlebars template to generate the service files')
2829
.parse(process.argv)
2930
.opts();
3031

@@ -46,6 +47,7 @@ if (OpenAPI) {
4647
postfixServices: params.postfixServices ?? params.postfix,
4748
postfixModels: params.postfixModels,
4849
request: params.request,
50+
serviceTemplate: params.serviceTemplate,
4951
})
5052
.then(() => {
5153
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

@@ -28,6 +28,7 @@ export type Options = {
2828
postfixServices?: string;
2929
postfixModels?: string;
3030
request?: string;
31+
serviceTemplate?: string;
3132
write?: boolean;
3233
};
3334

@@ -66,6 +67,7 @@ export const generate = async ({
6667
postfixServices = 'Service',
6768
postfixModels = '',
6869
request,
70+
serviceTemplate,
6971
write = true,
7072
}: Options): Promise<void> => {
7173
const openApi = isString(input) ? await getOpenApiSpec(input) : input;
@@ -76,10 +78,15 @@ export const generate = async ({
7678
useOptions,
7779
});
7880

81+
if (serviceTemplate) {
82+
exportServices = false;
83+
}
84+
85+
let clientFinal;
7986
switch (openApiVersion) {
8087
case OpenApiVersion.V2: {
8188
const client = parseV2(openApi);
82-
const clientFinal = postProcessClient(client);
89+
clientFinal = postProcessClient(client);
8390
if (!write) break;
8491
await writeClient(
8592
clientFinal,
@@ -103,7 +110,7 @@ export const generate = async ({
103110

104111
case OpenApiVersion.V3: {
105112
const client = parseV3(openApi);
106-
const clientFinal = postProcessClient(client);
113+
clientFinal = postProcessClient(client);
107114
if (!write) break;
108115
await writeClient(
109116
clientFinal,
@@ -125,6 +132,19 @@ export const generate = async ({
125132
break;
126133
}
127134
}
135+
136+
if (serviceTemplate) {
137+
await writeClientServicesCustomTemplate(
138+
clientFinal,
139+
output,
140+
httpClient,
141+
useOptions,
142+
useUnionTypes,
143+
indent,
144+
postfix,
145+
serviceTemplate
146+
);
147+
}
128148
};
129149

130150
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)