@prostojs/mate 0.3.2 → 0.4.0

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/package.json CHANGED
@@ -1,81 +1,70 @@
1
1
  {
2
- "name": "@prostojs/mate",
3
- "version": "0.3.2",
4
- "description": "MATE is TS Metadata Organizer",
5
- "main": "index.js",
6
- "module": "dist/mate.esm-bundler.js",
7
- "types": "dist/mate.d.ts",
8
- "files": [
9
- "index.js",
10
- "dist"
11
- ],
12
- "scripts": {
13
- "build": "node ./scripts/build",
14
- "release": "node ./scripts/release",
15
- "test": "jest --runInBand",
16
- "test:cov": "jest --runInBand --coverage",
17
- "lint": "eslint --ext .ts src/**.ts --fix",
18
- "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
19
- "version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
20
- },
21
- "repository": {
22
- "type": "git",
23
- "url": "git+https://github.com/prostojs/mate.git"
24
- },
25
- "keywords": [
26
- "metadata",
27
- "decorators",
28
- "mate",
29
- "prostojs"
30
- ],
31
- "buildOptions": {
32
- "name": "ProstoMate",
33
- "formats": [
34
- "esm-bundler",
35
- "cjs"
36
- ]
37
- },
38
- "dependencies": {},
39
- "gitHooks": {
40
- "commit-msg": "node scripts/verifyCommit.js"
41
- },
42
- "author": "Artem Maltsev",
43
- "license": "MIT",
44
- "bugs": {
45
- "url": "https://github.com/prostojs/mate/issues"
46
- },
47
- "homepage": "https://github.com/prostojs/mate#readme",
48
- "devDependencies": {
49
- "@microsoft/api-extractor": "^7.30.0",
50
- "@prostojs/dye": "^0.3.0",
51
- "@rollup/plugin-commonjs": "^21.0.0",
52
- "@rollup/plugin-node-resolve": "^13.0.6",
53
- "@rollup/plugin-replace": "^3.0.0",
54
- "@types/jest": "^27.5.2",
55
- "@types/node": "^16.11.57",
56
- "@typescript-eslint/eslint-plugin": "^4.33.0",
57
- "@typescript-eslint/parser": "^4.33.0",
58
- "brotli": "^1.3.3",
59
- "conventional-changelog": "^3.1.24",
60
- "conventional-changelog-cli": "^2.1.1",
61
- "enquirer": "^2.3.6",
62
- "eslint": "^7.32.0",
63
- "eslint-config-prettier": "^8.3.0",
64
- "eslint-plugin-import": "^2.24.2",
65
- "execa": "^5.1.1",
66
- "fs-extra": "^10.0.0",
67
- "jest": "^27.2.5",
68
- "minimist": "^1.2.6",
69
- "reflect-metadata": "^0.1.13",
70
- "rollup": "^2.58.0",
71
- "rollup-plugin-terser": "^7.0.2",
72
- "rollup-plugin-typescript2": "^0.30.0",
73
- "semver": "^7.3.5",
74
- "ts-jest": "^27.0.5",
75
- "ts-node": "^10.9.1",
76
- "tslib": "^2.3.1",
77
- "typescript": "^4.8.2",
78
- "yorkie": "^2.0.0",
79
- "zlib": "^1.0.5"
80
- }
2
+ "name": "@prostojs/mate",
3
+ "version": "0.4.0",
4
+ "description": "MATE is TS Metadata Organizer",
5
+ "keywords": [
6
+ "decorators",
7
+ "mate",
8
+ "metadata",
9
+ "prostojs"
10
+ ],
11
+ "homepage": "https://github.com/prostojs/mate#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/prostojs/mate/issues"
14
+ },
15
+ "license": "MIT",
16
+ "author": "Artem Maltsev",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/prostojs/mate.git"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "main": "dist/index.cjs",
25
+ "module": "dist/index.mjs",
26
+ "types": "dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.mjs",
31
+ "require": "./dist/index.cjs"
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "scripts": {
36
+ "build": "tsc -p tsconfig.build.json && rolldown -c rolldown.config.mjs && rm -rf dts-build",
37
+ "release": "node ./scripts/release",
38
+ "test": "vitest run",
39
+ "test:cov": "vitest run --coverage",
40
+ "lint": "oxlint",
41
+ "lint:fix": "oxlint --fix",
42
+ "format": "oxfmt",
43
+ "format:check": "oxfmt --check",
44
+ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
45
+ "version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md"
46
+ },
47
+ "devDependencies": {
48
+ "@prostojs/dye": "^0.4.9",
49
+ "@swc/core": "^1.15.13",
50
+ "@types/node": "^22.19.11",
51
+ "conventional-changelog": "^3.1.25",
52
+ "conventional-changelog-cli": "^2.2.2",
53
+ "enquirer": "^2.4.1",
54
+ "execa": "^5.1.1",
55
+ "minimist": "^1.2.8",
56
+ "oxfmt": "^0.35.0",
57
+ "oxlint": "^1.50.0",
58
+ "reflect-metadata": "^0.1.14",
59
+ "rolldown": "1.0.0-rc.5",
60
+ "rolldown-plugin-dts": "^0.22.1",
61
+ "semver": "^7.7.4",
62
+ "typescript": "^5.9.3",
63
+ "unplugin-swc": "^1.5.9",
64
+ "vitest": "^3.2.4",
65
+ "yorkie": "^2.0.0"
66
+ },
67
+ "gitHooks": {
68
+ "commit-msg": "node scripts/verifyCommit.js"
69
+ }
81
70
  }
