@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
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@trapi/metadata",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Generate REST-API metadata scheme from TypeScript Decorators.",
5
5
  "main": "./dist/index.js",
6
6
  "typings": "./dist/index.d.ts",
7
+ "files": [
8
+ "dist/"
9
+ ],
7
10
  "scripts": {
8
11
  "start": "tsc -p tsconfig.build.json -w",
9
12
  "build": "npm run clean && tsc -p tsconfig.build.json",
@@ -27,12 +30,12 @@
27
30
  },
28
31
  "license": "MIT",
29
32
  "devDependencies": {
30
- "@types/glob": "^7.1.4",
31
- "@types/jest": "^27.0.1",
33
+ "@types/glob": "^7.2.0",
34
+ "@types/jest": "^27.0.3",
32
35
  "@types/minimatch": "^3.0.5",
33
- "@types/node": "^16.9.1",
36
+ "@types/node": "^16.11.12",
34
37
  "@types/yup": "^0.29.13",
35
- "jest": "^27.1.1",
38
+ "jest": "^27.4.5",
36
39
  "jsonata": "^1.8.5",
37
40
  "rimraf": "^3.0.2"
38
41
  },
@@ -40,11 +43,11 @@
40
43
  "@trapi/metadata-utils": "^0.0.4",
41
44
  "glob": "^7.1.7",
42
45
  "minimatch": "^3.0.4",
43
- "typescript": "^4.4.3",
44
- "yup": "^0.32.9"
46
+ "typescript": "^4.5.4",
47
+ "yup": "^0.32.11"
45
48
  },
46
49
  "publishConfig": {
47
50
  "access": "public"
48
51
  },
49
- "gitHead": "bb7b7c1db8284ac4ce054024abc3a79c3b64b92a"
52
+ "gitHead": "9a4e74ccaab05b36dfbcf0513085c4f9589dcddc"
50
53
  }
