-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(getItemThumbnailUrl): add token if needed and support for IReque…
…stOptions AFFECTS PACKAGES: @esri/hub-common
- Loading branch information
Showing
2 changed files
with
28 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters