@trapi/metadata 0.1.2 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/dist/cache/driver.js +1 -2
  2. package/dist/cache/driver.js.map +1 -1
  3. package/dist/cache/utils.d.ts.map +1 -1
  4. package/dist/cache/utils.js +12 -12
  5. package/dist/cache/utils.js.map +1 -1
  6. package/dist/decorator/index.d.ts +1 -1
  7. package/dist/decorator/index.d.ts.map +1 -1
  8. package/dist/decorator/index.js +1 -3
  9. package/dist/decorator/index.js.map +1 -1
  10. package/dist/decorator/mapper/index.js +1 -2
  11. package/dist/decorator/mapper/index.js.map +1 -1
  12. package/dist/decorator/representation/index.js +2 -4
  13. package/dist/decorator/representation/index.js.map +1 -1
  14. package/dist/decorator/representation/property/utils.js +4 -3
  15. package/dist/decorator/representation/property/utils.js.map +1 -1
  16. package/dist/generator/controller.js +1 -1
  17. package/dist/generator/controller.js.map +1 -1
  18. package/dist/generator/endpoint.js +0 -3
  19. package/dist/generator/endpoint.js.map +1 -1
  20. package/dist/generator/index.d.ts +5 -33
  21. package/dist/generator/index.d.ts.map +1 -1
  22. package/dist/generator/index.js +15 -151
  23. package/dist/generator/index.js.map +1 -1
  24. package/dist/generator/method.js +0 -2
  25. package/dist/generator/method.js.map +1 -1
  26. package/dist/generator/module.d.ts +41 -0
  27. package/dist/generator/module.d.ts.map +1 -0
  28. package/dist/generator/module.js +166 -0
  29. package/dist/generator/module.js.map +1 -0
  30. package/dist/generator/parameter.js +1 -9
  31. package/dist/generator/parameter.js.map +1 -1
  32. package/dist/resolver/type-node.js +28 -61
  33. package/dist/resolver/type-node.js.map +1 -1
  34. package/dist/resolver/utils/initializer.d.ts.map +1 -1
  35. package/dist/resolver/utils/initializer.js +2 -1
  36. package/dist/resolver/utils/initializer.js.map +1 -1
  37. package/dist/resolver/utils/validator.js +2 -1
  38. package/dist/resolver/utils/validator.js.map +1 -1
  39. package/dist/type.d.ts +5 -0
  40. package/dist/type.d.ts.map +1 -1
  41. package/dist/utils/generator.js +1 -1
  42. package/dist/utils/generator.js.map +1 -1
  43. package/dist/utils/js-doc.js +1 -1
  44. package/dist/utils/js-doc.js.map +1 -1
  45. package/package.json +18 -12
  46. package/src/cache/driver.ts +0 -97
  47. package/src/cache/index.ts +0 -10
  48. package/src/cache/type.ts +0 -43
  49. package/src/cache/utils.ts +0 -49
  50. package/src/config/index.ts +0 -8
  51. package/src/config/path.ts +0 -29
  52. package/src/decorator/functions.ts +0 -142
  53. package/src/decorator/index.ts +0 -13
  54. package/src/decorator/mapper/index.ts +0 -153
  55. package/src/decorator/mapper/maps/decorators-express.ts +0 -117
  56. package/src/decorator/mapper/maps/internal.ts +0 -95
  57. package/src/decorator/mapper/maps/typescript-rest.ts +0 -150
  58. package/src/decorator/mapper/utils.ts +0 -88
  59. package/src/decorator/representation/index.ts +0 -82
  60. package/src/decorator/representation/property/utils.ts +0 -146
  61. package/src/decorator/type.ts +0 -235
  62. package/src/decorator/utils/index.ts +0 -9
  63. package/src/decorator/utils/node.ts +0 -59
  64. package/src/decorator/utils/validator.ts +0 -104
  65. package/src/generator/controller.ts +0 -65
  66. package/src/generator/endpoint.ts +0 -212
  67. package/src/generator/index.ts +0 -219
  68. package/src/generator/method.ts +0 -198
  69. package/src/generator/parameter.ts +0 -392
  70. package/src/index.ts +0 -13
  71. package/src/resolver/error.ts +0 -38
  72. package/src/resolver/index.ts +0 -11
  73. package/src/resolver/type-node.ts +0 -1287
  74. package/src/resolver/type.ts +0 -257
  75. package/src/resolver/utils/index.ts +0 -9
  76. package/src/resolver/utils/initializer.ts +0 -81
  77. package/src/resolver/utils/validator.ts +0 -144
  78. package/src/type.ts +0 -185
  79. package/src/utils/generator.ts +0 -42
  80. package/src/utils/index.ts +0 -10
  81. package/src/utils/js-doc.ts +0 -125
  82. package/src/utils/validator.ts +0 -43
  83. package/test/data/library/@decorators-express/decorators.ts +0 -7
  84. package/test/data/library/self/api.ts +0 -37
  85. package/test/data/library/type.ts +0 -138
  86. package/test/data/library/typescript-rest/api.ts +0 -366
  87. package/test/data/library/typescript-rest/decorators/index.ts +0 -10
  88. package/test/data/library/typescript-rest/decorators/methods.ts +0 -34
  89. package/test/data/library/typescript-rest/decorators/parameters.ts +0 -62
  90. package/test/data/library/typescript-rest/decorators/services.ts +0 -47
  91. package/test/data/library/typescript-rest/return-types.ts +0 -94
  92. package/test/data/library/utils.ts +0 -7
  93. package/test/jest.config.js +0 -49
  94. package/test/unit/cache.spec.ts +0 -48
  95. package/test/unit/decorator/mapper/index.spec.ts +0 -83
  96. package/test/unit/decorator/representation/index.spec.ts +0 -102
  97. package/test/unit/decorator/utils/node.spec.ts +0 -47
  98. package/test/unit/library/typescript-rest.spec.ts +0 -342
  99. package/test/unit/resolver/type.spec.ts +0 -134
  100. package/test/unit/utils/generator.spec.ts +0 -41
  101. package/test/unit/utils/js-doc.spec.ts +0 -95
  102. package/tsconfig.build.json +0 -11
  103. package/tsconfig.json +0 -9
  104. package/writable/.gitignore +0 -3
