-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathutils.ts
468 lines (443 loc) · 10.4 KB
/
utils.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* Copyright (c) 2018-2021 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import { IUser, UserSession } from "@esri/arcgis-rest-auth";
import { IGroup, ISearchOptions } from "@esri/arcgis-rest-portal";
import { ISearchResponse } from "../types";
import { cloneObject, unique } from "../util";
import {
IMatchOptions,
IDateRange,
IRelativeDate,
IWellKnownApis,
IApiDefinition,
NamedApis,
} from "./types";
/**
* Well known APIs
* Short-forms for specifying common APIs
* We will likely deprecate this
*/
export const SEARCH_APIS: IWellKnownApis = {
arcgis: {
label: "ArcGIS Online",
url: "https://www.arcgis.com",
type: "arcgis",
},
arcgisQA: {
label: "ArcGIS Online QAEXT",
url: "https://qaext.arcgis.com",
type: "arcgis",
},
arcgisDEV: {
label: "ArcGIS Online DEVEXT",
url: "https://devext.arcgis.com",
type: "arcgis",
},
hub: {
label: "ArcGIS Hub",
url: "https://hub.arcgis.com/api",
type: "arcgis-hub",
},
hubDEV: {
label: "ArcGIS Hub DEV",
url: "https://hubdev.arcgis.com/api",
type: "arcgis-hub",
},
hubQA: {
label: "ArcGIS Hub QA",
url: "https://hubqa.arcgis.com/api",
type: "arcgis-hub",
},
};
/**
* @private
* Convert array of api "names" into full ApiDefinitions
* @param apis
* @returns
*/
export function expandApis(
apis: Array<NamedApis | IApiDefinition>
): IApiDefinition[] {
return apis.map(expandApi);
}
/**
* @private
* Convert an api "name" into a full ApiDefinition
* @param api
* @returns
*/
export function expandApi(api: NamedApis | IApiDefinition): IApiDefinition {
if (typeof api === "string" && api in SEARCH_APIS) {
return SEARCH_APIS[api];
} else {
// it's an object, so we trust that it's well formed
return api as IApiDefinition;
}
}
/**
* @private
* Merge two date ranges by taking the longest span
* @param dr1
* @param dr2
* @returns
*/
export function mergeDateRange(
dr1: IDateRange<number>,
dr2: IDateRange<number>
): IDateRange<number> {
const result = cloneObject(dr1);
// feels like there is a more concise way to do this...
if (dr2.from < dr1.from) {
result.from = dr2.from;
}
if (dr2.to > dr1.to) {
result.to = dr2.to;
}
return result;
}
/**
* @private
* Merge two [`MatchOptions`](../MatchOptions)
*
* Currently a naieve implementation where the arrays are simply merged
*
* @param mo1
* @param mo2
* @returns
*/
export function mergeMatchOptions(
mo1: IMatchOptions,
mo2: IMatchOptions
): IMatchOptions {
const result = {} as IMatchOptions;
// None of these props are required, so we can't just
// use Object.keys/.entries
const props = ["any", "all", "not", "exact"];
props.forEach((prop) => {
const key = prop as keyof IMatchOptions;
const merged = [...getMatchValue(mo1, key), ...getMatchValue(mo2, key)];
if (merged.length) {
// remove any dupes and set on the return
result[key] = merged.filter(unique);
}
});
return result;
}
/**
* Get the value of a property on an IMatchOptions
*
* This is complex b/c all the props are optional, and
* they could be a simple string, or an array of strings.
*
* This function normalizes all that and returns an array,
* which may or may not be empty
* @param option
* @param key
* @returns
*/
function getMatchValue(
option: IMatchOptions,
key: keyof IMatchOptions
): string[] {
let matchValue: string[] = [];
if (option[key]) {
const val = option[key];
if (Array.isArray(val)) {
matchValue = val as string[];
} else {
matchValue = [val];
}
}
return matchValue;
}
/**
* @private
* Convert a field value into a MatchOptions if it's not already one
* @param value
* @returns
*/
export function valueToMatchOptions(
value: string | string[] | IMatchOptions
): IMatchOptions {
let result = {};
if (Array.isArray(value)) {
result = {
any: value,
};
} else {
if (typeof value === "string") {
result = {
any: [value],
};
}
if (typeof value === "object") {
result = value;
}
}
return result;
}
/**
* @private
* Convert a RelativeDate to a DateRange<number>
* @param relative
* @returns
*/
export function relativeDateToDateRange(
relative: IRelativeDate
): IDateRange<number> {
// hash of offsets
const offsetMs = {
min: 1000 * 60,
hours: 1000 * 60 * 60,
days: 1000 * 60 * 60 * 24,
weeks: 1000 * 60 * 60 * 24 * 7,
};
const now = new Date();
// default
const result: IDateRange<number> = {
type: "date-range",
from: now.getTime(),
to: now.getTime(),
};
//
switch (relative.unit) {
case "hours":
case "days":
case "weeks":
result.from = result.to - offsetMs[relative.unit] * relative.num;
break;
case "months":
// get the current month and subtract num
now.setMonth(now.getMonth() - relative.num);
result.from = now.getTime();
break;
case "years":
now.setFullYear(now.getFullYear() - relative.num);
result.from = now.getTime();
break;
}
return result;
}
/**
* @private
* As a final `ISearchOptions` object gets created, many such objects are created, and
* need to be systematically "merged" so as to return consistently structured `q` and `filter`
* values.
* @param so1
* @param so2
* @param join
* @returns
*/
export function mergeSearchOptions(
so1: ISearchOptions,
so2: ISearchOptions,
join: "AND" | "OR"
): ISearchOptions {
const result = cloneObject(so1) as ISearchOptions;
const { q, filter } = so2;
if (q) {
result.q = (
(result.q ? ` (${result.q} ${join} ${q})` : q) as string
).trim();
}
if (filter) {
result.filter = (
result.filter ? `(${result.filter} ${join} ${filter})` : filter
).trim();
}
return result;
}
/**
* @private
* Serialize a `MatchOptions` into `q` or `filter` on an `ISearchOptions`
* @param key
* @param opts
* @returns
*/
export function serializeMatchOptions(
key: string,
opts: IMatchOptions
): ISearchOptions {
const result = {
q: "",
filter: "",
} as ISearchOptions;
if (opts.exact) {
// defined separately for refactoring later
const userFilterableFields = [
"username",
"firstname",
"lastname",
"fullname",
"email",
];
const itemFilterableFields = [
"title",
"tags",
"typekeywords",
"type",
"name",
"owner",
];
const groupFilterableFields = ["title", "typekeywords", "owner"];
const filterableFields = [
...userFilterableFields,
...itemFilterableFields,
...groupFilterableFields,
];
if (filterableFields.includes(key)) {
result.filter = serializeStringOrArray("AND", key, opts.exact);
} else {
// Treat it the same as .all
if (typeof opts.exact === "string") {
if (!opts.all) {
opts.all = [];
}
if (typeof opts.all === "string") {
opts.all = [opts.all];
}
opts.all.push(opts.exact);
}
if (Array.isArray(opts.exact)) {
if (!opts.all) {
opts.all = [];
}
if (typeof opts.all === "string") {
opts.all = [opts.all];
}
opts.all = opts.all.concat(opts.exact);
}
}
}
// Handle the other props
if (opts.any) {
result.q = serializeStringOrArray("OR", key, opts.any);
}
if (opts.all) {
result.q =
(result.q ? result.q + " AND " : "") +
serializeStringOrArray("AND", key, opts.all);
}
if (opts.not) {
// negate the entries if they are not
result.q =
(result.q ? result.q + " AND " : "") +
serializeStringOrArray("OR", `-${key}`, opts.not);
}
return result;
}
/**
* @private
* Serialize a DateRange<number> into a Portal Query string
* @param key
* @param range
* @returns
*/
export function serializeDateRange(
key: string,
range: IDateRange<number>
): ISearchOptions {
return {
q: `${key}:[${range.from} TO ${range.to}]`,
filter: "",
};
}
/**
* @private
* Serialize a `string` or `string[]` into a string
* @param join
* @param key
* @param value
* @returns
*/
export function serializeStringOrArray(
join: "AND" | "OR",
key: string,
value: string | string[]
): string {
let q = "";
if (Array.isArray(value)) {
q = `(${key}:"${value.join(`" ${join} ${key}:"`)}")`;
} else {
q = `${key}:"${value}"`;
}
return q;
}
/**
* Create a `.next()` function for a type
* @param request
* @param nextStart
* @param total
* @param fn
* @returns
*/
export function getNextFunction<T>(
request: ISearchOptions,
nextStart: number,
total: number,
fn: (r: any) => Promise<ISearchResponse<T>>
): () => Promise<ISearchResponse<T>> {
const clonedRequest = cloneObject(request);
// clone will not handle authentication so we do it manually
if (request.authentication) {
clonedRequest.authentication = UserSession.deserialize(
(request.authentication as UserSession).serialize()
);
}
// figure out the start
clonedRequest.start = nextStart > -1 ? nextStart : total + 1;
return (authentication?: UserSession) => {
if (authentication) {
clonedRequest.authentication = authentication;
}
return fn(clonedRequest);
};
}
/**
* Construct a the full url to a group thumbnail
*
* - If the group has a thumbnail, construct the full url
* - If the group is not public, append on the token (if passed in)
* @param portalUrl
* @param group
* @param token
* @returns
*/
export function getGroupThumbnailUrl(
portalUrl: string,
group: IGroup,
token?: string
): string {
let thumbnailUrl = null;
if (group.thumbnail) {
thumbnailUrl = `${portalUrl}/community/groups/${group.id}/info/${group.thumbnail}`;
if (token && group.access !== "public") {
thumbnailUrl = `${thumbnailUrl}?token=${token}`;
}
}
return thumbnailUrl;
}
/**
* Construct a the full url to a user thumbnail
*
* - If the user has a thumbnail, construct the full url
* - If the user is not public, append on the token
* @param portalUrl
* @param user
* @param token
* @returns
*/
export function getUserThumbnailUrl(
portalUrl: string,
user: IUser,
token?: string
): string {
let thumbnailUrl = null;
if (user.thumbnail) {
thumbnailUrl = `${portalUrl}/community/users/${user.username}/info/${user.thumbnail}`;
if (token && user.access !== "public") {
thumbnailUrl = `${thumbnailUrl}?token=${token}`;
}
}
return thumbnailUrl;
}