Skip to content

Commit

Permalink
feat(:two_men_holding_hands:): add getRelatedItems function to items …
Browse files Browse the repository at this point in the history
…package

AFFECTS PACKAGES:
@esri/arcgis-rest-items

ISSUES CLOSED: #282
  • Loading branch information
jgravois committed Feb 12, 2019
1 parent c612a40 commit 4e67637
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 8 deletions.
65 changes: 64 additions & 1 deletion packages/arcgis-rest-items/src/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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<IGetRelatedItemsResponse> {
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
*
Expand Down
35 changes: 35 additions & 0 deletions packages/arcgis-rest-items/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
82 changes: 75 additions & 7 deletions packages/arcgis-rest-items/test/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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(
Expand All @@ -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"
Expand All @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions packages/arcgis-rest-items/test/mocks/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: {
Expand Down

0 comments on commit 4e67637

Please sign in to comment.