Skip to content

Commit

Permalink
feat(arcgis-rest-portal): add searchUsers function
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-portal
  • Loading branch information
mjuniper committed Oct 21, 2019
1 parent fa637ea commit 235766c
Show file tree
Hide file tree
Showing 7 changed files with 571 additions and 111 deletions.
187 changes: 84 additions & 103 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/arcgis-rest-portal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from "./users/get-user-tags";
export * from "./users/get-user-url";
export * from "./users/invitation";
export * from "./users/notification";
export * from "./users/search-users";
export * from "./users/update";

export * from "./sharing/access";
Expand Down
37 changes: 37 additions & 0 deletions packages/arcgis-rest-portal/src/users/search-users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* Copyright (c) 2018-2019 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import { IAuthenticatedRequestOptions } from "@esri/arcgis-rest-auth";
import { IAuthenticationManager } from "@esri/arcgis-rest-request";
import { IPagingParams, IUser } from "@esri/arcgis-rest-types";
import { SearchQueryBuilder } from "../util/SearchQueryBuilder";
import { ISearchOptions, ISearchResult } from "../util/search";
import { genericSearch } from "../util/generic-search";

// export interface IUserSearchOptions extends IAuthenticatedRequestOptions, IPagingParams {
// q: string | SearchQueryBuilder;
// sortField?: string;
// sortOrder?: string;
// [key: string]: any;
// }

export interface IUserSearchOptions extends ISearchOptions {
authentication: IAuthenticationManager;
}

/**
* ```js
* import { searchItems } from "@esri/arcgis-rest-portal";
* //
* searchUsers({ q: 'tommy', authentication })
* .then(response) // response.total => 355
* ```
* Search a portal for users.
*
* @param search - A RequestOptions object to pass through to the endpoint.
* @returns A Promise that will resolve with the data from the response.
*/
export function searchUsers(
search: IUserSearchOptions | SearchQueryBuilder
): Promise<ISearchResult<IUser>> {
return genericSearch<IUser>(search, "user");
}
23 changes: 17 additions & 6 deletions packages/arcgis-rest-portal/src/util/generic-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
IRequestOptions,
appendCustomParams
} from "@esri/arcgis-rest-request";
import { IItem, IGroup } from "@esri/arcgis-rest-types";
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>(
export function genericSearch<T extends IItem | IGroup | IUser>(
search: string | ISearchOptions | SearchQueryBuilder,
searchType: "item" | "group"
searchType: "item" | "group" | "user"
): Promise<ISearchResult<T>> {
let url: string;
let options: IRequestOptions;
Expand All @@ -35,9 +35,20 @@ export function genericSearch<T extends IItem | IGroup>(
);
}

url =
getPortalUrl(options) +
(searchType === "item" ? "/search" : "/community/groups");
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 => {
Expand Down
4 changes: 2 additions & 2 deletions packages/arcgis-rest-portal/src/util/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Apache-2.0 */

import { IRequestOptions } from "@esri/arcgis-rest-request";
import { IPagingParams, IItem, IGroup } from "@esri/arcgis-rest-types";
import { IPagingParams, IItem, IGroup, IUser } from "@esri/arcgis-rest-types";
import { SearchQueryBuilder } from "./SearchQueryBuilder";

export interface ISearchOptions extends IRequestOptions, IPagingParams {
Expand All @@ -15,7 +15,7 @@ export interface ISearchOptions extends IRequestOptions, IPagingParams {
/**
* Results from an item or group search.
*/
export interface ISearchResult<T extends IItem | IGroup> {
export interface ISearchResult<T extends IItem | IGroup | IUser> {
query: string; // matches the api's form param
total: number;
start: number;
Expand Down
Loading

0 comments on commit 235766c

Please sign in to comment.