-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathparams.ts
191 lines (174 loc) · 4.9 KB
/
params.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
import * as Serialize from './serialize';
import { QueryParamConfig } from './types';
/**
* String values
*/
export const StringParam: QueryParamConfig<
string | null | undefined,
string | null | undefined
> = {
encode: Serialize.encodeString,
decode: Serialize.decodeString,
};
/**
* String enum
*/
export const createEnumParam = <T extends string>(
enumValues: T[]
): QueryParamConfig<T | null | undefined, T | null | undefined> => ({
encode: Serialize.encodeString,
decode: (input) => Serialize.decodeEnum(input, enumValues),
});
/**
* Array enum
*/
export const createEnumArrayParam = <T extends string>(
enumValues: T[]
): QueryParamConfig<T[] | null | undefined, T[] | null | undefined> => ({
encode: (text) =>
Serialize.encodeArray(text == null || Array.isArray(text) ? text : [text]),
decode: (input) => Serialize.decodeArrayEnum(input, enumValues),
});
/**
* Array delimited enum
*/
export const createEnumDelimitedArrayParam = <T extends string>(
enumValues: T[],
entrySeparator = '_'
): QueryParamConfig<T[] | null | undefined, T[] | null | undefined> => ({
encode: (text) =>
Serialize.encodeDelimitedArray(
text == null || Array.isArray(text) ? text : [text],
entrySeparator
),
decode: (input) =>
Serialize.decodeDelimitedArrayEnum(input, enumValues, entrySeparator),
});
/**
* Numbers (integers or floats)
*/
export const NumberParam: QueryParamConfig<
number | null | undefined,
number | null | undefined
> = {
encode: Serialize.encodeNumber,
decode: Serialize.decodeNumber,
};
/**
* For flat objects where values are strings
*/
export const ObjectParam: QueryParamConfig<
{ [key: string]: string | undefined } | null | undefined,
{ [key: string]: string | undefined } | null | undefined
> = {
encode: Serialize.encodeObject,
decode: Serialize.decodeObject,
};
/**
* For flat arrays of strings, filters out undefined values during decode
*/
export const ArrayParam: QueryParamConfig<
(string | null)[] | null | undefined,
(string | null)[] | null | undefined
> = {
encode: Serialize.encodeArray,
decode: Serialize.decodeArray,
};
/**
* For flat arrays of strings, filters out undefined values during decode
*/
export const NumericArrayParam: QueryParamConfig<
(number | null)[] | null | undefined,
(number | null)[] | null | undefined
> = {
encode: Serialize.encodeNumericArray,
decode: Serialize.decodeNumericArray,
};
/**
* For any type of data, encoded via JSON.stringify
*/
export const JsonParam: QueryParamConfig<any, any> = {
encode: Serialize.encodeJson,
decode: Serialize.decodeJson,
};
/**
* For simple dates (YYYY-MM-DD)
*/
export const DateParam: QueryParamConfig<
Date | null | undefined,
Date | null | undefined
> = {
encode: Serialize.encodeDate,
decode: Serialize.decodeDate,
equals: (
valueA: Date | null | undefined,
valueB: Date | null | undefined
) => {
if (valueA === valueB) return true;
if (valueA == null || valueB == null) return valueA === valueB;
// ignore time of day
return (
valueA.getFullYear() === valueB.getFullYear() &&
valueA.getMonth() === valueB.getMonth() &&
valueA.getDate() === valueB.getDate()
);
},
};
/**
* For dates in simplified extended ISO format (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ)
*/
export const DateTimeParam: QueryParamConfig<
Date | null | undefined,
Date | null | undefined
> = {
encode: Serialize.encodeDateTime,
decode: Serialize.decodeDateTime,
equals: (
valueA: Date | null | undefined,
valueB: Date | null | undefined
) => {
if (valueA === valueB) return true;
if (valueA == null || valueB == null) return valueA === valueB;
return valueA.valueOf() === valueB.valueOf();
},
};
/**
* For boolean values: 1 = true, 0 = false
*/
export const BooleanParam: QueryParamConfig<
boolean | null | undefined,
boolean | null | undefined
> = {
encode: Serialize.encodeBoolean,
decode: Serialize.decodeBoolean,
};
/**
* For flat objects where the values are numbers
*/
export const NumericObjectParam: QueryParamConfig<
{ [key: string]: number | null | undefined } | null | undefined,
{ [key: string]: number | null | undefined } | null | undefined
> = {
encode: Serialize.encodeNumericObject,
decode: Serialize.decodeNumericObject,
};
/**
* For flat arrays of strings, filters out undefined values during decode
*/
export const DelimitedArrayParam: QueryParamConfig<
(string | null)[] | null | undefined,
(string | null)[] | null | undefined
> = {
encode: Serialize.encodeDelimitedArray,
decode: Serialize.decodeDelimitedArray,
};
/**
* For flat arrays where the values are numbers, filters out undefined values during decode
*/
export const DelimitedNumericArrayParam: QueryParamConfig<
(number | null)[] | null | undefined,
(number | null)[] | null | undefined
> = {
encode: Serialize.encodeDelimitedNumericArray,
decode: Serialize.decodeDelimitedNumericArray,
};