@@ -1,392 +0,0 @@
1
- /*
2
- * Copyright (c) 2021.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */
7
-
8
- import * as ts from 'typescript';
9
- import {RepresentationManager} from "../decorator/representation";
10
- import {Decorator} from "../decorator/type";
11
- import {MetadataGenerator} from './index';
12
- import {TypeNodeResolver} from '../resolver';
13
- import {Resolver} from "../resolver";
14
- import {getInitializerValue} from "../resolver";
15
- import {ArrayParameter, Parameter} from "../type";
16
- import {getNodeDecorators} from "../decorator/utils/node";
17
-
18
- const supportedParameterKeys : Decorator.ParameterServerType[] = [
19
- 'SERVER_CONTEXT',
20
- 'SERVER_PARAMS',
21
- 'SERVER_QUERY',
22
- 'SERVER_FORM',
23
- 'SERVER_BODY',
24
- 'SERVER_HEADERS',
25
- 'SERVER_COOKIES',
26
- 'SERVER_PATH_PARAMS',
27
- 'SERVER_FILES_PARAM'
28
- ];
29
-
30
- export class ParameterGenerator {
31
- constructor(
32
- private readonly parameter: ts.ParameterDeclaration,
33
- private readonly method: string,
34
- private readonly path: string,
35
- private readonly current: MetadataGenerator
36
- ) { }
37
-
38
- public generate(): Parameter{
39
- const decorators = getNodeDecorators(this.parameter);
40
-
41
- for(let i=0; i<supportedParameterKeys.length; i++) {
42
- const representation = this.current.decoratorMapper.match(supportedParameterKeys[i], decorators);
43
- if(typeof representation === 'undefined') {
44
- continue;
45
- }
46
-
47
- switch (supportedParameterKeys[i]) {
48
- case 'SERVER_CONTEXT':
49
- return this.getContextParameter();
50
- case 'SERVER_PARAMS':
51
- return this.getRequestParameter(representation);
52
- case 'SERVER_FORM':
53
- return this.getFormParameter(representation);
54
- case 'SERVER_QUERY':
55
- return this.getQueryParameter(representation);
56
- case 'SERVER_BODY':
57
- return this.getBodyParameter(representation);
58
- case 'SERVER_HEADERS':
59
- return this.getHeaderParameter(representation);
60
- case 'SERVER_COOKIES':
61
- return this.getCookieParameter(representation);
62
- case 'SERVER_PATH_PARAMS':
63
- return this.getPathParameter(representation);
64
- case 'SERVER_FILE_PARAM':
65
- return this.getFileParameter(representation);
66
- case 'SERVER_FILES_PARAM':
67
- return this.getFileParameter(representation, true);
68
- }
69
- }
70
-
71
- return this.getBodyParameter();
72
- }
73
-
74
- private getCurrentLocation() {
75
- const methodId = (this.parameter.parent as ts.MethodDeclaration).name as ts.Identifier;
76
- const controllerId = ((this.parameter.parent as ts.MethodDeclaration).parent as ts.ClassDeclaration).name as ts.Identifier;
77
- return `${controllerId.text}.${methodId.text}`;
78
- }
79
-
80
- private getRequestParameter(representationManager: RepresentationManager<'SERVER_PARAMS'>): Parameter{
81
- const parameterName = (this.parameter.name as ts.Identifier).text;
82
- let name = parameterName;
83
- const type = this.getValidatedType(this.parameter);
84
-
85
- if (!this.supportsBodyParameters(this.method)) {
86
- throw new Error(`Param can't support '${this.getCurrentLocation()}' method.`);
87
- }
88
-
89
- const value = representationManager.getPropertyValue('DEFAULT');
90
- if(typeof value === 'string') {
91
- name = value;
92
- }
93
-
94
-
95
- return {
96
- description: this.getParameterDescription(this.parameter),
97
- in: 'param',
98
- name: name || parameterName,
99
- parameterName: parameterName,
100
- required: !this.parameter.questionToken,
101
- type: type
102
- };
103
- }
104
-
105
- private getContextParameter(): Parameter {
106
- const parameterName = (this.parameter.name as ts.Identifier).text;
107
-
108
- return {
109
- description: this.getParameterDescription(this.parameter),
110
- in: 'context',
111
- name: parameterName,
112
- parameterName: parameterName,
113
- required: !this.parameter.questionToken,
114
- type: null
115
- };
116
- }
117
-
118
- /*
119
- private getFileParameter(parameter: ts.ParameterDeclaration): Metadata.Parameter{
120
- const parameterName = (parameter.name as ts.Identifier).text;
121
-
122
- if (!this.supportsBodyParameters(this.method)) {
123
- throw new Error(`FileParam can't support '${this.getCurrentLocation()}' method.`);
124
- }
125
-
126
- return {
127
- description: this.getParameterDescription(parameter),
128
- in: 'formData',
129
- name: getDecoratorTextValue(this.parameter, ident => ident.text === 'FileParam') || parameterName,
130
- parameterName: parameterName,
131
- required: !parameter.questionToken,
132
- type: { typeName: 'file' }
133
- };
134
- }
135
- */
136
-
137
- private getFileParameter(
138
- representationManager: RepresentationManager<'SERVER_FILE_PARAM' | 'SERVER_FILES_PARAM'>,
139
- isArray?: boolean
140
- ) : Parameter{
141
- const parameterName = (this.parameter.name as ts.Identifier).text;
142
- let name = parameterName;
143
-
144
- if (!this.supportsBodyParameters(this.method)) {
145
- throw new Error(`File(s)Param can't support '${this.getCurrentLocation()}' method.`);
146
- }
147
-
148
- const value = representationManager.getPropertyValue('DEFAULT');
149
- if(typeof value === 'string') {
150
- name = value;
151
- }
152
-
153
- const elementType: Resolver.Type = { typeName: 'file' };
154
- let type: Resolver.Type;
155
- if (isArray) {
156
- type = { typeName: 'array', elementType: elementType };
157
- } else {
158
- type = elementType;
159
- }
160
-
161
- return {
162
- description: this.getParameterDescription(this.parameter),
163
- in: 'formData',
164
- name: name || parameterName,
165
- parameterName: parameterName,
166
- required: !this.parameter.questionToken && !this.parameter.initializer,
167
- type: type
168
- };
169
- }
170
-
171
- private getFormParameter(representationManager: RepresentationManager<'SERVER_FORM'>): Parameter{
172
- const parameterName = (this.parameter.name as ts.Identifier).text;
173
- let name = parameterName;
174
-
175
- const type = this.getValidatedType(this.parameter);
176
-
177
- if (!this.supportsBodyParameters(this.method)) {
178
- throw new Error(`Form can't support '${this.getCurrentLocation()}' method.`);
179
- }
180
-
181
- const value = representationManager.getPropertyValue('DEFAULT');
182
- if(typeof value === 'string') {
183
- name = value;
184
- }
185
-
186
-
187
- return {
188
- description: this.getParameterDescription(this.parameter),
189
- in: 'formData',
190
- name: name || parameterName,
191
- parameterName: parameterName,
192
- required: !this.parameter.questionToken && !this.parameter.initializer,
193
- type: type
194
- };
195
- }
196
-
197
- private getCookieParameter(representationManager: RepresentationManager<'SERVER_COOKIES'>): Parameter{
198
- const parameterName = (this.parameter.name as ts.Identifier).text;
199
- let name = parameterName;
200
-
201
- const type = this.getValidatedType(this.parameter);
202
-
203
- if (!this.supportPathDataType(type)) {
204
- throw new Error(`Cookie can't support '${this.getCurrentLocation()}' method.`);
205
- }
206
-
207
- const value = representationManager.getPropertyValue('DEFAULT');
208
- if(typeof value === 'string') {
209
- name = value;
210
- }
211
-
212
- return {
213
- description: this.getParameterDescription(this.parameter),
214
- in: 'cookie',
215
- name: name || parameterName,
216
- parameterName: parameterName,
217
- required: !this.parameter.questionToken && !this.parameter.initializer,
218
- type: type
219
- };
220
- }
221
-
222
- private getBodyParameter(representationManager?: RepresentationManager<'SERVER_BODY'>): Parameter{
223
- const parameterName = (this.parameter.name as ts.Identifier).text;
224
- let name = parameterName;
225
-
226
- const type = this.getValidatedType(this.parameter);
227
-
228
- if (!this.supportsBodyParameters(this.method)) {
229
- throw new Error(`Body can't support ${this.method} method`);
230
- }
231
-
232
- if(typeof representationManager !== 'undefined') {
233
- const value = representationManager.getPropertyValue('DEFAULT');
234
- if (typeof value === 'string') {
235
- name = value;
236
- }
237
- }
238
-
239
- return {
240
- description: this.getParameterDescription(this.parameter),
241
- in: 'body',
242
- name: name || parameterName,
243
- parameterName: parameterName,
244
- required: !this.parameter.questionToken && !this.parameter.initializer,
245
- type: type
246
- };
247
- }
248
-
249
- private getHeaderParameter(representationManager: RepresentationManager<'SERVER_HEADERS'>) : Parameter{
250
- const parameterName = (this.parameter.name as ts.Identifier).text;
251
- let name = parameterName;
252
-
253
- const type = this.getValidatedType(this.parameter);
254
-
255
- if (!this.supportPathDataType(type)) {
256
- throw new InvalidParameterException(`Parameter '${parameterName}' can't be passed as a header parameter in '${this.getCurrentLocation()}'.`);
257
- }
258
-
259
- const value = representationManager.getPropertyValue('DEFAULT');
260
- if(typeof value === 'string') {
261
- name = value;
262
- }
263
-
264
- return {
265
- description: this.getParameterDescription(this.parameter),
266
- in: 'header',
267
- name: name || parameterName,
268
- parameterName: parameterName,
269
- required: !this.parameter.questionToken && !this.parameter.initializer,
270
- type: type
271
- };
272
- }
273
-
274
- private getQueryParameter(representationManager: RepresentationManager<'SERVER_QUERY'>): Parameter| ArrayParameter {
275
- const parameterName = (this.parameter.name as ts.Identifier).text;
276
- const type = this.getValidatedType(this.parameter);
277
-
278
- if (!this.supportQueryDataType(type)) {
279
- /*
280
- const arrayType = getCommonPrimitiveAndArrayUnionType(parameter.type);
281
- if (arrayType && this.supportQueryDataType(arrayType)) {
282
- type = arrayType;
283
- } else {
284
- throw new InvalidParameterException(`Parameter '${parameterName}' can't be passed as a query parameter in '${this.getCurrentLocation()}'.`);
285
- }
286
- */
287
- // throw new InvalidParameterException(`Parameter '${parameterName}' can't be passed as a query parameter in '${this.getCurrentLocation()}'.`);
288
- }
289
- let name : string = parameterName;
290
- let options : any = {};
291
-
292
- const nameValue = representationManager.getPropertyValue('DEFAULT');
293
- if(typeof nameValue === 'string') {
294
- name = nameValue;
295
- }
296
-
297
- const optionsValue = representationManager.getPropertyValue('OPTIONS');
298
- if(typeof optionsValue !== 'undefined') {
299
- options = optionsValue;
300
- }
301
-
302
- const properties : Parameter= {
303
- allowEmptyValue: options.allowEmptyValue,
304
- collectionFormat: options.collectionFormat,
305
- default: getInitializerValue(this.parameter.initializer, this.current.typeChecker, type),
306
- description: this.getParameterDescription(this.parameter),
307
- in: 'query',
308
- maxItems: options.maxItems,
309
- minItems: options.minItems,
310
- name: name,
311
- parameterName: parameterName,
312
- required: !this.parameter.questionToken && !this.parameter.initializer,
313
- type: type
314
- };
315
-
316
- if (type.typeName === 'array') {
317
- return {
318
- ...properties,
319
- collectionFormat: 'multi',
320
- type: type,
321
- };
322
- }
323
-
324
- return properties;
325
- }
326
-
327
- private getPathParameter(representationManager: RepresentationManager<'SERVER_PATH_PARAMS'>): Parameter {
328
- const parameterName = (this.parameter.name as ts.Identifier).text;
329
- let pathName = parameterName;
330
-
331
- const type = this.getValidatedType(this.parameter);
332
-
333
- const value = representationManager.getPropertyValue('DEFAULT');
334
- if(typeof value === 'string') {
335
- pathName = value;
336
- }
337
-
338
-
339
- if (!this.supportPathDataType(type)) {
340
- throw new InvalidParameterException(`Parameter '${parameterName}:${type}' can't be passed as a path parameter in '${this.getCurrentLocation()}'.`);
341
- }
342
-
343
- if ((!this.path.includes(`{${pathName}}`)) && (!this.path.includes(`:${pathName}`))) {
344
- throw new Error(`Parameter '${parameterName}' can't match in path: '${this.path}'`);
345
- }
346
-
347
- return {
348
- description: this.getParameterDescription(this.parameter),
349
- in: 'path',
350
- name: pathName,
351
- parameterName: parameterName,
352
- required: true,
353
- type: type
354
- };
355
- }
356
-
357
- private getParameterDescription(node: ts.ParameterDeclaration) {
358
- const symbol = this.current.typeChecker.getSymbolAtLocation(node.name);
359
-
360
- if (symbol) {
361
- const comments = symbol.getDocumentationComment(this.current.typeChecker);
362
- if (comments.length) { return ts.displayPartsToString(comments); }
363
- }
364
-
365
- return '';
366
- }
367
-
368
- private supportsBodyParameters(method: string) {
369
- return ['delete', 'post', 'put', 'patch', 'get'].some(m => m === method);
370
- }
371
- private supportPathDataType(parameterType: Resolver.BaseType) {
372
- return ['string', 'integer', 'long', 'float', 'double', 'date', 'datetime', 'buffer', 'boolean', 'enum'].find(t => t === parameterType.typeName);
373
- }
374
-
375
- private supportQueryDataType(parameterType: Resolver.BaseType) {
376
- // Copied from supportPathDataType and added 'array'. Not sure if all options apply to queries, but kept to avoid breaking change.
377
- return ['string', 'integer', 'long', 'float', 'double', 'date',
378
- 'datetime', 'buffer', 'boolean', 'enum', 'array', 'object'].find(t => t === parameterType.typeName);
379
- }
380
-
381
- private getValidatedType(parameter: ts.ParameterDeclaration) {
382
- let typeNode = parameter.type;
383
- if (!typeNode) {
384
- const type = this.current.typeChecker.getTypeAtLocation(parameter);
385
- typeNode = this.current.typeChecker.typeToTypeNode(type, undefined, ts.NodeBuilderFlags.NoTruncation) as ts.TypeNode;
386
- }
387
-
388
- return new TypeNodeResolver(typeNode, this.current, parameter).resolve();
389
- }
390
- }
391
-
392
- class InvalidParameterException extends Error { }
package/src/index.ts DELETED
@@ -1,13 +0,0 @@
1
- /*
2
- * Copyright (c) 2021.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */
7
-
8
- export * from './cache';
9
- export * from './decorator';
10
- export * from './generator';
11
- export * from './resolver';
12
- export * from './utils';
13
- export * from './type';
@@ -1,38 +0,0 @@
1
- /*
2
- * Copyright (c) 2021.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */
7
-
8
- import { normalize } from "path";
9
- import { Node, TypeNode } from 'typescript';
10
-
11
- export class ResolverError extends Error {
12
- constructor(message?: string, node?: Node | TypeNode, onlyCurrent = false) {
13
- super(message);
14
-
15
- if (node) {
16
- this.message = `${message!}\n${prettyLocationOfNode(node)}\n${prettyTroubleCause(node, onlyCurrent)}`;
17
- }
18
- }
19
- }
20
-
21
- export function prettyLocationOfNode(node: Node | TypeNode) {
22
- const sourceFile = node.getSourceFile();
23
- const token = node.getFirstToken() || node.parent.getFirstToken();
24
- const start = token ? `:${sourceFile.getLineAndCharacterOfPosition(token.getStart()).line + 1}` : '';
25
- const end = token ? `:${sourceFile.getLineAndCharacterOfPosition(token.getEnd()).line + 1}` : '';
26
- const normalizedPath = normalize(`${sourceFile.fileName}${start}${end}`);
27
- return `At: ${normalizedPath}.`;
28
- }
29
-
30
- export function prettyTroubleCause(node: Node | TypeNode, onlyCurrent = false) {
31
- let name: string;
32
- if (onlyCurrent || !node.parent) {
33
- name = node.pos !== -1 ? node.getText() : (node as any).name.text;
34
- } else {
35
- name = node.parent.pos !== -1 ? node.parent.getText() : (node as any).parent.name.text;
36
- }
37
- return `This was caused by '${name}'`;
38
- }
@@ -1,11 +0,0 @@
1
- /*
2
- * Copyright (c) 2021.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */
7
-
8
- export * from './error';
9
- export * from './type';
10
- export * from './type-node';
11
- export * from './utils/initializer';