phecda-core 4.5.1 → 5.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/dist/index.mjs CHANGED
@@ -1,57 +1,50 @@
1
1
  var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __decorateClass = (decorators, target, key, kind) => {
4
- var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
5
- for (var i = decorators.length - 1, decorator; i >= 0; i--)
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
- return target.prototype;
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
- proto[PHECDA_KEY].__META__.set(property, { data: [], params: /* @__PURE__ */ new Map() });
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 [...proto[PHECDA_KEY].__META__.keys()];
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 [...set2];
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 [...params.keys()];
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 [...set2].sort((a, b) => a - b);
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] = [...set2];
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")?.({ eventName, instance, property, options });
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
- json = (v) => JSON.parse(v);
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")?.({ instance, property, tag, toJSON: json, toString: stringify });
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, { rules });
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, { required: true });
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, { required: false });
533
+ setMeta(target, property, index, {
534
+ required: false
535
+ });
466
536
  }
467
- function Doc(doc) {
537
+ __name(Optional, "Optional");
538
+ function Min(min) {
468
539
  return (target, property, index) => {
469
- setMeta(target, property, index, { doc });
540
+ setMeta(target, property, index, {
541
+ min
542
+ });
543
+ };
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, index) => {
556
+ setMeta(target, property, index, {
557
+ nested: model
558
+ });
470
559
  };
471
560
  }
