-
Notifications
You must be signed in to change notification settings - Fork 118
/
generic-search.ts
78 lines (70 loc) · 1.9 KB
/
generic-search.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
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
IRequestOptions,
appendCustomParams
} from "@esri/arcgis-rest-request";
import { IItem, IGroup, IUser } from "@esri/arcgis-rest-types";
import { SearchQueryBuilder } from "./SearchQueryBuilder";
import { getPortalUrl } from "../util/get-portal-url";
import { ISearchOptions, ISearchResult } from "../util/search";
export function genericSearch<T extends IItem | IGroup | IUser>(
search: string | ISearchOptions | SearchQueryBuilder,
searchType: "item" | "group" | "user"
): Promise<ISearchResult<T>> {
let url: string;
let options: IRequestOptions;
if (typeof search === "string" || search instanceof SearchQueryBuilder) {
options = {
httpMethod: "GET",
params: {
q: search
}
};
} else {
options = appendCustomParams<ISearchOptions>(
search,
["q", "num", "start", "sortField", "sortOrder"],
{
httpMethod: "GET"
}
);
}
let path = searchType === "item" ? "/search" : "/community/groups";
switch (searchType) {
case "item":
path = "/search";
break;
case "group":
path = "/community/groups";
break;
default:
// "users"
path = "/portals/self/users/search";
break;
}
url = getPortalUrl(options) + path;
// send the request
return request(url, options).then(r => {
if (r.nextStart && r.nextStart !== -1) {
r.nextPage = function() {
let newOptions: ISearchOptions;
if (
typeof search === "string" ||
search instanceof SearchQueryBuilder
) {
newOptions = {
q: search,
start: r.nextStart
};
} else {
newOptions = search;
newOptions.start = r.nextStart;
}
return genericSearch<T>(newOptions, searchType);
};
}
return r;
});
}