Skip to content

Commit

Permalink
feat(service-admin): add updateServiceDefinition method
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-service-admin
  • Loading branch information
Chase Gruber committed Sep 18, 2020
1 parent 27f162f commit 63c2bc0
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/arcgis-rest-service-admin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./create";
export * from "./addTo";
export * from "./update";
export {
IFeatureServiceDefinition,
IExtent,
Expand Down
50 changes: 50 additions & 0 deletions packages/arcgis-rest-service-admin/src/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Copyright (c) 2018-2019 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request, cleanUrl } from "@esri/arcgis-rest-request";
import { IFeatureServiceDefinition } from "@esri/arcgis-rest-types";
import { IUserRequestOptions } from "@esri/arcgis-rest-auth";

export interface IUpdateServiceDefinitionOptions extends IUserRequestOptions {
updateDefinition?: Partial<IFeatureServiceDefinition>;
}

export interface IUpdateServiceDefinitionResult {
success: boolean;
}

/**
* ```js
* import { updateServiceDefinition } from '@esri/arcgis-rest-service-admin';
* //
* updateServiceDefinition(serviceurl, {
* authentication: userSession,
* updateDefinition: serviceDefinition
* });
* ```
* Update a definition property in a hosted feature service. See the [REST Documentation](https://developers.arcgis.com/rest/services-reference/update-definition-feature-service-.htm) for more information.
*
* @param url - URL of feature service
* @param requestOptions - Options for the request
* @returns A Promise that resolves with success or error
*/
export function updateServiceDefinition(
url: string,
requestOptions: IUpdateServiceDefinitionOptions
): Promise<IUpdateServiceDefinitionResult> {
const adminUrl = `${cleanUrl(url).replace(
`/rest/services`,
`/rest/admin/services`
)}/updateDefinition`;

requestOptions.params = {
updateDefinition: {},
...requestOptions.params
};

if (requestOptions.updateDefinition) {
requestOptions.params.updateDefinition = requestOptions.updateDefinition;
}

return request(adminUrl, requestOptions);
}
12 changes: 12 additions & 0 deletions packages/arcgis-rest-service-admin/test/mocks/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { ICreateServiceResult } from "../../src/create";
import { IAddToServiceDefinitionResult } from "../../src/addTo";
import { IUpdateServiceDefinitionResult } from "../../src/update";

export const FeatureServiceResponse: ICreateServiceResult = {
encodedServiceURL:
Expand Down Expand Up @@ -67,3 +68,14 @@ export const AddToFeatureServiceError: any = {
details: ["Object reference not set to an instance of an object."]
}
};

export const UpdateServiceDefinitionSuccess: IUpdateServiceDefinitionResult = {
success: true
}
export const UpdateServiceDefinitionError: any = {
error: {
code: 400,
message: "Invalid URL",
details: ["Invalid URL"]
}
};
115 changes: 115 additions & 0 deletions packages/arcgis-rest-service-admin/test/update.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import * as fetchMock from "fetch-mock";

import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";
import { encodeParam } from "@esri/arcgis-rest-request";
import { updateServiceDefinition } from "../src/update";
import { UpdateServiceDefinitionSuccess } from "./mocks/service";

describe("update service definition", () => {
afterEach(fetchMock.restore);

describe("Authenticated methods", () => {
// setup a UserSession to use in all these tests
const MOCK_USER_SESSION = new UserSession({
clientId: "clientId",
redirectUri: "https://example-app.com/redirect-uri",
token: "fake-token",
tokenExpires: TOMORROW,
refreshToken: "refreshToken",
refreshTokenExpires: TOMORROW,
refreshTokenTTL: 1440,
username: "casey",
password: "123456",
portal: "https://myorg.maps.arcgis.com/sharing/rest"
});

const MOCK_USER_REQOPTS = {
authentication: MOCK_USER_SESSION
};

const updateDefinition = {
capabilities: 'Create,Update'
};

it("should update feature service defintion", done => {
fetchMock.once("*", UpdateServiceDefinitionSuccess);

updateServiceDefinition(
"https://services1.arcgis.com/ORG/arcgis/rest/services/FEATURE_SERVICE/FeatureServer",
{
updateDefinition,
...MOCK_USER_REQOPTS
}
)
.then(response => {
// Check service call
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");

expect(url).toEqual(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/updateDefinition"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(
encodeParam(
"updateDefinition",
JSON.stringify(updateDefinition)
)
);

// Check response
expect(response).toEqual(
UpdateServiceDefinitionSuccess
);
done();
})
.catch(e => {
fail(e);
});
});
it("should update feature service defintion (params.updateDefinition)", done => {
fetchMock.once("*", UpdateServiceDefinitionSuccess);

updateServiceDefinition(
"https://services1.arcgis.com/ORG/arcgis/rest/services/FEATURE_SERVICE/FeatureServer",
{
params: { updateDefinition },
...MOCK_USER_REQOPTS
}
)
.then(response => {
// Check service call
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");

expect(url).toEqual(
"https://services1.arcgis.com/ORG/arcgis/rest/admin/services/FEATURE_SERVICE/FeatureServer/updateDefinition"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(
encodeParam(
"updateDefinition",
JSON.stringify(updateDefinition)
)
);

// Check response
expect(response).toEqual(
UpdateServiceDefinitionSuccess
);
done();
})
.catch(e => {
fail(e);
});
});
}); // auth requests
});

0 comments on commit 63c2bc0

Please sign in to comment.