-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_declaration.ts
106 lines (88 loc) · 3.09 KB
/
field_declaration.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
import { DECLARATION_PROP } from './constants'
import { CastPrimitiveTo } from './converter'
import { AllKeysAre } from './global_types'
import { ModelWrapper } from './model_wrapper'
import { PropDeclaration } from './prop_declaration'
import { createSchemeFromOptions } from './scheme'
export declare type ModelDeclaration<T = any> =
| ModelWrapper<T>
| AllKeysAre<CommonFieldCreator | PropDeclaration>
export declare type ModelArrayDeclaration<T = any> =
| ModelDeclaration<T>
| keyof CastPrimitiveTo
export declare interface BasePropertyOptions {
arrayType: boolean
}
export declare interface CommonPropertyOptions {
optional: boolean
}
export declare interface FieldConfiguration
extends Partial<BasePropertyOptions>,
Partial<CommonPropertyOptions> {
name: string
type?: PropertyType
usageType?: PropertyType
}
export declare type PropertyNameDeclaration = string
export declare type PropertyType = keyof CastPrimitiveTo
export declare type CustomSerializerFunc = (
originalModel: AllKeysAre<any>
) => any
export declare type CustomDeserializerFunc = (
usageModel: AllKeysAre<any>
) => any
export declare type FieldOptions =
| [PropertyNameDeclaration, PropertyType?, PropertyType?]
| [PropertyNameDeclaration, ModelDeclaration]
| [FieldConfiguration]
| [CustomSerializerFunc, CustomDeserializerFunc?]
export declare type FieldArrayDeclaration =
| [PropertyNameDeclaration, PropertyType?, PropertyType?]
| [PropertyNameDeclaration, ModelDeclaration]
export declare interface CommonFieldCreator {
(propertyOptions: Partial<CommonPropertyOptions>): PropDeclaration
[DECLARATION_PROP]: boolean
}
declare type StringsFieldDeclaration = (
originalProperty: PropertyNameDeclaration,
originalType?: PropertyType,
usageType?: PropertyType
) => CommonFieldCreator
declare type ModelFieldDeclaration = (
originalProperty: PropertyNameDeclaration,
DeclaredModel: ModelDeclaration
) => CommonFieldCreator
declare type ConfigurationFieldDeclaration = (
fieldConfiguration: FieldConfiguration
) => CommonFieldCreator
declare type SerializersFieldDeclaration = (
customSerializer: CustomSerializerFunc,
customDeserializer?: CustomDeserializerFunc
) => CommonFieldCreator
export declare type FieldCreatorDeclaration =
| StringsFieldDeclaration
| ModelFieldDeclaration
| ConfigurationFieldDeclaration
| SerializersFieldDeclaration
export declare type FieldsArrayCreatorDeclaration =
| StringsFieldDeclaration
| ModelFieldDeclaration
const createFieldDeclaration = (
options: FieldOptions | FieldArrayDeclaration,
arrayType: BasePropertyOptions['arrayType']
): CommonFieldCreator => {
const commonFieldCreator = ({ optional = false } = {}) => ({
[DECLARATION_PROP]: true,
scheme: createSchemeFromOptions({
arrayType,
optional,
options
})
})
commonFieldCreator[DECLARATION_PROP] = true
return commonFieldCreator as CommonFieldCreator
}
export const createField = (...options: FieldOptions) =>
createFieldDeclaration(options, false)
export const createFieldsArray = (...options: FieldArrayDeclaration) =>
createFieldDeclaration(options, true)