Skip to content

Commit

Permalink
feat(getPortalUrl): take a Portal API URL and strip the /sharing/rest
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/hub-common
  • Loading branch information
tomwayson committed Aug 26, 2020
1 parent dea1dd4 commit d962e05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
10 changes: 8 additions & 2 deletions packages/common/src/urls/get-portal-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import { IPortal } from "@esri/arcgis-rest-portal";

/**
* Return the Portal url based on the portal self
* @param {Object} portal Portal Self
* @param {Object} urlOrObject a Portal API URL, request options object, or Portal self object
*/
export function getPortalUrl(portal: IPortal): string {
export function getPortalUrl(portalOrUrl: IPortal | string): string {
if (typeof portalOrUrl === "string") {
// assume this is the URL of the Portal API, strip the `/sharing/rest`
return portalOrUrl.replace(/\/sharing\/rest\/?$/, "");
}
// this is a Portal self object
const portal = portalOrUrl;
if (portal.isPortal) {
return `https://${portal.portalHostname}`;
} else {
Expand Down
36 changes: 24 additions & 12 deletions packages/common/test/urls/get-portal-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IPortal } from "@esri/arcgis-rest-portal";
import { getPortalUrl } from "../../src";

describe("getPortalUrl", function() {
const portalSelfResponse: IPortal = {
const portalSelf: IPortal = {
isPortal: true,
id: "some-id",
name: "Portal Name",
Expand All @@ -11,17 +11,29 @@ describe("getPortalUrl", function() {
customBaseUrl: "custom-base-url.com"
};

it("uses portalHostname when isPortal", function() {
portalSelfResponse.isPortal = true;
expect(getPortalUrl(portalSelfResponse)).toEqual(
`https://${portalSelfResponse.portalHostname}`
);
});
describe("when passed a portal", () => {
it("uses portalHostname when isPortal", function() {
portalSelf.isPortal = true;
expect(getPortalUrl(portalSelf)).toEqual(
`https://${portalSelf.portalHostname}`
);
});

it("constructs url when NOT isPortal", function() {
portalSelfResponse.isPortal = false;
expect(getPortalUrl(portalSelfResponse)).toEqual(
`https://${portalSelfResponse.urlKey}.${portalSelfResponse.customBaseUrl}`
);
it("constructs url when NOT isPortal", function() {
portalSelf.isPortal = false;
expect(getPortalUrl(portalSelf)).toEqual(
`https://${portalSelf.urlKey}.${portalSelf.customBaseUrl}`
);
});
});
describe("when passed a portal API URL", () => {
it("should strip /sharing/rest", () => {
const result = getPortalUrl("https://www.arcgis.com/sharing/rest");
expect(result).toBe("https://www.arcgis.com");
});
it("should strip /sharing/rest/", () => {
const result = getPortalUrl("https://www.arcgis.com/sharing/rest/");
expect(result).toBe("https://www.arcgis.com");
});
});
});

0 comments on commit d962e05

Please sign in to comment.