-
Notifications
You must be signed in to change notification settings - Fork 119
/
process-params.ts
109 lines (96 loc) · 2.95 KB
/
process-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
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
/**
* Checks parameters to see if we should use FormData to send the request
* @param params The object whose keys will be encoded.
* @return A boolean indicating if FormData will be required.
*/
export function requiresFormData(params: any) {
return Object.keys(params).some(key => {
let value = params[key];
if (!value) {
return false;
}
if (value && value.toParam) {
value = value.toParam();
}
const type = value.constructor.name;
switch (type) {
case "Array":
return false;
case "Object":
return false;
case "Date":
return false;
case "Function":
return false;
case "Boolean":
return false;
case "String":
return false;
case "Number":
return false;
default:
return true;
}
});
}
/**
* Converts parameters to the proper representation to send to the ArcGIS REST API.
* @param params The object whose keys will be encoded.
* @return A new object with properly encoded values.
*/
export function processParams(params: any): any {
const newParams: any = {};
Object.keys(params).forEach(key => {
let param = params[key];
if (param && param.toParam) {
param = param.toParam();
}
if (
!param &&
param !== 0 &&
typeof param !== "boolean" &&
typeof param !== "string"
) {
return;
}
const type = param.constructor.name;
let value: any;
// properly encodes objects, arrays and dates for arcgis.com and other services.
// ported from https://github.com/Esri/esri-leaflet/blob/master/src/Request.js#L22-L30
// also see https://github.com/Esri/arcgis-rest-js/issues/18:
// null, undefined, function are excluded. If you want to send an empty key you need to send an empty string "".
switch (type) {
case "Array":
// Based on the first element of the array, classify array as an array of arrays, an array of objects
// to be stringified, or an array of non-objects to be comma-separated
// eslint-disable-next-line no-case-declarations
const firstElementType = param[0]?.constructor?.name;
value =
firstElementType === "Array" ? param : // pass thru array of arrays
firstElementType === "Object" ? JSON.stringify(param) : // stringify array of objects
param.join(","); // join other types of array elements
break;
case "Object":
value = JSON.stringify(param);
break;
case "Date":
value = param.valueOf();
break;
case "Function":
value = null;
break;
case "Boolean":
value = param + "";
break;
default:
value = param;
break;
}
if (value || value === 0 || typeof value === "string" || Array.isArray(value)) {
newParams[key] = value;
}
});
return newParams;
}