/
routeGenerator.ts
255 lines (221 loc) · 9.17 KB
/
routeGenerator.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import * as path from 'path';
import { ExtendedRoutesConfig } from '../cli';
import { Tsoa, TsoaRoute, assertNever } from '@tsoa/runtime';
import { isRefType } from '../utils/internalTypeGuards';
import { convertBracesPathParams, normalisePath } from '../utils/pathUtils';
import { fsExists, fsReadFile } from '../utils/fs';
export abstract class AbstractRouteGenerator<Config extends ExtendedRoutesConfig> {
constructor(
protected readonly metadata: Tsoa.Metadata,
protected readonly options: Config,
) {}
/**
* This is the entrypoint for a generator to create a custom set of routes
*/
public abstract GenerateCustomRoutes(): Promise<void>;
public buildModels(): TsoaRoute.Models {
const models = {} as TsoaRoute.Models;
Object.keys(this.metadata.referenceTypeMap).forEach(name => {
const referenceType = this.metadata.referenceTypeMap[name];
let model: TsoaRoute.ModelSchema;
if (referenceType.dataType === 'refEnum') {
const refEnumModel: TsoaRoute.RefEnumModelSchema = {
dataType: 'refEnum',
enums: referenceType.enums,
};
model = refEnumModel;
} else if (referenceType.dataType === 'refObject') {
const propertySchemaDictionary: TsoaRoute.RefObjectModelSchema['properties'] = {};
referenceType.properties.forEach(property => {
propertySchemaDictionary[property.name] = this.buildPropertySchema(property);
});
const refObjModel: TsoaRoute.RefObjectModelSchema = {
dataType: 'refObject',
properties: propertySchemaDictionary,
};
if (referenceType.additionalProperties) {
refObjModel.additionalProperties = this.buildProperty(referenceType.additionalProperties);
} else if (this.options.noImplicitAdditionalProperties !== 'ignore') {
refObjModel.additionalProperties = false;
} else {
// Since Swagger allows "excess properties" (to use a TypeScript term) by default
refObjModel.additionalProperties = true;
}
model = refObjModel;
} else if (referenceType.dataType === 'refAlias') {
const refType: TsoaRoute.RefTypeAliasModelSchema = {
dataType: 'refAlias',
type: {
...this.buildProperty(referenceType.type),
validators: referenceType.validators,
default: referenceType.default,
},
};
model = refType;
} else {
model = assertNever(referenceType);
}
models[name] = model;
});
return models;
}
protected pathTransformer(path: string): string {
return convertBracesPathParams(path);
}
protected buildContext() {
const authenticationModule = this.options.authenticationModule ? this.getRelativeImportPath(this.options.authenticationModule) : undefined;
const iocModule = this.options.iocModule ? this.getRelativeImportPath(this.options.iocModule) : undefined;
// Left in for backwards compatibility, previously if we're working locally then tsoa runtime code wasn't an importable module but now it is.
const canImportByAlias = true;
const normalisedBasePath = normalisePath(this.options.basePath as string, '/');
return {
authenticationModule,
basePath: normalisedBasePath,
canImportByAlias,
controllers: this.metadata.controllers.map(controller => {
const normalisedControllerPath = this.pathTransformer(normalisePath(controller.path, '/'));
return {
actions: controller.methods.map(method => {
const parameterObjs: { [name: string]: TsoaRoute.ParameterSchema } = {};
method.parameters.forEach(parameter => {
parameterObjs[parameter.parameterName] = this.buildParameterSchema(parameter);
});
const normalisedMethodPath = this.pathTransformer(normalisePath(method.path, '/'));
const normalisedFullPath = normalisePath(`${normalisedBasePath}${normalisedControllerPath}${normalisedMethodPath}`, '/', '', false);
const uploadFilesWithDifferentFieldParameter = method.parameters.filter(parameter => parameter.type.dataType === 'file');
const uploadFilesParameter = method.parameters.find(parameter => parameter.type.dataType === 'array' && parameter.type.elementType.dataType === 'file');
return {
fullPath: normalisedFullPath,
method: method.method.toLowerCase(),
name: method.name,
parameters: parameterObjs,
path: normalisedMethodPath,
uploadFile: uploadFilesWithDifferentFieldParameter.length > 0,
uploadFileName: uploadFilesWithDifferentFieldParameter.map(parameter => ({
name: parameter.name,
})),
uploadFiles: !!uploadFilesParameter,
uploadFilesName: uploadFilesParameter?.name,
security: method.security,
successStatus: method.successStatus ? method.successStatus : 'undefined',
};
}),
modulePath: this.getRelativeImportPath(controller.location),
name: controller.name,
path: normalisedControllerPath,
};
}),
environment: process.env,
iocModule,
minimalSwaggerConfig: { noImplicitAdditionalProperties: this.options.noImplicitAdditionalProperties, bodyCoercion: this.options.bodyCoercion },
models: this.buildModels(),
useFileUploads: this.metadata.controllers.some(controller =>
controller.methods.some(
method =>
!!method.parameters.find(parameter => {
if (parameter.type.dataType === 'file') {
return true;
} else if (parameter.type.dataType === 'array' && parameter.type.elementType.dataType === 'file') {
return true;
}
return false;
}),
),
),
multerOpts: {
limits: {
fileSize: 8388608, // 8mb
},
...this.options.multerOpts,
},
useSecurity: this.metadata.controllers.some(controller => controller.methods.some(method => !!method.security.length)),
esm: this.options.esm,
};
}
protected getRelativeImportPath(fileLocation: string) {
const currentExt = path.extname(fileLocation);
let newExtension = '';
if (this.options.esm) {
switch (currentExt) {
case '.ts':
default:
newExtension = '.js';
break;
case '.mts':
newExtension = '.mjs';
break;
case '.cts':
newExtension = '.cjs';
break;
}
}
fileLocation = fileLocation.replace(/\.(ts|mts|cts)$/, ''); // no ts extension in import
return `./${path.relative(this.options.routesDir, fileLocation).replace(/\\/g, '/')}${newExtension}`;
}
protected buildPropertySchema(source: Tsoa.Property): TsoaRoute.PropertySchema {
const propertySchema = this.buildProperty(source.type);
propertySchema.default = source.default;
propertySchema.required = source.required ? true : undefined;
if (Object.keys(source.validators).length > 0) {
propertySchema.validators = source.validators;
}
return propertySchema;
}
protected buildParameterSchema(source: Tsoa.Parameter): TsoaRoute.ParameterSchema {
const property = this.buildProperty(source.type);
const parameter = {
default: source.default,
in: source.in,
name: source.name,
required: source.required ? true : undefined,
} as TsoaRoute.ParameterSchema;
const parameterSchema = Object.assign(parameter, property);
if (Object.keys(source.validators).length > 0) {
parameterSchema.validators = source.validators;
}
return parameterSchema;
}
protected buildProperty(type: Tsoa.Type): TsoaRoute.PropertySchema {
const schema: TsoaRoute.PropertySchema = {
dataType: type.dataType,
};
if (isRefType(type)) {
schema.dataType = undefined;
schema.ref = type.refName;
}
if (type.dataType === 'array') {
const arrayType = type;
if (isRefType(arrayType.elementType)) {
schema.array = {
dataType: arrayType.elementType.dataType,
ref: arrayType.elementType.refName,
};
} else {
schema.array = this.buildProperty(arrayType.elementType);
}
}
if (type.dataType === 'enum') {
schema.enums = type.enums;
}
if (type.dataType === 'union' || type.dataType === 'intersection') {
schema.subSchemas = type.types.map(type => this.buildProperty(type));
}
if (type.dataType === 'nestedObjectLiteral') {
const objLiteral = type;
schema.nestedProperties = objLiteral.properties.reduce((acc, prop) => {
return { ...acc, [prop.name]: this.buildPropertySchema(prop) };
}, {});
schema.additionalProperties = objLiteral.additionalProperties && this.buildProperty(objLiteral.additionalProperties);
}
return schema;
}
protected async shouldWriteFile(fileName: string, content: string) {
if (this.options.noWriteIfUnchanged) {
if (await fsExists(fileName)) {
const existingContent = (await fsReadFile(fileName)).toString();
return content !== existingContent;
}
}
return true;
}
}