Skip to content

Commit

Permalink
fix: ensure JSON and binary data are both handled consistently
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-portal

BREAKING CHANGE:
deprecated addItemJsonData
  • Loading branch information
jgravois committed Apr 19, 2019
1 parent 36540c5 commit 9f85087
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 56 deletions.
57 changes: 11 additions & 46 deletions packages/arcgis-rest-portal/src/items/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
determineOwner,
IManageItemRelationshipOptions
} from "./helpers";
import { updateItem, IUpdateItemOptions } from "./update";

export interface IAddItemDataOptions extends IUserItemOptions {
/**
Expand All @@ -20,42 +21,6 @@ export interface IAddItemDataOptions extends IUserItemOptions {
data: any;
}

/**
* ```js
* import { addItemJsonData } from "@esri/arcgis-rest-portal";
* //
* addItemJsonData({
* id: '3ef',
* data: {}
* authentication
* })
* .then(response)
* ```
* Send json to an item to be stored as the `/data` resource. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/update-item.htm) for more information.
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with an object reporting
* success/failure and echoing the item id.
*/
export function addItemJsonData(
requestOptions: IAddItemDataOptions
): Promise<IUpdateItemResponse> {
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/update`;

// Portal API requires that the 'data' be stringified and POSTed in
// a `text` form field. It can also be sent with the `.create` call by sending
// a `.data` property.
requestOptions.params = {
text: requestOptions.data,
...requestOptions.params
};

return request(url, requestOptions);
}

/**
* ```js
* import { addItemData } from "@esri/arcgis-rest-portal";
Expand All @@ -77,18 +42,18 @@ export function addItemData(
requestOptions: IAddItemDataOptions
): Promise<IUpdateItemResponse> {
const owner = determineOwner(requestOptions);

const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/update`;

// Portal API requires that the 'data' be POSTed in a `file` form field.
requestOptions.params = {
file: requestOptions.data,
...requestOptions.params
const options: any = {
item: {
id: requestOptions.id,
data: requestOptions.data
},
...requestOptions
};

return request(url, requestOptions);
delete options.id;
delete options.data;

return updateItem(options as IUpdateItemOptions);
}

/**
Expand Down
10 changes: 7 additions & 3 deletions packages/arcgis-rest-portal/src/items/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,16 @@ export function serializeItem(item: IItemAdd | IItemUpdate | IItem): any {
// create a clone so we're not messing with the original
const clone = JSON.parse(JSON.stringify(item));

// convert .data to .text
// binary data needs POSTed as a `file`
// JSON object literals should be passed as `text`.
if (clone.data) {
clone.text = clone.data;
(typeof Blob !== "undefined" && item.data instanceof Blob) ||
// Node.js doesn't implement Blob
item.data.constructor.name === "ReadStream"
? (clone.file = item.data)
: (clone.text = item.data);
delete clone.data;
}

return clone;
}

Expand Down
15 changes: 8 additions & 7 deletions packages/arcgis-rest-portal/test/items/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import * as fetchMock from "fetch-mock";
import { attachmentFile } from "../../../arcgis-rest-feature-layer/test/attachments.test";

import {
addItemJsonData,
addItemData,
addItemResource,
addItemRelationship
Expand Down Expand Up @@ -47,8 +46,8 @@ describe("search", () => {
key: "someValue"
}
};
// addItemJsonData("3ef", "dbouwman", fakeData, MOCK_REQOPTS)
addItemJsonData({
// addItemData("3ef", "dbouwman", fakeData, MOCK_REQOPTS)
addItemData({
id: "3ef",
owner: "dbouwman",
data: fakeData,
Expand Down Expand Up @@ -82,8 +81,8 @@ describe("search", () => {
key: "someValue"
}
};
// addItemJsonData("3ef", "dbouwman", fakeData, MOCK_REQOPTS)
addItemJsonData({
// addItemData("3ef", "dbouwman", fakeData, MOCK_REQOPTS)
addItemData({
id: "3ef",
data: fakeData,
...MOCK_USER_REQOPTS
Expand Down Expand Up @@ -116,8 +115,8 @@ describe("search", () => {
key: "someValue"
}
};
// addItemJsonData("3ef", "dbouwman", fakeData, MOCK_REQOPTS)
addItemJsonData({
// addItemData("3ef", "dbouwman", fakeData, MOCK_REQOPTS)
addItemData({
id: "3ef",
data: fakeData,
...MOCK_USER_REQOPTS,
Expand Down Expand Up @@ -154,6 +153,7 @@ describe("search", () => {
const file = attachmentFile();

addItemData({
// this would work on item: { type: "Code Sample" }
id: "3ef",
// File() is only available in the browser
data: file,
Expand All @@ -167,6 +167,7 @@ describe("search", () => {
);
expect(options.method).toBe("POST");
expect(options.body instanceof FormData).toBeTruthy();
// to do: figure out how to inspect these parameters from Node.js
const params = options.body as FormData;
if (params.get) {
expect(params.get("token")).toEqual("fake-token");
Expand Down

0 comments on commit 9f85087

Please sign in to comment.