From fa604dec4424b7c1df8d9ce82a93daaa79103ecf Mon Sep 17 00:00:00 2001 From: john gravois Date: Tue, 26 Mar 2019 12:08:20 -0700 Subject: [PATCH] feat(join): new joinGroup and leaveGroup methods AFFECTS PACKAGES: @esri/arcgis-rest-groups --- packages/arcgis-rest-groups/src/index.ts | 1 + packages/arcgis-rest-groups/src/join.ts | 60 +++++++++++++++++ packages/arcgis-rest-groups/src/protect.ts | 15 ++++- packages/arcgis-rest-groups/test/join.test.ts | 64 +++++++++++++++++++ 4 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 packages/arcgis-rest-groups/src/join.ts create mode 100644 packages/arcgis-rest-groups/test/join.test.ts diff --git a/packages/arcgis-rest-groups/src/index.ts b/packages/arcgis-rest-groups/src/index.ts index 955c883657..1b33f819e9 100644 --- a/packages/arcgis-rest-groups/src/index.ts +++ b/packages/arcgis-rest-groups/src/index.ts @@ -9,3 +9,4 @@ export * from "./protect"; export * from "./remove"; export * from "./search"; export * from "./update"; +export * from "./join"; diff --git a/packages/arcgis-rest-groups/src/join.ts b/packages/arcgis-rest-groups/src/join.ts new file mode 100644 index 0000000000..a54b2462be --- /dev/null +++ b/packages/arcgis-rest-groups/src/join.ts @@ -0,0 +1,60 @@ +/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. + * Apache-2.0 */ + +import { request, getPortalUrl } from "@esri/arcgis-rest-request"; + +import { IGroupIdRequestOptions } from "./helpers"; + +/** + * ```js + * import { joinGroup } from '@esri/arcgis-rest-groups'; + * // + * joinGroup({ + * id: groupId, + * authentication + * }) + * .then(response) + * ``` + * Join a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/join-group.htm) for more information. + * + * @param requestOptions - Options for the request + * @returns A Promise that will resolve with the success/failure status of the request + */ +export function joinGroup( + requestOptions: IGroupIdRequestOptions +): Promise<{ success: boolean; groupId: string }> { + const url = `${getPortalUrl(requestOptions)}/community/groups/${ + requestOptions.id + }/join`; + const options: IGroupIdRequestOptions = { + ...requestOptions + }; + return request(url, options); +} + +/** + * ```js + * import { joinGroup } from '@esri/arcgis-rest-groups'; + * // + * joinGroup({ + * id: groupId, + * authentication + * }) + * .then(response) + * ``` + * Leave a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/leave-group.htm) for more information. + * + * @param requestOptions - Options for the request + * @returns A Promise that will resolve with the success/failure status of the request + */ +export function leaveGroup( + requestOptions: IGroupIdRequestOptions +): Promise<{ success: boolean; groupId: string }> { + const url = `${getPortalUrl(requestOptions)}/community/groups/${ + requestOptions.id + }/leave`; + const options: IGroupIdRequestOptions = { + ...requestOptions + }; + return request(url, options); +} diff --git a/packages/arcgis-rest-groups/src/protect.ts b/packages/arcgis-rest-groups/src/protect.ts index ade6ee7b90..6d1d5d75a1 100644 --- a/packages/arcgis-rest-groups/src/protect.ts +++ b/packages/arcgis-rest-groups/src/protect.ts @@ -22,7 +22,7 @@ import { IGroupIdRequestOptions } from "./helpers"; */ export function protectGroup( requestOptions: IGroupIdRequestOptions -): Promise { +): Promise<{ success: boolean }> { const url = `${getPortalUrl(requestOptions)}/community/groups/${ requestOptions.id }/protect`; @@ -33,13 +33,22 @@ export function protectGroup( } /** - * Unprotect a Group. + * ```js + * import { unprotectGroup } from '@esri/arcgis-rest-groups'; + * // + * unprotectGroup({ + * id: groupId, + * authentication + * }) + * .then(response) + * ``` + * Unprotect a Group. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/unprotect-group.htm) for more information. * @param requestOptions - Options for the request * @returns A Promise that will resolve with the success/failure status of the request */ export function unprotectGroup( requestOptions: IGroupIdRequestOptions -): Promise { +): Promise<{ success: boolean }> { const url = `${getPortalUrl(requestOptions)}/community/groups/${ requestOptions.id }/unprotect`; diff --git a/packages/arcgis-rest-groups/test/join.test.ts b/packages/arcgis-rest-groups/test/join.test.ts new file mode 100644 index 0000000000..b468e0dcbb --- /dev/null +++ b/packages/arcgis-rest-groups/test/join.test.ts @@ -0,0 +1,64 @@ +/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. + * Apache-2.0 */ + +import { joinGroup, leaveGroup } from "../src/join"; + +import { GroupEditResponse } from "./mocks/responses"; + +import { encodeParam } from "@esri/arcgis-rest-request"; +import * as fetchMock from "fetch-mock"; + +describe("groups", () => { + afterEach(fetchMock.restore); + + describe("authenticted methods", () => { + const MOCK_AUTH = { + getToken() { + return Promise.resolve("fake-token"); + }, + portal: "https://myorg.maps.arcgis.com/sharing/rest" + }; + const MOCK_REQOPTS = { + authentication: MOCK_AUTH + }; + + it("should help a user join a group", done => { + fetchMock.once("*", GroupEditResponse); + + joinGroup({ id: "5bc", ...MOCK_REQOPTS }) + .then(() => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://myorg.maps.arcgis.com/sharing/rest/community/groups/5bc/join" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain(encodeParam("f", "json")); + expect(options.body).toContain(encodeParam("token", "fake-token")); + done(); + }) + .catch(e => { + fail(e); + }); + }); + it("should help a user leave a group", done => { + fetchMock.once("*", GroupEditResponse); + + leaveGroup({ id: "5bc", ...MOCK_REQOPTS }) + .then(() => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://myorg.maps.arcgis.com/sharing/rest/community/groups/5bc/leave" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain(encodeParam("f", "json")); + expect(options.body).toContain(encodeParam("token", "fake-token")); + done(); + }) + .catch(e => { + fail(e); + }); + }); + }); +});