Skip to content

Commit

Permalink
Merge pull request #411 from Esri/b/fix-item-thumbumail-url
Browse files Browse the repository at this point in the history
fix(getItemThumbnailUrl): pass token when calling from withPortalUrls…
  • Loading branch information
tomwayson authored Oct 22, 2020
2 parents 6cc68f0 + c0f455d commit b7cfcad
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
9 changes: 2 additions & 7 deletions packages/common/src/resources/get-item-thumbnail-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { IItem } from "@esri/arcgis-rest-types";
import { IPortal } from "@esri/arcgis-rest-portal";
import { IHubRequestOptions } from "../types";
import { getItemApiUrl } from "../urls/get-item-api-url";
import { type } from "os";

export interface IThumbnailOptions {
token?: string;
width?: number;
}

/**
Expand All @@ -30,18 +28,15 @@ export function getItemThumbnailUrl(
return null;
}
// tslint:disable-next-line prefer-const
let { token, width } = (optionsOrToken as IThumbnailOptions) || {};
// TODO: at the next breaking change drop support for passing token as string
let { token } = (optionsOrToken as IThumbnailOptions) || {};
// TODO: at the next breaking change drop support for passing options
if (!token && typeof optionsOrToken === "string") {
token = optionsOrToken;
}
const itemApiUrl = getItemApiUrl(item, portalUrlOrObject, token);
const [baseUrl, search] = itemApiUrl.split("?");
const searchParams = new URLSearchParams(search);
searchParams.delete("f");
if (width) {
searchParams.append("w", width + "");
}
const newSearch = searchParams.toString();
const url = `${baseUrl}/info/${item.thumbnail}`;
return newSearch ? `${url}?${newSearch}` : url;
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/urls/get-item-api-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const getItemApiUrl = (
const { id, access } = item;
const url = `${getPortalApiUrl(portalUrlOrObject)}/content/items/${id}`;
const params = new URLSearchParams({ f: "json" });
// TODO: derive token from from requestOptions if passed in?
if (access !== "public" && token) {
params.append("token", token);
}
Expand Down
7 changes: 2 additions & 5 deletions packages/common/test/resources/get-item-thumbnail-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ describe("getItemThumbnailUrl", function() {

it("computes url with options", function() {
const token = "token";
const width = 1200;
const url = getItemThumbnailUrl(item, portalApiUrl, { token, width });
expect(url).toBe(
`${itemApiUrlBase}/info/thumbnail.png?token=${token}&w=1200`
);
const url = getItemThumbnailUrl(item, portalApiUrl, { token });
expect(url).toBe(`${itemApiUrlBase}/info/thumbnail.png?token=${token}`);
});
});
6 changes: 5 additions & 1 deletion packages/content/src/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ export function withPortalUrls(
// the URL of the item's data API end point
newContent.portalDataUrl = getItemDataUrl(newContent, requestOptions, token);
// the full URL of the thumbnail
newContent.thumbnailUrl = getItemThumbnailUrl(newContent, requestOptions);
newContent.thumbnailUrl = getItemThumbnailUrl(
newContent,
requestOptions,
token
);
return newContent;
}

Expand Down

0 comments on commit b7cfcad

Please sign in to comment.