forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriteClientServices.ts
More file actions
47 lines (45 loc) · 1.67 KB
/
Copy pathwriteClientServices.ts
File metadata and controls
47 lines (45 loc) · 1.67 KB
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
38
39
40
41
42
43
44
45
46
47
import { resolve } from 'path';
import type { Service } from '../client/interfaces/Service';
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 { isDefined } from './isDefined';
import type { Templates } from './registerHandlebarTemplates';
/**
* Generate Services using the Handlebar template and write to disk.
* @param services Array of Services 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 useOptions Use options or arguments functions
* @param indent Indentation options (4, 2 or tab)
* @param postfix Service name postfix
* @param clientName Custom client class name
*/
export const writeClientServices = async (
services: Service[],
templates: Templates,
outputPath: string,
httpClient: HttpClient,
useUnionTypes: boolean,
useOptions: boolean,
indent: Indent,
postfix: string,
clientName?: string
): Promise<void> => {
for (const service of services) {
const file = resolve(outputPath, `${service.name}${postfix}.ts`);
const templateResult = templates.exports.service({
...service,
httpClient,
useUnionTypes,
useOptions,
postfix,
exportClient: isDefined(clientName),
});
await writeFile(file, i(f(templateResult), indent));
}
};