Skip to content

Commit

Permalink
Merge pull request #400 from Esri/feat/trim
Browse files Browse the repository at this point in the history
add utility method to trim whitespace and trailing slashes from input urls
  • Loading branch information
jgravois authored Nov 26, 2018
2 parents d228c86 + 8f86578 commit 834f242
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 33 deletions.
21 changes: 11 additions & 10 deletions packages/arcgis-rest-geocoder/src/bulk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request } from "@esri/arcgis-rest-request";
import { request, cleanUrl } from "@esri/arcgis-rest-request";

import { ISpatialReference, IPoint } from "@esri/arcgis-rest-common-types";

Expand Down Expand Up @@ -86,15 +86,16 @@ export function bulkGeocode(
);
}

return request(options.endpoint + "geocodeAddresses", options).then(
response => {
const sr = response.spatialReference;
response.locations.forEach(function(address: { location: IPoint }) {
address.location.spatialReference = sr;
});
return response;
}
);
return request(
`${cleanUrl(options.endpoint)}/geocodeAddresses`,
options
).then(response => {
const sr = response.spatialReference;
response.locations.forEach(function(address: { location: IPoint }) {
address.location.spatialReference = sr;
});
return response;
});
}

export default {
Expand Down
34 changes: 18 additions & 16 deletions packages/arcgis-rest-geocoder/src/geocode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import {
request,
IParams,
appendCustomParams
appendCustomParams,
cleanUrl
} from "@esri/arcgis-rest-request";

import {
Expand Down Expand Up @@ -108,21 +109,22 @@ export function geocode(
}

// add spatialReference property to individual matches
return request(options.endpoint + "findAddressCandidates", options).then(
response => {
const sr = response.spatialReference;
response.candidates.forEach(function(candidate: {
location: IPoint;
extent?: IExtent;
}) {
candidate.location.spatialReference = sr;
if (candidate.extent) {
candidate.extent.spatialReference = sr;
}
});
return response;
}
);
return request(
`${cleanUrl(options.endpoint)}/findAddressCandidates`,
options
).then(response => {
const sr = response.spatialReference;
response.candidates.forEach(function(candidate: {
location: IPoint;
extent?: IExtent;
}) {
candidate.location.spatialReference = sr;
if (candidate.extent) {
candidate.extent.spatialReference = sr;
}
});
return response;
});
}

export default {
Expand Down
4 changes: 2 additions & 2 deletions packages/arcgis-rest-geocoder/src/reverse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request } from "@esri/arcgis-rest-request";
import { request, cleanUrl } from "@esri/arcgis-rest-request";
import { IPoint } from "@esri/arcgis-rest-common-types";

import { ILocation, worldGeocoder, IEndpointRequestOptions } from "./helpers";
Expand Down Expand Up @@ -75,7 +75,7 @@ export function reverseGeocode(
options.params.location = coords;
}

return request(options.endpoint + "reverseGeocode", options);
return request(`${cleanUrl(options.endpoint)}/reverseGeocode`, options);
}

export default {
Expand Down
4 changes: 2 additions & 2 deletions packages/arcgis-rest-geocoder/src/suggest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request } from "@esri/arcgis-rest-request";
import { request, cleanUrl } from "@esri/arcgis-rest-request";

import { worldGeocoder, IEndpointRequestOptions } from "./helpers";

Expand Down Expand Up @@ -61,7 +61,7 @@ export function suggest(
options.params.magicKey = requestOptions.magicKey;
}

return request(options.endpoint + "suggest", options);
return request(`${cleanUrl(options.endpoint)}/suggest`, options);
}

export default {
Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-request/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from "./utils/process-params";
export * from "./utils/get-portal";
export * from "./utils/get-portal-url";
export * from "./utils/append-custom-params";
export * from "./utils/clean-url";
16 changes: 16 additions & 0 deletions packages/arcgis-rest-request/src/utils/clean-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

/**
* Helper method to ensure that user supplied urls don't include whitespace or a trailing slash.
*/
export function cleanUrl(url: string) {
// trim leading and trailing spaces, but not spaces inside the url
url = url.trim();

// remove the trailing slash to the url if one was included
if (url[url.length - 1] === "/") {
url = url.slice(0, -1);
}
return url;
}
6 changes: 3 additions & 3 deletions packages/arcgis-rest-request/src/utils/get-portal-url.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import { IRequestOptions } from "../request";

import { cleanUrl } from "./clean-url";
/**
* Helper that returns the appropriate portal url for a given request. `requestOptions.portal` is given
* precedence over `authentication.portal`. If neither are present, `www.arcgis.com/sharing/rest` is returned.
Expand All @@ -12,12 +12,12 @@ import { IRequestOptions } from "../request";
export function getPortalUrl(requestOptions: IRequestOptions = {}): string {
// use portal in options if specified
if (requestOptions.portal) {
return requestOptions.portal;
return cleanUrl(requestOptions.portal);
}

// if auth was passed, use that portal
if (requestOptions.authentication) {
return requestOptions.authentication.portal;
return cleanUrl(requestOptions.authentication.portal);
}

// default to arcgis.com
Expand Down
47 changes: 47 additions & 0 deletions packages/arcgis-rest-request/test/utils/clean-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { cleanUrl } from "../../src/utils/clean-url";

const validUrl =
"https://server.com/arcgis/rest/services/Custom Space/MapServer";

describe("cleanUrl", () => {
it("should not mangle a valid url", () => {
expect(
cleanUrl(
"https://server.com/arcgis/rest/services/Custom Space/MapServer/"
)
).toEqual(validUrl);
});

it("should add a trailing slash", () => {
expect(
cleanUrl("https://server.com/arcgis/rest/services/Custom Space/MapServer")
).toEqual(validUrl);
});

it("should remove leading whitespace", () => {
expect(
cleanUrl(
" https://server.com/arcgis/rest/services/Custom Space/MapServer/"
)
).toEqual(validUrl);
});

it("should remove trailing whitespace", () => {
expect(
cleanUrl(
"https://server.com/arcgis/rest/services/Custom Space/MapServer/ "
)
).toEqual(validUrl);
});

it("should do it all at once", () => {
expect(
cleanUrl(
" https://server.com/arcgis/rest/services/Custom Space/MapServer "
)
).toEqual(validUrl);
});
});

0 comments on commit 834f242

Please sign in to comment.