@@ -1,232 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- function getConstructor(instance) {
6
- return isConstructor(instance) ?
7
- instance : instance.constructor ?
8
- instance.constructor : Object.getPrototypeOf(instance).constructor;
9
- }
10
- function isConstructor(v) {
11
- return typeof v === 'function' && Object.getOwnPropertyNames(v).includes('prototype') && !Object.getOwnPropertyNames(v).includes('caller') && !!v.name;
12
- }
13
-
14
- const classMetadata = new WeakMap();
15
- const paramMetadata = new WeakMap();
16
- const root = typeof global === 'object' ? global : typeof self === 'object' ? self : {};
17
- function getMetaObject(target, prop) {
18
- const isParam = typeof prop !== 'undefined';
19
- const metadata = isParam ? paramMetadata : classMetadata;
20
- const targetKey = getConstructor(target);
21
- let meta = metadata.get(targetKey);
22
- if (!meta) {
23
- meta = {};
24
- metadata.set(targetKey, meta);
25
- }
26
- if (isParam) {
27
- meta = (meta[prop] =
28
- meta[prop] || {});
29
- }
30
- return meta;
31
- }
32
- const _reflect = {
33
- getOwnMetadata(key, target, prop) {
34
- return getMetaObject(target, prop)[key];
35
- },
36
- defineMetadata(key, data, target, prop) {
37
- const meta = getMetaObject(target, prop);
38
- meta[key] = data;
39
- },
40
- metadata(key, data) {
41
- return ((target, propKey) => {
42
- Reflect$1.defineMetadata(key, data, target, propKey);
43
- });
44
- },
45
- };
46
- if (!root.Reflect) {
47
- root.Reflect = _reflect;
48
- }
49
- else {
50
- const funcs = [
51
- 'getOwnMetadata',
52
- 'defineMetadata',
53
- 'metadata',
54
- ];
55
- const target = root.Reflect;
56
- for (const func of funcs) {
57
- if (typeof target[func] !== 'function') {
58
- Object.defineProperty(target, func, { configurable: true, writable: true, value: _reflect[func] });
59
- }
60
- }
61
- }
62
- const Reflect$1 = _reflect;
63
-
64
- const Reflect = (global === null || global === void 0 ? void 0 : global.Reflect) || (self === null || self === void 0 ? void 0 : self.Reflect) || Reflect$1;
65
- class Mate {
66
- constructor(workspace, options = {}) {
67
- this.workspace = workspace;
68
- this.options = options;
69
- this.logger = options.logger || console;
70
- }
71
- set(args, key, value, isArray) {
72
- var _a;
73
- let level = 'CLASS';
74
- const newArgs = args.level === 'CLASS' ? { target: args.target }
75
- : args.level === 'PROP' ? { target: args.target, propKey: args.propKey }
76
- : args;
77
- let meta = (Reflect.getOwnMetadata(this.workspace, newArgs.target, newArgs.propKey) || {});
78
- if (newArgs.propKey && this.options.readReturnType && !meta.returnType && args.descriptor) {
79
- meta.returnType = Reflect.getOwnMetadata('design:returntype', newArgs.target, newArgs.propKey);
80
- }
81
- if (newArgs.propKey && this.options.readType && !meta.type) {
82
- meta.type = Reflect.getOwnMetadata('design:type', newArgs.target, newArgs.propKey);
83
- }
84
- const { index } = newArgs;
85
- const cb = typeof key === 'function' ? key : undefined;
86
- let data = meta;
87
- if (!data.params) {
88
- data.params = (_a = Reflect.getOwnMetadata('design:paramtypes', newArgs.target, newArgs.propKey)) === null || _a === void 0 ? void 0 : _a.map((f) => ({ type: f }));
89
- }
90
- if (typeof index === 'number') {
91
- level = 'PARAM';
92
- data.params = data.params || [];
93
- data.params[index] = data.params[index] || {
94
- type: undefined,
95
- };
96
- if (cb) {
97
- data.params[index] = cb(data.params[index], level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
98
- }
99
- else {
100
- data = data.params[index];
101
- }
102
- }
103
- else if (!index && !args.descriptor && args.propKey && this.options.collectPropKeys && args.level !== 'CLASS') {
104
- this.set({ ...args, level: 'CLASS' }, (meta) => {
105
- if (!meta.properties) {
106
- meta.properties = [args.propKey];
107
- }
108
- else if (!meta.properties.includes(args.propKey)) {
109
- meta.properties.push(args.propKey);
110
- }
111
- return meta;
112
- });
113
- }
114
- level = typeof index === 'number' ? 'PARAM' : newArgs.propKey && newArgs.descriptor ? 'METHOD' : newArgs.propKey ? 'PROP' : 'CLASS';
115
- if (typeof key !== 'function') {
116
- if (isArray) {
117
- const newArray = (data[key] || []);
118
- if (!Array.isArray(newArray)) {
119
- this.logger.error('Mate.add (isArray=true) called for non-array metadata');
120
- }
121
- newArray.unshift(value);
122
- data[key] = newArray;
123
- }
124
- else {
125
- data[key] = value;
126
- }
127
- }
128
- else if (cb && typeof index !== 'number') {
129
- meta = cb(data, level, args.propKey, typeof args.index === 'number' ? args.index : undefined);
130
- }
131
- Reflect.defineMetadata(this.workspace, meta, newArgs.target, newArgs.propKey);
132
- }
133
- read(target, propKey) {
134
- var _a;
135
- const isConstr = isConstructor(target);
136
- const constructor = isConstr ? target : getConstructor(target);
137
- const proto = constructor.prototype;
138
- let ownMeta = Reflect.getOwnMetadata(this.workspace, typeof propKey === 'string' ? proto : constructor, propKey);
139
- if (ownMeta && propKey === undefined && ownMeta.params === undefined) {
140
- const parent = Object.getPrototypeOf(constructor);
141
- if (typeof parent === 'function' &&
142
- parent !== fnProto &&
143
- parent !== constructor) {
144
- ownMeta.params = (_a = this.read(parent)) === null || _a === void 0 ? void 0 : _a.params;
145
- }
146
- }
147
- if (this.options.inherit) {
148
- const inheritFn = typeof this.options.inherit === 'function'
149
- ? this.options.inherit
150
- : undefined;
151
- let shouldInherit = this.options.inherit;
152
- if (inheritFn) {
153
- if (typeof propKey === 'string') {
154
- const classMeta = Reflect.getOwnMetadata(this.workspace, constructor);
155
- shouldInherit = inheritFn(classMeta, ownMeta, 'PROP', propKey);
156
- }
157
- else {
158
- shouldInherit = inheritFn(ownMeta, ownMeta, 'CLASS');
159
- }
160
- }
161
- if (shouldInherit) {
162
- const parent = Object.getPrototypeOf(constructor);
163
- if (typeof parent === 'function' &&
164
- parent !== fnProto &&
165
- parent !== constructor) {
166
- const inheritedMeta = (this.read(parent, propKey) ||
167
- {});
168
- const ownParams = ownMeta === null || ownMeta === void 0 ? void 0 : ownMeta.params;
169
- ownMeta = { ...inheritedMeta, ...ownMeta };
170
- if (typeof propKey === 'string' &&
171
- ownParams &&
172
- (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params)) {
173
- for (let i = 0; i < ownParams.length; i++) {
174
- if (typeof (inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i]) !==
175
- 'undefined') {
176
- const ownParam = ownParams[i];
177
- if (ownMeta.params &&
178
- inheritFn &&
179
- inheritFn(ownMeta, ownParam, 'PARAM', typeof propKey === 'string'
180
- ? propKey
181
- : undefined)) {
182
- ownMeta.params[i] = {
183
- ...inheritedMeta === null || inheritedMeta === void 0 ? void 0 : inheritedMeta.params[i],
184
- ...ownParams[i],
185
- };
186
- }
187
- }
188
- }
189
- }
190
- }
191
- }
192
- }
193
- return ownMeta;
194
- }
195
- apply(...decorators) {
196
- return ((target, propKey, descriptor) => {
197
- for (const d of decorators) {
198
- d(target, propKey, descriptor);
199
- }
200
- });
201
- }
202
- decorate(key, value, isArray, level) {
203
- return ((target, propKey, descriptor) => {
204
- const args = {
205
- target,
206
- propKey,
207
- descriptor: typeof descriptor === 'number' ? undefined : descriptor,
208
- index: typeof descriptor === 'number' ? descriptor : undefined,
209
- level,
210
- };
211
- this.set(args, key, value, isArray);
212
- });
213
- }
214
- decorateConditional(ccb) {
215
- return ((target, propKey, descriptor) => {
216
- const hasIndex = typeof descriptor === 'number';
217
- const decoratorLevel = hasIndex ? 'PARAM' : propKey && descriptor ? 'METHOD' : propKey ? 'PROP' : 'CLASS';
218
- const d = ccb(decoratorLevel);
219
- if (d) {
220
- d(target, propKey, descriptor);
221
- }
222
- });
223
- }
224
- decorateClass(key, value, isArray) {
225
- return this.decorate(key, value, isArray, 'CLASS');
226
- }
227
- }
228
- const fnProto = Object.getPrototypeOf(Function);
229
-
230
- exports.Mate = Mate;
231
- exports.getConstructor = getConstructor;
232
- exports.isConstructor = isConstructor;
package/dist/mate.d.ts DELETED
@@ -1,83 +0,0 @@
1
- declare type ArrayElementType<T> = T extends (infer E)[] ? E : never;
2
-
3
- export declare function getConstructor<T = TAny>(instance: T): TFunction;
4
-
5
- export declare function isConstructor<T = TAny>(v: T): v is (T & TClassConstructor);
6
-
7
- export declare class Mate<TClass extends TObject = TMateClassMeta<TMateParamMeta>, TProp extends {
8
- params: TMateParamMeta[];
9
- } = Required<TMatePropMeta<TMateParamMeta>>> {
10
- protected workspace: string;
11
- protected options: TMateOptions<TClass, TProp>;
12
- protected logger: TConsoleBase;
13
- constructor(workspace: string, options?: TMateOptions<TClass, TProp>);
14
- set<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>>(args: TMergedDecoratorArgs, cb: ((meta: T, level: TLevels, propKey?: string | symbol, index?: number) => T)): void;
15
- set<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T>(args: TMergedDecoratorArgs, key: keyof T, value: T[K]): void;
16
- set<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T>(args: TMergedDecoratorArgs, key: keyof T, value: T[K], isArray: boolean | undefined): void;
17
- read<PK>(target: TFunction | TObject, propKey?: PK): PK extends PropertyKey ? (TClass & TProp & TCommonMate<TProp['params'][0]> | undefined) : TClass | undefined;
18
- apply(...decorators: (MethodDecorator | ClassDecorator | ParameterDecorator | PropertyDecorator)[]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
19
- decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>>(cb: ((meta: T, level: TLevels, propKey?: string | symbol, index?: number) => T)): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
20
- decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T>(key: K, value: T[K]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
21
- decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K, value: TIsArray extends true ? ArrayElementType<T[K]> : T[K], isArray: TIsArray): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
22
- decorate<T = TClass & TProp & TCommonMateWithParam<TProp['params'][0]>, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K | ((meta: T, level: TLevels, propKey?: string | symbol, index?: number) => T), value: TIsArray extends true ? ArrayElementType<T[K]> : T[K], isArray: TIsArray, level: TMergedDecoratorArgs['level']): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
23
- decorateConditional(ccb: (level: TLevels) => (MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator) | void | undefined): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
24
- decorateClass<T = TClass>(cb: ((meta: T, level: TLevels, propKey?: string | symbol, index?: number) => T)): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
25
- decorateClass<T = TClass, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K, value: TIsArray extends true ? ArrayElementType<T[K]> : T[K]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
26
- decorateClass<T = TClass, K extends keyof T = keyof T, TIsArray extends boolean = false>(key: K, value: TIsArray extends true ? ArrayElementType<T[K]> : T[K], isArray: boolean | undefined): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
27
- }
28
-
29
- declare type TAny = any;
30
-
31
- declare type TClassConstructor<T = unknown> = new (...args: TAny[]) => T;
32
-
33
- declare type TCommonMate<TParam extends TObject> = TMateClassMeta<TParam> & TMatePropMeta<TParam>;
34
-
35
- declare type TCommonMateWithParam<TParam extends TObject> = TMateClassMeta<TParam> & TMatePropMeta<TParam> & TParam;
36
-
37
- declare interface TConsoleBase {
38
- error: ((...args: any) => void);
39
- }
40
-
41
- declare interface TEmpty {
42
- }
43
-
44
- declare type TFunction = Function;
45
-
46
- declare type TLevels = 'CLASS' | 'METHOD' | 'PROP' | 'PARAM';
47
-
48
- export declare interface TMateClassMeta<TParam> {
49
- properties?: (string | symbol)[];
50
- type?: TFunction;
51
- returnType?: TFunction;
52
- params?: (TParam & TMateParamMeta)[];
53
- }
54
-
55
- export declare interface TMateOptions<TClass extends TObject = TMateClassMeta<TMateParamMeta>, TProp extends {
56
- params: TMateParamMeta[];
57
- } = Required<TMatePropMeta<TMateParamMeta>>> {
58
- logger?: TConsoleBase;
59
- readReturnType?: boolean;
60
- readType?: boolean;
61
- collectPropKeys?: boolean;
62
- inherit?: boolean | ((classMeta: TClass & TMateClassMeta<TMateParamMeta>, targetMeta: (TMatePropMeta<TProp['params'][0]> & TProp) | (TMateParamMeta & TProp['params'][0]), level: 'CLASS' | 'PROP' | 'PARAM', key?: string) => boolean);
63
- }
64
-
65
- export declare interface TMateParamMeta {
66
- type?: TFunction;
67
- }
68
-
69
- export declare interface TMatePropMeta<TParam extends TObject = TEmpty> {
70
- params?: (TParam & TMateParamMeta)[];
71
- }
72
-
73
- export declare interface TMergedDecoratorArgs {
74
- target: TFunction | TObject;
75
- propKey?: string | symbol;
76
- descriptor?: TypedPropertyDescriptor<TAny>;
77
- index?: number;
78
- level?: TLevels;
79
- }
80
-
81
- declare type TObject = object;
82
-
83
- export { }