561
+ __name(Nested, "Nested");
562
+ function OneOf(...validations) {
563
+ return (target, property, index) => {
564
+ setMeta(target, property, index, {
565
+ oneOf: validations
566
+ });
567
+ };
568
+ }
569
+ __name(OneOf, "OneOf");
570
+ function Enum(map) {
571
+ return (target, property, index) => {
572
+ setMeta(target, property, index, {
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,180 @@ 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
- __decorateClass([
505
- Init
506
- ], Base.prototype, "init", 1);
507
- __decorateClass([
508
- Unmount
509
- ], Base.prototype, "_unmount", 1);
510
- Base = __decorateClass([
511
- Empty
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 pass one of these validations 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
+ if (!isObject(data2)) {
682
+ errors.push("data must be an object");
683
+ return errors;
684
+ }
685
+ for (const key of getMetaKey(model2)) {
686
+ const meta = getMergedMeta(model2, key);
687
+ const property = key === SHARE_KEY ? "" : key;
688
+ const type = property === "" ? model2 : Reflect.getMetadata("design:type", model2.prototype, key);
689
+ const { rules = [], nested, oneOf, min, max, enum: enumMap, required } = meta;
690
+ const value = property === "" ? data2 : data2?.[key];
691
+ const allRules = [
692
+ async (args2) => {
693
+ const { value: value2 } = args2;
694
+ if (required === false && value2 === void 0) return true;
695
+ if (required !== false && value2 === void 0) return createErrMsg("required", args2);
696
+ if (type === String && typeof value2 !== "string") return createErrMsg("string", args2);
697
+ if (type === Number && typeof value2 !== "number") return createErrMsg("number", args2);
698
+ if (type === Boolean && typeof value2 !== "boolean") return createErrMsg("boolean", args2);
699
+ if (type === Array) {
700
+ if (!Array.isArray(value2)) return createErrMsg("array", args2);
701
+ if (!nested) return;
702
+ for (const i in value2) {
703
+ if (nested === String && typeof value2[i] !== "string") return createErrMsg("stringArray", args2);
704
+ if (nested === Number && typeof value2[i] !== "number") return createErrMsg("numberArray", args2);
705
+ if (nested === Boolean && typeof value2[i] !== "boolean") return createErrMsg("booleanArray", args2);
706
+ if (isPhecda(nested)) {
707
+ if (!isObject(value2[i])) return createErrMsg("object", {
708
+ ...args2,
709
+ arrayIndex: i
710
+ });
711
+ const errs = await parse(nested, value2[i]);
712
+ if (errs.length) {
713
+ errors.push(errs[0]);
714
+ break;
715
+ }
716
+ }
717
+ }
718
+ } else {
719
+ if (nested && isPhecda(nested)) {
720
+ if (!isObject(value2)) return createErrMsg("object", args2);
721
+ const errs = await parse(nested, value2);
722
+ if (errs.length) errors.push(errs[0]);
723
+ }
724
+ }
725
+ if (min) {
726
+ if (typeof value2 === "number" && value2 < min) return createErrMsg("min", args2);
727
+ if (typeof value2 === "string" && value2.length < min) return createErrMsg("min", args2);
728
+ if (Array.isArray(value2) && value2.length < min) return createErrMsg("min", args2);
729
+ }
730
+ if (max) {
731
+ if (typeof value2 === "number" && value2 > max) return createErrMsg("max", args2);
732
+ if (typeof value2 === "string" && value2.length > max) return createErrMsg("max", args2);
733
+ if (Array.isArray(value2) && value2.length > max) return createErrMsg("max", args2);
734
+ }
735
+ if (enumMap) {
736
+ if (!Object.values(enumMap).includes(value2)) return createErrMsg("enum", args2);
737
+ }
738
+ if (oneOf) {
739
+ let isCorrect = false;
740
+ for (const modelOrRule of oneOf) {
741
+ switch (modelOrRule) {
742
+ case String:
743
+ if (typeof value2 === "string") isCorrect = true;
744
+ break;
745
+ case Number:
746
+ if (typeof value2 === "number") isCorrect = true;
747
+ break;
748
+ case Boolean:
749
+ if (typeof value2 === "boolean") isCorrect = true;
750
+ break;
751
+ default:
752
+ if (isPhecda(modelOrRule)) {
753
+ const errs = await validate(modelOrRule, value2);
754
+ if (!errs.length) {
755
+ isCorrect = true;
756
+ break;
757
+ }
758
+ } else if (typeof modelOrRule === "function") {
759
+ const ret = await modelOrRule(args2);
760
+ if (ret) {
761
+ isCorrect = true;
762
+ break;
763
+ }
764
+ }
765
+ }
766
+ }
767
+ if (!isCorrect) return createErrMsg("oneOf", args2);
768
+ }
769
+ },
770
+ ...rules
771
+ ];
772
+ const args = {
773
+ value,
774
+ property,
775
+ meta,
776
+ model: model2
777
+ };
778
+ for (const rule of allRules) {
779
+ const errMsg = await rule(args);
780
+ if (errMsg === true) break;
781
+ if (errMsg) {
782
+ errors.push(errMsg);
783
+ break;
784
+ }
785
+ }
786
+ if (errors.length !== 0 && !collectErrors) break;
787
+ }
788
+ return errors;
789
+ }
790
+ __name(parse, "parse");
791
+ return await parse(model, data);
792
+ }
793
+ __name(validate, "validate");
513
794
  export {
514
795
  Assign,
515
796
  Base,
@@ -519,6 +800,7 @@ export {
519
800
  Doc,
520
801
  Effect,
521
802
  Empty,
803
+ Enum,
522
804
  Err,
523
805
  Expose,
524
806
  Global,
@@ -527,6 +809,10 @@ export {
527
809
  Inject,
528
810
  Injectable,
529
811
  Isolate,
812
+ Max,
813
+ Min,
814
+ Nested,
815
+ OneOf,
530
816
  Optional,
531
817
  PHECDA_KEY,
532
818
  Pipeline,
@@ -539,6 +825,7 @@ export {
539
825
  Unique,
540
826
  Unmount,
541
827
  Watcher,
828
+ _createErrorMessage,
542
829
  activeInstance,
543
830
  addDecoToClass,
544
831
  functionToClass,
@@ -567,5 +854,6 @@ export {
567
854
  set,
568
855
  setInject,
569
856
  setMeta,
857
+ validate,
570
858
  wait
571
859
  };