Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search param builder #509

Merged
merged 12 commits into from
Apr 16, 2019
83 changes: 43 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"rollup-plugin-filesize": "^5.0.1",
"rollup-plugin-json": "^2.3.0",
"rollup-plugin-node-resolve": "^3.0.3",
"rollup-plugin-typescript2": "^0.4.6",
"rollup-plugin-typescript": "^1.0.1",
"rollup-plugin-uglify": "^3.0.0",
"shelljs": "^0.7.8",
"slug": "^0.9.1",
Expand Down
6 changes: 6 additions & 0 deletions packages/arcgis-rest-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"dependencies": {
"tslib": "^1.9.3"
},
"devDependencies": {
"@esri/arcgis-rest-request": "^1.19.2"
},
"peerDependencies": {
"@esri/arcgis-rest-request": "^1.19.2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a devDependency?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd say yes. fixed in 99300a8

},
"scripts": {
"prepare": "npm run build",
"build": "npm run build:node && npm run build:umd && npm run build:esm",
Expand Down
1 change: 1 addition & 0 deletions packages/arcgis-rest-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// utility methods
export * from "./util/location";
export * from "./util/append-custom-params";

// types
export * from "./types/feature";
Expand Down
49 changes: 49 additions & 0 deletions packages/arcgis-rest-common/src/util/append-custom-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

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

/**
* Helper for methods with lots of first order request options to pass through as request parameters.
*/
export function appendCustomParams<T extends IRequestOptions>(
customOptions: T,
keys: Array<keyof T>,
baseOptions?: Partial<T>
): IRequestOptions {
const requestOptionsKeys = [
"params",
"httpMethod",
"rawResponse",
"authentication",
"portal",
"fetch",
"maxUrlLength",
"headers"
];

const options: T = {
...{ params: {} },
...baseOptions,
...customOptions
};

// merge all keys in customOptions into options.params
options.params = keys.reduce((value, key) => {
if (customOptions[key] || typeof customOptions[key] === "boolean") {
value[key as any] = customOptions[key];
}
return value;
}, options.params);

// now remove all properties in options that don't exist in IRequestOptions
return requestOptionsKeys.reduce(
(value, key) => {
if (options[key]) {
value[key] = options[key];
}
return value;
},
{} as IRequestOptions
);
}
61 changes: 61 additions & 0 deletions packages/arcgis-rest-common/test/append-custom-params.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { appendCustomParams } from "../src/util/append-custom-params";
import { IRequestOptions } from "@esri/arcgis-rest-request/src";

describe("appendCustomParams() helper", () => {
it("should assign custom params from a sub class of IRequestOptions", () => {
interface ICustomOptions extends IRequestOptions {
foo: string;
}

expect(
appendCustomParams<ICustomOptions>({ foo: "bar" }, ["foo"], {
httpMethod: "GET"
})
).toEqual({
httpMethod: "GET",
params: {
foo: "bar"
}
});
});

it("should assign custom params even if theyre falsey", () => {
interface IFalseyCustomOptions extends IRequestOptions {
foo: boolean;
}

expect(
appendCustomParams<IFalseyCustomOptions>({ foo: false }, ["foo"], {
httpMethod: "GET"
})
).toEqual({
httpMethod: "GET",
params: {
foo: false
}
});
});

it("should pass through manual params if nothing is present to overwrite them", () => {
interface IEmptyCustomOptions extends IRequestOptions {
foo?: boolean;
}

expect(
appendCustomParams<IEmptyCustomOptions>({}, ["foo"], {
httpMethod: "GET",
params: {
foo: "bar"
}
})
).toEqual({
httpMethod: "GET",
params: {
foo: "bar"
}
});
});
});
7 changes: 2 additions & 5 deletions packages/arcgis-rest-feature-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
},
"devDependencies": {
"@esri/arcgis-rest-request": "^1.19.2",
"@esri/arcgis-rest-common": "^1.19.2",
"@esri/arcgis-rest-auth": "^1.19.2",
"@esri/arcgis-rest-portal": "^1.19.2"

"@esri/arcgis-rest-common": "^1.19.2"
},
"peerDependencies": {
"@esri/arcgis-rest-request": "^1.19.2",
"@esri/arcgis-rest-common": "^1.19.2",
"@esri/arcgis-rest-auth": "^1.19.2",
"@esri/arcgis-rest-portal": "^1.19.2"
"@esri/arcgis-rest-common": "^1.19.2"
},
"scripts": {
"prepare": "npm run build",
Expand Down
21 changes: 8 additions & 13 deletions packages/arcgis-rest-feature-service/src/add.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IFeature } from "@esri/arcgis-rest-common";
import {
request,
IRequestOptions,
appendCustomParams,
cleanUrl
} from "@esri/arcgis-rest-request";
import { request, IRequestOptions, cleanUrl } from "@esri/arcgis-rest-request";
import { IFeature, appendCustomParams } from "@esri/arcgis-rest-common";

import { IEditFeaturesParams, IEditFeatureResult } from "./helpers";

/**
Expand Down Expand Up @@ -64,12 +60,11 @@ export function addFeatures(
const url = `${cleanUrl(requestOptions.url)}/addFeatures`;

// edit operations are POST only
const options: IAddFeaturesRequestOptions = {
params: {},
...requestOptions
};

appendCustomParams(requestOptions, options);
const options = appendCustomParams<IAddFeaturesRequestOptions>(
requestOptions,
["features", "gdbVersion", "returnEditMoment", "rollbackOnFailure"],
{ params: { ...requestOptions.params } }
);

return request(url, options);
}
25 changes: 13 additions & 12 deletions packages/arcgis-rest-feature-service/src/delete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import {
request,
IRequestOptions,
appendCustomParams,
cleanUrl
} from "@esri/arcgis-rest-request";
import { request, IRequestOptions, cleanUrl } from "@esri/arcgis-rest-request";
import { appendCustomParams } from "@esri/arcgis-rest-common";
import {
IEditFeaturesParams,
IEditFeatureResult,
Expand Down Expand Up @@ -70,12 +66,17 @@ export function deleteFeatures(
const url = `${cleanUrl(requestOptions.url)}/deleteFeatures`;

// edit operations POST only
const options: IDeleteFeaturesRequestOptions = {
params: {},
...requestOptions
};

appendCustomParams(requestOptions, options);
const options = appendCustomParams<IDeleteFeaturesRequestOptions>(
requestOptions,
[
"where",
"objectIds",
"gdbVersion",
"returnEditMoment",
"rollbackOnFailure"
],
{ params: { ...requestOptions.params } }
);

return request(url, options);
}
Loading