diff --git a/packages/arcgis-rest-service-admin/src/index.ts b/packages/arcgis-rest-service-admin/src/index.ts index 3b8e5c9014..9c66fd4026 100644 --- a/packages/arcgis-rest-service-admin/src/index.ts +++ b/packages/arcgis-rest-service-admin/src/index.ts @@ -1,5 +1,6 @@ export * from "./create"; export * from "./addTo"; +export * from "./update"; export { IFeatureServiceDefinition, IExtent, diff --git a/packages/arcgis-rest-service-admin/src/update.ts b/packages/arcgis-rest-service-admin/src/update.ts new file mode 100644 index 0000000000..3ac96c87ff --- /dev/null +++ b/packages/arcgis-rest-service-admin/src/update.ts @@ -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; +} + +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 { + 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); +} diff --git a/packages/arcgis-rest-service-admin/test/mocks/service.ts b/packages/arcgis-rest-service-admin/test/mocks/service.ts index 70c1742a3a..f15c035339 100644 --- a/packages/arcgis-rest-service-admin/test/mocks/service.ts +++ b/packages/arcgis-rest-service-admin/test/mocks/service.ts @@ -3,6 +3,7 @@ import { ICreateServiceResult } from "../../src/create"; import { IAddToServiceDefinitionResult } from "../../src/addTo"; +import { IUpdateServiceDefinitionResult } from "../../src/update"; export const FeatureServiceResponse: ICreateServiceResult = { encodedServiceURL: @@ -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"] + } +}; diff --git a/packages/arcgis-rest-service-admin/test/update.test.ts b/packages/arcgis-rest-service-admin/test/update.test.ts new file mode 100644 index 0000000000..f3869ba470 --- /dev/null +++ b/packages/arcgis-rest-service-admin/test/update.test.ts @@ -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 +});