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

Fix crud param names #396

Merged
merged 2 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions packages/arcgis-rest-feature-service/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { IFeature } from "@esri/arcgis-rest-common-types";
import {
request,
IRequestOptions,
appendCustomParams
appendCustomParams,
warn
} from "@esri/arcgis-rest-request";
import { IEditFeaturesParams, IEditFeatureResult } from "./helpers";

/**
* Add features request options. See the [REST Documentation](https://developers.arcgis.com/rest/services-reference/add-features.htm) for more information.
*
* @param url - Feature service url.
* @param adds - Array of JSON features to add.
* @param features - Array of JSON features to add.
* @param params - Query parameters to be sent to the feature service via the request.
*/
export interface IAddFeaturesRequestOptions
Expand All @@ -26,7 +27,13 @@ export interface IAddFeaturesRequestOptions
/**
* Array of JSON features to add.
*/
adds: IFeature[];
features: IFeature[];
/**
* Deprecated. Please use `features` instead.
*
* Array of JSON features to add.
*/
adds?: IFeature[];
Copy link
Contributor

Choose a reason for hiding this comment

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

/**
* Deprecated. Please use `features` instead.

Array of JSON features to add.

these two will show up right next to each other in the API reference, so no need to repeat the meat of the description.

}

/**
Expand All @@ -49,7 +56,7 @@ export interface IAddFeaturesResult {
*
* addFeatures({
* url,
* adds: [{
* features: [{
* geometry: { x: -120, y: 45, spatialReference: { wkid: 4326 } },
* attributes: { status: "alive" }
* }]
Expand All @@ -72,9 +79,14 @@ export function addFeatures(

appendCustomParams(requestOptions, options);

// mixin, don't overwrite
options.params.features = requestOptions.adds;
delete options.params.adds;
if (options.params.adds && options.params.adds.length) {
// mixin, don't overwrite
options.params.features = requestOptions.adds;
delete options.params.adds;
warn(
"The `adds` parameter is deprecated and will be removed in a future release. Please use `features` instead."
);
}

return request(url, options);
}
26 changes: 19 additions & 7 deletions packages/arcgis-rest-feature-service/src/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import {
request,
IRequestOptions,
appendCustomParams
appendCustomParams,
warn
} from "@esri/arcgis-rest-request";
import {
IEditFeaturesParams,
Expand All @@ -23,7 +24,7 @@ export interface IDeleteFeaturesParams
* Delete features request options. See the [REST Documentation](https://developers.arcgis.com/rest/services-reference/delete-features.htm) for more information.
*
* @param url - Feature service url.
* @param deletes - Array of objectIds to delete.
* @param objectIds - Array of objectIds to delete.
* @param params - Query parameters to be sent to the feature service via the request.
*/
export interface IDeleteFeaturesRequestOptions
Expand All @@ -36,7 +37,13 @@ export interface IDeleteFeaturesRequestOptions
/**
* Array of objectIds to delete.
*/
deletes: number[];
objectIds: number[];
/**
* Deprecated. Please use `objectIds` instead.
*
* Array of objectIds to delete.
*/
deletes?: number[];
}

/**
Expand All @@ -59,7 +66,7 @@ export interface IDeleteFeaturesResult {
*
* deleteFeatures({
* url,
* deletes: [1,2,3]
* objectIds: [1,2,3]
* });
* ```
*
Expand All @@ -79,9 +86,14 @@ export function deleteFeatures(

appendCustomParams(requestOptions, options);

// mixin, don't overwrite
options.params.objectIds = requestOptions.deletes;
delete options.params.deletes;
if (options.params.deletes && options.params.deletes.length) {
// mixin, don't overwrite
options.params.objectIds = requestOptions.deletes;
delete options.params.deletes;
warn(
"The `deletes` parameter is deprecated and will be removed in a future release. Please use `objectIds` instead."
);
}

return request(url, options);
}
26 changes: 19 additions & 7 deletions packages/arcgis-rest-feature-service/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import { IFeature } from "@esri/arcgis-rest-common-types";
import {
request,
IRequestOptions,
appendCustomParams
appendCustomParams,
warn
} from "@esri/arcgis-rest-request";
import { IEditFeaturesParams, IEditFeatureResult } from "./helpers";

/**
* Update features request options. See the [REST Documentation](https://developers.arcgis.com/rest/services-reference/update-features.htm) for more information.
*
* @param url - Feature service url.
* @param updates - Array of JSON features to update.
* @param features - Array of JSON features to update.
* @param params - Query parameters to be sent to the feature service via the request.
*/
export interface IUpdateFeaturesRequestOptions
Expand All @@ -26,7 +27,13 @@ export interface IUpdateFeaturesRequestOptions
/**
* Array of JSON features to update.
*/
updates: IFeature[];
features: IFeature[];
/**
* Deprecated. Please use `features` instead.
*
* Array of JSON features to update.
*/
updates?: IFeature[];
}

