Skip to content

Commit

Permalink
fix: encode item extent prior to calling request
Browse files Browse the repository at this point in the history
  • Loading branch information
dbouwman committed Sep 22, 2022
1 parent d0dcbcb commit 135600c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
12 changes: 11 additions & 1 deletion packages/arcgis-rest-portal/src/items/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
IAddFolderResponse,
IUpdateItemResponse,
ICreateUpdateItemOptions,
determineOwner
determineOwner,
isBBox,
bboxToString
} from "./helpers.js";

export interface ICreateFolderOptions extends ICreateUpdateItemOptions {
Expand Down Expand Up @@ -99,6 +101,14 @@ export function createItemInFolder(
...requestOptions.item
};

// convert extent, if present, into a string from bbox
// processParams was previously doing this sort of work,
// however now we need to let array of arrays through
// Thus for extents we need to move this logic here
if (requestOptions.params.extent && isBBox(requestOptions.params.extent)) {
requestOptions.params.extent = bboxToString(requestOptions.params.extent);
}

// serialize the item into something Portal will accept
const options = appendCustomParams<ICreateItemOptions>(
requestOptions,
Expand Down
23 changes: 23 additions & 0 deletions packages/arcgis-rest-portal/src/items/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,26 @@ export function determineOwner(requestOptions: any): Promise<string> {
);
}
}

/**
* checks if the extent is a valid BBox (2 element array of coordinate pair arrays)
* @param extent
* @returns
*/
export function isBBox(extent: unknown): boolean {
return (
Array.isArray(extent) &&
Array.isArray(extent[0]) &&
Array.isArray(extent[1])
);
}

/**
* Given a Bbox, convert it to a string. Some api endpoints expect a string
*
* @param {BBox} extent
* @return {*} {string}
*/
export function bboxToString(extent: number[][]): string {
return extent.join(",");
}
12 changes: 11 additions & 1 deletion packages/arcgis-rest-portal/src/items/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
IItemInfoResponse,
IItemResourceResponse,
IUpdateItemResponse,
determineOwner
determineOwner,
isBBox,
bboxToString
} from "./helpers.js";

export interface IUpdateItemOptions extends ICreateUpdateItemOptions {
Expand Down Expand Up @@ -69,6 +71,14 @@ export function updateItem(
...requestOptions.item
};

// convert extent, if present, into a string from bbox
// processParams was previously doing this sort of work,
// however now we need to let array of arrays through
// Thus for extents we need to move this logic here
if (requestOptions.params.extent && isBBox(requestOptions.params.extent)) {
requestOptions.params.extent = bboxToString(requestOptions.params.extent);
}

return request(url, requestOptions);
});
}
Expand Down
7 changes: 6 additions & 1 deletion packages/arcgis-rest-portal/test/items/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ describe("search", () => {
type: "Web Mapping Application",
typeKeywords: ["fake", "kwds"],
tags: ["fakey", "mcfakepants"],
extent: [
[1, 2],
[3, 4]
],
properties: {
key: "somevalue"
},
Expand All @@ -73,6 +77,7 @@ describe("search", () => {
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain("owner=dbouwman");
// ensure the array props are serialized into strings
expect(options.body).toContain(encodeParam("extent", "1,2,3,4"));
expect(options.body).toContain(
encodeParam("typeKeywords", "fake,kwds")
);
Expand Down Expand Up @@ -383,7 +388,7 @@ describe("search", () => {
fetchMock.post("*", () => 200);
createItemInFolder({
item: fakeItem,
file: new File(["some text"], undefined, {
file: new File(["some text"], undefined as unknown as string, {
type: "text/html"
}),
multipart: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/arcgis-rest-portal/test/items/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ describe("search", () => {
type: "Web Mapping Application",
typeKeywords: ["fake", "kwds"],
tags: ["fakey", "mcfakepants"],
extent: [
[1, 2],
[3, 4]
],
properties: {
key: "somevalue"
},
Expand All @@ -74,6 +78,7 @@ describe("search", () => {
expect(options.body).toContain(encodeParam("token", "fake-token"));
expect(options.body).toContain(encodeParam("owner", "dbouwman"));
// ensure the array props are serialized into strings
expect(options.body).toContain(encodeParam("extent", "1,2,3,4"));
expect(options.body).toContain(
encodeParam("typeKeywords", "fake,kwds")
);
Expand Down

0 comments on commit 135600c

Please sign in to comment.