@trapi/metadata 0.1.1 → 0.1.5

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 (93) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cache/driver.js +1 -2
  3. package/dist/cache/driver.js.map +1 -1
  4. package/dist/cache/utils.d.ts.map +1 -1
  5. package/dist/cache/utils.js +12 -12
  6. package/dist/cache/utils.js.map +1 -1
  7. package/dist/decorator/mapper/index.js +1 -2
  8. package/dist/decorator/mapper/index.js.map +1 -1
  9. package/dist/decorator/representation/index.js +2 -4
  10. package/dist/decorator/representation/index.js.map +1 -1
  11. package/dist/decorator/representation/property/utils.js +4 -3
  12. package/dist/decorator/representation/property/utils.js.map +1 -1
  13. package/dist/generator/controller.js +1 -1
  14. package/dist/generator/controller.js.map +1 -1
  15. package/dist/generator/endpoint.js +0 -3
  16. package/dist/generator/endpoint.js.map +1 -1
  17. package/dist/generator/index.js +3 -9
  18. package/dist/generator/index.js.map +1 -1
  19. package/dist/generator/method.js +0 -2
  20. package/dist/generator/method.js.map +1 -1
  21. package/dist/generator/parameter.js +1 -9
  22. package/dist/generator/parameter.js.map +1 -1
  23. package/dist/resolver/type-node.js +28 -61
  24. package/dist/resolver/type-node.js.map +1 -1
  25. package/dist/resolver/utils/initializer.d.ts.map +1 -1
  26. package/dist/resolver/utils/initializer.js +2 -1
  27. package/dist/resolver/utils/initializer.js.map +1 -1
  28. package/dist/resolver/utils/validator.js +2 -1
  29. package/dist/resolver/utils/validator.js.map +1 -1
  30. package/dist/utils/generator.js +1 -1
  31. package/dist/utils/generator.js.map +1 -1
  32. package/dist/utils/js-doc.js +1 -1
  33. package/dist/utils/js-doc.js.map +1 -1
  34. package/package.json +18 -12
  35. package/src/cache/driver.ts +0 -97
  36. package/src/cache/index.ts +0 -10
  37. package/src/cache/type.ts +0 -43
  38. package/src/cache/utils.ts +0 -51
  39. package/src/config/index.ts +0 -8
  40. package/src/config/path.ts +0 -29
  41. package/src/decorator/functions.ts +0 -142
  42. package/src/decorator/index.ts +0 -13
  43. package/src/decorator/mapper/index.ts +0 -153
  44. package/src/decorator/mapper/maps/decorators-express.ts +0 -117
  45. package/src/decorator/mapper/maps/internal.ts +0 -95
  46. package/src/decorator/mapper/maps/typescript-rest.ts +0 -150
  47. package/src/decorator/mapper/utils.ts +0 -88
  48. package/src/decorator/representation/index.ts +0 -82
  49. package/src/decorator/representation/property/utils.ts +0 -146
  50. package/src/decorator/type.ts +0 -235
  51. package/src/decorator/utils/index.ts +0 -9
  52. package/src/decorator/utils/node.ts +0 -59
  53. package/src/decorator/utils/validator.ts +0 -104
  54. package/src/generator/controller.ts +0 -65
  55. package/src/generator/endpoint.ts +0 -212
  56. package/src/generator/index.ts +0 -219
  57. package/src/generator/method.ts +0 -198
  58. package/src/generator/parameter.ts +0 -392
  59. package/src/index.ts +0 -13
  60. package/src/resolver/error.ts +0 -38
  61. package/src/resolver/index.ts +0 -11
  62. package/src/resolver/type-node.ts +0 -1287
  63. package/src/resolver/type.ts +0 -257
  64. package/src/resolver/utils/index.ts +0 -9
  65. package/src/resolver/utils/initializer.ts +0 -81
  66. package/src/resolver/utils/validator.ts +0 -144
  67. package/src/type.ts +0 -185
  68. package/src/utils/generator.ts +0 -42
  69. package/src/utils/index.ts +0 -10
  70. package/src/utils/js-doc.ts +0 -125
  71. package/src/utils/validator.ts +0 -43
  72. package/test/data/library/@decorators-express/decorators.ts +0 -7
  73. package/test/data/library/self/api.ts +0 -37
  74. package/test/data/library/type.ts +0 -138
  75. package/test/data/library/typescript-rest/api.ts +0 -366
  76. package/test/data/library/typescript-rest/decorators/index.ts +0 -10
  77. package/test/data/library/typescript-rest/decorators/methods.ts +0 -34
  78. package/test/data/library/typescript-rest/decorators/parameters.ts +0 -62
  79. package/test/data/library/typescript-rest/decorators/services.ts +0 -47
  80. package/test/data/library/typescript-rest/return-types.ts +0 -94
  81. package/test/data/library/utils.ts +0 -7
  82. package/test/jest.config.js +0 -49
  83. package/test/unit/cache.spec.ts +0 -48
  84. package/test/unit/decorator/mapper/index.spec.ts +0 -83
  85. package/test/unit/decorator/representation/index.spec.ts +0 -102
  86. package/test/unit/decorator/utils/node.spec.ts +0 -47
  87. package/test/unit/library/typescript-rest.spec.ts +0 -342
  88. package/test/unit/resolver/type.spec.ts +0 -134
  89. package/test/unit/utils/generator.spec.ts +0 -41
  90. package/test/unit/utils/js-doc.spec.ts +0 -95
  91. package/tsconfig.build.json +0 -11
  92. package/tsconfig.json +0 -9
  93. package/writable/.gitignore +0 -3
