-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.d.ts
1236 lines (1047 loc) · 36.1 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-dupe-class-members */
/* eslint-disable no-redeclare */
/* eslint-disable no-unused-vars */
/**
* 用于得到某个class的构造函数,例如:
* class Some {}
* ConstructorOf<Some> -> Some类型的构造函数,也就是class Some本身
* 用处:
* class Some {
* static fn<T>(this: ConstructorOf<T>): void; // -> this: ConstructorOf<T> 规定了该静态方法内的this类型,由于类型推导,此处的this被推导为Some本身
* }
*/
type ConstructorOf<T> = new (...args: any[]) => T
/**
* InstanceType
*/
// type InstanceOf<T> = InstanceType<T>
/**
* 读取数组的项的类型
*/
type ItemOf<T> = T extends Array<infer P> ? P : never
// https://lifesaver.codes/answer/type-manipulations-union-to-tuple-13298
// https://note.xiexuefeng.cc/post/ts-union-to-tuple/
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true
type UnionToOvlds<U> = UnionToIntersection<U extends any ? (f: U) => void : never>
type PopUnion<U> = UnionToOvlds<U> extends ((a: infer A) => void) ? (A extends boolean ? boolean extends U ? boolean : A : A) : never
type GetUnionLast<Unoin> = IsUnion<Unoin> extends true ? PopUnion<Unoin> : Unoin
// https://www.tangshuang.net/8487.html
type GetUnionKeys<Unoin> = Unoin extends any
? {
[key in keyof Unoin]: key
} extends {
[key in keyof Unoin]: infer K
}
? K
: never
: never
type UnionToInterByKeys<Union, Keys extends string | number | symbol> = {
[key in Keys]: GetUnionLast<
Union extends any
? {
[k in keyof Union]: k extends key ? Union[k] : never
} extends {
[k in keyof Union]: infer P
}
? P
: never
: never
>
}
type UnionToInter<Unoin> = UnionToInterByKeys<Unoin, GetUnionKeys<Unoin>>
// -------------
interface Obj { [key: string]: any }
// -------------
interface PrototypeOptions {
name?: string
validate: (value: any) => boolean
}
export declare class Prototype {
constructor(options: PrototypeOptions)
name?: string
validate: (value: any) => boolean
static register(proto: any, validate: (value: any) => boolean): void
static unregister(...args: any[]): void
static is(proto: any): {
existing: () => boolean
typeof: (value: any) => boolean
}
}
export declare class Null extends Prototype {
name: 'Null'
}
export declare class Undefined extends Prototype {
name: 'Undefined'
}
export declare class None extends Prototype {
name: 'None'
}
export declare class Any extends Prototype {
name: 'Any'
}
export declare class Numeric extends Prototype {
name: 'Numeric'
static Number: Numeric
static String: Numeric
}
export declare class Int extends Prototype {
name: 'Int'
static Number: Int
static String: Int
}
export declare class Float extends Prototype {
name: 'Float'
static Number: Float
static String: Float
}
export declare class Negative extends Prototype {
name: 'Negative'
static Number: Negative
static String: Negative
}
export declare class Positive extends Prototype {
name: 'Positive'
static Number: Positive
static String: Positive
}
export declare class Zero extends Prototype {
name: 'Zero'
static Number: Zero
static String: Zero
}
export declare class Natural extends Prototype {
name: 'Natural'
static Number: Natural
static String: Natural
}
export declare class Finity extends Prototype {
name: 'Finity'
}
export declare class String8 extends Prototype {
name: 'String8'
}
export declare class String16 extends Prototype {
name: 'String16'
}
export declare class String32 extends Prototype {
name: 'String32'
}
export declare class String64 extends Prototype {
name: 'String64'
}
export declare class String128 extends Prototype {
name: 'String128'
}
export declare class Type {
readonly name: string
readonly strict: Type
readonly Strict: Type
readonly loose: Type
readonly Loose: Type
constructor(pattern: any)
cache(value: any): Error | null
assert(value: any): void | never
test(value: any): boolean
track(value: any): Promise<Error | null>
trace(value: any): Promise<Error | null>
clone(): Type
toBeStrict(mode: boolean): this
toBeLoose(mode: boolean): this
/**
* format error message text
* @param options
*/
with(options: {
name?: string
strict?: boolean
message?: string
prefix?: string // prefix of an error message
suffix?: string
}): this
toString(): string
}
export declare class Dict extends Type {
constructor(pattern: Obj)
extend(fields: Obj): Dict
extract(fields: { [key: string]: boolean }): Dict
}
export declare function dict(pattern: Obj): Dict
export declare class List extends Type {
constructor(pattern: any[])
}
export declare function list(pattern: any[]): List
export declare class Tupl extends Type {
constructor(pattern: any[])
}
export declare function tupl(pattern: any[]): Tupl
/**
* @deprecated use Tupl instead
*/
export declare type Tuple = Tupl
/**
* @deprecated use tupl instead
*/
export declare type tuple = typeof tupl
export declare class Enum extends Type {
constructor(pattern: any[])
}
export declare function enumerate(pattern: any[]): Enum
export declare class Range extends Type {
constructor(options: {
min: number
max: number
minBound?: boolean
maxBound?: boolean
})
}
export declare function range(options: {
min: number
max: number
minBound?: boolean
maxBound?: boolean
}): Range
export declare class Mapping extends Type {
constructor(options: { key: any, value: any })
}
export declare function mapping(options: { key: any, value: any }): Mapping
export declare class SelfRef {
constructor(fn: (self: SelfRef) => any)
}
export declare function selfref(fn: (self: SelfRef) => any): SelfRef
export declare class Shape extends Type {
constructor(pattern: Obj)
}
export declare function shape(pattern: Obj): Shape
export declare class Rule {
readonly strict: Rule
readonly Strict: Rule
readonly loose: Rule
readonly Loose: Rule
constructor(options: {
name: string
pattern: any
message: string
})
validate(data: Obj, key: string, pattern: any): Error | null
catch(data: Obj, key: string): Error | null
clone(): Rule
toBeStrict(): this
toBeLoose(): this
toString(): string
}
export declare function lazy(fn: () => any): Rule
type IMessage = string | ((data: Obj, key: string) => string)
export declare function match(patterns: any[]): Rule
export declare function match(pattern: any, message: IMessage): Rule
export declare function determine(fn: (value: Obj) => boolean, A: any, B: any): Rule
export declare function shouldmatch(pattern: any, message: IMessage): Rule
export declare function shouldnotmatch(pattern: any, message: IMessage): Rule
export declare function ifexist(pattern: any): Rule
export declare function ifnotmatch(pattern: any, callback: (data: Obj, key: string) => any): Rule
export declare function ifnotmatch(pattern: any, callback: any): Rule
export declare function ifmatch(pattern: any, callback: (data: Obj, key: string) => any): Rule
export declare function ifmatch(pattern: any, callback: any): Rule
export declare function shouldexist(determine: (data: Obj) => boolean, pattern: any): Rule
export declare function shouldnotexist(determine: (data: Obj) => boolean, pattern: any): Rule
export declare function instance(pattern: any): Rule
export declare function equal(pattern: any): Rule
export declare function nullable(pattern: any): Rule
export declare function nonable(pattern: any): Rule
export declare function lambda(input: Tupl | [any, any], output: any): Rule
interface IDecorate {
(source: any): {
with: (...types: any[]) => any
}
with: (...types: any[]) => ClassDecorator | MethodDecorator | PropertyDecorator
}
type ITypes = Dict | List | Enum | Tupl | Range | Mapping | SelfRef | Shape | Type
export declare class Ty {
bind(fn: (error: Error) => void): this
unbind(fn: (error: Error) => void): this
silent(is: boolean): void
throw(error: Error): never
expect(value: any): {
to: {
match: (type: any) => boolean | never
be: (type: any) => boolean | never
},
}
catch(value: any): {
by: (type: any) => Error | null
}
track(value: any): {
by: (type: any) => Promise<Error | null>
}
trace(value: any): {
by: (type: any) => Promise<Error | null>
}
is(value: any): {
typeof: (value: any) => boolean
of: (type: any) => boolean
}
decorate: IDecorate
static expect(value: any): {
to: {
match: (type: any) => boolean | never
be: (type: any) => boolean | never
},
}
static catch(value: any): {
by: (type: any) => Error | null
}
static track(value: any): {
by: (type: any) => Promise<Error | null>
}
static trace(value: any): {
by: (type: any) => Promise<Error | null>
}
static is(value: any): {
typeof: (value: any) => boolean
of: (type: any) => boolean
}
static create(value: any): ITypes
static readonly decorate: IDecorate
}
export declare class Parser {
constructor(types?: Obj)
init(types: Obj): void
define(text: string, target: any): this
parse(description: Obj): ITypes & { __comments__: Obj }
describe(dict: Obj, options?: { arrayStyle: number, ruleStyle: number }): Obj
/**
* give a real data object, give its type shape
* @param data
*/
guess(data: Obj): ITypes
/**
* merge two type shapes
* @param exist
* @param data
*/
merge(exist: Obj, data: Obj): Obj
static defaultTypes: Obj
}
type ILoader = [any, () => any] | ((target: any, path: string | any[], next: Function) => any | void)
type ILoaders = Array<ILoader>
export declare class Mocker {
constructor(loaders: ILoaders)
define(loader: ILoader): this
mock(type: any): any
static defaultLoaders: ILoaders
}
interface IBindFn {
(store: Store, on: string): IBindFn
}
interface IWatchFn {
(e: { target: string, key: string[], value: any, next: any, prev: any, active: any, invalid: any }): void
}
export declare class Store {
constructor(params?: Obj)
init(params: Obj): void
get(keyPath: string | (string | symbol)[]): any
set(keyPath: string | (string | symbol)[], value: any, silent?: boolean): any
del(keyPath: string | (string | symbol)[]): void
update(data: Obj, async?: boolean, silent?: boolean): void
define(key: string, options: (() => any) | { get: () => any }): any
bind(key: string): IBindFn
observe(target: Store | Model | Function, subscribe: (target: Store | Model) => (dispatch: IWatchFn) => Function | void, unsubcribe: (target: Store | Model) => (dispatch: IWatchFn) => void): Function
watch(keyPath: string | (string | symbol)[], fn: IWatchFn, deep?: boolean, context?: Model): this
unwatch(keyPath: string | (string | symbol)[], fn: IWatchFn): this
dispatch(keyPath: string | (string | symbol)[], info: { value: any, next: any, prev: any, active: any, invalid: any, compute: boolean }, force?: boolean): boolean
forceDispatch(keyPath: string | (string | symbol)[], ...args: any[]): boolean
}
interface ValidatorOptions<T extends Model = Model, I extends any = any> {
name?: string
determine?: boolean | ((this: T, value: I, key: string) => boolean | Promise<boolean>)
validate: (this: T, value: I, key: string) => boolean | Promise<boolean> | Error
message?: string
break?: boolean
async?: boolean
}
export declare class Validator<T extends Model = Model, I extends any = any> {
constructor(options: ValidatorOptions<T, I>)
extend<M extends Model = T>(attrs: Obj): Validator<M, I>
static readonly required: <T extends Model>(message: string, emptyFn?: (this: T, value: any) => boolean) => Validator
static readonly maxLen: (message: string, len?: number) => Validator
static readonly minLen: (message: string, len?: number) => Validator
static readonly max: (message: string, len?: number) => Validator
static readonly min: (message: string, len?: number) => Validator
static readonly integer: (message: string, len?: number) => Validator
static readonly decimal: (message: string, len?: number) => Validator
static readonly email: (message: string) => Validator
static readonly url: (message: string) => Validator
static readonly date: (message: string) => Validator
static readonly match: <T extends Model>(validator: RegExp | string | number | boolean | ((this: T, value: any) => boolean) | any, message: string, name?: string) => Validator
static readonly allOf: (validators: Validator[], message: string) => Validator
static readonly anyOf: (validators: Validator[], message: string) => Validator
}
type ModelClass = new (...args: any[]) => Model
type MetaClass<T = any, I = T, M extends Model = Model, U extends Obj = Obj> = new (options: Attrs<T, I, M, U>) => Meta<T, I, M, U>
export type Attrs<T = any, I = T, M extends Model = Model, U extends Obj = Obj> = {
/**
* field default value, used by `reset` `formJSON` and so on
*/
default: T
/**
* field is a computed field, value will be computed until be changed by `set`
*/
compute?(this: M): T
/**
* calculate value when init and the dependencies change,
* different from `compute`, it will rewrite value when inside dependencies change,
* you can change the value manually, however, the manual value will be changed by `activate` later if dependencies change
*/
activate?(this: M): T
/**
* calculate value when init and the dependencies change,
* different from `compute`, it will rewrite value when inside dependencies change,
* you can change the value manually, however, the manual value will be changed by `activate` later if dependencies change.
* it is almost like `activate` but not trigger the fields which depend on current field.
*/
accept?(this: M): T
/**
* field value type
*/
type?: any
/**
* error message when set a value not match `type`
*/
message?: string
/**
* force set `default` when value not match `type`
*/
force?: boolean
/**
* validators used by `validate` or `validateAsync`
*/
validators?: (Validator<M, I> | ValidatorOptions<M, I>)[] | {
[key: string]: Validator<M, I> | ValidatorOptions<M, I>
}
/**
* create field value used by `formJSON`
*/
create?(this: M, value: any, key: string, data: U): T
/**
* export field value used by `toJSON`
*/
save?(this: M, value: T, key: string, data: U): Obj | any | void
/**
* save another filed data to output data of `toJSON()`
*/
saveAs?(this: M, value: T, key: string, data: U, output: Obj): Obj | void
/**
* if without `create` and `save`, asset will used as field read proof
*/
asset?: string
/**
* whether drop this field when `toData()`
*/
drop?: boolean | ((this: M, value: I, key: string, data: U) => boolean)
/**
* transfer the field value when `toData()`
* when `drop` is `false`, map will not work
*/
map?(this: M, value: I, key: string, data: U): any | void
/**
* map another filed data to output data of `toData()`
* `drop` has no effect to mapAs
*/
mapAs?(this: M, value: I, key: string, data: U, output: Obj): Obj | void
/**
* transfer field name to `to` when `toData`
* can use keyPath like 'some.any'
*/
to?: string
/**
* transfer given value when `set`
*/
setter?(this: M, value: I, key: string): T
/**
* transfer output value when `get`
*/
getter?(this: M, value: T, key: string): I
/**
* format field value when use `view.text`
*/
formatter?(this: M, value: T, key: string): string
/**
* whether the field is readonly, `set` will not work
*/
readonly?: boolean | ((this: M, value: I, key: string) => boolean)
/**
* whether the field is useless, `drop` will be set true, validators will not work
*/
disabled?: boolean | ((this: M, value: I, key: string) => boolean)
/**
* whether hide the field, without any effect on model, just a UI helper
*/
hidden?: boolean | ((this: M, value: I, key: string) => boolean)
/**
* whether to make the field available, if false, disabled & drop & readonly & hidden will be forcely set `true`
*/
available?: boolean | ((this: M, value: I, key: string) => boolean)
/**
* whether the field is required, should be used together with Validator.required in `vlaidators`
*/
required?: boolean | ((this: M, value: I, key: string) => boolean)
/**
* determine the field is empty, used with `required`
*/
empty?(this: M, value: I, key: string): boolean
/**
* works with Validator.max when value is numeric
*/
max?: number
/**
* works with Validator.min when value is numeric
*/
min?: number
/**
* works with Validator.max when value is string
*/
maxLen?: number
/**
* works with Validator.min when value is string
*/
minLen?: number
/**
* provide deps
*/
deps?(this: M, key: string): { [key: string]: Meta | MetaClass }
/**
* provide information about needs, it means this field should must work with this metas
*/
needs?(this: M, key: string): Array<Meta | MetaClass | ModelClass>
/**
* provide information about factors, it means this field will be trigger by the given metas
*/
factors?(): Array<Meta | MetaClass | ModelClass>
/**
* provide state
*/
state?(this: M, key: string): Obj
/**
* invoked when Model initialized
*/
init?(this: M, key: string): void
/**
* invoked when field value changed
*/
watch?(this: M, e: { value: I } & Obj, key: string): void
/**
* when **other** fields changed, follow function will be triggered,
* current field changing will NOT be triggered (use watch instead)
*/
follow?(this: M, e: Obj, key: string, keyOfChangedField: string):
| void
| Array<{
/**
* the target to follow
*/
key?: string
/**
* the target to follow
*/
meta?: Meta
/**
* when target field changed, this `action` will be invoked
*/
action: (this: M, valueOfChangedField: any, keyOfChangedField: string) => void
}>
/**
* invoked errors occur when field change
*/
catch?(this: M, error: Error, key: string): void
} & Obj & ThisType<M>
export declare class Meta<T = any, I = T, M extends Model = Model, U extends Obj = Obj> {
constructor(options?: Attrs<T, I, M, U>)
extend<D extends T = T, V extends I = I, O extends M = M, B extends Obj = U>(attrs: Partial<Attrs<D, V, O, B>>): Meta<D, V, O, B>
static extend<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>): typeof Meta & MetaClass<T, I, M, U>
static create<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>): typeof Meta & MetaClass<T, I, M, U>
}
export declare class AsyncMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj> extends Meta<T, I, M, U> {
fetchAsyncAttrs(): Promise<Omit<Attrs<T, I, M, U>, 'default'>>
}
export declare class SceneMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj> extends Meta<T, I, M, U> {
/**
* define scenes mapping
*/
defineScenes(): {
[sceneCode: string]: Partial<Attrs<T, I, M, U>> | (() => Partial<Attrs<T, I, M, U>>) | (() => Promise<Partial<Attrs<T, I, M, U>>>)
}
/**
* switch self to new scene
* @param sceneCodes
*/
switchScene(sceneCodes: string | string[]): this
/**
* switch to a new meta instance with given scene
* @param sceneCodes
*/
Scene(sceneCodes: string | string[]): this
/**
* get a new SceneMeta constructor with given scene
* @param sceneCodes
*/
static Scene<T>(this: ConstructorOf<T>, sceneCodes: string | string[]): ConstructorOf<T> & typeof SceneMeta
}
export declare class StateMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj> extends Meta<T, I, M, U> {
constructor(attrs?: Omit<Attrs<T, I, M, U>, 'default' | 'validators' | 'drop' | 'to' | 'map' | 'disabled' | 'state'> & { value: T })
}
export declare class SceneStateMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj> extends SceneMeta<T, I, M, U> {
constructor(attrs?: Omit<Attrs<T, I, M, U>, 'default' | 'validators' | 'drop' | 'to' | 'map' | 'disabled' | 'state'> & { value: T })
}
/**
* crete a meta by given attributes
* T: the value type
* I: the type of output of getter and input of setter
* M: the attached Model
* U: the type of whole data node
* @param attrs
*/
export declare function createMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>): Meta<T, I, M, U>
// /**
// * create a meta by given Model
// * @param entries Model
// * @param attrs append attributes, omit 'default'
// * @param hooks factory hooks
// * @example
// * class A extends Model {}
// * const meta = createMeta(A)
// * class C extends Model {
// * static some = meta
// * // same as: static some = A
// * }
// */
// declare function createMeta<T extends ModelClass = ModelClass, M extends Model = Model, U extends Obj = Obj>(entries: T, attrs?: Omit<Attrs<InstanceOf<T>, InstanceOf<T>, M, U>, 'default'>, hooks?: FactoryHooks): Meta<InstanceOf<T>, InstanceOf<T>, M, U>
// /**
// * create a meta by given Models
// * T: union of ModelClass, i.e. SomeModel | AnyModel
// * @param entries Model[]
// * @param attrs
// * @param hooks
// * @example
// * class A extends Model {}
// * class B extends Model {}
// * const meta = createMeta<A | B>([A, B])
// * class C extends Model {
// * static some = meta
// * // same as: static some = [A, B]
// * }
// */
// declare function createMeta<T extends ModelClass[] = ModelClass[], M extends Model = Model, U extends Obj = Obj>(entries: T, attrs?: Omit<Attrs<InstanceOf<ItemOf<T>>[], InstanceOf<ItemOf<T>>[], M, U>, 'default'>, hooks?: FactoryHooks): Meta<InstanceOf<ItemOf<T>>[], InstanceOf<ItemOf<T>>[], M, U>
/**
* @deprecated
*/
export declare function createMetaGroup<T extends Meta[]>(create: (...args: Meta[]) => T): T
/**
* create serval metas as an array
* @param count how many metas do you want to create
* @param create
* @example
* const [A, B, C] = createMetaRef((A, B, C) => [
* createMeta(...),
* createMeta(...),
* createMeta(...),
* ])
*/
export declare function createMetaRef<T extends Meta[]>(create: (...args: Meta[]) => T): T
/**
* create an async meta, which can be overrided by asyncGetter return value
* @param attrs
* @param asyncGetter
*/
export declare function createAsyncMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>, asyncGetter: () => Promise<Partial<Attrs<T, I, M, U>>>): AsyncMeta<T, I, M, U>
type SceneMapping<T, I, M, U> = {
[sceneCode: string]: Partial<Attrs<T, I, M, U>> | (() => Partial<Attrs<T, I, M, U>>) | (() => Promise<Partial<Attrs<T, I, M, U>>>)
}
/**
* create a scene meta, which can be switch to certain scene by Model#Scene(sceneCode)
* @param attrs
* @param mapping
*/
export declare function createSceneMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Attrs<T, I, M, U>, mapping: SceneMapping<T, I, M, U> | (() => SceneMapping<T, I, M, U>)): SceneMeta<T, I, M, U>
/**
* create a state meta, whose disabled is force set to be true
* @param attrs
*/
export declare function createStateMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Omit<Attrs<T, I, M, U>, 'default' | 'drop' | 'to' | 'map' | 'state'> & { value: T }): Meta<T, I, M, U>
/**
* create a state meta which is in scene mode
* @param attrs
* @param mapping
*/
export declare function createSceneStateMeta<T = any, I = T, M extends Model = Model, U extends Obj = Obj>(attrs: Omit<Attrs<T, I, M, U>, 'default' | 'drop' | 'to' | 'map' | 'state'> & { value: T }, mapping: SceneMapping<T, I, M, U> | (() => SceneMapping<T, I, M, U>)): SceneMeta<T, I, M, U>
/**
* use field value type from a meta
* @example
* ReflectMeta<SomeMeta> extends string
* ReflectMeta<SomeMeta, 'data'> extends object
* ReflectMeta<SomeMeta, 'default'> extends string, from attrs
*/
export declare type ReflectMeta<A extends Meta | MetaClass, key = 'value'> =
A extends SceneStateMeta<infer T, infer I, infer M, infer U> ?
key extends 'value' ? I
: key extends 'originalValue' ? T
: key extends 'model' ? M
: key extends 'data' ? U
: never
: A extends SceneMeta<infer T, infer I, infer M, infer U> ?
key extends 'value' ? I
: key extends 'originalValue' ? T
: key extends 'model' ? M
: key extends 'data' ? U
: never
: A extends AsyncMeta<infer T, infer I, infer M, infer U> ?
key extends 'value' ? I
: key extends 'originalValue' ? T
: key extends 'model' ? M
: key extends 'data' ? U
: never
: A extends Meta<infer T, infer I, infer M, infer U> ?
key extends 'value' ? I
: key extends 'originalValue' ? T
: key extends 'model' ? M
: key extends 'data' ? U
: never
: A extends MetaClass<infer T, infer I, infer M, infer U> ?
key extends 'value' ? I
: key extends 'originalValue' ? T
: key extends 'model' ? M
: key extends 'data' ? U
: never
: never
/**
* use field view type from a meta
* @example
* ReflectView<SomeMeta> extends View
*/
export declare type ReflectView<M extends Meta | MetaClass> =
M extends SceneMeta<infer T, infer I> ? View<T, I>
: M extends SceneMeta<infer T, infer I> ? View<T, I>
: M extends AsyncMeta<infer T, infer I> ? View<T, I>
: M extends Meta<infer T, infer I> ? View<T, I>
: M extends MetaClass<infer T, infer I> ? View<T, I>
: View
type View<T = any, I = T> = {
/**
* field name
*/
key: string
/**
* field value, transfered by `getter`
*/
value: I
/**
* field original value, stored inside a Store
*/
data: T
/**
* field text formatted by `formatter`
*/
text: string
/**
* state refer to current field
*/
state: Obj
/**
* field absolute keyPath
*/
absKeyPath: string[]
/**
* errors by validators
*/
errors: Error[] | any[]
/**
* is empty? by `empty`
*/
empty: boolean
/**
* is readonly? by `readonly`
*/
readonly: boolean
/**
* is disabled? by `disabled`
*/
disabled: boolean
/**
* is hidden? by `hidden`
*/
hidden: boolean
/**
* is required? by `required`
*/
required: boolean
/**
* is the field value changed
*/
changed: boolean
} & Obj
export declare class Model implements Obj {
constructor(data?: Obj)
$views: {
[field: string]: View
} & {
$changed: boolean
$state: Obj,
$errors: Error[] | any[]
/**
* if it is validating, you can use this to check
*/
$validatings: Promise<Error[] | any[]>
}
$schema: {
[field: string]: Meta
}
$root: Model
$parent: Model
$keyPath: string[]
$absKeyPath: string[]
schema(Schema?: any): Obj
state(): Obj
attrs(): Obj
restore(data: Obj): this
get(keyPath: string | string[]): any
set(keyPath: string | string[], next: any, force?: boolean): this
update(data: Obj): this
reset(key: string | Meta | MetaClass): this
patch(data: Obj): this
define<V = any>(key: string, value: (() => V) | V): V
define(key: string): void
daemon<V = any>(getter: (this: this) => V, runner: (this: this, value: V) => void, immediate: boolean, silent: boolean): V
lock(): void
unlock(): void
setParent(parent: [Model, string]): this
watch(key: string | Meta, fn: IWatchFn, deep?: boolean): this
unwatch(key: string | Meta, fn: IWatchFn): this
/**
* operate model with a chunk,
* you can patch a chunk (Factory.chunk) to a static Chunk property of a Model
* @param chunk
*/
Chunk<U extends any[] = any[]>(chunk?: FactoryChunk<Model, any, U>): {
fromChunk: (...args: U) => Promise<any>,
fromJSON: (data: Obj) => void,
toData: () => any,
toJSON: () => any,
}
fromJSON(data: Obj): this
fromJSONPatch(data: Obj): this
toJSON(): Obj
toData(): Obj
toParams(determine?: (value: any) => boolean): Obj
toFormData(determine?: (value: any) => boolean): Obj
validate(key?: string | string[]): Error[] | any[]
validateAsync(key?: string | string[]): Promise<Error[] | any[]>
on(hook: string, fn: Function): this
off(hook: string, fn: Function): this
emit(hook: string, ...args: any[]): void
Edit(next?: Obj): this & EditorModel
use(keyPath: string[]): View