Skip to content

Commit

Permalink
feat(getItemBaseUrl): add function to get the base REST API URL for a…
Browse files Browse the repository at this point in the history
…n item

AFFECTS PACKAGES:
@esri/arcgis-rest-portal
  • Loading branch information
tomwayson committed Sep 10, 2020
1 parent 2092bb3 commit d6ec9fc
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 30 deletions.
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 40 additions & 7 deletions packages/arcgis-rest-portal/src/items/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function getItem(
id: string,
requestOptions?: IRequestOptions
): Promise<IItem> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}`;
const url = getItemBaseUrl(id, requestOptions);

// default to a GET request
const options: IRequestOptions = {
Expand All @@ -46,6 +46,38 @@ export function getItem(
return request(url, options);
}

// /**
// * Get the fully qualified URL to the REST end point for an item.
// * @param item id (string) or an item object w/ `id` and `access`
// * @param portalUrlOrRequestOptions a portal base or API URL, a portal object, or request options containing either of those
// * @returns URL to the item's REST end point, defaults to `https://www.arcgis.com/sharing/rest/content/items/{itemId}?f=json`
// */
// export const getItemApiLink = (
// itemOrId: IItem | string,
// portalUrlOrRequestOptions?: string | IRequestOptions
// ) => {
// const baseUrl = getItemApiUrl(itemOrId, portalUrlOrRequestOptions)
// // TODO: append token if supplied and access is not public?
// return `${baseUrl}?f=json`
// }

/**
* Get the fully qualified base URL to the REST end point for an item.
* @param item id (string) or an item object w/ `id` and `access`
* @param portalUrlOrRequestOptions a portal URL or request options
* @returns URL to the item's REST end point, defaults to `https://www.arcgis.com/sharing/rest/content/items/{itemId}`
*/
export const getItemBaseUrl = (
itemId: IItem | string,
portalUrlOrRequestOptions?: string | IRequestOptions
) => {
const portalUrl =
typeof portalUrlOrRequestOptions === "string"
? portalUrlOrRequestOptions
: getPortalUrl(portalUrlOrRequestOptions);
return `${portalUrl}/content/items/${itemId}`;
};

/**
* ```
* import { getItemData } from "@esri/arcgis-rest-portal";
Expand All @@ -65,7 +97,7 @@ export function getItemData(
id: string,
requestOptions?: IItemDataOptions
): Promise<any> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}/data`;
const url = `${getItemBaseUrl(id, requestOptions)}/data`;
// default to a GET request
const options: IItemDataOptions = {
...{ httpMethod: "GET", params: {} },
Expand Down Expand Up @@ -112,9 +144,10 @@ export interface IGetRelatedItemsResponse {
export function getRelatedItems(
requestOptions: IItemRelationshipOptions
): Promise<IGetRelatedItemsResponse> {
const url = `${getPortalUrl(requestOptions)}/content/items/${
requestOptions.id
}/relatedItems`;
const url = `${getItemBaseUrl(
requestOptions.id,
requestOptions
)}/relatedItems`;

const options: IItemRelationshipOptions = {
httpMethod: "GET",
Expand Down Expand Up @@ -146,7 +179,7 @@ export function getItemResources(
id: string,
requestOptions?: IRequestOptions
): Promise<any> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}/resources`;
const url = `${getItemBaseUrl(id, requestOptions)}/resources`;

// Mix in num:1000 with any user supplied params
// Key thing - we don't want to mutate the passed in requestOptions
Expand Down Expand Up @@ -183,7 +216,7 @@ export function getItemGroups(
id: string,
requestOptions?: IRequestOptions
): Promise<IGetItemGroupsResponse> {
const url = `${getPortalUrl(requestOptions)}/content/items/${id}/groups`;
const url = `${getItemBaseUrl(id, requestOptions)}/groups`;

return request(url, requestOptions);
}
Expand Down
12 changes: 10 additions & 2 deletions packages/arcgis-rest-portal/test/items/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import * as fetchMock from "fetch-mock";

import {
getItemBaseUrl,
getItem,
getItemData,
getItemResources,
Expand All @@ -17,8 +18,6 @@ import {
ItemResponse,
ItemDataResponse,
ItemGroupResponse,
ItemStatusResponse,
ItemPartsResponse,
RelatedItemsResponse
} from "../mocks/items/item";

Expand All @@ -27,6 +26,15 @@ import { GetItemResourcesResponse } from "../mocks/items/resources";
import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";

describe("get base url", () => {
it("should return base url when passed a portal url", () => {
const id = "foo";
const portalUrl = "https://org.maps.arcgis.com/sharing/rest/";
const result = getItemBaseUrl(id, portalUrl);
expect(result).toBe(`${portalUrl}/content/items/${id}`);
});
});

describe("get", () => {
afterEach(fetchMock.restore);

Expand Down

0 comments on commit d6ec9fc

Please sign in to comment.