Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add getItemGroups() function to items package #445

Merged
merged 2 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/arcgis-rest-items/src/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getPortalUrl
} from "@esri/arcgis-rest-request";

import { IItem } from "@esri/arcgis-rest-common-types";
import { IItem, IGroup } from "@esri/arcgis-rest-common-types";

import { IItemIdRequestOptions, IItemDataRequestOptions } from "./helpers";

Expand Down Expand Up @@ -74,6 +74,34 @@ export function getItemResources(
...requestOptions.params,
num: 1000
};
// at v2, the argument signature of this method should match getItemData() and getItemGroups() if requests can be made anonymously
return request(url, requestOptions);
}

export interface IItemGroupResponse {
admin?: IGroup[];
member?: IGroup[];
other?: IGroup[];
}

/**
* ```js
* import { getItemGroups } from "@esri/arcgis-rest-items";
* //
* getItemGroups("30e5fe3149c34df1ba922e6f5bbf808f")
* .then(response)
* ```
* Lists the groups of which the item is a part, only showing the groups that the calling user can access. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/groups.htm) for more information.
*
* @param id - The Id of the item to query group association for.
* @param requestOptions - Options for the request
* @returns A Promise to get some item groups.
*/
export function getItemGroups(
id: string,
requestOptions?: IRequestOptions
): Promise<IItemGroupResponse> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}/groups`;

return request(url, requestOptions);
}
43 changes: 41 additions & 2 deletions packages/arcgis-rest-items/test/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

import * as fetchMock from "fetch-mock";

import { getItem, getItemData, getItemResources } from "../src/get";
import {
getItem,
getItemData,
getItemResources,
getItemGroups
} from "../src/get";

import { ItemResponse, ItemDataResponse } from "./mocks/item";
import { ItemResponse, ItemDataResponse, ItemGroupResponse } from "./mocks/item";

import { GetItemResourcesResponse } from "./mocks/resources";

Expand Down Expand Up @@ -177,5 +182,39 @@ describe("get", () => {
fail(e);
});
});

it("get item groups anonymously", done => {
fetchMock.once("*", ItemGroupResponse);
getItemGroups("3ef")
.then(() => {
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://www.arcgis.com/sharing/rest/content/items/3ef/groups"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
done();
})
.catch(e => {
fail(e);
});
});

it("get item groups with credentials", done => {
fetchMock.once("*", ItemGroupResponse);
getItemGroups("3ef", { authentication: MOCK_USER_SESSION })
.then(() => {
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/content/items/3ef/groups"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
done();
})
.catch(e => {
fail(e);
});
});
}); // auth requests
});
83 changes: 83 additions & 0 deletions packages/arcgis-rest-items/test/mocks/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Apache-2.0 */

import { IItem } from "@esri/arcgis-rest-common-types";
import { IItemGroupResponse } from "../../src/get";

export const ItemSuccessResponse: any = {
success: true,
Expand Down Expand Up @@ -33,3 +34,85 @@ export const ItemDataResponse: any = {
key: "value"
}
};

export const ItemGroupResponse: IItemGroupResponse = {
"admin": [{
"id": "2ecb37a8c8fb4051af9c086c25503bb0",
"title": "Street Maps",
"isInvitationOnly": false,
"owner": "jsmith",
"description": "The street maps group provides street maps for the city of Redlands.",
"snippet": null,
"tags": [
"Redlands",
"Street",
"Maps"
],
"phone": "909.555.1234",
"thumbnail": "streets.jpg",
"created": 1247082196000,
"modified": 1276793808000,
"access": "public",
"userMembership": {
"username": "jsmith",
"memberType": "owner",
"applications": 1
},
"isFav": false,
"autoJoin": false,
"isViewOnly": false,
"protected": false
}],
"member": [{
"id": "bf51aa6e879e4676b683dcbefb0ab0a9",
"title": "Parks and Recreation",
"isInvitationOnly": true,
"owner": "swilson",
"description": "The Parks and Recreation group contains maps and applications used by the Parks Department.",
"snippet": null,
"tags": [
"Redlands",
"Parks",
"Recreation"
],
"phone": "909.555.1234",
"thumbnail": "parks.jpg",
"created": 1247082197000,
"modified": 1276793919000,
"access": "private",
"userMembership": {
"username": "jsmith",
"memberType": "member",
},
"isFav": false,
"autoJoin": false,
"isViewOnly": false,
"protected": false
}],
"other":[{
"id": "dbc385ac1b7d4231b24b97750f0e633c",
"title": "Featured Maps and Apps",
"isInvitationOnly": true,
"owner": "city_redlands",
"description": "These items are featured on the gallery page.",
"snippet": null,
"tags": [
"gallery",
"Featured Maps",
"Featured Apps"
],
"phone": "909.555.1234",
"thumbnail": "gallery.jpg",
"created": 1327099662000,
"modified": 1327099662000,
"access": "public",
"userMembership": {
"username": "jsmith",
"memberType": "nonmember"
},
"isFav": false,
"autoJoin": false,
"isViewOnly": false,
"protected": false
}]
}