@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,146 +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 {
9
- Expression
10
- } from "typescript";
11
- import {hasOwnProperty} from "@trapi/metadata-utils";
12
- import {Decorator} from "../../type";
13
- import {getInitializerValue} from "../../../resolver";
14
-
15
- export function extendRepresentationPropertyConfig(property: Decorator.Property): Decorator.Property {
16
- if(typeof property.isType === 'undefined') {
17
- property.isType = false;
18
- }
19
-
20
- if (typeof property.type === 'undefined') {
21
- property.type = 'element';
22
- }
23
-
24
- if (typeof property.srcArgumentType === 'undefined') {
25
- property.srcArgumentType = 'argument';
26
- }
27
-
28
- if (
29
- property.type === 'element' &&
30
- typeof property.srcPosition === 'undefined'
31
- ) {
32
- property.srcPosition = 0;
33
- }
34
-
35
- return property;
36
- }
37
-
38
- export function extractRepresentationPropertyValue<
39
- T extends Decorator.Type,
40
- P extends keyof Decorator.TypePropertyMap[T]
41
- >(
42
- decorator: Decorator.Data,
43
- config: Decorator.Property
44
- ): Decorator.TypePropertyMap[T][P] | undefined {
45
- let items : unknown[] = [];
46
-
47
- switch (config.srcArgumentType) {
48
- case 'typeArgument':
49
- items = decorator.typeArguments;
50
- break;
51
- case "argument":
52
- items = decorator.arguments;
53
- break;
54
- }
55
-
56
- if(!config.isType) {
57
- items = extractValueFromArgumentType(items);
58
- }
59
-
60
- const srcPosition : number = config.srcPosition ?? 0;
61
- const srcAmount : number = config.srcAmount ?? 1;
62
-
63
- if (items.length <= srcPosition) {
64
- switch (config.type) {
65
- case 'element':
66
- return undefined;
67
- case 'array':
68
- return [] as unknown as Decorator.TypePropertyMap[T][P];
69
- }
70
- }
71
-
72
- const data : unknown[] | unknown[][] = srcAmount >= 1 ? items.slice(srcPosition, srcPosition + srcAmount) : items.slice(srcPosition);
73
-
74
- if(data.length === 0) {
75
- return (config.type === 'array' ? [] : undefined) as unknown as Decorator.TypePropertyMap[T][P];
76
- }
77
-
78
- const srcStrategy : Decorator.PropertyStrategy = config.srcStrategy ?? 'none';
79
-
80
- switch (srcStrategy) {
81
- case "merge":
82
- switch (config.type) {
83
- case 'array':
84
- return mergeArrayArguments(data) as unknown as Decorator.TypePropertyMap[T][P];
85
- case 'element':
86
- default:
87
- return mergeObjectArguments(data) as Decorator.TypePropertyMap[T][P];
88
- }
89
- case "none":
90
- // if we dont have any merge strategy, we just return the first argument.
91
- switch (config.type) {
92
- case 'array':
93
- const arr = Array.isArray(data[0]) ? data[0] : [data[0]];
94
- return arr as unknown as Decorator.TypePropertyMap[T][P];
95
- case 'element':
96
- default:
97
- return data[0] as Decorator.TypePropertyMap[T][P];
98
- }
99
- default:
100
- if(typeof config.srcStrategy === 'function') {
101
- return config.srcStrategy(data) as Decorator.TypePropertyMap[T][P];
102
- }
103
-
104
- return (config.type === 'array' ? [] : undefined) as unknown as Decorator.TypePropertyMap[T][P];
105
- }
106
- }
107
-
108
- export function mergeObjectArguments(data: unknown[]) {
109
- let output : Record<string, any> = {};
110
- for(let i=0; i<data.length; i++) {
111
- const prototype = Object.prototype.toString.call(data[i]);
112
- if(prototype === '[object Object]') {
113
- output = Object.assign(output, data[i]);
114
- }
115
- }
116
-
117
- return output;
118
- }
119
-
120
- export function mergeArrayArguments(data: unknown[]) {
121
- let merged : unknown[] = [];
122
- for(let i=0; i<data.length; i++) {
123
- if(Array.isArray(data[i])) {
124
- merged = [...merged, ...data[i] as unknown[]];
125
- } else {
126
- merged.push(data[i]);
127
- }
128
- }
129
-
130
- return merged;
131
- }
132
-
133
- function extractValueFromArgumentType(argument: unknown[]) {
134
- const values : unknown[] = [];
135
-
136
- for(let i=0; i<argument.length; i++) {
137
- if(!hasOwnProperty(argument[i], 'kind')) {
138
- values.push(argument[i]);
139
- continue;
140
- }
141
-
142
- values.push(getInitializerValue(argument[i] as Expression));
143
- }
144
-
145
- return values;
146
- }
@@ -1,235 +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 namespace Decorator {
9
- export interface Data {
10
- text: string;
11
- arguments: any[];
12
- typeArguments: any[];
13
- }
14
-
15
- // -------------------------------------------
16
-
17
- export interface Config {
18
- /**
19
- * Use a pre defined third party TypeRepresentationMap in full scope or
20
- * only use a partial amount of defined type representations.
21
- *
22
- * Default: []
23
- */
24
- library?: ConfigLibrary;
25
- /**
26
- * Use all internal defined type representations or only use a subset.
27
- * Default: true
28
- */
29
- internal?: TypeRepresentationConfig;
30
- /**
31
- * Set up self defined type representations.
32
- */
33
- map?: Partial<TypeRepresentationMap>;
34
- }
35
-
36
- // -------------------------------------------
37
-
38
- /**
39
- * These are the current by default supported third party libraries.
40
- */
41
- export type Library = 'typescript-rest' | 'decorators-express';
42
- export type ConfigLibrary = Library | Library[] | Record<string, TypeRepresentationConfig>;
43
-
44
- // -------------------------------------------
45
-
46
- /**
47
- * Activate/Deactivate specific type representations of a TypeRepresentationMap.
48
- */
49
- export type TypeRepresentationConfig = boolean | Type | Type[] | { [K in Type]?: boolean };
50
-
51
- // -------------------------------------------
52
-
53
- /**
54
- * This type maps a decorator type to its representation config.
55
- */
56
- export type TypeRepresentationMap = {
57
- [T in keyof TypePropertyMap]: Representation<T> | Array<Representation<T>>;
58
- };
59
-
60
- /**
61
- * The id property is the name/text of the defined decorator.
62
- */
63
- export interface Representation<T extends keyof TypePropertyMap> {
64
- id: string;
65
- properties?: RepresentationProperties<TypePropertyMap[T]>;
66
- }
67
-
68
- export type RepresentationProperties<P> = {
69
- [K in keyof P]: Property
70
- };
71
-
72
-
73
- /**
74
- * A decorator type is an identifier which is associated
75
- * to specific decorator names.
76
- */
77
-
78
- export interface TypePropertyMap {
79
- // Class Type
80
- SWAGGER_TAGS: {
81
- DEFAULT: string[]
82
- };
83
- CLASS_PATH: {
84
- DEFAULT: string
85
- };
86
-
87
- // Method and Class
88
- REQUEST_ACCEPT: undefined;
89
- RESPONSE_EXAMPLE: {
90
- TYPE: unknown,
91
- PAYLOAD: unknown | unknown[]
92
- };
93
- RESPONSE_DESCRIPTION: {
94
- TYPE: unknown;
95
- STATUS_CODE: number | string;
96
- DESCRIPTION: string;
97
- PAYLOAD: unknown | unknown[];
98
- };
99
- REQUEST_CONSUMES: {
100
- DEFAULT: string[]
101
- };
102
- RESPONSE_PRODUCES: {
103
- DEFAULT: string[]
104
- };
105
- HIDDEN: {};
106
- EXTENSION: {
107
- KEY: string,
108
- VALUE: unknown | unknown[]
109
- };
110
-
111
- // Method
112
- METHOD_PATH: {
113
- DEFAULT: string
114
- };
115
- DEPRECATED: undefined;
116
-
117
- // METHOD HTTP
118
- ALL: {
119
- DEFAULT?: string
120
- };
121
- GET: {
122
- DEFAULT?: string
123
- };
124
- POST: {
125
- DEFAULT?: string
126
- };
127
- PUT: {
128
- DEFAULT?: string
129
- };
130
- DELETE: {
131
- DEFAULT?: string
132
- };
133
- PATCH: {
134
- DEFAULT?: string
135
- };
136
- OPTIONS: {
137
- DEFAULT?: string
138
- };
139
- HEAD: {
140
- DEFAULT?: string
141
- };
142
-
143
- // Parameter
144
- IS_INT: undefined;
145
- IS_LONG: undefined;
146
- IS_FlOAT: undefined;
147
- IS_DOUBLE: undefined;
148
-
149
- // Parameter Server
150
- SERVER_CONTEXT: {};
151
- SERVER_PARAMS: {
152
- // typescript-rest
153
- DEFAULT?: string
154
- };
155
- SERVER_QUERY: {
156
- // typescript-rest
157
- DEFAULT?: string,
158
- OPTIONS?: Record<string, any>
159
- } | undefined;
160
- SERVER_FORM: {
161
- // typescript-rest
162
- DEFAULT?: string
163
- } | undefined;
164
- SERVER_BODY: {
165
- // typescript-rest
166
- DEFAULT?: string
167
- };
168
- SERVER_HEADERS: {
169
- // typescript-rest
170
- DEFAULT?: string
171
- };
172
- SERVER_COOKIES: {
173
- // typescript-rest
174
- DEFAULT?: string
175
- };
176
- SERVER_PATH_PARAMS: {
177
- // typescript-rest
178
- DEFAULT?: string
179
- };
180
- SERVER_FILE_PARAM: {
181
- // typescript-rest
182
- DEFAULT?: string
183
- };
184
- SERVER_FILES_PARAM: {
185
- // typescript-rest
186
- DEFAULT?: string
187
- };
188
- }
189
-
190
- export type Type = keyof TypePropertyMap;
191
-
192
- export type MethodHttpVerbType = Extract<Type, 'ALL' | 'GET' | 'POST' | 'PUT' | 'DELETE' |
193
- 'PATCH' | 'OPTIONS' | 'HEAD'>;
194
-
195
-
196
- export type ParameterServerType = Extract<Type, 'SERVER_CONTEXT' | 'SERVER_PARAMS' | 'SERVER_QUERY' | 'SERVER_FORM' |
197
- 'SERVER_BODY' | 'SERVER_HEADERS' | 'SERVER_COOKIES' | 'SERVER_PATH_PARAMS' |
198
- 'SERVER_FILE_PARAM' | 'SERVER_FILES_PARAM'>;
199
-
200
- // -------------------------------------------
201
-
202
- export type PropertyStrategy = 'merge' | 'none' | ((...items: unknown[] | unknown[][]) => unknown | unknown[]);
203
-
204
- export interface Property {
205
- /**
206
- * Default: 'element'
207
- */
208
- type?: 'element' | 'array';
209
-
210
- /**
211
- * Default: false
212
- */
213
- isType?: boolean;
214
-
215
- /**
216
- * Default: 'argument'
217
- */
218
- srcArgumentType?: 'argument' | 'typeArgument';
219
-
220
- /**
221
- * Default: 0
222
- */
223
- srcPosition?: number;
224
-
225
- /**
226
- * Default: undefined
227
- */
228
- srcAmount?: number;
229
-
230
- /**
231
- * Default: 'none'
232
- */
233
- srcStrategy?: PropertyStrategy
234
- }
235
- }
@@ -1,9 +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 './node';
9
- export * from './validator';
@@ -1,59 +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 {isCallExpression, isNumericLiteral, isStringLiteral, Node} from 'typescript';
9
- import {Decorator} from "../type";
10
-
11
- /**
12
- * Get Decorators for a specific node.
13
- *
14
- * @param node
15
- * @param isMatching
16
- */
17
- export function getNodeDecorators(
18
- node: Node,
19
- isMatching?: (data: Decorator.Data) => boolean
20
- ): Decorator.Data[] {
21
- const decorators = node.decorators;
22
- if (!decorators || !decorators.length) {
23
- return [];
24
- }
25
-
26
- const items = decorators
27
- .map(d => {
28
- const result: any = {
29
- arguments: [],
30
- typeArguments: []
31
- };
32
-
33
- let x: any = d.expression;
34
-
35
- if (isCallExpression(x)) {
36
- if (x.arguments) {
37
- result.arguments = x.arguments.map((argument: any) => {
38
- if (isStringLiteral(argument) || isNumericLiteral(argument)) {
39
- return argument.text;
40
- } else {
41
- return argument;
42
- }
43
- });
44
- }
45
-
46
- if (x.typeArguments) {
47
- result.typeArguments = x.typeArguments;
48
- }
49
-
50
- x = x.expression;
51
- }
52
-
53
- result.text = x.text || x.name.text;
54
-
55
- return result as Decorator.Data;
56
- });
57
-
58
- return typeof isMatching === 'undefined' ? items : items.filter(isMatching);
59
- }
@@ -1,104 +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 {array, boolean, lazy, mixed, number, object, SchemaOf, string} from "yup";
9
- import {mapYupRuleForDictionary} from "@trapi/metadata-utils";
10
- import {Decorator} from "../type";
11
-
12
- let validatorInstance : undefined | SchemaOf<Decorator.Config>;
13
-
14
- export function useDecoratorConfigValidator() : SchemaOf<Decorator.Config> {
15
- if(typeof validatorInstance !== 'undefined') {
16
- return validatorInstance;
17
- }
18
-
19
- const configMappingOptionValidator : SchemaOf<Decorator.TypeRepresentationConfig> = lazy(value => {
20
- if(typeof value === 'boolean') {
21
- return boolean();
22
- }
23
-
24
- if(typeof value === 'string') {
25
- return string();
26
- }
27
-
28
- if(Array.isArray(value)) {
29
- return array().of(string());
30
- }
31
-
32
- if(Object.prototype.toString.call(value) === '[object Object]') {
33
- // todo: setup type key check :)
34
- return object(mapYupRuleForDictionary(value, boolean())).optional().default({});
35
- }
36
-
37
- return mixed().optional().default(undefined);
38
- }) as unknown as SchemaOf<Decorator.TypeRepresentationConfig>;
39
-
40
- const useLibraryValidator : SchemaOf<Decorator.ConfigLibrary> = lazy(value => {
41
- if(typeof value === 'string') {
42
- return string();
43
- }
44
-
45
- if(Array.isArray(value)) {
46
- return array().of(string());
47
- }
48
-
49
- if(Object.prototype.toString.call(value) === '[object Object]') {
50
- // todo: setup library key check :)
51
- return object(mapYupRuleForDictionary(value, configMappingOptionValidator));
52
- }
53
-
54
- return mixed().optional().default(undefined);
55
- }) as unknown as SchemaOf<Decorator.ConfigLibrary>;
56
-
57
- const representationPropertyValidator : SchemaOf<Decorator.Property> = object({
58
- type: mixed().oneOf(['element', 'array'] as Array<Decorator.Property['type']>),
59
- isType: boolean().optional().default(undefined),
60
- srcArgumentType: mixed().oneOf(['argument', 'typeArgument'] as Array<Decorator.Property['srcArgumentType']>),
61
- srcPosition: number().min(0).optional().default(0),
62
- srcAmount: number().optional().default(undefined),
63
- // todo: check if 'merge', 'none' or function
64
- srcStrategy: mixed().optional().default(undefined),
65
- });
66
-
67
- const representationValidator : SchemaOf<Decorator.Representation<any>> = object({
68
- id: string().required(),
69
- properties: lazy(value => {
70
- if(Object.prototype.toString.call(value) === '[object Object]') {
71
- return object(mapYupRuleForDictionary(value, representationPropertyValidator)).optional().default({});
72
- }
73
-
74
- return mixed().optional().default(undefined);
75
- })
76
- }) as unknown as SchemaOf<Decorator.Representation<any>>;
77
-
78
- const overrideValidator : SchemaOf<Decorator.TypeRepresentationMap> = lazy(value => {
79
- if(Object.prototype.toString.call(value) === '[object Object]') {
80
- return object(mapYupRuleForDictionary(value, lazy(val => {
81
- if (Array.isArray(val)) {
82
- return array().of(representationValidator);
83
- }
84
-
85
- return representationValidator;
86
- }))
87
- );
88
- }
89
-
90
- return mixed().optional().default(undefined);
91
- }) as unknown as SchemaOf<Decorator.TypeRepresentationMap>;
92
-
93
-
94
- validatorInstance = object({
95
- library: useLibraryValidator,
96
- internal: configMappingOptionValidator,
97
- map: overrideValidator
98
- }).optional().default({
99
- library: ['typescript-rest', '@decorators/express'],
100
- internal: true
101
- } as Decorator.Config);
102
-
103
- return validatorInstance;
104
- }
@@ -1,65 +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 {ClassDeclaration, MethodDeclaration, SyntaxKind} from 'typescript';
9
- import {EndpointGenerator} from './endpoint';
10
- import {MetadataGenerator} from './index';
11
- import {MethodGenerator} from './method';
12
- import {Controller, Method} from "../type";
13
-
14
- export class ControllerGenerator extends EndpointGenerator<ClassDeclaration> {
15
- private genMethods: Set<string> = new Set<string>();
16
-
17
- // --------------------------------------------------------------------
18
-
19
- constructor(node: ClassDeclaration, current: MetadataGenerator) {
20
- super(node, current);
21
-
22
- this.generatePath('CLASS_PATH');
23
- }
24
- public isValid() {
25
- return !!this.path || this.path === '';
26
- }
27
-
28
- public generate(): Controller {
29
- if (!this.node.parent) { throw new Error('Controller node doesn\'t have a valid parent source file.'); }
30
- if (!this.node.name) { throw new Error('Controller node doesn\'t have a valid name.'); }
31
-
32
- const sourceFile = this.node.parent.getSourceFile();
33
-
34
- return {
35
- consumes: this.getConsumes(),
36
- location: sourceFile.fileName,
37
- methods: this.buildMethods(),
38
- name: this.getCurrentLocation(),
39
- path: this.path || '',
40
- produces: this.getProduces(),
41
- responses: this.getResponses(),
42
- security: this.getSecurity(),
43
- tags: this.getTags(),
44
- };
45
- }
46
-
47
- protected getCurrentLocation(): string {
48
- return (this.node as ClassDeclaration).name.text;
49
- }
50
-
51
- private buildMethods() : Method[] {
52
- return this.node.members
53
- .filter((method: { kind: unknown; }) => (method.kind === SyntaxKind.MethodDeclaration))
54
- .filter((method: MethodDeclaration) => !this.isHidden(method))
55
- .map((method: MethodDeclaration) => new MethodGenerator(method, this.current,this.path || ''))
56
- .filter((generator: MethodGenerator) => {
57
- if (generator.isValid() && !this.genMethods.has(generator.getMethodName())) {
58
- this.genMethods.add(generator.getMethodName());
59
- return true;
60
- }
61
- return false;
62
- })
63
- .map((generator: MethodGenerator) => generator.generate());
64
- }
65
- }