Skip to content

Commit b1830dd

Browse files
committed
First part of changes
1 parent ec16d8d commit b1830dd

File tree

7 files changed

+109
-27
lines changed

7 files changed

+109
-27
lines changed

.gitignore

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
node_modules
1+
# Package Management related stuff
2+
dist/
3+
node_modules/
4+
package-lock.json
5+
6+
# IDE folder
7+
.idea/
8+
.vscode/
9+
210
npm-debug.log*
311
yarn-debug.log*
412
yarn-error.log*
513
junit.xml
614
.DS_Store
715
.tmp
8-
.idea
9-
.vscode
1016
*.iml
11-
dist
1217
coverage
1318
test/generated
1419
test/e2e/generated

src/index.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { isString } from './utils/isString';
77
import { postProcessClient } from './utils/postProcessClient';
88
import { registerHandlebarTemplates } from './utils/registerHandlebarTemplates';
99
import { writeClient } from './utils/writeClient';
10+
import { Client } from "./client/interfaces/Client";
11+
import { OpenApi as OpenApiV3 } from "./openApi/v3/interfaces/OpenApi";
12+
import { OpenApi as OpenApiV2 } from "./openApi/v2/interfaces/OpenApi";
1013

1114
export { HttpClient } from './HttpClient';
1215

@@ -61,21 +64,25 @@ export async function generate({
6164
useOptions,
6265
});
6366

67+
const clientFinal = getClient(openApiVersion, openApi);
68+
if (write) {
69+
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
70+
}
71+
}
72+
73+
function getClient (openApiVersion: OpenApiVersion, openApi: OpenApiV3 | OpenApiV2): Client {
74+
let parsed_client = null;
75+
6476
switch (openApiVersion) {
65-
case OpenApiVersion.V2: {
66-
const client = parseV2(openApi);
67-
const clientFinal = postProcessClient(client);
68-
if (!write) break;
69-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
77+
case OpenApiVersion.V2:
78+
case OpenApiVersion.V3:
79+
//@ts-ignore
80+
parsed_client = parseV3(openApi);
7081
break;
71-
}
7282

73-
case OpenApiVersion.V3: {
74-
const client = parseV3(openApi);
75-
const clientFinal = postProcessClient(client);
76-
if (!write) break;
77-
await writeClient(clientFinal, templates, output, httpClient, useOptions, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas, request);
78-
break;
79-
}
83+
default:
84+
throw new Error('Invalid API number');
8085
}
86+
87+
return postProcessClient(parsed_client);
8188
}

src/templates/exportController.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hello world');

src/utils/registerHandlebarTemplates.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import xhrGetResponseBody from '../templates/core/xhr/getResponseBody.hbs';
3535
import xhrGetResponseHeader from '../templates/core/xhr/getResponseHeader.hbs';
3636
import xhrRequest from '../templates/core/xhr/request.hbs';
3737
import xhrSendRequest from '../templates/core/xhr/sendRequest.hbs';
38+
import templateExportController from '../templates/exportController.hbs';
3839
import templateExportModel from '../templates/exportModel.hbs';
3940
import templateExportSchema from '../templates/exportSchema.hbs';
4041
import templateExportService from '../templates/exportService.hbs';
@@ -74,6 +75,7 @@ export interface Templates {
7475
model: Handlebars.TemplateDelegate;
7576
schema: Handlebars.TemplateDelegate;
7677
service: Handlebars.TemplateDelegate;
78+
controllers: Handlebars.TemplateDelegate;
7779
};
7880
core: {
7981
settings: Handlebars.TemplateDelegate;
@@ -98,6 +100,7 @@ export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOp
98100
model: Handlebars.template(templateExportModel),
99101
schema: Handlebars.template(templateExportSchema),
100102
service: Handlebars.template(templateExportService),
103+
controllers: Handlebars.template(templateExportController),
101104
},
102105
core: {
103106
settings: Handlebars.template(templateCoreSettings),
@@ -108,6 +111,16 @@ export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOp
108111
},
109112
};
110113

114+
d();
115+
c();
116+
b();
117+
a();
118+
register_node_client();
119+
120+
return templates;
121+
}
122+
123+
function d( ) {
111124
// Partials for the generations of the models, services, etc.
112125
Handlebars.registerPartial('exportEnum', Handlebars.template(partialExportEnum));
113126
Handlebars.registerPartial('exportInterface', Handlebars.template(partialExportInterface));
@@ -136,7 +149,9 @@ export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOp
136149
Handlebars.registerPartial('typeUnion', Handlebars.template(partialTypeUnion));
137150
Handlebars.registerPartial('typeIntersection', Handlebars.template(partialTypeIntersection));
138151
Handlebars.registerPartial('base', Handlebars.template(partialBase));
152+
}
139153