/**
Expand All @@ -49,7 +56,7 @@ export interface IUpdateFeaturesResult {
*
* updateFeatures({
* url,
* updates: [{
* features: [{
* geometry: { x: -120, y: 45, spatialReference: { wkid: 4326 } },
* attributes: { status: "alive" }
* }]
Expand All @@ -72,9 +79,14 @@ export function updateFeatures(

appendCustomParams(requestOptions, options);

// mixin, don't overwrite
options.params.features = requestOptions.updates;
delete options.params.updates;
if (options.params.updates && options.params.updates.length) {
// mixin, don't overwrite
options.params.features = requestOptions.updates;
delete options.params.updates;
warn(
"The `updates` parameter is deprecated and will be removed in a future release. Please use `features` instead."
);
}

return request(url, options);
}
133 changes: 133 additions & 0 deletions packages/arcgis-rest-feature-service/test/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,130 @@ describe("feature", () => {
it("should return objectId of the added feature and a truthy success", done => {
const requestOptions = {
url: serviceUrl,
features: [
{
geometry: {
x: -9177311.62541634,
y: 4247151.205222242,
spatialReference: {
wkid: 102100,
latestWkid: 3857
}
},
attributes: {
Tree_ID: 102,
Collected: 1349395200000,
Crew: "Linden+ Forrest+ Johnny"
}
}
]
};
fetchMock.once("*", addFeaturesResponse);
addFeatures(requestOptions)
.then(response => {
expect(fetchMock.called()).toBeTruthy();
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(`${requestOptions.url}/addFeatures`);
expect(options.body).toContain(
"features=" +
encodeURIComponent(
'[{"geometry":{"x":-9177311.62541634,"y":4247151.205222242,"spatialReference":{"wkid":102100,"latestWkid":3857}},"attributes":{"Tree_ID":102,"Collected":1349395200000,"Crew":"Linden+ Forrest+ Johnny"}}]'
)
);
expect(options.method).toBe("POST");
expect(response.addResults[0].objectId).toEqual(1001);
expect(response.addResults[0].success).toEqual(true);
done();
})
.catch(e => {
fail(e);
});
});

it("should return objectId of the updated feature and a truthy success", done => {
const requestOptions = {
url: serviceUrl,
features: [
{
attributes: {
OBJECTID: 1001,
Street: "NO",
Native: "YES"
}
}
],
rollbackOnFailure: false
} as IUpdateFeaturesRequestOptions;
fetchMock.once("*", updateFeaturesResponse);
updateFeatures(requestOptions)
.then(response => {
expect(fetchMock.called()).toBeTruthy();
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(`${requestOptions.url}/updateFeatures`);
expect(options.method).toBe("POST");
expect(options.body).toContain(
"features=" +
encodeURIComponent(
'[{"attributes":{"OBJECTID":1001,"Street":"NO","Native":"YES"}}]'
)
);
expect(options.body).toContain("rollbackOnFailure=false");
expect(response.updateResults[0].success).toEqual(true);
done();
})
.catch(e => {
fail(e);
});
});

it("should return objectId of the deleted feature and a truthy success", done => {
const requestOptions = {
url: serviceUrl,
objectIds: [1001],
where: "1=1"
} as IDeleteFeaturesRequestOptions;
fetchMock.once("*", deleteFeaturesResponse);
deleteFeatures(requestOptions)
.then(response => {
expect(fetchMock.called()).toBeTruthy();
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(`${requestOptions.url}/deleteFeatures`);
expect(options.body).toContain("objectIds=1001");
expect(options.body).toContain("where=1%3D1");
expect(options.method).toBe("POST");
expect(response.deleteResults[0].objectId).toEqual(
requestOptions.objectIds[0]
);
expect(response.deleteResults[0].success).toEqual(true);
done();
})
.catch(e => {
fail(e);
});
});

// tests for depreciated params
// remove when `adds`, `updates` and `deletes` params removed
it("should return objectId of the added feature and a truthy success", done => {
const requestOptions = {
url: serviceUrl,
features: [
{
geometry: {
x: -9177311.62541634,
y: 4247151.205222242,
spatialReference: {
wkid: 102100,
latestWkid: 3857
}
},
attributes: {
Tree_ID: 102,
Collected: 1349395200000,
Crew: "Linden+ Forrest+ Johnny"
}
}
], // for linting
adds: [
{
geometry: {
Expand Down Expand Up @@ -69,6 +193,15 @@ describe("feature", () => {
it("should return objectId of the updated feature and a truthy success", done => {
const requestOptions = {
url: serviceUrl,
features: [
{
attributes: {
OBJECTID: 1001,
Street: "NO",
Native: "YES"
}
}
], // for linting
updates: [
{
attributes: {
Expand Down