@trapi/metadata 0.1.4 → 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 (60) hide show
  1. package/package.json +11 -8
  2. package/src/cache/driver.ts +0 -97
  3. package/src/cache/index.ts +0 -10
  4. package/src/cache/type.ts +0 -43
  5. package/src/cache/utils.ts +0 -49
  6. package/src/config/index.ts +0 -8
  7. package/src/config/path.ts +0 -29
  8. package/src/decorator/functions.ts +0 -142
  9. package/src/decorator/index.ts +0 -13
  10. package/src/decorator/mapper/index.ts +0 -153
  11. package/src/decorator/mapper/maps/decorators-express.ts +0 -117
  12. package/src/decorator/mapper/maps/internal.ts +0 -95
  13. package/src/decorator/mapper/maps/typescript-rest.ts +0 -150
  14. package/src/decorator/mapper/utils.ts +0 -88
  15. package/src/decorator/representation/index.ts +0 -82
  16. package/src/decorator/representation/property/utils.ts +0 -146
  17. package/src/decorator/type.ts +0 -235
  18. package/src/decorator/utils/index.ts +0 -9
  19. package/src/decorator/utils/node.ts +0 -59
  20. package/src/decorator/utils/validator.ts +0 -104
  21. package/src/generator/controller.ts +0 -65
  22. package/src/generator/endpoint.ts +0 -212
  23. package/src/generator/index.ts +0 -219
  24. package/src/generator/method.ts +0 -198
  25. package/src/generator/parameter.ts +0 -392
  26. package/src/index.ts +0 -13
  27. package/src/resolver/error.ts +0 -38
  28. package/src/resolver/index.ts +0 -11
  29. package/src/resolver/type-node.ts +0 -1287
  30. package/src/resolver/type.ts +0 -257
  31. package/src/resolver/utils/index.ts +0 -9
  32. package/src/resolver/utils/initializer.ts +0 -84
  33. package/src/resolver/utils/validator.ts +0 -144
  34. package/src/type.ts +0 -185
  35. package/src/utils/generator.ts +0 -42
  36. package/src/utils/index.ts +0 -10
  37. package/src/utils/js-doc.ts +0 -125
  38. package/src/utils/validator.ts +0 -43
  39. package/test/data/library/@decorators-express/decorators.ts +0 -7
  40. package/test/data/library/self/api.ts +0 -37
  41. package/test/data/library/type.ts +0 -138
  42. package/test/data/library/typescript-rest/api.ts +0 -366
  43. package/test/data/library/typescript-rest/decorators/index.ts +0 -10
  44. package/test/data/library/typescript-rest/decorators/methods.ts +0 -34
  45. package/test/data/library/typescript-rest/decorators/parameters.ts +0 -62
  46. package/test/data/library/typescript-rest/decorators/services.ts +0 -47
  47. package/test/data/library/typescript-rest/return-types.ts +0 -94
  48. package/test/data/library/utils.ts +0 -7
  49. package/test/jest.config.js +0 -49
  50. package/test/unit/cache.spec.ts +0 -48
  51. package/test/unit/decorator/mapper/index.spec.ts +0 -83
  52. package/test/unit/decorator/representation/index.spec.ts +0 -102
  53. package/test/unit/decorator/utils/node.spec.ts +0 -47
  54. package/test/unit/library/typescript-rest.spec.ts +0 -342
  55. package/test/unit/resolver/type.spec.ts +0 -134
  56. package/test/unit/utils/generator.spec.ts +0 -41
  57. package/test/unit/utils/js-doc.spec.ts +0 -104
  58. package/tsconfig.build.json +0 -11
  59. package/tsconfig.json +0 -9
  60. package/writable/.gitignore +0 -3
@@ -1,117 +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: 'Controller',
14
- properties: {
15
- DEFAULT: {}
16
- }
17
- },
18
-
19
- // Method
20
- METHOD_PATH: [
21
- {
22
- id: 'All',
23
- properties: {
24
- DEFAULT: {}
25
- }
26
- },
27
- {
28
- id: 'Get',
29
- properties: {
30
- DEFAULT: {}
31
- }
32
- },
33
- {
34
- id: 'Post',
35
- properties: {
36
- DEFAULT: {}
37
- }
38
- },
39
- {
40
- id: 'Put',
41
- properties: {
42
- DEFAULT: {}
43
- }
44
- },
45
- {
46
- id: 'Delete',
47
- properties: {
48
- DEFAULT: {}
49
- }
50
- },
51
- {
52
- id: 'Patch',
53
- properties: {
54
- DEFAULT: {}
55
- }
56
- },
57
- {
58
- id: 'Options',
59
- properties: {
60
- DEFAULT: {}
61
- }
62
- },
63
- {
64
- id: 'Head',
65
- properties: {
66
- DEFAULT: {}
67
- }
68
- }
69
- ],
70
-
71
- // Parameter
72
- SERVER_CONTEXT: [
73
- {
74
- id: 'Request',
75
- properties: {}
76
- },
77
- {
78
- id: 'Response',
79
- properties: {}
80
- },
81
- {
82
- id: 'Next',
83
- properties: {}
84
- },
85
- ],
86
- SERVER_QUERY: {
87
- id: 'Query',
88
- properties: {
89
- DEFAULT: {}
90
- }
91
- },
92
- SERVER_FORM: undefined,
93
- SERVER_BODY: {
94
- id: 'Body',
95
- properties: {
96
- DEFAULT: {}
97
- }
98
- },
99
- SERVER_HEADERS: {
100
- id: 'Headers',
101
- properties: {
102
- DEFAULT: {}
103
- }
104
- },
105
- SERVER_COOKIES: {
106
- id: 'Cookies',
107
- properties: {
108
- DEFAULT: {}
109
- }
110
- },
111
- SERVER_PATH_PARAMS: {
112
- id: 'Params',
113
- properties: {
114
- DEFAULT: {}
115
- }
116
- }
117
- } as Partial<Decorator.TypeRepresentationMap>;
@@ -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
- }