154+
function c () {
140155
// Generic functions used in 'request' file @see src/templates/core/request.hbs for more info
141156
Handlebars.registerPartial('functions/catchErrors', Handlebars.template(functionCatchErrors));
142157
Handlebars.registerPartial('functions/getFormData', Handlebars.template(functionGetFormData));
@@ -149,30 +164,39 @@ export function registerHandlebarTemplates(root: { httpClient: HttpClient; useOp
149164
Handlebars.registerPartial('functions/isStringWithValue', Handlebars.template(functionIsStringWithValue));
150165
Handlebars.registerPartial('functions/isSuccess', Handlebars.template(functionIsSuccess));
151166
Handlebars.registerPartial('functions/resolve', Handlebars.template(functionResolve));
167+
}
152168

169+
function b () {
153170
// Specific files for the fetch client implementation
171+
154172
Handlebars.registerPartial('fetch/getHeaders', Handlebars.template(fetchGetHeaders));
155173
Handlebars.registerPartial('fetch/getRequestBody', Handlebars.template(fetchGetRequestBody));
156174
Handlebars.registerPartial('fetch/getResponseBody', Handlebars.template(fetchGetResponseBody));
157175
Handlebars.registerPartial('fetch/getResponseHeader', Handlebars.template(fetchGetResponseHeader));
158176
Handlebars.registerPartial('fetch/sendRequest', Handlebars.template(fetchSendRequest));
159177
Handlebars.registerPartial('fetch/request', Handlebars.template(fetchRequest));
160178

179+
180+
}
181+
182+
function a () {
161183
// Specific files for the xhr client implementation
162184
Handlebars.registerPartial('xhr/getHeaders', Handlebars.template(xhrGetHeaders));
163185
Handlebars.registerPartial('xhr/getRequestBody', Handlebars.template(xhrGetRequestBody));
164186
Handlebars.registerPartial('xhr/getResponseBody', Handlebars.template(xhrGetResponseBody));
165187
Handlebars.registerPartial('xhr/getResponseHeader', Handlebars.template(xhrGetResponseHeader));
166188
Handlebars.registerPartial('xhr/sendRequest', Handlebars.template(xhrSendRequest));
167189
Handlebars.registerPartial('xhr/request', Handlebars.template(xhrRequest));
190+
}
168191

169-
// Specific files for the node client implementation
192+
/**
193+
* Specific files for the node client implementation
194+
*/
195+
function register_node_client () {
170196
Handlebars.registerPartial('node/getHeaders', Handlebars.template(nodeGetHeaders));
171197
Handlebars.registerPartial('node/getRequestBody', Handlebars.template(nodeGetRequestBody));
172198
Handlebars.registerPartial('node/getResponseBody', Handlebars.template(nodeGetResponseBody));
173199
Handlebars.registerPartial('node/getResponseHeader', Handlebars.template(nodeGetResponseHeader));
174200
Handlebars.registerPartial('node/sendRequest', Handlebars.template(nodeSendRequest));
175201
Handlebars.registerPartial('node/request', Handlebars.template(nodeRequest));
176-
177-
return templates;
178202
}

src/utils/writeClient.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { writeClientCore } from './writeClientCore';
99
import { writeClientIndex } from './writeClientIndex';
1010
import { writeClientModels } from './writeClientModels';
1111
import { writeClientSchemas } from './writeClientSchemas';
12-
import { writeClientServices } from './writeClientServices';
12+
import { writeBackendControllers, writeClientServices } from './writeClientServices';
1313

1414
/**
1515
* Write our OpenAPI client, using the given templates at the given output
@@ -38,11 +38,15 @@ export async function writeClient(
3838
exportSchemas: boolean,
3939
request?: string
4040
): Promise<void> {
41+
// Todo :: Configurable folder names
4142
const outputPath = resolve(process.cwd(), output);
4243
const outputPathCore = resolve(outputPath, 'core');
4344
const outputPathModels = resolve(outputPath, 'models');
4445
const outputPathSchemas = resolve(outputPath, 'schemas');
4546
const outputPathServices = resolve(outputPath, 'services');
47+
const output_path_controllers = resolve(outputPath, 'controllers');
48+
49+
const promises: Promise<any>[] = [];
4650

4751
if (!isSubDirectory(process.cwd(), output)) {
4852
throw new Error(`Output folder is not a subdirectory of the current working directory`);
@@ -51,29 +55,43 @@ export async function writeClient(
5155
if (exportCore) {
5256
await rmdir(outputPathCore);
5357
await mkdir(outputPathCore);
54-
await writeClientCore(client, templates, outputPathCore, httpClient, request);
58+
const promise = writeClientCore(client, templates, outputPathCore, httpClient, request);
59+
promises.push(promise);
5560
}
5661

5762
if (exportServices) {
5863
await rmdir(outputPathServices);
5964
await mkdir(outputPathServices);
60-
await writeClientServices(client.services, templates, outputPathServices, httpClient, useUnionTypes, useOptions);
65+
const promise = writeClientServices(client.services, templates, outputPathServices, httpClient, useUnionTypes, useOptions);
66+
promises.push(promise);
67+
}
68+
69+
if (true) {
70+
await rmdir(output_path_controllers);
71+
await mkdir(output_path_controllers);
72+
const promise = writeBackendControllers(client.services, templates, output_path_controllers, httpClient, useUnionTypes, useOptions);
73+
promises.push(promise);
6174
}
6275

6376
if (exportSchemas) {
6477
await rmdir(outputPathSchemas);
6578
await mkdir(outputPathSchemas);
66-
await writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes);
79+
const promise = writeClientSchemas(client.models, templates, outputPathSchemas, httpClient, useUnionTypes);
80+
promises.push(promise);
6781
}
6882

6983
if (exportModels) {
7084
await rmdir(outputPathModels);
7185
await mkdir(outputPathModels);
72-
await writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes);
86+
const promise = writeClientModels(client.models, templates, outputPathModels, httpClient, useUnionTypes);
87+
promises.push(promise);
7388
}
7489

7590
if (exportCore || exportServices || exportSchemas || exportModels) {
7691
await mkdir(outputPath);
77-
await writeClientIndex(client, templates, outputPath, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
92+
const promise = writeClientIndex(client, templates, outputPath, useUnionTypes, exportCore, exportServices, exportModels, exportSchemas);
93+
promises.push(promise);
7894
}
95+
96+
await Promise.all(promises);
7997
}

src/utils/writeClientModels.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ import { Templates } from './registerHandlebarTemplates';
1515
* @param useUnionTypes Use union types instead of enums
1616
*/
1717
export async function writeClientModels(models: Model[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean): Promise<void> {
18+
const promises: Promise<any>[] = [];
19+
1820
for (const model of models) {
1921
const file = resolve(outputPath, `${model.name}.ts`);
2022
const templateResult = templates.exports.model({
2123
...model,
2224
httpClient,
2325
useUnionTypes,
2426
});
25-
await writeFile(file, format(templateResult));
27+
28+
const fileWrite = writeFile(file, format(templateResult));
29+
promises.push(fileWrite);
2630
}
31+
32+
await Promise.all(promises);
2733
}

src/utils/writeClientServices.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export async function writeClientServices(services: Service[], templates: Templa
2121
for (const service of services) {
2222
const file = resolve(outputPath, `${service.name}.ts`);
2323
const useVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING));
24+
2425
const templateResult = templates.exports.service({
2526
...service,
2627
httpClient,
@@ -31,3 +32,23 @@ export async function writeClientServices(services: Service[], templates: Templa
3132
await writeFile(file, format(templateResult));
3233
}
3334
}
35+
36+
export async function writeBackendControllers (services: Service[], templates: Templates, outputPath: string, httpClient: HttpClient, useUnionTypes: boolean, useOptions: boolean): Promise<void> {
37+
if (templates.exports.controllers === undefined) {
38+
throw new Error('Controller template is not defined!');
39+
}
40+
41+
for (const service of services) {
42+
const file = resolve(outputPath, `${service.name}.ts`);
43+
const useVersion = service.operations.some(operation => operation.path.includes(VERSION_TEMPLATE_STRING));
44+
45+
const templateResult = templates.exports.controllers({
46+
...service,
47+
httpClient,
48+
useUnionTypes,
49+
useVersion,
50+
useOptions,
51+
});
52+
await writeFile(file, format(templateResult));
53+
}
54+
}

0 commit comments

Comments
 (0)