Skip to content

Commit

Permalink
feat(thumbnail url): getItemThumbnailUrl() can take the portal URL in…
Browse files Browse the repository at this point in the history
…stead of request options

AFFECTS PACKAGES:
@esri/hub-common
  • Loading branch information
tomwayson committed Aug 1, 2020
1 parent a130406 commit fd40319
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 49 deletions.
17 changes: 10 additions & 7 deletions packages/common/src/resources/get-item-thumbnail-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ import { getPortalApiUrl } from "../urls";
*/
export function getItemThumbnailUrl(
item: IItem,
hubRequestOptions: IHubRequestOptions
portalApiUrlOrHubRequestOptions: string | IHubRequestOptions
): string | null {
if (item.thumbnail) {
const portalRestUrl = getPortalApiUrl(hubRequestOptions.portalSelf);
const itemUrl = `${portalRestUrl}/content/items/${item.id}`;
const url = `${itemUrl}/info/${item.thumbnail}`;
return url;
} else {
if (!item || !item.thumbnail) {
return null;
}
const portalRestUrl =
typeof portalApiUrlOrHubRequestOptions === "string"
? portalApiUrlOrHubRequestOptions
: getPortalApiUrl(portalApiUrlOrHubRequestOptions.portalSelf);
const itemRestUrl = `${portalRestUrl}/content/items/${item.id}`;
// TODO: handle image types by returning the image itself?
const url = `${itemRestUrl}/info/${item.thumbnail}`;
return url;
}
63 changes: 21 additions & 42 deletions packages/common/test/resources/get-item-thumbnail-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ import { IItem } from "@esri/arcgis-rest-types";
import { mockUserSession } from "../test-helpers/fake-user-session";

describe("getItemThumbnailUrl", function() {
it("computes url when item.thumbnail", function() {
const ro: IHubRequestOptions = {
isPortal: false,
hubApiUrl: "",
portalSelf: {
id: "",
name: "",
isPortal: false
},
authentication: mockUserSession
};

const item: IItem = {
const portalApiUrl = "https://portal-api-url";
let item: IItem;
beforeEach(function() {
item = {
id: "abcitemid",
thumbnail: "thumbnail.png",
owner: "a",
Expand All @@ -28,22 +19,8 @@ describe("getItemThumbnailUrl", function() {
title: "title",
type: "CSV"
};

const portalApiUrl = "https://portal-api-url";
const getPortalApiSpy = spyOn(
urlsModule,
"getPortalApiUrl"
).and.returnValue(portalApiUrl);

const url = getItemThumbnailUrl(item, ro);

expect(getPortalApiSpy.calls.count()).toBe(1);
expect(url).toBe(
`https://portal-api-url/content/items/abcitemid/info/thumbnail.png`
);
});

it("returns null when no item.thumbnail", function() {
it("computes url when passed request options", function() {
const ro: IHubRequestOptions = {
isPortal: false,
hubApiUrl: "",
Expand All @@ -55,27 +32,29 @@ describe("getItemThumbnailUrl", function() {
authentication: mockUserSession
};

const item: IItem = {
id: "abcitemid",
owner: "a",
tags: ["x"],
created: 1,
modified: 1,
numViews: 1,
size: 1,
title: "title",
type: "CSV"
};

const portalApiUrl = "https://portal-api-url";
const getPortalApiSpy = spyOn(
urlsModule,
"getPortalApiUrl"
).and.returnValue(portalApiUrl);

const url = getItemThumbnailUrl(item, ro);

expect(getPortalApiSpy.calls.count()).toBe(0);
expect(getPortalApiSpy.calls.count()).toBe(1);
expect(url).toBe(
`https://portal-api-url/content/items/abcitemid/info/thumbnail.png`
);
});

it("computes url when passed portal api url", function() {
const url = getItemThumbnailUrl(item, portalApiUrl);
expect(url).toBe(
`https://portal-api-url/content/items/abcitemid/info/thumbnail.png`
);
});

it("returns null when no item.thumbnail", function() {
delete item.thumbnail;
const url = getItemThumbnailUrl(item, portalApiUrl);
expect(url).toBeNull();
});
});

0 comments on commit fd40319

Please sign in to comment.