phecda-core 4.5.0 → 5.0.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/dist/index.d.mts +26 -5
- package/dist/index.d.ts +26 -5
- package/dist/index.js +371 -91
- package/dist/index.mjs +363 -91
- package/package.json +4 -1
package/dist/index.mjs
CHANGED
|
@@ -1,57 +1,50 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (decorator = decorators[i])
|
|
7
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
-
if (kind && result) __defProp(target, key, result);
|
|
9
|
-
return result;
|
|
10
|
-
};
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/index.ts
|
|
5
|
+
export * from "reflect-metadata";
|
|
11
6
|
|
|
12
7
|
// src/core.ts
|
|
13
8
|
var SHARE_KEY = Symbol("phecda[share]");
|
|
14
9
|
var CLEAR_KEY = Symbol("phecda[clear]");
|
|
15
10
|
var PHECDA_KEY = Symbol("phecda");
|
|
16
11
|
function isPhecda(model) {
|
|
17
|
-
if (typeof model === "function")
|
|
18
|
-
return !!model.prototype[PHECDA_KEY];
|
|
12
|
+
if (typeof model === "function") return !!model.prototype[PHECDA_KEY];
|
|
19
13
|
return false;
|
|
20
14
|
}
|
|
15
|
+
__name(isPhecda, "isPhecda");
|
|
21
16
|
function init(proto) {
|
|
22
|
-
if (!proto)
|
|
23
|
-
return;
|
|
17
|
+
if (!proto) return;
|
|
24
18
|
if (!proto.hasOwnProperty(PHECDA_KEY)) {
|
|
25
19
|
proto[PHECDA_KEY] = {
|
|
26
20
|
/**
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
* 元数据
|
|
22
|
+
*/
|
|
29
23
|
__META__: /* @__PURE__ */ new Map()
|
|
30
24
|
};
|
|
31
25
|
}
|
|
32
26
|
}
|
|
27
|
+
__name(init, "init");
|
|
33
28
|
function getPhecdaFromTarget(target) {
|
|
34
|
-
if (typeof target === "function")
|
|
35
|
-
|
|
36
|
-
if (target.hasOwnProperty(PHECDA_KEY))
|
|
37
|
-
return target;
|
|
29
|
+
if (typeof target === "function") return target.prototype;
|
|
30
|
+
if (target.hasOwnProperty(PHECDA_KEY)) return target;
|
|
38
31
|
return Object.getPrototypeOf(target);
|
|
39
32
|
}
|
|
33
|
+
__name(getPhecdaFromTarget, "getPhecdaFromTarget");
|
|
40
34
|
function setMeta(proto, property, index, meta) {
|
|
41
|
-
if (typeof proto === "function" && proto.prototype)
|
|
42
|
-
proto = proto.prototype;
|
|
35
|
+
if (typeof proto === "function" && proto.prototype) proto = proto.prototype;
|
|
43
36
|
if (!property) {
|
|
44
37
|
property = SHARE_KEY;
|
|
45
|
-
if (proto.prototype)
|
|
46
|
-
proto = proto.prototype;
|
|
38
|
+
if (proto.prototype) proto = proto.prototype;
|
|
47
39
|
}
|
|
48
40
|
init(proto);
|
|
49
|
-
if (!proto[PHECDA_KEY].__META__.has(property))
|
|
50
|
-
|
|
41
|
+
if (!proto[PHECDA_KEY].__META__.has(property)) proto[PHECDA_KEY].__META__.set(property, {
|
|
42
|
+
data: [],
|
|
43
|
+
params: /* @__PURE__ */ new Map()
|
|
44
|
+
});
|
|
51
45
|
const oldMeta = proto[PHECDA_KEY].__META__.get(property);
|
|
52
46
|
if (typeof index === "number") {
|
|
53
|
-
if (!oldMeta.params.has(index))
|
|
54
|
-
oldMeta.params.set(index, []);
|
|
47
|
+
if (!oldMeta.params.has(index)) oldMeta.params.set(index, []);
|
|
55
48
|
const paramsMeta = oldMeta.params.get(index);
|
|
56
49
|
paramsMeta.push(meta);
|
|
57
50
|
} else {
|
|
@@ -59,27 +52,36 @@ function setMeta(proto, property, index, meta) {
|
|
|
59
52
|
}
|
|
60
53
|
proto[PHECDA_KEY].__META__.set(property, oldMeta);
|
|
61
54
|
}
|
|
55
|
+
__name(setMeta, "setMeta");
|
|
62
56
|
function getOwnMetaKey(target) {
|
|
63
57
|
const proto = getPhecdaFromTarget(target);
|
|
64
|
-
return [
|
|
58
|
+
return [
|
|
59
|
+
...proto[PHECDA_KEY].__META__.keys()
|
|
60
|
+
];
|
|
65
61
|
}
|
|
62
|
+
__name(getOwnMetaKey, "getOwnMetaKey");
|
|
66
63
|
function getMetaKey(target) {
|
|
67
64
|
let proto = getPhecdaFromTarget(target);
|
|
68
65
|
const set2 = /* @__PURE__ */ new Set();
|
|
69
66
|
while (proto?.[PHECDA_KEY]) {
|
|
70
67
|
if (proto.hasOwnProperty(PHECDA_KEY)) {
|
|
71
|
-
for (const property of proto[PHECDA_KEY].__META__.keys())
|
|
72
|
-
set2.add(property);
|
|
68
|
+
for (const property of proto[PHECDA_KEY].__META__.keys()) set2.add(property);
|
|
73
69
|
}
|
|
74
70
|
proto = Object.getPrototypeOf(proto);
|
|
75
71
|
}
|
|
76
|
-
return [
|
|
72
|
+
return [
|
|
73
|
+
...set2
|
|
74
|
+
];
|
|
77
75
|
}
|
|
76
|
+
__name(getMetaKey, "getMetaKey");
|
|
78
77
|
function getOwnMetaParams(target, key = SHARE_KEY) {
|
|
79
78
|
const proto = getPhecdaFromTarget(target);
|
|
80
79
|
const { params } = proto[PHECDA_KEY].__META__.get(key);
|
|
81
|
-
return [
|
|
80
|
+
return [
|
|
81
|
+
...params.keys()
|
|
82
|
+
];
|
|
82
83
|
}
|
|
84
|
+
__name(getOwnMetaParams, "getOwnMetaParams");
|
|
83
85
|
function getMetaParams(target, key = SHARE_KEY) {
|
|
84
86
|
let proto = getPhecdaFromTarget(target);
|
|
85
87
|
const set2 = /* @__PURE__ */ new Set();
|
|
@@ -87,14 +89,16 @@ function getMetaParams(target, key = SHARE_KEY) {
|
|
|
87
89
|
if (proto.hasOwnProperty(PHECDA_KEY)) {
|
|
88
90
|
const meta = proto[PHECDA_KEY].__META__.get(key);
|
|
89
91
|
if (meta) {
|
|
90
|
-
for (const index of meta.params.keys())
|
|
91
|
-
set2.add(index);
|
|
92
|
+
for (const index of meta.params.keys()) set2.add(index);
|
|
92
93
|
}
|
|
93
94
|
}
|
|
94
95
|
proto = Object.getPrototypeOf(proto);
|
|
95
96
|
}
|
|
96
|
-
return [
|
|
97
|
+
return [
|
|
98
|
+
...set2
|
|
99
|
+
].sort((a, b) => a - b);
|
|
97
100
|
}
|
|
101
|
+
__name(getMetaParams, "getMetaParams");
|
|
98
102
|
function getMeta(target, property = SHARE_KEY, index) {
|
|
99
103
|
let proto = getPhecdaFromTarget(target);
|
|
100
104
|
const ret = [];
|
|
@@ -107,14 +111,12 @@ function getMeta(target, property = SHARE_KEY, index) {
|
|
|
107
111
|
if (paramMeta) {
|
|
108
112
|
const index2 = paramMeta.findIndex((item) => item[CLEAR_KEY]);
|
|
109
113
|
ret.unshift(...paramMeta.slice(index2 + 1));
|
|
110
|
-
if (index2 > -1)
|
|
111
|
-
break;
|
|
114
|
+
if (index2 > -1) break;
|
|
112
115
|
}
|
|
113
116
|
} else {
|
|
114
117
|
const index2 = meta.data.findIndex((item) => item[CLEAR_KEY]);
|
|
115
118
|
ret.unshift(...meta.data.slice(index2 + 1));
|
|
116
|
-
if (index2 > -1)
|
|
117
|
-
break;
|
|
119
|
+
if (index2 > -1) break;
|
|
118
120
|
}
|
|
119
121
|
}
|
|
120
122
|
}
|
|
@@ -122,18 +124,22 @@ function getMeta(target, property = SHARE_KEY, index) {
|
|
|
122
124
|
}
|
|
123
125
|
return ret;
|
|
124
126
|
}
|
|
127
|
+
__name(getMeta, "getMeta");
|
|
125
128
|
function getOwnMeta(target, property = SHARE_KEY, index) {
|
|
126
129
|
const proto = getPhecdaFromTarget(target);
|
|
127
130
|
const meta = proto[PHECDA_KEY].__META__.get(property);
|
|
128
131
|
return typeof index === "number" ? meta.params.get(index) : meta.data;
|
|
129
132
|
}
|
|
133
|
+
__name(getOwnMeta, "getOwnMeta");
|
|
130
134
|
function set(proto, property, value) {
|
|
131
135
|
init(proto);
|
|
132
136
|
proto[`__${property.toUpperCase()}__`] = value;
|
|
133
137
|
}
|
|
138
|
+
__name(set, "set");
|
|
134
139
|
function get(proto, property) {
|
|
135
140
|
return proto[`__${property.toUpperCase()}__`];
|
|
136
141
|
}
|
|
142
|
+
__name(get, "get");
|
|
137
143
|
|
|
138
144
|
// src/decorators/core.ts
|
|
139
145
|
function Init(proto, property) {
|
|
@@ -143,6 +149,7 @@ function Init(proto, property) {
|
|
|
143
149
|
}
|
|
144
150
|
});
|
|
145
151
|
}
|
|
152
|
+
__name(Init, "Init");
|
|
146
153
|
function Unmount(proto, property) {
|
|
147
154
|
setMeta(proto, property, void 0, {
|
|
148
155
|
async unmount(instance) {
|
|
@@ -150,39 +157,46 @@ function Unmount(proto, property) {
|
|
|
150
157
|
}
|
|
151
158
|
});
|
|
152
159
|
}
|
|
160
|
+
__name(Unmount, "Unmount");
|
|
153
161
|
function Expose(proto, property, index) {
|
|
154
162
|
setMeta(proto, property, index, {});
|
|
155
163
|
}
|
|
164
|
+
__name(Expose, "Expose");
|
|
156
165
|
function Empty(model) {
|
|
157
166
|
init(model.prototype);
|
|
158
167
|
}
|
|
168
|
+
__name(Empty, "Empty");
|
|
159
169
|
function Clear(proto, property, index) {
|
|
160
170
|
setMeta(proto, property, index, {
|
|
161
171
|
[CLEAR_KEY]: true
|
|
162
172
|
});
|
|
163
173
|
}
|
|
174
|
+
__name(Clear, "Clear");
|
|
164
175
|
function Injectable() {
|
|
165
176
|
return (target) => Empty(target);
|
|
166
177
|
}
|
|
178
|
+
__name(Injectable, "Injectable");
|
|
167
179
|
|
|
168
180
|
// src/helper.ts
|
|
169
181
|
function getTag(moduleOrInstance) {
|
|
170
|
-
if (typeof moduleOrInstance === "object")
|
|
171
|
-
moduleOrInstance = moduleOrInstance.constructor;
|
|
182
|
+
if (typeof moduleOrInstance === "object") moduleOrInstance = moduleOrInstance.constructor;
|
|
172
183
|
return get(moduleOrInstance.prototype, "tag") || moduleOrInstance.name;
|
|
173
184
|
}
|
|
185
|
+
__name(getTag, "getTag");
|
|
174
186
|
function addDecoToClass(c, key, handler) {
|
|
175
187
|
handler(key ? c.prototype : c, key);
|
|
176
188
|
}
|
|
189
|
+
__name(addDecoToClass, "addDecoToClass");
|
|
177
190
|
function Pipeline(...decos) {
|
|
178
191
|
return (...args) => {
|
|
179
|
-
for (const d of decos)
|
|
180
|
-
d(...args);
|
|
192
|
+
for (const d of decos) d(...args);
|
|
181
193
|
};
|
|
182
194
|
}
|
|
195
|
+
__name(Pipeline, "Pipeline");
|
|
183
196
|
function isAsyncFunc(fn) {
|
|
184
197
|
return fn[Symbol.toStringTag] === "AsyncFunction";
|
|
185
198
|
}
|
|
199
|
+
__name(isAsyncFunc, "isAsyncFunc");
|
|
186
200
|
function invoke(instance, key, ...params) {
|
|
187
201
|
const metaKeys = getMetaKey(instance);
|
|
188
202
|
return Promise.allSettled(metaKeys.map((k) => {
|
|
@@ -194,12 +208,15 @@ function invoke(instance, key, ...params) {
|
|
|
194
208
|
return res;
|
|
195
209
|
});
|
|
196
210
|
}
|
|
211
|
+
__name(invoke, "invoke");
|
|
197
212
|
function invokeInit(instance) {
|
|
198
213
|
return instance.__PROMISE_SYMBOL__ = invoke(instance, "init");
|
|
199
214
|
}
|
|
215
|
+
__name(invokeInit, "invokeInit");
|
|
200
216
|
function invokeUnmount(instance) {
|
|
201
217
|
return instance.__PROMISE_SYMBOL__ = invoke(instance, "unmount");
|
|
202
218
|
}
|
|
219
|
+
__name(invokeUnmount, "invokeUnmount");
|
|
203
220
|
function If(value, ...decorators) {
|
|
204
221
|
if (value) {
|
|
205
222
|
return (...args) => {
|
|
@@ -209,22 +226,22 @@ function If(value, ...decorators) {
|
|
|
209
226
|
return () => {
|
|
210
227
|
};
|
|
211
228
|
}
|
|
229
|
+
__name(If, "If");
|
|
212
230
|
function getMergedMeta(target, property, index, merger = defaultMerger) {
|
|
213
231
|
const meta = getMeta(target, property, index);
|
|
214
232
|
return meta.reduce((p, c) => {
|
|
215
233
|
return merger(p, c);
|
|
216
234
|
}, {});
|
|
217
235
|
}
|
|
236
|
+
__name(getMergedMeta, "getMergedMeta");
|
|
218
237
|
function defaultMerger(prev, cur) {
|
|
219
238
|
const newMeta = {};
|
|
220
239
|
for (const key in prev) {
|
|
221
|
-
if (key === CLEAR_KEY)
|
|
222
|
-
continue;
|
|
240
|
+
if (key === CLEAR_KEY) continue;
|
|
223
241
|
newMeta[key] = prev[key];
|
|
224
242
|
}
|
|
225
243
|
for (const key in cur) {
|
|
226
|
-
if (key === CLEAR_KEY)
|
|
227
|
-
continue;
|
|
244
|
+
if (key === CLEAR_KEY) continue;
|
|
228
245
|
if (newMeta[key] && cur[key]) {
|
|
229
246
|
if (Array.isArray(newMeta[key]) && Array.isArray(cur[key])) {
|
|
230
247
|
const set2 = new Set(newMeta[key]);
|
|
@@ -232,7 +249,9 @@ function defaultMerger(prev, cur) {
|
|
|
232
249
|
set2.delete(item);
|
|
233
250
|
set2.add(item);
|
|
234
251
|
});
|
|
235
|
-
newMeta[key] = [
|
|
252
|
+
newMeta[key] = [
|
|
253
|
+
...set2
|
|
254
|
+
];
|
|
236
255
|
} else if (typeof newMeta[key] === "object" && typeof cur[key] === "object") {
|
|
237
256
|
newMeta[key] = defaultMerger(newMeta[key], cur[key]);
|
|
238
257
|
} else {
|
|
@@ -244,9 +263,11 @@ function defaultMerger(prev, cur) {
|
|
|
244
263
|
}
|
|
245
264
|
return newMeta;
|
|
246
265
|
}
|
|
266
|
+
__name(defaultMerger, "defaultMerger");
|
|
247
267
|
function wait(...instances) {
|
|
248
268
|
return Promise.all(instances.map((i) => i.__PROMISE_SYMBOL__));
|
|
249
269
|
}
|
|
270
|
+
__name(wait, "wait");
|
|
250
271
|
function objectToClass(obj) {
|
|
251
272
|
return class {
|
|
252
273
|
constructor() {
|
|
@@ -254,15 +275,21 @@ function objectToClass(obj) {
|
|
|
254
275
|
}
|
|
255
276
|
};
|
|
256
277
|
}
|
|
278
|
+
__name(objectToClass, "objectToClass");
|
|
257
279
|
function functionToClass(fn) {
|
|
258
280
|
return class {
|
|
281
|
+
prototype;
|
|
259
282
|
constructor(...args) {
|
|
260
283
|
Object.setPrototypeOf(this, fn(...args));
|
|
261
284
|
}
|
|
262
285
|
};
|
|
263
286
|
}
|
|
287
|
+
__name(functionToClass, "functionToClass");
|
|
264
288
|
function omit(classFn, ...properties) {
|
|
265
289
|
const newClass = class extends classFn {
|
|
290
|
+
static {
|
|
291
|
+
__name(this, "newClass");
|
|
292
|
+
}
|
|
266
293
|
constructor(...args) {
|
|
267
294
|
super(...args);
|
|
268
295
|
properties.forEach((k) => {
|
|
@@ -271,21 +298,23 @@ function omit(classFn, ...properties) {
|
|
|
271
298
|
}
|
|
272
299
|
};
|
|
273
300
|
getMetaKey(classFn).forEach((k) => {
|
|
274
|
-
if (properties.includes(k))
|
|
275
|
-
addDecoToClass(newClass, k, Clear);
|
|
301
|
+
if (properties.includes(k)) addDecoToClass(newClass, k, Clear);
|
|
276
302
|
});
|
|
277
303
|
return newClass;
|
|
278
304
|
}
|
|
305
|
+
__name(omit, "omit");
|
|
279
306
|
function pick(classFn, ...properties) {
|
|
280
307
|
const newClass = class {
|
|
308
|
+
static {
|
|
309
|
+
__name(this, "newClass");
|
|
310
|
+
}
|
|
281
311
|
constructor(...args) {
|
|
282
312
|
const instance = new classFn(...args);
|
|
283
313
|
properties.forEach((prop) => {
|
|
284
314
|
Object.defineProperty(this, prop, {
|
|
285
315
|
get() {
|
|
286
316
|
const data = instance[prop];
|
|
287
|
-
if (typeof data === "function")
|
|
288
|
-
return data.bind(this);
|
|
317
|
+
if (typeof data === "function") return data.bind(this);
|
|
289
318
|
return data;
|
|
290
319
|
},
|
|
291
320
|
set(val) {
|
|
@@ -305,77 +334,89 @@ function pick(classFn, ...properties) {
|
|
|
305
334
|
});
|
|
306
335
|
return newClass;
|
|
307
336
|
}
|
|
337
|
+
__name(pick, "pick");
|
|
308
338
|
function partial(classFn, ...properties) {
|
|
309
339
|
const newClass = class extends classFn {
|
|
340
|
+
static {
|
|
341
|
+
__name(this, "newClass");
|
|
342
|
+
}
|
|
310
343
|
};
|
|
311
344
|
getMetaKey(classFn).forEach((k) => {
|
|
312
|
-
if (properties.length === 0 || properties.includes(k))
|
|
313
|
-
addDecoToClass(newClass, k, Optional);
|
|
345
|
+
if (properties.length === 0 || properties.includes(k)) addDecoToClass(newClass, k, Optional);
|
|
314
346
|
});
|
|
315
347
|
return newClass;
|
|
316
348
|
}
|
|
349
|
+
__name(partial, "partial");
|
|
317
350
|
function override(classFn, ...properties) {
|
|
318
351
|
return classFn;
|
|
319
352
|
}
|
|
353
|
+
__name(override, "override");
|
|
320
354
|
|
|
321
355
|
// src/di.ts
|
|
322
356
|
var DataMap = {};
|
|
323
357
|
function Provide(key, value) {
|
|
324
358
|
DataMap[key] = value;
|
|
325
359
|
}
|
|
360
|
+
__name(Provide, "Provide");
|
|
326
361
|
function Inject(key) {
|
|
327
362
|
return DataMap[key];
|
|
328
363
|
}
|
|
364
|
+
__name(Inject, "Inject");
|
|
329
365
|
var activeInstance = {};
|
|
330
366
|
function setInject(key, value) {
|
|
331
367
|
activeInstance[key] = value;
|
|
332
368
|
return activeInstance;
|
|
333
369
|
}
|
|
370
|
+
__name(setInject, "setInject");
|
|
334
371
|
function getInject(key) {
|
|
335
372
|
return activeInstance[key];
|
|
336
373
|
}
|
|
374
|
+
__name(getInject, "getInject");
|
|
337
375
|
|
|
338
376
|
// src/decorators/function.ts
|
|
339
377
|
function Isolate(model) {
|
|
340
378
|
set(model.prototype, "isolate", true);
|
|
341
379
|
}
|
|
380
|
+
__name(Isolate, "Isolate");
|
|
342
381
|
function Tag(tag) {
|
|
343
382
|
return (model) => {
|
|
344
383
|
set(model.prototype, "tag", tag);
|
|
345
384
|
};
|
|
346
385
|
}
|
|
386
|
+
__name(Tag, "Tag");
|
|
347
387
|
function Unique(desc) {
|
|
348
388
|
return (model) => {
|
|
349
389
|
set(model.prototype, "tag", Symbol(desc || model.name));
|
|
350
390
|
};
|
|
351
391
|
}
|
|
392
|
+
__name(Unique, "Unique");
|
|
352
393
|
function Assign(cb) {
|
|
353
394
|
return (model) => {
|
|
354
395
|
setMeta(model, void 0, void 0, {
|
|
355
|
-
init: async (instance) => {
|
|
396
|
+
init: /* @__PURE__ */ __name(async (instance) => {
|
|
356
397
|
const value = await cb(instance);
|
|
357
398
|
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
358
|
-
for (const i in value)
|
|
359
|
-
instance[i] = value[i];
|
|
399
|
+
for (const i in value) instance[i] = value[i];
|
|
360
400
|
}
|
|
361
|
-
}
|
|
401
|
+
}, "init")
|
|
362
402
|
});
|
|
363
403
|
};
|
|
364
404
|
}
|
|
405
|
+
__name(Assign, "Assign");
|
|
365
406
|
function Global(model) {
|
|
366
407
|
setMeta(model, void 0, void 0, {
|
|
367
|
-
init: async (instance) => {
|
|
408
|
+
init: /* @__PURE__ */ __name(async (instance) => {
|
|
368
409
|
const tag = getTag(instance);
|
|
369
|
-
if (!globalThis.__PHECDA__)
|
|
370
|
-
globalThis.__PHECDA__ = {};
|
|
410
|
+
if (!globalThis.__PHECDA__) globalThis.__PHECDA__ = {};
|
|
371
411
|
globalThis.__PHECDA__[tag] = instance.constructor;
|
|
372
|
-
}
|
|
412
|
+
}, "init")
|
|
373
413
|
});
|
|
374
414
|
}
|
|
415
|
+
__name(Global, "Global");
|
|
375
416
|
function Err(cb, isCatch = false) {
|
|
376
417
|
return (proto, property) => {
|
|
377
418
|
setMeta(proto, property, void 0, {
|
|
378
|
-
init: (instance) => {
|
|
419
|
+
init: /* @__PURE__ */ __name((instance) => {
|
|
379
420
|
if (typeof instance[property] === "function") {
|
|
380
421
|
const oldFn = instance[property].bind(instance);
|
|
381
422
|
if (isAsyncFunc(oldFn)) {
|
|
@@ -384,8 +425,7 @@ function Err(cb, isCatch = false) {
|
|
|
384
425
|
await oldFn(...args);
|
|
385
426
|
} catch (e) {
|
|
386
427
|
cb(e, instance, property);
|
|
387
|
-
if (!isCatch)
|
|
388
|
-
throw e;
|
|
428
|
+
if (!isCatch) throw e;
|
|
389
429
|
}
|
|
390
430
|
};
|
|
391
431
|
} else {
|
|
@@ -394,22 +434,27 @@ function Err(cb, isCatch = false) {
|
|
|
394
434
|
oldFn(...args);
|
|
395
435
|
} catch (e) {
|
|
396
436
|
cb(e, instance, property);
|
|
397
|
-
if (!isCatch)
|
|
398
|
-
throw e;
|
|
437
|
+
if (!isCatch) throw e;
|
|
399
438
|
}
|
|
400
439
|
};
|
|
401
440
|
}
|
|
402
441
|
}
|
|
403
|
-
}
|
|
442
|
+
}, "init")
|
|
404
443
|
});
|
|
405
444
|
};
|
|
406
445
|
}
|
|
446
|
+
__name(Err, "Err");
|
|
407
447
|
function Watcher(eventName, options) {
|
|
408
448
|
let cb;
|
|
409
449
|
return (proto, property) => {
|
|
410
450
|
setMeta(proto, property, void 0, {
|
|
411
451
|
init(instance) {
|
|
412
|
-
return cb = getInject("watcher")?.({
|
|
452
|
+
return cb = getInject("watcher")?.({
|
|
453
|
+
eventName,
|
|
454
|
+
instance,
|
|
455
|
+
property,
|
|
456
|
+
options
|
|
457
|
+
});
|
|
413
458
|
},
|
|
414
459
|
unmount() {
|
|
415
460
|
return cb?.();
|
|
@@ -417,6 +462,7 @@ function Watcher(eventName, options) {
|
|
|
417
462
|
});
|
|
418
463
|
};
|
|
419
464
|
}
|
|
465
|
+
__name(Watcher, "Watcher");
|
|
420
466
|
function Effect(cb) {
|
|
421
467
|
return (proto, property) => {
|
|
422
468
|
setMeta(proto, property, void 0, {
|
|
@@ -436,44 +482,119 @@ function Effect(cb) {
|
|
|
436
482
|
});
|
|
437
483
|
};
|
|
438
484
|
}
|
|
485
|
+
__name(Effect, "Effect");
|
|
439
486
|
function Storage({ key, json, stringify } = {}) {
|
|
440
|
-
if (!json)
|
|
441
|
-
|
|
442
|
-
if (!stringify)
|
|
443
|
-
stringify = (v) => JSON.stringify(v);
|
|
487
|
+
if (!json) json = /* @__PURE__ */ __name((v) => JSON.parse(v), "json");
|
|
488
|
+
if (!stringify) stringify = /* @__PURE__ */ __name((v) => JSON.stringify(v), "stringify");
|
|
444
489
|
return (proto, property) => {
|
|
445
490
|
const tag = key || getTag(proto);
|
|
446
491
|
init(proto);
|
|
447
492
|
setMeta(proto, property, void 0, {
|
|
448
|
-
init: (instance) => {
|
|
449
|
-
return getInject("storage")?.({
|
|
450
|
-
|
|
493
|
+
init: /* @__PURE__ */ __name((instance) => {
|
|
494
|
+
return getInject("storage")?.({
|
|
495
|
+
instance,
|
|
496
|
+
property,
|
|
497
|
+
tag,
|
|
498
|
+
toJSON: json,
|
|
499
|
+
toString: stringify
|
|
500
|
+
});
|
|
501
|
+
}, "init")
|
|
451
502
|
});
|
|
452
503
|
};
|
|
453
504
|
}
|
|
505
|
+
__name(Storage, "Storage");
|
|
454
506
|
|
|
455
507
|
// src/decorators/other.ts
|
|
508
|
+
function Doc(doc) {
|
|
509
|
+
return (target, property, index) => {
|
|
510
|
+
setMeta(target, property, index, {
|
|
511
|
+
doc
|
|
512
|
+
});
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
__name(Doc, "Doc");
|
|
516
|
+
|
|
517
|
+
// src/decorators/rule.ts
|
|
456
518
|
function Rule(...rules) {
|
|
457
519
|
return (target, property, index) => {
|
|
458
|
-
setMeta(target, property, index, {
|
|
520
|
+
setMeta(target, property, index, {
|
|
521
|
+
rules
|
|
522
|
+
});
|
|
459
523
|
};
|
|
460
524
|
}
|
|
525
|
+
__name(Rule, "Rule");
|
|
461
526
|
function Required(target, property, index) {
|
|
462
|
-
setMeta(target, property, index, {
|
|
527
|
+
setMeta(target, property, index, {
|
|
528
|
+
required: true
|
|
529
|
+
});
|
|
463
530
|
}
|
|
531
|
+
__name(Required, "Required");
|
|
464
532
|
function Optional(target, property, index) {
|
|
465
|
-
setMeta(target, property, index, {
|
|
533
|
+
setMeta(target, property, index, {
|
|
534
|
+
required: false
|
|
535
|
+
});
|
|
466
536
|
}
|
|
467
|
-
|
|
537
|
+
__name(Optional, "Optional");
|
|
538
|
+
function Min(min) {
|
|
468
539
|
return (target, property, index) => {
|
|
469
|
-
setMeta(target, property, index, {
|
|
540
|
+
setMeta(target, property, index, {
|
|
541
|
+
min
|
|
542
|
+
});
|
|
470
543
|
};
|
|
471
544
|
}
|
|
545
|
+
__name(Min, "Min");
|
|
546
|
+
function Max(max) {
|
|
547
|
+
return (target, property, index) => {
|
|
548
|
+
setMeta(target, property, index, {
|
|
549
|
+
max
|
|
550
|
+
});
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
__name(Max, "Max");
|
|
554
|
+
function Nested(model) {
|
|
555
|
+
return (target, property) => {
|
|
556
|
+
setMeta(target, property, void 0, {
|
|
557
|
+
nested: model
|
|
558
|
+
});
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
__name(Nested, "Nested");
|
|
562
|
+
function OneOf(...models) {
|
|
563
|
+
return (target, property) => {
|
|
564
|
+
setMeta(target, property, void 0, {
|
|
565
|
+
oneOf: models
|
|
566
|
+
});
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
__name(OneOf, "OneOf");
|
|
570
|
+
function Enum(map) {
|
|
571
|
+
return (target, property) => {
|
|
572
|
+
setMeta(target, property, void 0, {
|
|
573
|
+
enum: map
|
|
574
|
+
});
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
__name(Enum, "Enum");
|
|
472
578
|
|
|
473
579
|
// src/base.ts
|
|
580
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
581
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
582
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
583
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
584
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
585
|
+
}
|
|
586
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
587
|
+
function _ts_metadata(k, v) {
|
|
588
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
589
|
+
}
|
|
590
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
474
591
|
var Base = class {
|
|
592
|
+
static {
|
|
593
|
+
__name(this, "Base");
|
|
594
|
+
}
|
|
595
|
+
__UNMOUNT_SYMBOL__ = [];
|
|
596
|
+
__PROMISE_SYMBOL__;
|
|
475
597
|
constructor() {
|
|
476
|
-
this.__UNMOUNT_SYMBOL__ = [];
|
|
477
598
|
}
|
|
478
599
|
get tag() {
|
|
479
600
|
return getTag(this);
|
|
@@ -496,20 +617,164 @@ var Base = class {
|
|
|
496
617
|
onUnmount(cb) {
|
|
497
618
|
this.__UNMOUNT_SYMBOL__.push(cb);
|
|
498
619
|
}
|
|
499
|
-
// @ts-expect-error for internal
|
|
500
620
|
_unmount() {
|
|
501
621
|
return Promise.all(this.__UNMOUNT_SYMBOL__.map((fn) => fn()));
|
|
502
622
|
}
|
|
503
623
|
};
|
|
504
|
-
|
|
505
|
-
Init
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
], Base.prototype, "
|
|
510
|
-
|
|
511
|
-
|
|
624
|
+
_ts_decorate([
|
|
625
|
+
Init,
|
|
626
|
+
_ts_metadata("design:type", Function),
|
|
627
|
+
_ts_metadata("design:paramtypes", []),
|
|
628
|
+
_ts_metadata("design:returntype", void 0)
|
|
629
|
+
], Base.prototype, "init", null);
|
|
630
|
+
_ts_decorate([
|
|
631
|
+
Unmount,
|
|
632
|
+
_ts_metadata("design:type", Function),
|
|
633
|
+
_ts_metadata("design:paramtypes", []),
|
|
634
|
+
_ts_metadata("design:returntype", void 0)
|
|
635
|
+
], Base.prototype, "_unmount", null);
|
|
636
|
+
Base = _ts_decorate([
|
|
637
|
+
Empty,
|
|
638
|
+
_ts_metadata("design:type", Function),
|
|
639
|
+
_ts_metadata("design:paramtypes", [])
|
|
512
640
|
], Base);
|
|
641
|
+
|
|
642
|
+
// src/validate.ts
|
|
643
|
+
var _createErrorMessage = /* @__PURE__ */ __name((type, { property, meta }) => {
|
|
644
|
+
switch (type) {
|
|
645
|
+
case "string":
|
|
646
|
+
return `must be a string for "${property}"`;
|
|
647
|
+
case "number":
|
|
648
|
+
return `must be a number for "${property}"`;
|
|
649
|
+
case "boolean":
|
|
650
|
+
return `must be a boolean for "${property}"`;
|
|
651
|
+
case "oneOf":
|
|
652
|
+
return `must be one of models(${meta.oneof.map((m) => getTag(m)).join(", ")}) for "${property}"`;
|
|
653
|
+
case "min":
|
|
654
|
+
return `must be greater than ${meta.min} for "${property}"`;
|
|
655
|
+
case "max":
|
|
656
|
+
return `must be less than ${meta.max} for "${property}"`;
|
|
657
|
+
case "enum":
|
|
658
|
+
return `must be one of ${Object.values(meta.enum).join(", ")} for "${property}"`;
|
|
659
|
+
case "required":
|
|
660
|
+
return `it is required for "${property}"`;
|
|
661
|
+
case "object":
|
|
662
|
+
return `must be an object for "${property}"`;
|
|
663
|
+
case "array":
|
|
664
|
+
return `must be an array for "${property}"`;
|
|
665
|
+
case "stringArray":
|
|
666
|
+
return `must be an array of strings for "${property}"`;
|
|
667
|
+
case "numberArray":
|
|
668
|
+
return `must be an array of numbers for "${property}"`;
|
|
669
|
+
case "booleanArray":
|
|
670
|
+
return `must be an array of booleans for "${property}"`;
|
|
671
|
+
}
|
|
672
|
+
return `invalid value for "${property}"`;
|
|
673
|
+
}, "_createErrorMessage");
|
|
674
|
+
function isObject(value) {
|
|
675
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
676
|
+
}
|
|
677
|
+
__name(isObject, "isObject");
|
|
678
|
+
async function validate(model, data, collectErrors = false, createErrMsg = _createErrorMessage) {
|
|
679
|
+
async function parse(model2, data2) {
|
|
680
|
+
const errors = [];
|
|
681
|
+
for (const key of getMetaKey(model2)) {
|
|
682
|
+
const meta = getMergedMeta(model2, key);
|
|
683
|
+
const property = key === SHARE_KEY ? "" : key;
|
|
684
|
+
const type = property === "" ? model2 : Reflect.getMetadata("design:type", model2.prototype, key);
|
|
685
|
+
const { rules = [], nested, oneOf, min, max, enum: enumMap, required } = meta;
|
|
686
|
+
const value = property === "" ? data2 : data2?.[key];
|
|
687
|
+
const allRules = [
|
|
688
|
+
async (args2) => {
|
|
689
|
+
const { value: value2 } = args2;
|
|
690
|
+
if (required === false && value2 === void 0) return true;
|
|
691
|
+
if (required !== false && value2 === void 0) return createErrMsg("required", args2);
|
|
692
|
+
if (type === String && typeof value2 !== "string") return createErrMsg("string", args2);
|
|
693
|
+
if (type === Number && typeof value2 !== "number") return createErrMsg("number", args2);
|
|
694
|
+
if (type === Boolean && typeof value2 !== "boolean") return createErrMsg("boolean", args2);
|
|
695
|
+
if (type === Array) {
|
|
696
|
+
if (!Array.isArray(value2)) return createErrMsg("array", args2);
|
|
697
|
+
if (!nested) return;
|
|
698
|
+
for (const i in value2) {
|
|
699
|
+
if (nested === String && typeof value2[i] !== "string") return createErrMsg("stringArray", args2);
|
|
700
|
+
if (nested === Number && typeof value2[i] !== "number") return createErrMsg("numberArray", args2);
|
|
701
|
+
if (nested === Boolean && typeof value2[i] !== "boolean") return createErrMsg("booleanArray", args2);
|
|
702
|
+
if (isPhecda(nested)) {
|
|
703
|
+
if (!isObject(value2[i])) return createErrMsg("object", {
|
|
704
|
+
...args2,
|
|
705
|
+
arrayIndex: i
|
|
706
|
+
});
|
|
707
|
+
const errs = await parse(nested, value2[i]);
|
|
708
|
+
if (errs.length) {
|
|
709
|
+
errors.push(errs[0]);
|
|
710
|
+
break;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
} else {
|
|
715
|
+
if (nested && isPhecda(nested)) {
|
|
716
|
+
if (!isObject(value2)) return createErrMsg("object", args2);
|
|
717
|
+
const errs = await parse(nested, value2);
|
|
718
|
+
if (errs.length) errors.push(errs[0]);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
if (min) {
|
|
722
|
+
if (typeof value2 === "number" && value2 < min) return createErrMsg("min", args2);
|
|
723
|
+
if (typeof value2 === "string" && value2.length < min) return createErrMsg("min", args2);
|
|
724
|
+
if (Array.isArray(value2) && value2.length < min) return createErrMsg("min", args2);
|
|
725
|
+
}
|
|
726
|
+
if (max) {
|
|
727
|
+
if (typeof value2 === "number" && value2 > max) return createErrMsg("max", args2);
|
|
728
|
+
if (typeof value2 === "string" && value2.length > max) return createErrMsg("max", args2);
|
|
729
|
+
if (Array.isArray(value2) && value2.length > max) return createErrMsg("max", args2);
|
|
730
|
+
}
|
|
731
|
+
if (enumMap) {
|
|
732
|
+
if (!Object.values(enumMap).includes(value2)) return createErrMsg("enum", args2);
|
|
733
|
+
}
|
|
734
|
+
if (oneOf) {
|
|
735
|
+
let isCorrect = false;
|
|
736
|
+
for (const modelOrRule of oneOf) {
|
|
737
|
+
if (isPhecda(modelOrRule)) {
|
|
738
|
+
const errs = await validate(modelOrRule, value2);
|
|
739
|
+
if (!errs.length) {
|
|
740
|
+
isCorrect = true;
|
|
741
|
+
break;
|
|
742
|
+
}
|
|
743
|
+
} else if (typeof modelOrRule === "function") {
|
|
744
|
+
const errs = await modelOrRule(args2);
|
|
745
|
+
if (!errs.length) {
|
|
746
|
+
isCorrect = true;
|
|
747
|
+
break;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
if (!isCorrect) return createErrMsg("oneOf", args2);
|
|
752
|
+
}
|
|
753
|
+
},
|
|
754
|
+
...rules
|
|
755
|
+
];
|
|
756
|
+
const args = {
|
|
757
|
+
value,
|
|
758
|
+
property,
|
|
759
|
+
meta,
|
|
760
|
+
model: model2
|
|
761
|
+
};
|
|
762
|
+
for (const rule of allRules) {
|
|
763
|
+
const errMsg = await rule(args);
|
|
764
|
+
if (errMsg === true) break;
|
|
765
|
+
if (errMsg) {
|
|
766
|
+
errors.push(errMsg);
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
if (errors.length !== 0 && !collectErrors) break;
|
|
771
|
+
}
|
|
772
|
+
return errors;
|
|
773
|
+
}
|
|
774
|
+
__name(parse, "parse");
|
|
775
|
+
return await parse(model, data);
|
|
776
|
+
}
|
|
777
|
+
__name(validate, "validate");
|
|
513
778
|
export {
|
|
514
779
|
Assign,
|
|
515
780
|
Base,
|
|
@@ -519,6 +784,7 @@ export {
|
|
|
519
784
|
Doc,
|
|
520
785
|
Effect,
|
|
521
786
|
Empty,
|
|
787
|
+
Enum,
|
|
522
788
|
Err,
|
|
523
789
|
Expose,
|
|
524
790
|
Global,
|
|
@@ -527,6 +793,10 @@ export {
|
|
|
527
793
|
Inject,
|
|
528
794
|
Injectable,
|
|
529
795
|
Isolate,
|
|
796
|
+
Max,
|
|
797
|
+
Min,
|
|
798
|
+
Nested,
|
|
799
|
+
OneOf,
|
|
530
800
|
Optional,
|
|
531
801
|
PHECDA_KEY,
|
|
532
802
|
Pipeline,
|
|
@@ -539,6 +809,7 @@ export {
|
|
|
539
809
|
Unique,
|
|
540
810
|
Unmount,
|
|
541
811
|
Watcher,
|
|
812
|
+
_createErrorMessage,
|
|
542
813
|
activeInstance,
|
|
543
814
|
addDecoToClass,
|
|
544
815
|
functionToClass,
|
|
@@ -567,5 +838,6 @@ export {
|
|
|
567
838
|
set,
|
|
568
839
|
setInject,
|
|
569
840
|
setMeta,
|
|
841
|
+
validate,
|
|
570
842
|
wait
|
|
571
843
|
};
|