Skip to content

Commit

Permalink
feat(group): refactor searchGroups to make use of SearchQueryBuilder
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-portal

BREAKING CHANGE:
searchGroups signature change

ISSUES CLOSED: #104
  • Loading branch information
jgravois committed Apr 17, 2019
1 parent a871246 commit 72f2898
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 142 deletions.
65 changes: 12 additions & 53 deletions packages/arcgis-rest-portal/src/groups/search.ts
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");
}
5 changes: 4 additions & 1 deletion packages/arcgis-rest-portal/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export * from "./items/get";
export * from "./items/protect";
export * from "./items/remove";
export * from "./items/search";
export * from "./items/SearchQueryBuilder";
export * from "./items/update";
export * from "./items/helpers";

Expand All @@ -33,3 +32,7 @@ export * from "./sharing/helpers";

export * from "./util/get-portal";
export * from "./util/get-portal-url";
export * from "./util/search";
export * from "./util/SearchQueryBuilder";
// we dont export 'generic-search' because its an internal utility method
// export * from "./util/generic-search"; because its an internal utility method
82 changes: 5 additions & 77 deletions packages/arcgis-rest-portal/src/items/search.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,9 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
/* Copyright (c) 2018-2019 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import {
request,
IRequestOptions,
appendCustomParams,
IPagingParams,
IItem
} from "@esri/arcgis-rest-request/src";

import { getPortalUrl } from "../util/get-portal-url";
import { SearchQueryBuilder } from "./SearchQueryBuilder";

export interface ISearchRequestOptions extends IRequestOptions, IPagingParams {
q: string | SearchQueryBuilder;
sortField?: string;
sortDir?: string;
[key: string]: any;
}

/**
* Options to pass through when searching for items.
*/
export interface ISearchResult {
query: string; // matches the api's form param
total: number;
start: number;
num: number;
nextStart: number;
results: IItem[];
nextPage?: () => Promise<ISearchResult>;
}
import { SearchQueryBuilder } from "../util/SearchQueryBuilder";
import { ISearchRequestOptions, ISearchResult } from "../util/search";
import { genericSearch } from "../util/generic-search";

/**
* ```js
Expand All @@ -47,50 +20,5 @@ export interface ISearchResult {
export function searchItems(
search: string | ISearchRequestOptions | SearchQueryBuilder
): Promise<ISearchResult> {
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"],
{
httpMethod: "GET"
}
);
}

// construct the search url
const url = `${getPortalUrl(options)}/search`;

// 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 searchItems(newOptions);
};
}

return r;
});
return genericSearch(search, "item");
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IParamBuilder, warn } from "@esri/arcgis-rest-request/src";
import { IParamBuilder, warn } from "@esri/arcgis-rest-request";

export class SearchQueryBuilder implements IParamBuilder {
private termStack: any[] = [];
Expand Down
66 changes: 66 additions & 0 deletions packages/arcgis-rest-portal/src/util/generic-search.ts
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;
});
}
31 changes: 31 additions & 0 deletions packages/arcgis-rest-portal/src/util/search.ts
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>;
}
35 changes: 30 additions & 5 deletions packages/arcgis-rest-portal/test/groups/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Apache-2.0 */

import { searchGroups } from "../../src/groups/search";

import { GroupSearchResponse } from "../mocks/groups/responses";
import { SearchQueryBuilder } from "../../src/util/SearchQueryBuilder";

import * as fetchMock from "fetch-mock";

Expand All @@ -14,8 +14,8 @@ describe("groups", () => {
it("should make a simple, unauthenticated group search request", done => {
fetchMock.once("*", GroupSearchResponse);

searchGroups({ q: "water" })
.then(response => {
searchGroups("water")
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
Expand All @@ -42,7 +42,7 @@ describe("groups", () => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://www.arcgis.com/sharing/rest/community/groups?f=json&q=water&start=4&num=7&sortField=owner&sortOrder=desc"
"https://www.arcgis.com/sharing/rest/community/groups?f=json&q=water&num=7&start=4&sortField=owner&sortOrder=desc"
);
expect(options.method).toBe("GET");
done();
Expand All @@ -53,6 +53,31 @@ describe("groups", () => {
});
});

it("should make a simple, single search request with a builder", done => {
fetchMock.once("*", GroupSearchResponse);
const expectedParam = "Trees AND owner: USFS";
const q = new SearchQueryBuilder()
.match("Trees")
.and()
.match("USFS")
.in("owner");
searchGroups(q)
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
`https://www.arcgis.com/sharing/rest/community/groups?f=json&q=${encodeURIComponent(
expectedParam
)}`
);
expect(options.method).toBe("GET");
done();
})
.catch(e => {
fail(e);
});
});

describe("authenticted methods", () => {
const MOCK_AUTH = {
getToken() {
Expand All @@ -67,7 +92,7 @@ describe("groups", () => {
it("should make a simple, authenticated group search request", done => {
fetchMock.once("*", GroupSearchResponse);

searchGroups({ q: "water" }, MOCK_REQOPTS)
searchGroups({ q: "water", authentication: MOCK_AUTH })
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-portal/test/items/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { searchItems } from "../../src/items/search";
import { SearchResponse, BigSearchResponse } from "../mocks/items/search";
import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";
import { SearchQueryBuilder } from "../../src/items/SearchQueryBuilder";
import { SearchQueryBuilder } from "../../src/util/SearchQueryBuilder";

describe("search", () => {
afterEach(fetchMock.restore);
Expand Down
4 changes: 2 additions & 2 deletions packages/arcgis-rest-portal/test/mocks/groups/responses.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IGroupSearchResult } from "../../../src/groups/search";
import { ISearchResult } from "../../../src/util/search";
import {
IGroupContentResult,
IGroupUsersResult
} from "../../../src/groups/get";

import { IGroup } from "@esri/arcgis-rest-request";

export const GroupSearchResponse: IGroupSearchResult = {
export const GroupSearchResponse: ISearchResult = {
query: "* AND owner:dcadmin",
total: 358,
start: 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/arcgis-rest-portal/test/mocks/items/search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { ISearchResult } from "../../../src/items/search";
import { ISearchResult } from "../../../src/util/search";

export const SearchResponse: ISearchResult = {
query: "",
Expand Down
Loading

0 comments on commit 72f2898

Please sign in to comment.