-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service-admin): add updateServiceDefinition method
AFFECTS PACKAGES: @esri/arcgis-rest-service-admin
- Loading branch information
Chase Gruber
committed
Sep 18, 2020
1 parent
27f162f
commit 63c2bc0
Showing
4 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |