This repository has been archived by the owner on May 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
types.ts
46 lines (40 loc) · 1.43 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
/**
* 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;
}
/**
* 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;
};