Skip to content

Commit 7022183

Browse files
committed
add generateCustom function
1 parent a401aa1 commit 7022183

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

README.md

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

3+
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:
5+
6+
```js
7+
require('openapi-typescript-codegen').generateCustom('api.yaml', 'outputDir/', 'serviceTemplate.hbs')
8+
```
9+
10+
To release a new version, run `npm run release` and push everything, including the `dist/index.js` file to GitHub.
11+
12+
---
13+
314
[![NPM][npm-image]][npm-url]
415
[![License][license-image]][license-url]
516
[![Coverage][coverage-image]][coverage-url]

src/generateCustom.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { mkdir, readFile, remove } from 'fs-extra';
2+
import Handlebars from 'handlebars';
3+
import { resolve } from 'path';
4+
5+
import { HttpClient } from './HttpClient';
6+
import { Indent } from './Indent';
7+
import { parse as parseV3 } from './openApi/v3';
8+
import { writeFile } from './utils/fileSystem';
9+
import { formatCode } from './utils/formatCode';
10+
import { formatIndentation } from './utils/formatIndentation';
11+
import { getOpenApiSpec } from './utils/getOpenApiSpec';
12+
import { postProcessClient } from './utils/postProcessClient';
13+
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
14+
import { writeClient } from './utils/writeClient';
15+
16+
export const generateCustom = async (inputPath?: string, outputPath?: string, templatePath?: string) => {
17+
if (!inputPath) throw Error('No inputPath provided');
18+
if (!outputPath) throw Error('No outputPath provided');
19+
if (!templatePath) throw Error('No templatePath provided');
20+
21+
const httpClient = HttpClient.FETCH;
22+
const useUnionTypes = true;
23+
const useOptions = false;
24+
const exportCore = true;
25+
const exportServices = false;
26+
const exportModels = true;
27+
const exportSchemas = false;
28+
const indent = Indent.SPACE_2;
29+
const postfix = 'Service';
30+
31+
// Generate core, models (this is mostly copied from index.ts):
32+
33+
const openApi = await getOpenApiSpec(inputPath);
34+
const templates = registerHandlebarTemplates({
35+
httpClient,
36+
useUnionTypes,
37+
useOptions,
38+
});
39+
40+
const client = parseV3(openApi);
41+
const clientFinal = postProcessClient(client);
42+
43+
await writeClient(
44+
clientFinal,
45+
templates,
46+
outputPath,
47+
httpClient,
48+
useOptions,
49+
useUnionTypes,
50+
exportCore,
51+
exportServices,
52+
exportModels,
53+
exportSchemas,
54+
indent,
55+
postfix
56+
);
57+
58+
// Generate services (this is custom):
59+
60+
registerHandlebarTemplates({
61+
httpClient,
62+
useUnionTypes,
63+
useOptions,
64+
handlebars: Handlebars, // since we're not using precompiled templates, we need a different object here
65+
});
66+
Handlebars.registerHelper('capitalize', str => {
67+
return str.charAt(0).toUpperCase() + str.slice(1);
68+
});
69+
70+
const serviceTemplate = Handlebars.compile(await readFile(templatePath, 'utf8'));
71+
72+
const servicesDir = resolve(outputPath, 'services');
73+
await remove(servicesDir);
74+
await mkdir(servicesDir);
75+
76+
for (const service of clientFinal.services) {
77+
const file = resolve(outputPath, `services/${service.name}${postfix}.ts`);
78+
const templateResult = serviceTemplate({
79+
...service,
80+
serviceBaseUrl: clientFinal.server,
81+
httpClient,
82+
useUnionTypes,
83+
useOptions,
84+
postfix,
85+
});
86+
await writeFile(file, formatIndentation(formatCode(templateResult), indent));
87+
}
88+
};

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { postProcessClient } from './utils/postProcessClient';
99
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
1010
import { writeClient } from './utils/writeClient';
1111

12+
export { generateCustom } from './generateCustom';
1213
export { HttpClient } from './HttpClient';
1314
export { Indent } from './Indent';
1415

src/utils/registerHandlebarHelpers.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import camelCase from 'camelcase';
2-
import Handlebars from 'handlebars/runtime';
2+
import HandlebarsRuntime from 'handlebars/runtime';
33
import { EOL } from 'os';
44

55
import type { Enum } from '../client/interfaces/Enum';
@@ -11,7 +11,10 @@ export const registerHandlebarHelpers = (root: {
1111
httpClient: HttpClient;
1212
useOptions: boolean;
1313
useUnionTypes: boolean;
14+
handlebars?: typeof HandlebarsRuntime;
1415
}): void => {
16+
const Handlebars = root.handlebars || HandlebarsRuntime;
17+
1518
Handlebars.registerHelper('ifdef', function (this: any, ...args): string {
1619
const options = args.pop();
1720
if (!args.every(value => !value)) {

src/utils/registerHandlebarTemplates.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Handlebars from 'handlebars/runtime';
1+
import HandlebarsRuntime from 'handlebars/runtime';
22

33
import { HttpClient } from '../HttpClient';
44
import templateClient from '../templates/client.hbs';
@@ -113,7 +113,10 @@ export const registerHandlebarTemplates = (root: {
113113
httpClient: HttpClient;
114114
useOptions: boolean;
115115
useUnionTypes: boolean;
116+
handlebars?: typeof HandlebarsRuntime;
116117
}): Templates => {
118+
const Handlebars = root.handlebars || HandlebarsRuntime;
119+
117120
registerHandlebarHelpers(root);
118121

119122
// Main templates (entry points for the files we write to disk)

0 commit comments

Comments
 (0)