diff --git a/packages/arcgis-rest-auth/src/get-user-url.ts b/packages/arcgis-rest-auth/src/get-user-url.ts new file mode 100644 index 0000000000..36b9321ff6 --- /dev/null +++ b/packages/arcgis-rest-auth/src/get-user-url.ts @@ -0,0 +1,17 @@ +/* Copyright (c) 2019 Environmental Systems Research Institute, Inc. + * Apache-2.0 */ + +import { getPortalUrl } from "@esri/arcgis-rest-request"; +import { UserSession } from "./UserSession"; + +/** + * Helper that returns the [user](https://developers.arcgis.com/rest/users-groups-and-items/user.htm) for a given portal. + * + * @param session + * @returns User url to be used in API requests. + */ +export function getUserUrl(session: UserSession): string { + return `${getPortalUrl(session)}/community/users/${encodeURIComponent( + session.username + )}`; +} diff --git a/packages/arcgis-rest-auth/src/index.ts b/packages/arcgis-rest-auth/src/index.ts index b29cacaf66..6f6bc93ec4 100644 --- a/packages/arcgis-rest-auth/src/index.ts +++ b/packages/arcgis-rest-auth/src/index.ts @@ -6,3 +6,4 @@ export * from "./UserSession"; export * from "./fetch-token"; export * from "./generate-token"; export * from "./authenticated-request-options"; +export * from "./get-user-url"; diff --git a/packages/arcgis-rest-auth/test/getUserUrl.test.ts b/packages/arcgis-rest-auth/test/getUserUrl.test.ts new file mode 100644 index 0000000000..4ad46884d1 --- /dev/null +++ b/packages/arcgis-rest-auth/test/getUserUrl.test.ts @@ -0,0 +1,38 @@ +/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. + * Apache-2.0 */ + +import { UserSession, getUserUrl } from "../src/index"; + +describe("getUserUrl", () => { + it("should encode special characters", () => { + const session = new UserSession({ + username: "c@sey" + }); + + expect(getUserUrl(session)).toEqual( + "https://www.arcgis.com/sharing/rest/community/users/c%40sey" + ); + }); + + it("should recognize an explicit ArcGIS Online organization", () => { + const session = new UserSession({ + username: "c@sey", + portal: "https://custom.maps.arcgis.com/sharing/rest" + }); + + expect(getUserUrl(session)).toEqual( + "https://custom.maps.arcgis.com/sharing/rest/community/users/c%40sey" + ); + }); + + it("should recognize ArcGIS Enterprise", () => { + const session = new UserSession({ + username: "c@sey", + portal: "https://gis.city.gov/sharing/rest" + }); + + expect(getUserUrl(session)).toEqual( + "https://gis.city.gov/sharing/rest/community/users/c%40sey" + ); + }); +});