@@ -1,95 +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 {Decorator} from "../../type";
9
-
10
- export default {
11
- EXTENSION: {
12
- id: 'Extension',
13
- properties: {
14
- KEY: {type: "element", srcArgumentType: "argument", srcPosition: 0},
15
- VALUE: {type: "element", srcArgumentType: "argument", srcPosition: 1}
16
- }
17
- },
18
-
19
- // Class
20
- SWAGGER_TAGS: {
21
- id: 'SwaggerTags',
22
- properties: {
23
- DEFAULT: {type: 'array', srcArgumentType: "argument"}
24
- }
25
- },
26
-
27
- // Class + Method
28
- RESPONSE_EXAMPLE: {
29
- id: 'ResponseExample',
30
- properties: {
31
- TYPE: {isType: true, srcArgumentType: "typeArgument"},
32
- PAYLOAD: {type: "element", srcArgumentType: "argument", srcPosition: 0}
33
- }
34
- },
35
- RESPONSE_DESCRIPTION: {
36
- id: 'ResponseDescription',
37
- properties: {
38
- TYPE: {isType: true, srcArgumentType: "typeArgument"},
39
- STATUS_CODE: {type: "element", srcArgumentType: "argument", srcPosition: 0},
40
- DESCRIPTION: {type: "element", srcArgumentType: "argument", srcPosition: 1},
41
- PAYLOAD: {type: "element", srcArgumentType: "argument", srcPosition: 2}
42
- }
43
- },
44
- REQUEST_CONSUMES: {
45
- id: 'RequestConsumes',
46
- properties: {
47
- DEFAULT: {type: 'array', srcArgumentType: "argument", srcAmount: -1, srcStrategy: "merge"}
48
- }
49
- },
50
- RESPONSE_PRODUCES: {
51
- id: 'ResponseProduces',
52
- properties: {
53
- DEFAULT: {type: 'array', srcArgumentType: "argument", srcAmount: -1, srcStrategy: "merge"}
54
- }
55
- },
56
-
57
- HIDDEN: {
58
- id: 'SwaggerHidden',
59
- properties: []
60
- },
61
- DEPRECATED: {
62
- id: 'SwaggerDeprecated',
63
- properties: undefined
64
- },
65
-
66
- IS_INT: {
67
- id: 'IsInt',
68
- properties: undefined
69
- },
70
- IS_LONG: {
71
- id: 'IsLong',
72
- properties: undefined
73
- },
74
- IS_FlOAT: {
75
- id: 'IsFloat',
76
- properties: undefined
77
- },
78
- IS_DOUBLE: {
79
- id: 'IsDouble',
80
- properties: undefined
81
- },
82
-
83
- SERVER_FILES_PARAM: {
84
- id: 'RequestFileParam',
85
- properties: {
86
- DEFAULT: {}
87
- }
88
- },
89
- SERVER_FILE_PARAM: {
90
- id: 'RequestFileParam',
91
- properties: {
92
- DEFAULT: {}
93
- }
94
- },
95
- } as Partial<Decorator.TypeRepresentationMap>;
@@ -1,150 +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 {Decorator} from "../../type";
9
-
10
- export default {
11
- // Class
12
- CLASS_PATH: {
13
- id: 'Path',
14
- properties: {
15
- DEFAULT: {type: 'element', srcArgumentType: "argument"}
16
- }
17
- },
18
-
19
- // Class + Method
20
- REQUEST_ACCEPT: undefined,
21
- RESPONSE_EXAMPLE: {
22
- id: 'Example',
23
- properties: {
24
- TYPE: {isType: true, srcArgumentType: "typeArgument"},
25
- PAYLOAD: {type: "element", srcArgumentType: "argument"}
26
- }
27
- },
28
- RESPONSE_DESCRIPTION: {
29
- id: 'Response',
30
- properties: {
31
- TYPE: {type: "element", srcArgumentType: "typeArgument"},
32
- STATUS_CODE: {type: "element", srcArgumentType: "argument", srcPosition: 0},
33
- DESCRIPTION: {type: "element", srcArgumentType: "argument", srcPosition: 1},
34
- PAYLOAD: {type: "element", srcArgumentType: "argument", srcPosition: 2}
35
- }
36
- },
37
-
38
- // Method
39
- ALL: {
40
- id: 'ALL',
41
- properties: {}
42
- },
43
- GET: {
44
- id: 'GET',
45
- properties: {}
46
- },
47
- POST: {
48
- id: 'POST',
49
- properties: {}
50
- },
51
- PUT: {
52
- id: 'PUT',
53
- properties: {}
54
- },
55
- DELETE: {
56
- id: 'DELETE',
57
- properties: {}
58
- },
59
- PATCH: {
60
- id: 'PATCH',
61
- properties: {}
62
- },
63
- OPTIONS: {
64
- id: 'OPTIONS',
65
- properties: {}
66
- },
67
- HEAD: {
68
- id: 'HEAD',
69
- properties: {}
70
- },
71
-
72
- METHOD_PATH: {
73
- id: 'Path',
74
- properties: {
75
- DEFAULT: {type: 'element', srcArgumentType: "argument"}
76
- }
77
- },
78
-
79
- // Parameter
80
- SERVER_CONTEXT: [
81
- {
82
- id: 'Context',
83
- },
84
- {
85
- id: 'ContextRequest',
86
- },
87
- {
88
- id: 'ContextResponse',
89
- },
90
- {
91
- id: 'ContextNext',
92
- },
93
- {
94
- id: 'ContextLanguage',
95
- },
96
- {
97
- id: 'ContextAccept',
98
- }
99
- ],
100
- SERVER_PARAMS: {
101
- id: 'Param',
102
- properties: {
103
- DEFAULT: {}
104
- }
105
- },
106
- SERVER_QUERY: {
107
- id: 'QueryParam',
108
- properties: {
109
- DEFAULT: {}
110
- }
111
- },
112
- SERVER_FORM: {
113
- id: 'FormParam',
114
- properties: {
115
- DEFAULT: {}
116
- }
117
- },
118
- SERVER_BODY: undefined,
119
- SERVER_HEADERS: {
120
- id: 'HeaderParam',
121
- properties: {
122
- DEFAULT: {}
123
- }
124
- },
125
- SERVER_COOKIES: {
126
- id: 'CookieParam',
127
- properties: {
128
- DEFAULT: {}
129
- }
130
- },
131
- SERVER_PATH_PARAMS: {
132
- id: 'PathParam',
133
- properties: {
134
- DEFAULT: {}
135
- }
136
- },
137
- SERVER_FILES_PARAM: {
138
- id: 'FilesParam',
139
- properties: {
140
- DEFAULT: {}
141
- }
142
- },
143
- SERVER_FILE_PARAM: {
144
- id: 'FileParam',
145
- properties: {
146
- DEFAULT: {}
147
- }
148
- }
149
- } as Partial<Decorator.TypeRepresentationMap>;
150
-
@@ -1,88 +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 {hasOwnProperty} from "@trapi/metadata-utils";
9
- import {Decorator} from "../type";
10
-
11
- /**
12
- *
13
- *
14
- * @param mapping
15
- * @param reducer
16
- */
17
- export function reduceTypeRepresentationMapping(
18
- mapping: Partial<Decorator.TypeRepresentationMap>,
19
- reducer: (type: Decorator.Type) => boolean
20
- ): Partial<Decorator.TypeRepresentationMap> {
21
- const mappingKeys: Decorator.Type[] = (Object.keys(mapping) as Decorator.Type[]);
22
- const allowedTypes: Decorator.Type[] = mappingKeys
23
- .filter(reducer);
24
-
25
- if (mappingKeys.length === allowedTypes.length) {
26
- return mapping;
27
- }
28
-
29
- const result: Partial<Decorator.TypeRepresentationMap> = {};
30
- for (let i = 0; i < allowedTypes.length; i++) {
31
- // @ts-ignore
32
- result[allowedTypes[i]] = mapping[allowedTypes[i]];
33
- }
34
-
35
- return result;
36
- }
37
-
38
- /**
39
- *
40
- *
41
- * @param type
42
- * @param config
43
- */
44
- export function isMappingTypeIncluded(
45
- type: Decorator.Type,
46
- config: Decorator.TypeRepresentationConfig
47
- ): boolean {
48
- const allowedType = Object.prototype.toString.call(config);
49
- switch (allowedType) {
50
- case '[object Boolean]':
51
- return !!config;
52
- case '[object String]':
53
- return (config as string) === type;
54
- case '[object Array]':
55
- return (config as unknown as Decorator.Type[]).indexOf(type) !== -1;
56
- case '[object Object]':
57
- return hasOwnProperty((config as Record<Decorator.Type, boolean>), type) && (config as Record<Decorator.Type, boolean>)[type];
58
- }
59
-
60
- /* istanbul ignore next */
61
- return false;
62
- }
63
-
64
- let decoratorMap : Record<string, Partial<Decorator.TypeRepresentationMap>> = {};
65
-
66
- export function getDecoratorMap(name: string) : Partial<Decorator.TypeRepresentationMap> {
67
- if(hasOwnProperty(decoratorMap, name)) {
68
- return decoratorMap[name];
69
- }
70
-
71
- const content = loadDecoratorMap(name);
72
-
73
- (decoratorMap as Record<string, Partial<Decorator.TypeRepresentationMap>>)[name] = content;
74
-
75
- return content;
76
- }
77
-
78
- /* istanbul ignore next */
79
- function loadDecoratorMap(library: string) : Partial<Decorator.TypeRepresentationMap> {
80
- const exp = require(`./maps/${library}`);
81
-
82
- if(hasOwnProperty(exp, 'default')) {
83
- return exp.default;
84
- }
85
-
86
- /* istanbul ignore next */
87
- return exp;
88
- }
@@ -1,82 +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 {hasOwnProperty} from "@trapi/metadata-utils";
9
- import {Decorator} from "../type";
10
- import {extendRepresentationPropertyConfig, extractRepresentationPropertyValue} from "./property/utils";
11
- import TypePropertyMaps = Decorator.TypePropertyMap;
12
-
13
- export class RepresentationManager<T extends Decorator.Type> {
14
- protected extendedProperties : Partial<Decorator.RepresentationProperties<Decorator.TypePropertyMap[T]>> = {};
15
-
16
- constructor(
17
- protected representation: Decorator.Representation<T>,
18
- public decorators: Decorator.Data[]
19
- ) {
20
-
21
- }
22
-
23
- // -------------------------------------------
24
-
25
- /**
26
- * Get one or more specific value(s) of the decorator arguments or typeArguments.
27
- * @param type
28
- * @param decoratorOrIndex
29
- */
30
- public getPropertyValue<P extends keyof Decorator.TypePropertyMap[T]>(
31
- type: P,
32
- decoratorOrIndex?: number | Decorator.Data
33
- ) : TypePropertyMaps[T][P] | undefined {
34
- const config : Decorator.Property = this.getPropertyConfiguration(type);
35
- if(typeof config === 'undefined') {
36
- return undefined;
37
- }
38
-
39
- let decorator : Decorator.Data;
40
-
41
- if(
42
- typeof decoratorOrIndex === 'number' ||
43
- typeof decoratorOrIndex === 'undefined'
44
- ) {
45
- decoratorOrIndex = decoratorOrIndex ?? 0;
46
- if (decoratorOrIndex < 0 || decoratorOrIndex >= this.decorators.length) {
47
- return undefined;
48
- }
49
-
50
- decorator = this.decorators[decoratorOrIndex];
51
- } else {
52
- decorator = decoratorOrIndex;
53
- }
54
-
55
- return extractRepresentationPropertyValue<T, P>(decorator, config);
56
- }
57
-
58
- // -------------------------------------------
59
-
60
- public getPropertyConfiguration(type: keyof Decorator.TypePropertyMap[T]) : Decorator.Property | undefined {
61
- if(!hasOwnProperty(this.representation.properties, type)) {
62
- return undefined;
63
- }
64
-
65
- return this.extendProperty(type);
66
- }
67
-
68
- // -------------------------------------------
69
-
70
- protected extendProperty<P extends keyof Decorator.TypePropertyMap[T]>(type: P) : Decorator.Property {
71
- if(hasOwnProperty(this.extendedProperties, type)) {
72
- return this.extendedProperties[type];
73
- }
74
-
75
- const property = this.representation.properties[type];
76
- this.extendedProperties[type] = extendRepresentationPropertyConfig(property);
77
-
78
- return this.extendedProperties[type];
79
-
80
- }
81
- }
82
-
@@ -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
- }