@@ -1,97 +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 {Cache} from "./type";
9
- import {buildCacheConfig, buildFileHash} from "./utils";
10
- import fs from "fs";
11
- import * as glob from "glob";
12
- import path from "path";
13
-
14
- export class CacheDriver {
15
- private readonly config: Cache.Config;
16
-
17
- constructor(config: string | boolean | Partial<Cache.Config>) {
18
- this.config = buildCacheConfig(config);
19
- }
20
-
21
- // -------------------------------------------------------------------------
22
-
23
- public save(data: Cache.Data): string | undefined {
24
- if (!this.config.enabled) {
25
- return undefined;
26
- }
27
-
28
- const filePath: string = this.buildFilePath(undefined, data.sourceFilesSize);
29
-
30
- fs.writeFileSync(filePath, JSON.stringify(data));
31
-
32
- return filePath;
33
- }
34
-
35
- public get(sourceFilesSize: number): Cache.Data | undefined {
36
- if (!this.config.enabled) {
37
- return undefined;
38
- }
39
-
40
- this.clear();
41
-
42
- const filePath: string = this.buildFilePath(undefined, sourceFilesSize);
43
-
44
- try {
45
- const buffer: Buffer = fs.readFileSync(filePath);
46
-
47
- const content: string = buffer.toString('utf-8');
48
-
49
- // todo: maybe add shape validation here :)
50
- const cache: Cache.Data | undefined = JSON.parse(content) as Cache.Data;
51
-
52
- if (typeof cache === 'undefined' || cache.sourceFilesSize !== sourceFilesSize) {
53
- return undefined;
54
- }
55
-
56
- return cache;
57
- } catch (e) {
58
- /* istanbul ignore next */
59
- return undefined;
60
- }
61
- }
62
-
63
- // -------------------------------------------------------------------------
64
-
65
- /**
66
- * At a 10% chance, clear all cache files :)
67
- */
68
-
69
- /* istanbul ignore next */
70
- public clear(): void {
71
- if (!this.config.enabled || !this.config.clearAtRandom) {
72
- return;
73
- }
74
-
75
- const rand: number = Math.floor(Math.random() * 100) + 1;
76
- if (rand > 10) {
77
- return;
78
- }
79
-
80
- const files: string[] = glob.sync(this.buildFilePath('**'));
81
- files.map(file => fs.unlinkSync(file));
82
- }
83
-
84
- // -------------------------------------------------------------------------
85
-
86
- private buildFilePath(hash?: string, sourceFilesSize?: number): string {
87
- return path.join(this.config.directoryPath, this.buildFileName(hash, sourceFilesSize));
88
- }
89
-
90
- private buildFileName(hash?: string, sourceFilesSize?: number): string {
91
- if (typeof this.config.fileName === 'string') {
92
- return this.config.fileName;
93
- } else {
94
- return `.swagger-${hash ?? buildFileHash(sourceFilesSize)}.json`;
95
- }
96
- }
97
- }
@@ -1,10 +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 './driver';
9
- export * from './type';
10
- export * from './utils';
package/src/cache/type.ts DELETED
@@ -1,43 +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 {GeneratorOutput} from "../type";
9
-
10
- export namespace Cache {
11
- export interface Config {
12
- /**
13
- * Specify if the cache driver should be enabled.
14
- *
15
- * Default: false
16
- * */
17
- enabled: boolean,
18
- /**
19
- * Directory relative or absolute path.
20
- *
21
- * Default: process.cwd()
22
- */
23
- directoryPath: string,
24
- /**
25
- * Specify the cache file name.
26
- *
27
- * Default: metadata-{hash}.json
28
- */
29
- fileName?: string,
30
-
31
- /**
32
- * The cache file(s) will be cleared at a 10% percent change
33
- * each time.
34
- *
35
- * Default: true
36
- */
37
- clearAtRandom: boolean
38
- }
39
-
40
- export type Data = {
41
- sourceFilesSize: number;
42
- } & GeneratorOutput;
43
- }
@@ -1,49 +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 {Cache} from "./type";
9
- import path from "path";
10
- import crypto from "crypto";
11
-
12
- export function buildCacheConfig(config?: string | boolean | Partial<Cache.Config>) : Cache.Config {
13
- if(typeof config === 'string') {
14
- config = {
15
- enabled: true,
16
- directoryPath: config
17
- }
18
- }
19
-
20
- if(typeof config === 'boolean') {
21
- config = {
22
- enabled: config
23
- }
24
- }
25
-
26
- config ??= {};
27
-
28
- /* istanbul ignore next */
29
- const isTestEnvironment : boolean = !!process.env.NODE_ENV && process.env.NODE_ENV === 'test';
30
-
31
- return {
32
- fileName: config.fileName,
33
- directoryPath: typeof config.directoryPath === 'string' ?
34
- path.isAbsolute(config.directoryPath) ? config.directoryPath : path.join(process.cwd(), config.directoryPath) :
35
- process.cwd(),
36
- enabled: config.enabled ?? false,
37
- clearAtRandom: config.clearAtRandom ?? !isTestEnvironment
38
- };
39
- }
40
-
41
- export function buildFileHash(sourceFilesSize?: number): string {
42
- const hash = crypto.createHash('sha256');
43
-
44
- const strSize: string = (sourceFilesSize ?? 0).toString();
45
-
46
- hash.update(strSize);
47
-
48
- return hash.digest('hex');
49
- }
@@ -1,8 +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 './path';
@@ -1,29 +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 path from "path";
9
-
10
- let writableDirPath : string | undefined;
11
- let rootDirPath : string | undefined;
12
-
13
- export function getWritableDirPath() {
14
- if(typeof writableDirPath !== 'undefined') {
15
- return writableDirPath;
16
- }
17
-
18
- writableDirPath = path.join(getRootDirPath(), './writable');
19
- return writableDirPath;
20
- }
21
-
22
- export function getRootDirPath() {
23
- if(typeof rootDirPath !== 'undefined') {
24
- return rootDirPath;
25
- }
26
-
27
- rootDirPath = path.resolve(__dirname, '../../');
28
- return rootDirPath;
29
- }
@@ -1,142 +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
- 'use strict';
9
-
10
- /**
11
- * Document the method or class comsumes property in generated swagger docs
12
- */
13
- export function RequestConsumes(...values: Array<string | string[]>): any {
14
- return () => { return; };
15
- }
16
-
17
- /**
18
- *
19
- *
20
- *
21
- * @param key
22
- * @constructor
23
- */
24
- export function RequestFileParam(key: string) : any {
25
- return () => { return; };
26
- }
27
-
28
- /**
29
- * A decorator to document the responses that a given service method can return. It is used to generate
30
- * documentation for the REST service.
31
- * ```typescript
32
- * interface MyError {
33
- * message: string
34
- * }
35
- * @ Path('people')
36
- * class PeopleService {
37
- * @ ResponseDescription<string>(200, 'Retrieve a list of people.')
38
- * @ ResponseDescription<MyError>(401, 'The user is unauthorized.', {message: 'The user is not authorized to access this operation.'})
39
- * @ GET
40
- * getPeople(@ Param('name') name: string) {
41
- * // ...
42
- * }
43
- * }
44
- * ```
45
- * A Default response is created in swagger documentation from the method return analisys. So any response declared
46
- * through this decorator is an additional response created.
47
- * @param name
48
- * @param description A description for this response
49
- * @param example An optional example of response to be added to method documentation.
50
- */
51
- export function ResponseDescription<T>(name: string | number, description?: string, example?: T): any {
52
- return () => { return; };
53
- }
54
-
55
- /**
56
- * Used to provide an example of method return to be added into the method response section of the
57
- * generated documentation for this method.
58
- * ```typescript
59
- * @ Path('people')
60
- * class PeopleService {
61
- * @ ResponseExample<Array<Person>>([{
62
- * name: 'Joe'
63
- * }])
64
- * @ GET
65
- * getPeople(@ Param('name') name: string): Person[] {
66
- * // ...
67
- * }
68
- * }
69
- * ```
70
- * @param example The example returned object
71
- */
72
- export function ResponseExample<T>(example: T): any {
73
- return () => { return; };
74
- }
75
-
76
- /**
77
- * Document the method or class produces property in generated swagger docs
78
- */
79
- export function ResponseProduces(...values: string[]): any {
80
- return () => { return; };
81
- }
82
-
83
- /**
84
- * Add tags for a given method on generated swagger documentation.
85
- * ```typescript
86
- * @ Path('people')
87
- * class PeopleService {
88
- * @ SwaggerTags('adiministrative', 'department1')
89
- * @ GET
90
- * getPeople(@ Param('name') name: string) {
91
- * // ...
92
- * }
93
- * }
94
- * ```
95
- * @param values a list of tags
96
- */
97
- export function SwaggerTags(...values: string[]): any {
98
- return () => { return; };
99
- }
100
-
101
- /**
102
- * Document the method or class produces property in generated swagger docs.
103
- */
104
- export function SwaggerHidden(): any {
105
- return () => { return; };
106
- }
107
-
108
- /**
109
- * Document the method or collection of method paths are deprecated and should not be used.
110
- */
111
- export function SwaggerDeprecated() : any {
112
- return () => { return; };
113
- }
114
-
115
- /**
116
- * Document the type of a property or parameter as `integer ($int32)` in generated swagger docs
117
- */
118
- export function IsInt(target: any, propertyKey: string, parameterIndex?: number) {
119
- return;
120
- }
121
-
122
- /**
123
- * Document the type of a property or parameter as `integer ($int64)` in generated swagger docs
124
- */
125
- export function IsLong(target: any, propertyKey: string, parameterIndex?: number) {
126
- return;
127
- }
128
-
129
- /**
130
- * Document the type of a property or parameter as `number ($float)` in generated swagger docs
131
- */
132
- export function IsFloat(target: any, propertyKey: string, parameterIndex?: number) {
133
- return;
134
- }
135
-
136
- /**
137
- * Document the type of a property or parameter as `number ($double)` in generated swagger docs.
138
- * This is the default for `number` types without a specifying decorator.
139
- */
140
- export function IsDouble(target: any, propertyKey: string, parameterIndex?: number) {
141
- return;
142
- }
@@ -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 './mapper';
9
- export * from './representation';
10
- export * from './utils';
11
- export * from './functions';
12
- export * from './type';
13
- export {getNodeDecorators} from "./utils/node";
@@ -1,153 +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 {Node} from "typescript";
9
- import {hasOwnProperty} from "@trapi/metadata-utils";
10
- import {RepresentationManager} from "../representation";
11
- import {Decorator} from "../type";
12
- import {getDecoratorMap, isMappingTypeIncluded, reduceTypeRepresentationMapping} from "./utils";
13
- import {getNodeDecorators} from "../utils";
14
-
15
- export class DecoratorMapper {
16
- protected mapping : Partial<Decorator.TypeRepresentationMap> = {};
17
-
18
- constructor(
19
- protected config?: Decorator.Config
20
- ) {
21
- this.aggregate();
22
- }
23
-
24
- public setConfig(config?: Decorator.Config) {
25
- this.config = config;
26
- this.aggregate();
27
- }
28
-
29
- /**
30
- * Try to find a matching representation for a given decorator type and decorators or node.
31
- *
32
- * @param type
33
- * @param data
34
- */
35
- public match<T extends Decorator.Type>(
36
- type: T,
37
- data: Decorator.Data[] | Node
38
- ) {
39
- if(!hasOwnProperty(this.mapping, type)) {
40
- return undefined;
41
- }
42
-
43
- const decorators : Decorator.Data[] = Array.isArray(data) ? data : getNodeDecorators(data);
44
-
45
- const representations : Array<Decorator.Representation<T>> = (Array.isArray(this.mapping[type]) ? this.mapping[type] : [this.mapping[type]]) as Array<Decorator.Representation<T>>;
46
- for(let i=0; i<representations.length; i++) {
47
- const items = decorators.filter(decorator => decorator.text === representations[i].id);
48
- if(items.length > 0) {
49
- return new RepresentationManager<T>(
50
- representations[i],
51
- items
52
- );
53
- }
54
- }
55
-
56
- return undefined;
57
- }
58
-
59
- // -------------------------------------------
60
-
61
- private aggregate() : void {
62
- if(typeof this.config === 'undefined') {
63
- this.mapping = {};
64
- return;
65
- }
66
-
67
- const items : Array<Partial<Decorator.TypeRepresentationMap>> = [];
68
-
69
- // mapping - internal
70
- const internalMap = getDecoratorMap('internal');
71
- items.push(
72
- typeof this.config.internal === 'undefined' ?
73
- internalMap :
74
- reduceTypeRepresentationMapping(internalMap, (type) => {
75
- return isMappingTypeIncluded(type, this.config.internal);
76
- })
77
- );
78
-
79
- // mapping - extension
80
- if(typeof this.config.map !== 'undefined') {
81
- items.push(this.config.map);
82
- }
83
-
84
- // mapping - library
85
- if(typeof this.config.library !== 'undefined') {
86
- // check if string or string[]
87
-
88
- if(
89
- typeof this.config.library === 'string' ||
90
- Array.isArray(this.config.library)
91
- ) {
92
- const libraries : string[] = Array.isArray(this.config.library) ?
93
- this.config.library :
94
- [this.config.library];
95
-
96
- items.push(...libraries.map(library => getDecoratorMap(library)));
97
- } else {
98
- // tslint:disable-next-line:forin
99
- for(const key in this.config.library) {
100
- items.push(
101
- reduceTypeRepresentationMapping(getDecoratorMap(key), (type) => {
102
- return isMappingTypeIncluded(type, this.config.library[key]);
103
- })
104
- );
105
- }
106
- }
107
- }
108
-
109
- this.mapping = this.merge(...items);
110
- }
111
-
112
- /**
113
- * Merge decorator type-representation mappings of different libraries together.
114
- *
115
- * @param mappings
116
- * @private
117
- */
118
- private merge(...mappings: Array<Partial<Decorator.TypeRepresentationMap>>) : Partial<Decorator.TypeRepresentationMap> {
119
- const result : Partial<Decorator.TypeRepresentationMap> = {};
120
-
121
- // we need all available mapping keys :)
122
- let keys : Decorator.Type[] = mappings
123
- .map(mapping => Object.keys(mapping))
124
- .reduce(((previousValue, currentValue) => [...previousValue, ...currentValue])) as Decorator.Type[];
125
-
126
- keys = Array.from(new Set(keys));
127
-
128
- for(let i=0; i<keys.length; i++) {
129
- const representations : Array<Decorator.Representation<any>> = [];
130
-
131
- for(let j=0; j<mappings.length; j++) {
132
- if(hasOwnProperty(mappings[j], keys[i])) {
133
- const value : Decorator.Representation<any> | Array<Decorator.Representation<any>> = mappings[j][keys[i]];
134
-
135
- if(typeof value === 'undefined') {
136
- continue;
137
- }
138
-
139
- if(Array.isArray(value)) {
140
- representations.push(...value);
141
- } else {
142
- representations.push(value);
143
- }
144
- }
145
- }
146
-
147
- // @ts-ignore
148
- result[keys[i]] = representations;
149
- }
150
-
151
- return result;
152
- }
153
- }