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