Skip to content

Commit

Permalink
Merge pull request #675 from Esri/f/searchGroupContent
Browse files Browse the repository at this point in the history
Added support for group contents search
  • Loading branch information
tomwayson authored Apr 6, 2020
2 parents ea218f0 + af93ad4 commit ef4e404
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
26 changes: 24 additions & 2 deletions packages/arcgis-rest-portal/src/groups/search.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/* Copyright (c) 2018-2019 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IGroup } from "@esri/arcgis-rest-types";
import { IItem, IGroup } from "@esri/arcgis-rest-types";
import { SearchQueryBuilder } from "../util/SearchQueryBuilder";
import { ISearchOptions, ISearchResult } from "../util/search";
import {
ISearchOptions,
ISearchGroupContentOptions,
ISearchResult
} from "../util/search";
import { genericSearch } from "../util/generic-search";

/**
Expand All @@ -23,3 +27,21 @@ export function searchGroups(
): Promise<ISearchResult<IGroup>> {
return genericSearch<IGroup>(search, "group");
}

/**
* ```js
* import { searchGroupContent } from "@esri/arcgis-rest-portal";
* //
* searchGroupContent('water')
* .then(response) // response.total => 355
* ```
* Search a portal for items in a group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/group-content-search.htm) for more information.
*
* @param options - RequestOptions object amended with search parameters.
* @returns A Promise that will resolve with the data from the response.
*/
export function searchGroupContent(
options: ISearchGroupContentOptions
): Promise<ISearchResult<IItem>> {
return genericSearch<IItem>(options, "groupContent");
}
29 changes: 25 additions & 4 deletions packages/arcgis-rest-portal/src/util/generic-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ 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";
import {
ISearchOptions,
ISearchGroupContentOptions,
ISearchResult
} from "../util/search";

export function genericSearch<T extends IItem | IGroup | IUser>(
search: string | ISearchOptions | SearchQueryBuilder,
searchType: "item" | "group" | "user"
search:
| string
| ISearchOptions
| ISearchGroupContentOptions
| SearchQueryBuilder,
searchType: "item" | "group" | "groupContent" | "user"
): Promise<ISearchResult<T>> {
let url: string;
let options: IRequestOptions;
Expand All @@ -35,14 +43,27 @@ export function genericSearch<T extends IItem | IGroup | IUser>(
);
}

let path = searchType === "item" ? "/search" : "/community/groups";
let path;
switch (searchType) {
case "item":
path = "/search";
break;
case "group":
path = "/community/groups";
break;
case "groupContent":
// Need to have groupId property to do group contents search,
// cso filter out all but ISearchGroupContentOptions
if (
typeof search !== "string" &&
!(search instanceof SearchQueryBuilder) &&
search.groupId
) {
path = `/content/groups/${search.groupId}/search`;
} else {
return Promise.reject(new Error("you must pass a `groupId` option to `searchGroupContent`"));
}
break;
default:
// "users"
path = "/portals/self/users/search";
Expand Down
4 changes: 4 additions & 0 deletions packages/arcgis-rest-portal/src/util/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export interface ISearchOptions extends IRequestOptions, IPagingParams {
[key: string]: any;
}

export interface ISearchGroupContentOptions extends ISearchOptions {
groupId: string;
}

/**
* Results from an item or group search.
*/
Expand Down
43 changes: 42 additions & 1 deletion packages/arcgis-rest-portal/test/groups/search.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { searchGroups } from "../../src/groups/search";
import { searchGroups, searchGroupContent } from "../../src/groups/search";
import { GroupSearchResponse } from "../mocks/groups/responses";
import { SearchQueryBuilder } from "../../src/util/SearchQueryBuilder";
import { genericSearch } from "../../src/util/generic-search";

import * as fetchMock from "fetch-mock";

Expand Down Expand Up @@ -51,6 +52,46 @@ describe("groups", () => {
fail(e);
});
});

it("should search for group contents", done => {
fetchMock.once("*", GroupSearchResponse);

searchGroupContent({
groupId: "grp1234567890",
q: "water"
})
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://www.arcgis.com/sharing/rest/content/groups/grp1234567890/search?f=json&q=water"
);
expect(options.method).toBe("GET");
done();
})
.catch(e => {
fail(e);
});
});

it("should catch search for group contents without group id", done => {
genericSearch(
{
q: "water"
},
"groupContent"
).then(
() => fail(),
err => {
expect(err).toEqual(
new Error(
"you must pass a `groupId` option to `searchGroupContent`"
)
);
done();
}
);
});
});

it("should make a simple, single search request with a builder", done => {
Expand Down

0 comments on commit ef4e404

Please sign in to comment.