forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriteClient.ts
118 lines (109 loc) · 4.06 KB
/
writeClient.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { resolve } from 'path';
import type { Client } from '../client/interfaces/Client';
import type { HttpClient } from '../HttpClient';
import type { Indent } from '../Indent';
import { mkdir, rmdir } from './fileSystem';
import { isDefined } from './isDefined';
import { isSubDirectory } from './isSubdirectory';
import type { Templates } from './registerHandlebarTemplates';
import { writeClientClass } from './writeClientClass';
import { writeClientCore } from './writeClientCore';
import { writeClientIndex } from './writeClientIndex';
import { writeClientModels } from './writeClientModels';
import { writeClientSchemas } from './writeClientSchemas';
import { writeClientServices } from './writeClientServices';
/**
* Write our OpenAPI client, using the given templates at the given output
* @param client Client object with all the models, services, etc.
* @param templates Templates wrapper with all loaded Handlebars templates
* @param output The relative location of the output directory
* @param httpClient The selected httpClient (fetch, xhr, node or axios)
* @param useOptions Use options or arguments functions
* @param useUnionTypes Use union types instead of enums
* @param useJsonld Use JSON+LD interfaces
* @param exportCore Generate core client classes
* @param exportServices Generate services
* @param exportModels Generate models
* @param exportSchemas Generate schemas
* @param exportSchemas Generate schemas
* @param indent Indentation options (4, 2 or tab)
* @param postfix Service name postfix
* @param clientName Custom client class name
* @param request Path to custom request file
*/
export const writeClient = async (
client: Client,
templates: Templates,
output: string,
httpClient: HttpClient,
useOptions: boolean,
useUnionTypes: boolean,
useJsonld: boolean,
exportCore: boolean,
exportServices: boolean,
exportModels: boolean,
exportSchemas: boolean,
indent: Indent,
postfix: string,
clientName?: string,
request?: string
): Promise<void> => {
const outputPath = resolve(process.cwd(), output);
const outputPathCore = resolve(outputPath, 'core');
const outputPathModels = resolve(outputPath, 'models');
const outputPathSchemas = resolve(outputPath, 'schemas');
const outputPathServices = resolve(outputPath, 'services');
if (!isSubDirectory(process.cwd(), output)) {
throw new Error(`Output folder is not a subdirectory of the current working directory`);
}
if (exportCore) {
await rmdir(outputPathCore);
await mkdir(outputPathCore);
await writeClientCore(client, templates, outputPathCore, httpClient, indent, clientName, request);
}
if (exportServices) {
await rmdir(outputPathServices);
await mkdir(outputPathServices);
await writeClientServices(
client.services,
templates,
outputPathServices,
httpClient,
useUnionTypes,
useJsonld,
useOptions,
indent,
postfix,
clientName
);
}
if (exportSchemas) {
await rmdir(outputPathSchemas);
await mkdir(outputPathSchemas);
await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes, indent);
}
if (exportModels) {
await rmdir(outputPathModels);
await mkdir(outputPathModels);
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes, indent);
}
if (isDefined(clientName)) {
await mkdir(outputPath);
await writeClientClass(client, templates, outputPath, httpClient, clientName, indent, postfix);
}
if (exportCore || exportServices || exportSchemas || exportModels) {
await mkdir(outputPath);
await writeClientIndex(
client,
templates,
outputPath,
useUnionTypes,
exportCore,
exportServices,
exportModels,
exportSchemas,
postfix,
clientName
);
}
};