From 61031012d249fcaa9d86b5c68c9cbe7489b7a3b5 Mon Sep 17 00:00:00 2001 From: Noah Mulfinger Date: Tue, 1 May 2018 14:24:09 -0700 Subject: [PATCH] fix(getPortalUrl): make getPortalUrl use portal in request options if passed in AFFECTS PACKAGES: @esri/arcgis-rest-request ISSUES CLOSED: #180 --- .../src/utils/get-portal-url.ts | 20 +++++++++++-------- .../test/utils/get-portal-url.test.ts | 14 +++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/packages/arcgis-rest-request/src/utils/get-portal-url.ts b/packages/arcgis-rest-request/src/utils/get-portal-url.ts index a8504233de..fc12873226 100644 --- a/packages/arcgis-rest-request/src/utils/get-portal-url.ts +++ b/packages/arcgis-rest-request/src/utils/get-portal-url.ts @@ -5,17 +5,21 @@ import { IRequestOptions } from "../request"; /** * Helper that returns the portalUrl - either defaulting to www.arcgis.com or using * the passed in auth manager's .portal property - * + * * @param requestOptions - Request options that may have authentication manager * @returns Portal url to be used in API requests */ -export function getPortalUrl(requestOptions?: IRequestOptions): string { - // default to arcgis.com - let portalUrl = "https://www.arcgis.com/sharing/rest"; - // but if the auth was passed, use that portal... - if (requestOptions && requestOptions.authentication) { - portalUrl = requestOptions.authentication.portal; +export function getPortalUrl(requestOptions: IRequestOptions = {}): string { + // use portal in options if specified + if (requestOptions.portal) { + return requestOptions.portal; + } + + // if the auth was passed, use that portal + if (requestOptions.authentication) { + return requestOptions.authentication.portal; } - return portalUrl; + // default to arcgis.com + return "https://www.arcgis.com/sharing/rest"; } diff --git a/packages/arcgis-rest-request/test/utils/get-portal-url.test.ts b/packages/arcgis-rest-request/test/utils/get-portal-url.test.ts index 9c0ac70121..15502c751c 100644 --- a/packages/arcgis-rest-request/test/utils/get-portal-url.test.ts +++ b/packages/arcgis-rest-request/test/utils/get-portal-url.test.ts @@ -17,4 +17,18 @@ describe("getPortalUrl", () => { const url = getPortalUrl(requestOptions); expect(url).toEqual("https://foo.com/arcgis/sharing/rest"); }); + + it("should use the portal in the requestOptions if passed", () => { + const requestOptions = { + authentication: { + portal: "https://foo.com/arcgis/sharing/rest", + getToken() { + return Promise.resolve("fake"); + } + }, + portal: "https://bar.com/arcgis/sharing/rest" + }; + const url = getPortalUrl(requestOptions); + expect(url).toEqual("https://bar.com/arcgis/sharing/rest"); + }); });