-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathoptions.ts
185 lines (154 loc) · 4.7 KB
/
options.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
export interface SendOptions extends RequestInit {
// for backward compatibility and to minimize the verbosity,
// any top-level field that doesn't exist in RequestInit or the
// fields below will be treated as query parameter.
[key: string]: any;
/**
* Optional custom fetch function to use for sending the request.
*/
fetch?: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response>;
/**
* Custom headers to send with the requests.
*/
headers?: { [key: string]: string };
/**
* The body of the request (serialized automatically for json requests).
*/
body?: any;
/**
* Query parameters that will be appended to the request url.
*/
query?: { [key: string]: any };
/**
* @deprecated use `query` instead
*
* for backward-compatibility `params` values are merged with `query`,
* but this option may get removed in the final v1 release
*/
params?: { [key: string]: any };
/**
* The request identifier that can be used to cancel pending requests.
*/
requestKey?: string | null;
/**
* @deprecated use `requestKey:string` instead
*/
$cancelKey?: string;
/**
* @deprecated use `requestKey:null` instead
*/
$autoCancel?: boolean;
}
export interface CommonOptions extends SendOptions {
fields?: string;
}
export interface ListOptions extends CommonOptions {
page?: number;
perPage?: number;
sort?: string;
filter?: string;
skipTotal?: boolean;
}
export interface FullListOptions extends ListOptions {
batch?: number;
}
export interface RecordOptions extends CommonOptions {
expand?: string;
}
export interface RecordListOptions extends ListOptions, RecordOptions {}
export interface RecordFullListOptions extends FullListOptions, RecordOptions {}
export interface RecordSubscribeOptions extends SendOptions {
fields?: string;
filter?: string;
expand?: string;
}
export interface LogStatsOptions extends CommonOptions {
filter?: string;
}
export interface FileOptions extends CommonOptions {
thumb?: string;
download?: boolean;
}
export interface AuthOptions extends CommonOptions {
/**
* If autoRefreshThreshold is set it will take care to auto refresh
* when necessary the auth data before each request to ensure that
* the auth state is always valid.
*
* The value must be in seconds, aka. the amount of seconds
* that will be subtracted from the current token `exp` claim in order
* to determine whether it is going to expire within the specified time threshold.
*
* For example, if you want to auto refresh the token if it is
* going to expire in the next 30mins (or already has expired),
* it can be set to `1800`
*/
autoRefreshThreshold?: number;
}
// -------------------------------------------------------------------
// list of known SendOptions keys (everything else is treated as query param)
const knownSendOptionsKeys = [
"requestKey",
"$cancelKey",
"$autoCancel",
"fetch",
"headers",
"body",
"query",
"params",
// ---,
"cache",
"credentials",
"headers",
"integrity",
"keepalive",
"method",
"mode",
"redirect",
"referrer",
"referrerPolicy",
"signal",
"window",
];
// modifies in place the provided options by moving unknown send options as query parameters.
export function normalizeUnknownQueryParams(options?: SendOptions): void {
if (!options) {
return;
}
options.query = options.query || {};
for (let key in options) {
if (knownSendOptionsKeys.includes(key)) {
continue;
}
options.query[key] = options[key];
delete options[key];
}
}
export function serializeQueryParams(params: { [key: string]: any }): string {
const result: Array<string> = [];
for (const key in params) {
const encodedKey = encodeURIComponent(key);
const arrValue = Array.isArray(params[key]) ? params[key] : [params[key]];
for (let v of arrValue) {
v = prepareQueryParamValue(v);
if (v === null) {
continue
}
result.push(encodedKey + "=" + v);
}
}
return result.join("&");
}
// encodes and normalizes the provided query param value.
function prepareQueryParamValue(value: any): null|string {
if (value === null || typeof value === "undefined") {
return null;
}
if (value instanceof Date) {
return encodeURIComponent(value.toISOString().replace("T", " "));
}
if (typeof value === "object") {
return encodeURIComponent(JSON.stringify(value));
}
return encodeURIComponent(value)
}