Skip to content

Commit

Permalink
feat(getItemThumbnailUrl): add token if needed and support for IReque…
Browse files Browse the repository at this point in the history
…stOptions

AFFECTS PACKAGES:
@esri/hub-common
  • Loading branch information
tomwayson committed Aug 26, 2020
1 parent 0fbbc23 commit 5b07247
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 65 deletions.
41 changes: 17 additions & 24 deletions packages/common/src/resources/get-item-thumbnail-url.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,30 @@
import { IRequestOptions } from "@esri/arcgis-rest-request";
import { IItem } from "@esri/arcgis-rest-types";
import { IPortal } from "@esri/arcgis-rest-portal";
import { IHubRequestOptions } from "../types";
import { getPortalApiUrl } from "../urls";
import { getItemApiUrl } from "../urls/get-item-api-url";

/**
* Get the url for an item's thumbnail
* Returns null if the item does not have a thumbnail
* assigned
* @param item
* @param urlOrObject either a portal URL, a portal object, or request options
* Get the fully qualified URL for an item's thumbnail
* @param item w/ id, thumbnail, and access
* @param portalUrlOrObject a portal base or API URL, a portal object, or request options containing either of those
* @param token token for the current user's session; will only be appended as a query parameter if the item's access is **not** `public`
* @returns URL to the item's thumbnail, defaults to `https://www.arcgis.com/sharing/rest/content/items/{item.id}/info/{item.thumbnail}`. Returns `null` if the item does not have a thumbnail assigned.
*/
export function getItemThumbnailUrl(
item: IItem,
urlOrObject: string | IPortal | IHubRequestOptions
portalUrlOrObject?: string | IPortal | IHubRequestOptions | IRequestOptions,
token?: string
): string | null {
if (!item || !item.thumbnail) {
// TODO: handle image types by returning the image itself?
return null;
}
// get portal URL from second arg
let portalApiUrl;
if (typeof urlOrObject === "string") {
// assume it's the URL itself
portalApiUrl = urlOrObject;
} else {
const portalSelf = urlOrObject.portalSelf
? // assume it's request options
urlOrObject.portalSelf
: // assume it's a portal
urlOrObject;
portalApiUrl = getPortalApiUrl(portalSelf);
}
const itemRestUrl = `${portalApiUrl}/content/items/${item.id}`;
// TODO: handle image types by returning the image itself?
const url = `${itemRestUrl}/info/${item.thumbnail}`;
return url;
const itemApiUrl = getItemApiUrl(item, portalUrlOrObject, token);
const [baseUrl, search] = itemApiUrl.split("?");
const searchParams = new URLSearchParams(search);
searchParams.delete("f");
const newSearch = searchParams.toString();
const url = `${baseUrl}/info/${item.thumbnail}`;
return newSearch ? `${url}?${newSearch}` : url;
}
52 changes: 11 additions & 41 deletions packages/common/test/resources/get-item-thumbnail-url.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { getItemThumbnailUrl, IHubRequestOptions } from "../../src";
import { IPortal } from "@esri/arcgis-rest-portal";
import * as urlsModule from "../../src/urls";
import { getItemThumbnailUrl } from "../../src";
import { IItem } from "@esri/arcgis-rest-types";
import { mockUserSession } from "../test-helpers/fake-user-session";

describe("getItemThumbnailUrl", function() {
const portalApiUrl = "https://portal-api-url";
const portalApiUrl = "https://portal-api-url/sharing/rest";
let item: IItem;
let portalSelf: IPortal;
let getPortalApiSpy: jasmine.Spy;
let itemApiUrlBase: string;

beforeEach(function() {
item = {
Expand All @@ -23,44 +19,18 @@ describe("getItemThumbnailUrl", function() {
title: "title",
type: "CSV"
};
portalSelf = {
id: "",
name: "",
isPortal: false
};
getPortalApiSpy = spyOn(urlsModule, "getPortalApiUrl").and.returnValue(
portalApiUrl
);
});

it("computes url when passed portal self", function() {
const url = getItemThumbnailUrl(item, portalSelf);
expect(getPortalApiSpy.calls.argsFor(0)).toEqual([portalSelf]);
expect(url).toBe(
`https://portal-api-url/content/items/abcitemid/info/thumbnail.png`
);
itemApiUrlBase = `${portalApiUrl}/content/items/${item.id}`;
});

it("computes url when passed request options", function() {
const ro: IHubRequestOptions = {
isPortal: false,
hubApiUrl: "",
portalSelf,
authentication: mockUserSession
};
const url = getItemThumbnailUrl(item, ro);
expect(getPortalApiSpy.calls.argsFor(0)).toEqual([portalSelf]);
expect(url).toBe(
`https://portal-api-url/content/items/abcitemid/info/thumbnail.png`
);
it("computes url without a token", function() {
const url = getItemThumbnailUrl(item, portalApiUrl);
expect(url).toBe(`${itemApiUrlBase}/info/thumbnail.png`);
});

it("computes url when passed portal api url", function() {
const url = getItemThumbnailUrl(item, portalApiUrl);
expect(getPortalApiSpy.calls.count()).toBe(0);
expect(url).toBe(
`https://portal-api-url/content/items/abcitemid/info/thumbnail.png`
);
it("computes url with a token", function() {
const token = "token";
const url = getItemThumbnailUrl(item, portalApiUrl, token);
expect(url).toBe(`${itemApiUrlBase}/info/thumbnail.png?token=${token}`);
});

it("returns null when no item.thumbnail", function() {
Expand Down

0 comments on commit 5b07247

Please sign in to comment.