-
Notifications
You must be signed in to change notification settings - Fork 96
/
types.ts
64 lines (54 loc) · 1.94 KB
/
types.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
/**
* Encoded query parameters, possibly including null or undefined values
*/
export interface EncodedQuery {
[key: string]: string | (string | null)[] | null | undefined;
}
/**
* Configuration for a query param specifying how to encode it
* (convert it to a string) and decode it (convert it from a string
* back to its native type)
*
* D = type to be encoded
* D2 = type from decode (typically = D)
*/
export interface QueryParamConfig<D, D2 = D> {
/** Convert the query param value to a string */
encode: (value: D) => string | (string | null)[] | null | undefined;
/** Convert the query param string value to its native type */
decode: (value: string | (string | null)[] | null | undefined) => D2;
/** Checks if two values are equal (otherwise typically shallowEqual will be used) */
equals?: (valueA: D | D2, valueB: D | D2) => boolean;
/**
* optionally provide a default value for other tooling
* @note not typically used by serialize-query-params, but use-query-params
* does and it would be annoying for there to be two slightly different
* types.
*/
default?: D2;
/**
* optionally provide a different name when in the URL for other tooling
* @note not typically used by serialize-query-params, but use-query-params
* does and it would be annoying for there to be two slightly different
* types.
*/
urlName?: string;
}
/**
* Mapping from a query parameter name to a { encode, decode } config
*/
export interface QueryParamConfigMap {
[paramName: string]: QueryParamConfig<any, any>;
}
/**
* Mapping from a query parameter name to it's decoded value type
*/
export type DecodedValueMap<QPCMap extends QueryParamConfigMap> = {
[P in keyof QPCMap]: ReturnType<QPCMap[P]['decode']>;
};
/**
* Mapping from a query parameter name to it's encoded value type
*/
export type EncodedValueMap<QPCMap extends QueryParamConfigMap> = {
[P in keyof QPCMap]: string | (string | null)[] | null | undefined;
};