@prostojs/mate 0.0.1
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.
- package/LICENSE +21 -0
- package/README.md +199 -0
- package/dist/mate.cjs.prod.js +90 -0
- package/dist/mate.d.ts +35 -0
- package/dist/mate.esm-browser.prod.js +1 -0
- package/dist/mate.esm-bundler.js +86 -0
- package/dist/mate.global.prod.js +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/mate.d.ts +27 -0
- package/dist/src/mate.d.ts.map +1 -0
- package/dist/src/tests/mate.artifacts.d.ts +9 -0
- package/dist/src/tests/mate.artifacts.d.ts.map +1 -0
- package/dist/src/tests/mate.spec.d.ts +2 -0
- package/dist/src/tests/mate.spec.d.ts.map +1 -0
- package/dist/src/types.d.ts +5 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/utils/banner.d.ts +2 -0
- package/dist/src/utils/banner.d.ts.map +1 -0
- package/dist/src/utils/helpers.d.ts +4 -0
- package/dist/src/utils/helpers.d.ts.map +1 -0
- package/dist/src/utils/log.d.ts +3 -0
- package/dist/src/utils/log.d.ts.map +1 -0
- package/dist/src/utils/panic.d.ts +2 -0
- package/dist/src/utils/panic.d.ts.map +1 -0
- package/index.js +2 -0
- package/package.json +86 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 prostojs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# prostojs/mate
|
|
2
|
+
|
|
3
|
+
This small library is TS Metadata Organizer based on [`reflect-metadata`](https://github.com/rbuckton/reflect-metadata). "MA-TE" is for "ME-TA" in reverse order.
|
|
4
|
+
|
|
5
|
+
Make it easier to write, read and type metadata objects. Gather all your metadata at a single metadata key in a typed object and access it with a matter of one call of `mate.read()`
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
`npm install @prostojs/mate`
|
|
10
|
+
|
|
11
|
+
## Quick Example
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { TProstoMetadata, Mate } from '@prostojs/mate'
|
|
15
|
+
|
|
16
|
+
interface TMyMeta extends TProstoMetadata {
|
|
17
|
+
required: boolean
|
|
18
|
+
label: string
|
|
19
|
+
items: string[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Create an instance of Mate with metadata
|
|
23
|
+
// key 'my-metadata-key'
|
|
24
|
+
// This key will be used by Reflect.defineMetadata
|
|
25
|
+
// and Reflect.getMetadata
|
|
26
|
+
const mate = new Mate<TMyMeta>('my-metadata-key')
|
|
27
|
+
|
|
28
|
+
@mate.decorate('required', true)
|
|
29
|
+
@mate.decorate('label', 'My Class')
|
|
30
|
+
@mate.decorate('items', 'first item', true)
|
|
31
|
+
@mate.decorate('items', 'second item', true)
|
|
32
|
+
class MyClass {
|
|
33
|
+
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
console.log(mate.read(MyClass))
|
|
37
|
+
// {
|
|
38
|
+
// params: undefined,
|
|
39
|
+
// items: [ 'first item', 'second item' ],
|
|
40
|
+
// label: 'My Class',
|
|
41
|
+
// required: true
|
|
42
|
+
// }
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Method Decorators
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { Mate } from '@prostojs/mate'
|
|
49
|
+
const mate = new Mate('my-metadata-key')
|
|
50
|
+
class MyClass {
|
|
51
|
+
@mate.decorate('required', true)
|
|
52
|
+
@mate.decorate('label', 'My Method Label')
|
|
53
|
+
methodName() {}
|
|
54
|
+
}
|
|
55
|
+
console.log(mate.read(MyClass, 'methodName'))
|
|
56
|
+
// { params: [], label: 'My Method Label', required: true }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Constructor Params Decorators
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { Mate } from '@prostojs/mate'
|
|
63
|
+
const mate = new Mate('my-metadata-key')
|
|
64
|
+
class MyClass {
|
|
65
|
+
constructor(
|
|
66
|
+
@mate.decorate('label', 'my string')
|
|
67
|
+
str: string,
|
|
68
|
+
@mate.decorate('label', 'my number')
|
|
69
|
+
n: number,
|
|
70
|
+
) {}
|
|
71
|
+
}
|
|
72
|
+
console.log(mate.read(MyClass))
|
|
73
|
+
// {
|
|
74
|
+
// params: [
|
|
75
|
+
// { type: [Function: String], label: 'my string' },
|
|
76
|
+
// { type: [Function: Number], label: 'my number' }
|
|
77
|
+
// ]
|
|
78
|
+
// }
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Method Params Decorators
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { Mate } from '@prostojs/mate'
|
|
85
|
+
const mate = new Mate('my-metadata-key')
|
|
86
|
+
class MyClass {
|
|
87
|
+
myMethod(
|
|
88
|
+
@mate.decorate('label', 'my string')
|
|
89
|
+
str: string,
|
|
90
|
+
@mate.decorate('label', 'my number')
|
|
91
|
+
n: number,
|
|
92
|
+
) {}
|
|
93
|
+
}
|
|
94
|
+
console.log(mate.read(MyClass, 'myMethod'))
|
|
95
|
+
// {
|
|
96
|
+
// params: [
|
|
97
|
+
// { type: [Function: String], label: 'my string' },
|
|
98
|
+
// { type: [Function: Number], label: 'my number' }
|
|
99
|
+
// ]
|
|
100
|
+
// }
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Variety of use of Mate
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { TProstoMetadata, Mate } from '@prostojs/mate'
|
|
107
|
+
|
|
108
|
+
// Define the metadata type
|
|
109
|
+
interface MyMeta extends TProstoMetadata {
|
|
110
|
+
arg0: string
|
|
111
|
+
arg1: string
|
|
112
|
+
n: number
|
|
113
|
+
simple: boolean
|
|
114
|
+
}
|
|
115
|
+
const mate = new Mate<MyMeta>('my-metadata-key')
|
|
116
|
+
|
|
117
|
+
// Example of simple decorator
|
|
118
|
+
const SimpleDecorator = mate.decorate('simple', true)
|
|
119
|
+
|
|
120
|
+
// Example of factory decorator
|
|
121
|
+
const FactoryDecorator = (arg: string) => mate.decorate('arg0', arg)
|
|
122
|
+
|
|
123
|
+
// Example of complex decorator
|
|
124
|
+
// mate.apply returns a new decorator that applies
|
|
125
|
+
// all passed decorators
|
|
126
|
+
const ComplexDecorator = (arg: string) => mate.apply(
|
|
127
|
+
SimpleDecorator,
|
|
128
|
+
FactoryDecorator(arg),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
// Example of Callback Decorator
|
|
132
|
+
// meta.decorate may accept callback function
|
|
133
|
+
// instead of key/value pare in case if some
|
|
134
|
+
// complex logic need
|
|
135
|
+
const CallbackDecorator = (arg1: string, n: number) => mate.decorate(meta => {
|
|
136
|
+
meta.arg1 = [meta.arg0, arg1].join(', ')
|
|
137
|
+
meta.n = meta.simple ? n : n * n
|
|
138
|
+
return meta
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
// Applying decorators to our classes
|
|
142
|
+
@SimpleDecorator
|
|
143
|
+
@FactoryDecorator('arg0 value')
|
|
144
|
+
class MyClass1 {
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
console.log(mate.read(MyClass1))
|
|
148
|
+
// { params: undefined, arg0: 'arg0 value', simple: true }
|
|
149
|
+
|
|
150
|
+
@CallbackDecorator('arg1 value', 5)
|
|
151
|
+
@ComplexDecorator('arg0 for complex decorator')
|
|
152
|
+
class MyClass2 {
|
|
153
|
+
|
|
154
|
+
}
|
|
155
|
+
console.log(mate.read(MyClass2))
|
|
156
|
+
// {
|
|
157
|
+
// params: undefined,
|
|
158
|
+
// simple: true,
|
|
159
|
+
// arg0: 'arg0 for complex decorator',
|
|
160
|
+
// arg1: 'arg0 for complex decorator, arg1 value',
|
|
161
|
+
// n: 5
|
|
162
|
+
// }
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## TS Typing
|
|
166
|
+
|
|
167
|
+
Metadata type is shared for class metadata and class method metadata and based on `TProstoMetadata` interface.
|
|
168
|
+
|
|
169
|
+
Constructor params and method params metadata are stored in `TProstoMetadata['params']` array.
|
|
170
|
+
|
|
171
|
+
By default `TProstoMetadata['params']` has type `TProstoParamsMetadata`.
|
|
172
|
+
|
|
173
|
+
Here's an example of fully typed metadata:
|
|
174
|
+
```ts
|
|
175
|
+
import { TProstoMetadata, TProstoParamsMetadata, Mate } from '@prostojs/mate'
|
|
176
|
+
|
|
177
|
+
interface TMyParamsMeta extends TProstoParamsMetadata {
|
|
178
|
+
paramProp1: string
|
|
179
|
+
paramProp2: number
|
|
180
|
+
}
|
|
181
|
+
interface TMyMeta extends TProstoMetadata<TMyParamsMeta> {
|
|
182
|
+
prop1: string
|
|
183
|
+
prop2: number
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// class and method metadata will look like this:
|
|
187
|
+
// {
|
|
188
|
+
// prop1: string
|
|
189
|
+
// prop2: number
|
|
190
|
+
// params: { // inherited from TProstoMetadata
|
|
191
|
+
// type: Function // inherited from TProstoParamsMetadata
|
|
192
|
+
// paramProp1: string
|
|
193
|
+
// paramProp2: number
|
|
194
|
+
// }[]
|
|
195
|
+
// }
|
|
196
|
+
|
|
197
|
+
// pass TMyMeta to Mate constructor to define types for mate instance
|
|
198
|
+
const mate = new Mate<TMyMeta>('my-metadata-key')
|
|
199
|
+
```
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const banner = () => `[prostojs/mate][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
6
|
+
|
|
7
|
+
function logError(error) {
|
|
8
|
+
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function panic(error) {
|
|
12
|
+
logError(error);
|
|
13
|
+
return new Error(error);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function getConstructor(instance) {
|
|
17
|
+
return Object.getPrototypeOf(instance).constructor;
|
|
18
|
+
}
|
|
19
|
+
function isConstructor(v) {
|
|
20
|
+
return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
require('reflect-metadata');
|
|
24
|
+
class Mate {
|
|
25
|
+
constructor(workspace) {
|
|
26
|
+
this.workspace = workspace;
|
|
27
|
+
}
|
|
28
|
+
set(args, key, value, isArray) {
|
|
29
|
+
var _a;
|
|
30
|
+
let meta = Reflect.getMetadata(this.workspace, args.target, args.propKey) || {};
|
|
31
|
+
const { index } = args;
|
|
32
|
+
const cb = typeof key === 'function' ? key : undefined;
|
|
33
|
+
let data = meta;
|
|
34
|
+
if (!data.params) {
|
|
35
|
+
data.params = (_a = Reflect.getMetadata('design:paramtypes', args.target, args.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
|
|
36
|
+
}
|
|
37
|
+
if (typeof index === 'number') {
|
|
38
|
+
data.params = data.params || [];
|
|
39
|
+
data.params[index] = data.params[index] || {
|
|
40
|
+
type: undefined,
|
|
41
|
+
};
|
|
42
|
+
if (cb) {
|
|
43
|
+
data.params[index] = cb(data.params[index]);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
data = data.params[index];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (typeof key !== 'function') {
|
|
50
|
+
if (isArray) {
|
|
51
|
+
const newArray = (data[key] || []);
|
|
52
|
+
if (!Array.isArray(newArray)) {
|
|
53
|
+
panic('Mate.add (isArray=true) called for non-array metadata');
|
|
54
|
+
}
|
|
55
|
+
newArray.unshift(value);
|
|
56
|
+
data[key] = newArray;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
data[key] = value;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (cb && typeof index !== 'number') {
|
|
63
|
+
meta = cb(data);
|
|
64
|
+
}
|
|
65
|
+
Reflect.defineMetadata(this.workspace, meta, args.target, args.propKey);
|
|
66
|
+
}
|
|
67
|
+
read(target, propKey) {
|
|
68
|
+
return Reflect.getMetadata(this.workspace, typeof propKey === 'string' ? isConstructor(target) ? target.prototype : target : isConstructor(target) ? target : getConstructor(target), propKey);
|
|
69
|
+
}
|
|
70
|
+
apply(...decorators) {
|
|
71
|
+
return ((target, propKey, descriptor) => {
|
|
72
|
+
for (const d of decorators) {
|
|
73
|
+
d(target, propKey, descriptor);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
decorate(key, value, isArray) {
|
|
78
|
+
return ((target, propKey, descriptor) => {
|
|
79
|
+
const args = {
|
|
80
|
+
target,
|
|
81
|
+
propKey,
|
|
82
|
+
descriptor: typeof descriptor === 'number' ? undefined : descriptor,
|
|
83
|
+
index: typeof descriptor === 'number' ? descriptor : undefined,
|
|
84
|
+
};
|
|
85
|
+
this.set(args, key, value, isArray);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
exports.Mate = Mate;
|
package/dist/mate.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export declare class Mate<T extends TProstoMetadata = TProstoMetadata> {
|
|
2
|
+
protected workspace: string;
|
|
3
|
+
constructor(workspace: string);
|
|
4
|
+
set<R extends T = T, RP = R['params'][0]>(args: TMergedDecoratorArgs, cb: ((meta: R & RP) => R & RP)): void;
|
|
5
|
+
set<R extends T = T, RP = R['params'][0]>(args: TMergedDecoratorArgs, key: keyof R | keyof RP, value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined): void;
|
|
6
|
+
set<R extends T = T, RP = R['params'][0]>(args: TMergedDecoratorArgs, key: keyof R | keyof RP, value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined, isArray: boolean | undefined): void;
|
|
7
|
+
read<R = T>(target: TFunction | TObject, propKey?: string | symbol): R | undefined;
|
|
8
|
+
apply(...decorators: (MethodDecorator & ClassDecorator & ParameterDecorator)[]): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
9
|
+
decorate<R extends T = T, RP = R['params'][0]>(cb: ((meta: R & RP) => R & RP)): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
10
|
+
decorate<R extends T = T, RP = R['params'][0]>(key: keyof R | keyof RP | ((meta: R & RP) => R & RP), value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
11
|
+
decorate<R extends T = T, RP = R['params'][0]>(key: keyof R | keyof RP | ((meta: R & RP) => R & RP), value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined, isArray: boolean | undefined): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare type TAny = any;
|
|
15
|
+
|
|
16
|
+
declare type TFunction = Function;
|
|
17
|
+
|
|
18
|
+
export declare interface TMergedDecoratorArgs {
|
|
19
|
+
target: TFunction | TObject;
|
|
20
|
+
propKey?: string | symbol;
|
|
21
|
+
descriptor?: TypedPropertyDescriptor<TAny>;
|
|
22
|
+
index?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare type TObject = object;
|
|
26
|
+
|
|
27
|
+
export declare interface TProstoMetadata<TParams extends TProstoParamsMetadata = TProstoParamsMetadata> {
|
|
28
|
+
params: TParams[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare interface TProstoParamsMetadata {
|
|
32
|
+
type: TFunction;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){console.error(`[91m[1m[prostojs/mate][${(new Date).toISOString().replace("T"," ").replace(/\.\d{3}z$/i,"")}] `+e+"[0m")}function t(e){return"function"==typeof e&&Object.getOwnPropertyNames(e).includes("prototype")&&!Object.getOwnPropertyNames(e).includes("caller")&&!!e.name}require("reflect-metadata");class r{constructor(e){this.workspace=e}set(t,r,a,o){var p;let s=Reflect.getMetadata(this.workspace,t.target,t.propKey)||{};const{index:n}=t,c="function"==typeof r?r:void 0;let i=s;if(i.params||(i.params=null===(p=Reflect.getMetadata("design:paramtypes",t.target,t.propKey))||void 0===p?void 0:p.map((e=>({type:e})))),"number"==typeof n&&(i.params=i.params||[],i.params[n]=i.params[n]||{type:void 0},c?i.params[n]=c(i.params[n]):i=i.params[n]),"function"!=typeof r)if(o){const t=i[r]||[];Array.isArray(t)||(e(f="Mate.add (isArray=true) called for non-array metadata"),new Error(f)),t.unshift(a),i[r]=t}else i[r]=a;else c&&"number"!=typeof n&&(s=c(i));var f;Reflect.defineMetadata(this.workspace,s,t.target,t.propKey)}read(e,r){return Reflect.getMetadata(this.workspace,"string"==typeof r?t(e)?e.prototype:e:t(e)?e:Object.getPrototypeOf(e).constructor,r)}apply(...e){return(t,r,a)=>{for(const o of e)o(t,r,a)}}decorate(e,t,r){return(a,o,p)=>{this.set({target:a,propKey:o,descriptor:"number"==typeof p?void 0:p,index:"number"==typeof p?p:void 0},e,t,r)}}}export{r as Mate};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const banner = () => `[prostojs/mate][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
|
|
2
|
+
|
|
3
|
+
function logError(error) {
|
|
4
|
+
console.error('[91m' + '[1m' + banner() + error + '[0m');
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function panic(error) {
|
|
8
|
+
logError(error);
|
|
9
|
+
return new Error(error);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getConstructor(instance) {
|
|
13
|
+
return Object.getPrototypeOf(instance).constructor;
|
|
14
|
+
}
|
|
15
|
+
function isConstructor(v) {
|
|
16
|
+
return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
require('reflect-metadata');
|
|
20
|
+
class Mate {
|
|
21
|
+
constructor(workspace) {
|
|
22
|
+
this.workspace = workspace;
|
|
23
|
+
}
|
|
24
|
+
set(args, key, value, isArray) {
|
|
25
|
+
var _a;
|
|
26
|
+
let meta = Reflect.getMetadata(this.workspace, args.target, args.propKey) || {};
|
|
27
|
+
const { index } = args;
|
|
28
|
+
const cb = typeof key === 'function' ? key : undefined;
|
|
29
|
+
let data = meta;
|
|
30
|
+
if (!data.params) {
|
|
31
|
+
data.params = (_a = Reflect.getMetadata('design:paramtypes', args.target, args.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
|
|
32
|
+
}
|
|
33
|
+
if (typeof index === 'number') {
|
|
34
|
+
data.params = data.params || [];
|
|
35
|
+
data.params[index] = data.params[index] || {
|
|
36
|
+
type: undefined,
|
|
37
|
+
};
|
|
38
|
+
if (cb) {
|
|
39
|
+
data.params[index] = cb(data.params[index]);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
data = data.params[index];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (typeof key !== 'function') {
|
|
46
|
+
if (isArray) {
|
|
47
|
+
const newArray = (data[key] || []);
|
|
48
|
+
if (!Array.isArray(newArray)) {
|
|
49
|
+
panic('Mate.add (isArray=true) called for non-array metadata');
|
|
50
|
+
}
|
|
51
|
+
newArray.unshift(value);
|
|
52
|
+
data[key] = newArray;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
data[key] = value;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (cb && typeof index !== 'number') {
|
|
59
|
+
meta = cb(data);
|
|
60
|
+
}
|
|
61
|
+
Reflect.defineMetadata(this.workspace, meta, args.target, args.propKey);
|
|
62
|
+
}
|
|
63
|
+
read(target, propKey) {
|
|
64
|
+
return Reflect.getMetadata(this.workspace, typeof propKey === 'string' ? isConstructor(target) ? target.prototype : target : isConstructor(target) ? target : getConstructor(target), propKey);
|
|
65
|
+
}
|
|
66
|
+
apply(...decorators) {
|
|
67
|
+
return ((target, propKey, descriptor) => {
|
|
68
|
+
for (const d of decorators) {
|
|
69
|
+
d(target, propKey, descriptor);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
decorate(key, value, isArray) {
|
|
74
|
+
return ((target, propKey, descriptor) => {
|
|
75
|
+
const args = {
|
|
76
|
+
target,
|
|
77
|
+
propKey,
|
|
78
|
+
descriptor: typeof descriptor === 'number' ? undefined : descriptor,
|
|
79
|
+
index: typeof descriptor === 'number' ? descriptor : undefined,
|
|
80
|
+
};
|
|
81
|
+
this.set(args, key, value, isArray);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { Mate };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var ProstoMate=function(e){"use strict";function t(e){console.error(`[91m[1m[prostojs/mate][${(new Date).toISOString().replace("T"," ").replace(/\.\d{3}z$/i,"")}] `+e+"[0m")}function r(e){return"function"==typeof e&&Object.getOwnPropertyNames(e).includes("prototype")&&!Object.getOwnPropertyNames(e).includes("caller")&&!!e.name}require("reflect-metadata");return e.Mate=class{constructor(e){this.workspace=e}set(e,r,a,o){var p;let s=Reflect.getMetadata(this.workspace,e.target,e.propKey)||{};const{index:n}=e,c="function"==typeof r?r:void 0;let i=s;if(i.params||(i.params=null===(p=Reflect.getMetadata("design:paramtypes",e.target,e.propKey))||void 0===p?void 0:p.map((e=>({type:e})))),"number"==typeof n&&(i.params=i.params||[],i.params[n]=i.params[n]||{type:void 0},c?i.params[n]=c(i.params[n]):i=i.params[n]),"function"!=typeof r)if(o){const e=i[r]||[];Array.isArray(e)||(t(f="Mate.add (isArray=true) called for non-array metadata"),new Error(f)),e.unshift(a),i[r]=e}else i[r]=a;else c&&"number"!=typeof n&&(s=c(i));var f;Reflect.defineMetadata(this.workspace,s,e.target,e.propKey)}read(e,t){return Reflect.getMetadata(this.workspace,"string"==typeof t?r(e)?e.prototype:e:r(e)?e:Object.getPrototypeOf(e).constructor,t)}apply(...e){return(t,r,a)=>{for(const o of e)o(t,r,a)}}decorate(e,t,r){return(a,o,p)=>{this.set({target:a,propKey:o,descriptor:"number"==typeof p?void 0:p,index:"number"==typeof p?p:void 0},e,t,r)}}},Object.defineProperty(e,"__esModule",{value:!0}),e}({});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import { TAny, TFunction, TObject } from './types';
|
|
3
|
+
export interface TMergedDecoratorArgs {
|
|
4
|
+
target: TFunction | TObject;
|
|
5
|
+
propKey?: string | symbol;
|
|
6
|
+
descriptor?: TypedPropertyDescriptor<TAny>;
|
|
7
|
+
index?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface TProstoMetadata<TParams extends TProstoParamsMetadata = TProstoParamsMetadata> {
|
|
10
|
+
params: TParams[];
|
|
11
|
+
}
|
|
12
|
+
export interface TProstoParamsMetadata {
|
|
13
|
+
type: TFunction;
|
|
14
|
+
}
|
|
15
|
+
export declare class Mate<T extends TProstoMetadata = TProstoMetadata> {
|
|
16
|
+
protected workspace: string;
|
|
17
|
+
constructor(workspace: string);
|
|
18
|
+
set<R extends T = T, RP = R['params'][0]>(args: TMergedDecoratorArgs, cb: ((meta: R & RP) => R & RP)): void;
|
|
19
|
+
set<R extends T = T, RP = R['params'][0]>(args: TMergedDecoratorArgs, key: keyof R | keyof RP, value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined): void;
|
|
20
|
+
set<R extends T = T, RP = R['params'][0]>(args: TMergedDecoratorArgs, key: keyof R | keyof RP, value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined, isArray: boolean | undefined): void;
|
|
21
|
+
read<R = T>(target: TFunction | TObject, propKey?: string | symbol): R | undefined;
|
|
22
|
+
apply(...decorators: (MethodDecorator & ClassDecorator & ParameterDecorator)[]): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
23
|
+
decorate<R extends T = T, RP = R['params'][0]>(cb: ((meta: R & RP) => R & RP)): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
24
|
+
decorate<R extends T = T, RP = R['params'][0]>(key: keyof R | keyof RP | ((meta: R & RP) => R & RP), value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
25
|
+
decorate<R extends T = T, RP = R['params'][0]>(key: keyof R | keyof RP | ((meta: R & RP) => R & RP), value: ((R & RP)[keyof R] & (R & RP)[keyof RP]) | undefined, isArray: boolean | undefined): MethodDecorator & ClassDecorator & ParameterDecorator;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=mate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mate.d.ts","sourceRoot":"","sources":["../../src/mate.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mate.artifacts.d.ts","sourceRoot":"","sources":["../../../src/tests/mate.artifacts.ts"],"names":[],"mappings":"AASA,qBAGa,yBAAyB;gBAK1B,EAAE,EAAE,MAAM;IAQlB,IAAI,CAII,EAAE,EAAE,MAAM;IAgBlB,KAAK,CAMG,EAAE,EAAE,MAAM;CAGrB;AAED,qBACa,0BAA0B;IAInC,IAAI;CAGP"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mate.spec.d.ts","sourceRoot":"","sources":["../../../src/tests/mate.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AACA,oBAAY,IAAI,GAAG,GAAG,CAAA;AACtB,oBAAY,OAAO,GAAG,MAAM,CAAA;AAC5B,oBAAY,SAAS,GAAG,QAAQ,CAAA;AAChC,oBAAY,iBAAiB,CAAC,CAAC,GAAG,OAAO,IAAI,KAAK,GAAG,IAAI,EAAG,IAAI,EAAE,KAAK,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"banner.d.ts","sourceRoot":"","sources":["../../../src/utils/banner.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,MAAM,cAAsG,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { TAny, TClassConstructor, TFunction, TObject } from '../types';
|
|
2
|
+
export declare function getConstructor<T = TObject>(instance: T): TFunction;
|
|
3
|
+
export declare function isConstructor<T = TAny>(v: T): v is T & TClassConstructor;
|
|
4
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../../src/utils/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,iBAAiB,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAEtE,wBAAgB,cAAc,CAAC,CAAC,GAAG,OAAO,EAAE,QAAQ,EAAE,CAAC,GAAG,SAAS,CAGlE;AAED,wBAAgB,aAAa,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,iBAAiB,CAExE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../../../src/utils/log.ts"],"names":[],"mappings":"AAGA,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,QAE/B;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,QAErC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"panic.d.ts","sourceRoot":"","sources":["../../../src/utils/panic.ts"],"names":[],"mappings":"AAGA,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,SAGlC"}
|
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@prostojs/mate",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "MATE is TS Metadata Organizer",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "dist/mate.esm-bundler.js",
|
|
7
|
+
"unpkg": "dist/mate.global.prod.js",
|
|
8
|
+
"jsdelivr": "dist/mate.global.prod.js",
|
|
9
|
+
"types": "dist/mate.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"index.js",
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "node ./scripts/build",
|
|
16
|
+
"release": "node ./scripts/release",
|
|
17
|
+
"test": "jest --runInBand",
|
|
18
|
+
"test:cov": "jest --runInBand --coverage",
|
|
19
|
+
"lint": "eslint --ext .ts src/**.ts --fix",
|
|
20
|
+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
|
|
21
|
+
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/prostojs/mate.git"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"metadata",
|
|
29
|
+
"decorators",
|
|
30
|
+
"mate",
|
|
31
|
+
"prostojs"
|
|
32
|
+
],
|
|
33
|
+
"buildOptions": {
|
|
34
|
+
"name": "ProstoMate",
|
|
35
|
+
"formats": [
|
|
36
|
+
"esm-bundler",
|
|
37
|
+
"esm-browser",
|
|
38
|
+
"cjs",
|
|
39
|
+
"global"
|
|
40
|
+
]
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"reflect-metadata": "^0.1.13"
|
|
44
|
+
},
|
|
45
|
+
"gitHooks": {
|
|
46
|
+
"commit-msg": "node scripts/verifyCommit.js"
|
|
47
|
+
},
|
|
48
|
+
"author": "Artem Maltsev",
|
|
49
|
+
"license": "MIT",
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/prostojs/mate/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/prostojs/mate#readme",
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@microsoft/api-extractor": "^7.15.1",
|
|
56
|
+
"@prostojs/dye": "^0.2.0",
|
|
57
|
+
"@rollup/plugin-commonjs": "^21.0.0",
|
|
58
|
+
"@rollup/plugin-node-resolve": "^13.0.6",
|
|
59
|
+
"@rollup/plugin-replace": "^3.0.0",
|
|
60
|
+
"@types/jest": "^27.0.2",
|
|
61
|
+
"@types/node": "^16.10.3",
|
|
62
|
+
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
|
63
|
+
"@typescript-eslint/parser": "^4.33.0",
|
|
64
|
+
"brotli": "^1.3.2",
|
|
65
|
+
"conventional-changelog": "^3.1.24",
|
|
66
|
+
"conventional-changelog-cli": "^2.1.1",
|
|
67
|
+
"enquirer": "^2.3.6",
|
|
68
|
+
"eslint": "^7.32.0",
|
|
69
|
+
"eslint-config-prettier": "^8.3.0",
|
|
70
|
+
"eslint-plugin-import": "^2.24.2",
|
|
71
|
+
"execa": "^5.1.1",
|
|
72
|
+
"fs-extra": "^10.0.0",
|
|
73
|
+
"jest": "^27.2.5",
|
|
74
|
+
"minimist": "^1.2.5",
|
|
75
|
+
"rollup": "^2.58.0",
|
|
76
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
77
|
+
"rollup-plugin-typescript2": "^0.30.0",
|
|
78
|
+
"semver": "^7.3.5",
|
|
79
|
+
"ts-jest": "^27.0.5",
|
|
80
|
+
"ts-node": "^10.8.2",
|
|
81
|
+
"tslib": "^2.3.1",
|
|
82
|
+
"typescript": "^4.4.3",
|
|
83
|
+
"yorkie": "^2.0.0",
|
|
84
|
+
"zlib": "^1.0.5"
|
|
85
|
+
}
|
|
86
|
+
}
|