forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteClientModels.ts
37 lines (35 loc) · 1.29 KB
/
writeClientModels.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { resolve } from 'path';
import type { Model } from '../client/interfaces/Model';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { writeFile } from './fileSystem';
import { formatCode as f } from './formatCode';
import { formatIndentation as i } from './formatIndentation';
import type { Templates } from './registerHandlebarTemplates';
/**
* Generate Models using the Handlebar template and write to disk.
* @param models Array of Models to write
* @param templates The loaded handlebar templates
* @param outputPath Directory to write the generated files to
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
* @param useUnionTypes Use union types instead of enums
* @param indent Indentation options (4, 2 or tab)
*/
export const writeClientModels = async (
models: Model[],
templates: Templates,
outputPath: string,
httpClient: HttpClient,
useUnionTypes: boolean,
indent: Indent
): Promise<void> => {
for (const model of models) {
const file = resolve(outputPath, `${model.name}.ts`);
const templateResult = templates.exports.model({
...model,
httpClient,
useUnionTypes,
});
await writeFile(file, i(f(templateResult), indent));
}
};