-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(group): refactor searchGroups to make use of SearchQueryBuilder
AFFECTS PACKAGES: @esri/arcgis-rest-portal BREAKING CHANGE: searchGroups signature change ISSUES CLOSED: #104
- Loading branch information
Showing
11 changed files
with
154 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,24 @@ | ||
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. | ||
/* Copyright (c) 2018-2019 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
request, | ||
IRequestOptions, | ||
IPagingParams, | ||
IGroup | ||
} from "@esri/arcgis-rest-request"; | ||
|
||
import { getPortalUrl } from "../util/get-portal-url"; | ||
|
||
export interface IGroupSearchRequest extends IPagingParams { | ||
q: string; | ||
sortField?: string; | ||
sortOrder?: string; | ||
[key: string]: any; | ||
} | ||
|
||
export interface IGroupSearchResult { | ||
/** | ||
* Matches the REST APIs form param | ||
*/ | ||
query: string; | ||
total: number; | ||
start: number; | ||
num: number; | ||
nextStart: number; | ||
results: IGroup[]; | ||
} | ||
import { SearchQueryBuilder } from "../util/SearchQueryBuilder"; | ||
import { ISearchRequestOptions, ISearchResult } from "../util/search"; | ||
import { genericSearch } from "../util/generic-search"; | ||
|
||
/** | ||
* | ||
* ```js | ||
* import { searchGroups } from '@esri/arcgis-rest-groups'; | ||
* import { searchGroups } from "@esri/arcgis-rest-portal"; | ||
* // | ||
* searchGroups({ | ||
* q:'water' | ||
* }) | ||
* .then(response) // response.results.total => 355 | ||
* searchGroups('water') | ||
* .then(response) // response.total => 355 | ||
* ``` | ||
* Search a portal for groups. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/group-search.htm) for more information. | ||
* Search a portal for items. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/search.htm) for more information. | ||
* | ||
* @param searchForm - Search request | ||
* @param requestOptions - Options for the request | ||
* @param search - A string or RequestOptions object to pass through to the endpoint. | ||
* @returns A Promise that will resolve with the data from the response. | ||
*/ | ||
export function searchGroups( | ||
searchForm: IGroupSearchRequest, | ||
requestOptions?: IRequestOptions | ||
): Promise<IGroupSearchResult> { | ||
// construct the search url | ||
const url = `${getPortalUrl(requestOptions)}/community/groups`; | ||
|
||
// default to a GET | ||
const options: IRequestOptions = { | ||
...{ httpMethod: "GET" }, | ||
...requestOptions | ||
}; | ||
|
||
options.params = { ...searchForm }; | ||
|
||
// send the request | ||
return request(url, options); | ||
search: string | ISearchRequestOptions | SearchQueryBuilder | ||
): Promise<ISearchResult> { | ||
return genericSearch(search, "group"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...st-portal/src/items/SearchQueryBuilder.ts → ...est-portal/src/util/SearchQueryBuilder.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
request, | ||
IRequestOptions, | ||
appendCustomParams | ||
} from "@esri/arcgis-rest-request"; | ||
|
||
import { SearchQueryBuilder } from "./SearchQueryBuilder"; | ||
import { getPortalUrl } from "../util/get-portal-url"; | ||
import { ISearchRequestOptions, ISearchResult } from "../util/search"; | ||
|
||
export function genericSearch( | ||
search: string | ISearchRequestOptions | SearchQueryBuilder, | ||
searchType: "item" | "group" | ||
): Promise<ISearchResult> { | ||
let url: string; | ||
let options: IRequestOptions; | ||
if (typeof search === "string" || search instanceof SearchQueryBuilder) { | ||
options = { | ||
httpMethod: "GET", | ||
params: { | ||
q: search | ||
} | ||
}; | ||
} else { | ||
options = appendCustomParams<ISearchRequestOptions>( | ||
search, | ||
["q", "num", "start", "sortField", "sortDir", "sortOrder"], | ||
{ | ||
httpMethod: "GET" | ||
} | ||
); | ||
} | ||
|
||
searchType === "item" | ||
? (url = `${getPortalUrl(options)}/search`) | ||
: (url = `${getPortalUrl(options)}/community/groups`); | ||
|
||
// send the request | ||
return request(url, options).then(r => { | ||
if (r.nextStart && r.nextStart !== -1) { | ||
r.nextPage = function() { | ||
let newOptions: ISearchRequestOptions; | ||
|
||
if ( | ||
typeof search === "string" || | ||
search instanceof SearchQueryBuilder | ||
) { | ||
newOptions = { | ||
q: search, | ||
start: r.nextStart | ||
}; | ||
} else { | ||
newOptions = search; | ||
newOptions.start = r.nextStart; | ||
} | ||
|
||
return genericSearch(newOptions, searchType); | ||
}; | ||
} | ||
|
||
return r; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { | ||
IRequestOptions, | ||
IPagingParams, | ||
IItem, | ||
IGroup | ||
} from "@esri/arcgis-rest-request"; | ||
|
||
import { SearchQueryBuilder } from "./SearchQueryBuilder"; | ||
|
||
export interface ISearchRequestOptions extends IRequestOptions, IPagingParams { | ||
q: string | SearchQueryBuilder; | ||
sortField?: string; | ||
sortDir?: string; | ||
[key: string]: any; | ||
} | ||
|
||
/** | ||
* Results from an item or group search. | ||
*/ | ||
export interface ISearchResult { | ||
query: string; // matches the api's form param | ||
total: number; | ||
start: number; | ||
num: number; | ||
nextStart: number; | ||
results: IItem[] | IGroup[]; | ||
nextPage?: () => Promise<ISearchResult>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.