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
33 lines (30 loc) · 1.37 KB
/
Copy pathwriteClientServices.ts
File metadata and controls
33 lines (30 loc) · 1.37 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
import { resolve } from 'path';
import type { Service } from '../client/interfaces/Service';
import { HttpClient } from '../HttpClient';
import { writeFile } from './fileSystem';
import { format } from './format';
import { Templates } from './registerHandlebarTemplates';
const VERSION_TEMPLATE_STRING = 'OpenAPI.VERSION';
/**
* 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 or node)
* @param useUnionTypes Use union types instead of enums
* @param useOptions Use options or arguments functions
*/
export async function writeClientServices(services: Service[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean): Promise<void> {
for (const service of services) {
const file = resolve(outputPath, `${service.name}.ts`);
const useVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING));
const templateResult = templates.exports.service({
...service,
httpClient,
useUnionTypes,
useVersion,
useOptions,
});
await writeFile(file, format(templateResult));
}
}