From 4e676374f90e583639eb2c2f3dc9a554f8aab17a Mon Sep 17 00:00:00 2001 From: john gravois Date: Fri, 18 Jan 2019 14:28:35 -0800 Subject: [PATCH] feat(:two_men_holding_hands:): add getRelatedItems function to items package AFFECTS PACKAGES: @esri/arcgis-rest-items ISSUES CLOSED: #282 --- packages/arcgis-rest-items/src/get.ts | 65 ++++++++++++++- packages/arcgis-rest-items/src/helpers.ts | 35 ++++++++ packages/arcgis-rest-items/test/get.test.ts | 82 +++++++++++++++++-- packages/arcgis-rest-items/test/mocks/item.ts | 7 ++ 4 files changed, 181 insertions(+), 8 deletions(-) diff --git a/packages/arcgis-rest-items/src/get.ts b/packages/arcgis-rest-items/src/get.ts index bc29db6275..098bf52693 100644 --- a/packages/arcgis-rest-items/src/get.ts +++ b/packages/arcgis-rest-items/src/get.ts @@ -9,9 +9,21 @@ import { import { IItem, IGroup } from "@esri/arcgis-rest-common-types"; -import { IItemIdRequestOptions, IItemDataRequestOptions } from "./helpers"; +import { + IItemIdRequestOptions, + IItemDataRequestOptions, + IItemRelationshipRequestOptions + } from "./helpers"; /** + * ```import { getItem } from "@esri/arcgis-rest-items"; + * // + * getItem("ae7") + * .then(response); + * // or + * getItem("ae7", { authentication }) + * .then(response) + * ``` * Get an item by id. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/item.htm) for more information. * * @param id - Item Id @@ -33,6 +45,14 @@ export function getItem( } /** + * ```import { getItemData } from "@esri/arcgis-rest-items"; + * // + * getItemData("ae7") + * .then(response) + * // or + * getItemData("ae7", { authentication }) + * .then(response) + * ``` * Get the /data for an item. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/item-data.htm) for more information. * @param id - Item Id * @param requestOptions - Options for the request @@ -56,6 +76,49 @@ export function getItemData( return request(url, options); } +export interface IGetRelatedItemsResponse { + total: number; + relatedItems: IItem[]; +} +/** + * ```import { getRelatedItems } from "@esri/arcgis-rest-items"; + * // + * getRelatedItems({ + * id: "ae7", + * relationshipType: "Service2Layer" // or ["Service2Layer", "Map2Area"] + * }) + * .then(response) + * ``` + * Get the related items. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/related-items.htm) for more information. + * + * @param requestOptions - Options for the request + * @returns A Promise to get some item resources. + */ +export function getRelatedItems( + requestOptions: IItemRelationshipRequestOptions +): Promise { + const url = `${getPortalUrl(requestOptions)}/content/items/${requestOptions.id}/relatedItems`; + + const options:IItemRelationshipRequestOptions = { + httpMethod: "GET", + params: { + direction: requestOptions.direction + }, + ...requestOptions + } + + if (typeof requestOptions.relationshipType === "string") { + options.params.relationshipType = requestOptions.relationshipType; + } else { + options.params.relationshipTypes = requestOptions.relationshipType; + } + + delete options.direction; + delete options.relationshipType; + + return request(url, options); +} + /** * Get the resources associated with an item * diff --git a/packages/arcgis-rest-items/src/helpers.ts b/packages/arcgis-rest-items/src/helpers.ts index faf2efed39..a358035578 100644 --- a/packages/arcgis-rest-items/src/helpers.ts +++ b/packages/arcgis-rest-items/src/helpers.ts @@ -33,6 +33,41 @@ export interface IFolderIdRequestOptions extends IUserRequestOptions { owner?: string; } +export type ItemRelationshipType = "Map2Service" | +"WMA2Code" | +"Map2FeatureCollection" | +"MobileApp2Code" | +"Service2Data" | +"Service2Service" | +"Map2AppConfig" | +"Item2Attachment" | +"Item2Report" | +"Listed2Provisioned" | +"Style2Style" | +"Service2Style" | +"Survey2Service" | +"Survey2Data" | +"Service2Route" | +"Area2Package" | +"Map2Area" | +"Service2Layer" | +"Area2CustomPackage"; + +export interface IItemRelationshipRequestOptions extends IRequestOptions { + /** + * Id of the item. + */ + id: string; + /** + * The type of relationship between the two items. + */ + relationshipType: ItemRelationshipType | ItemRelationshipType[] + /** + * The direction of the relationship. Either forward (from origin -> destination) or reverse (from destination -> origin). + */ + direction?: "forward" | "reverse"; +} + export interface IItemResourceRequestOptions extends IItemIdRequestOptions { /** * New resource filename. diff --git a/packages/arcgis-rest-items/test/get.test.ts b/packages/arcgis-rest-items/test/get.test.ts index e95dee4c68..69701970a8 100644 --- a/packages/arcgis-rest-items/test/get.test.ts +++ b/packages/arcgis-rest-items/test/get.test.ts @@ -3,14 +3,20 @@ import * as fetchMock from "fetch-mock"; -import { +import { getItem, getItemData, getItemResources, - getItemGroups + getItemGroups, + getRelatedItems } from "../src/get"; -import { ItemResponse, ItemDataResponse, ItemGroupResponse } from "./mocks/item"; +import { + ItemResponse, + ItemDataResponse, + ItemGroupResponse, + RelatedItemsResponse +} from "./mocks/item"; import { GetItemResourcesResponse } from "./mocks/resources"; @@ -42,7 +48,7 @@ describe("get", () => { fetchMock.once("*", ItemDataResponse); getItemData("3ef") - .then(response => { + .then(() => { expect(fetchMock.called()).toEqual(true); const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); expect(url).toEqual( @@ -84,6 +90,48 @@ describe("get", () => { } }); + it("should return related items", done => { + fetchMock.once("*", RelatedItemsResponse); + + getRelatedItems({ + id: "3ef", + relationshipType: "Service2Layer" + }) + .then(() => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://www.arcgis.com/sharing/rest/content/items/3ef/relatedItems?f=json&relationshipType=Service2Layer" + ); + expect(options.method).toBe("GET"); + done(); + }) + .catch(e => { + fail(e); + }); + }); + + it("should return related items for more than one relationship type", done => { + fetchMock.once("*", RelatedItemsResponse); + + getRelatedItems({ + id: "3ef", + relationshipType: ["Service2Layer", "Area2CustomPackage"] + }) + .then(() => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://www.arcgis.com/sharing/rest/content/items/3ef/relatedItems?f=json&relationshipTypes=Service2Layer%2CArea2CustomPackage" + ); + expect(options.method).toBe("GET"); + done(); + }) + .catch(e => { + fail(e); + }); + }); + describe("Authenticated methods", () => { // setup a UserSession to use in all these tests const MOCK_USER_SESSION = new UserSession({ @@ -125,7 +173,7 @@ describe("get", () => { fetchMock.once("*", ItemDataResponse); getItemData("3ef", MOCK_USER_REQOPTS) - .then(response => { + .then(() => { expect(fetchMock.called()).toEqual(true); const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); expect(url).toEqual( @@ -139,13 +187,33 @@ describe("get", () => { }); }); + it("get related items", done => { + fetchMock.once("*", RelatedItemsResponse); + getRelatedItems({ + id: "3ef", + relationshipType: "Service2Layer", + ...MOCK_USER_REQOPTS + }) + .then(() => { + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://myorg.maps.arcgis.com/sharing/rest/content/items/3ef/relatedItems?f=json&relationshipType=Service2Layer&token=fake-token" + ); + expect(options.method).toBe("GET"); + done(); + }) + .catch(e => { + fail(e); + }); + }); + it("get item resources", done => { fetchMock.once("*", GetItemResourcesResponse); getItemResources({ id: "3ef", ...MOCK_USER_REQOPTS }) - .then(response => { + .then(() => { const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); expect(url).toEqual( "https://myorg.maps.arcgis.com/sharing/rest/content/items/3ef/resources" @@ -168,7 +236,7 @@ describe("get", () => { resourcesPrefix: "foolder" } }) - .then(response => { + .then(() => { const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); expect(url).toEqual( "https://myorg.maps.arcgis.com/sharing/rest/content/items/3ef/resources" diff --git a/packages/arcgis-rest-items/test/mocks/item.ts b/packages/arcgis-rest-items/test/mocks/item.ts index 4ace5295ce..bb02fb2af2 100644 --- a/packages/arcgis-rest-items/test/mocks/item.ts +++ b/packages/arcgis-rest-items/test/mocks/item.ts @@ -4,6 +4,8 @@ import { IItem } from "@esri/arcgis-rest-common-types"; import { IItemGroupResponse } from "../../src/get"; +import { IGetRelatedItemsResponse } from "../../src/get" + export const ItemSuccessResponse: any = { success: true, id: "3efakeitemid0000" @@ -28,6 +30,11 @@ export const ItemResponse: IItem = { protected: false }; +export const RelatedItemsResponse: IGetRelatedItemsResponse = { + total: 1, + relatedItems: [ItemResponse] +} + export const ItemDataResponse: any = { source: "3ef", values: {