Skip to content

Commit

Permalink
feat(resources): new method to add a new resource to an item
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-items

ISSUES CLOSED: #281
  • Loading branch information
jgravois committed Aug 17, 2018
1 parent be17cab commit 9c63075
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/arcgis-rest-items/src/add.ts
Expand Up @@ -73,3 +73,27 @@ export function addItemData(

return request(url, requestOptions);
}

/**
* Add a resource associated with an item
*
* @param requestOptions - Options for the request
* @returns A Promise to add item resources.
*/
export function addItemResource(
requestOptions: IItemResourceAddRequestOptions
): Promise<IItemResourceResponse> {
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/addResources`;

requestOptions.params = {
file: requestOptions.resource,
fileName: requestOptions.name,
text: requestOptions.content,
...requestOptions.params
};

return request(url, requestOptions);
}
39 changes: 38 additions & 1 deletion packages/arcgis-rest-items/test/add.test.ts
Expand Up @@ -2,7 +2,7 @@ import * as fetchMock from "fetch-mock";

import { attachmentFile } from "../../arcgis-rest-feature-service/test/attachments.test";

import { addItemJsonData, addItemData } from "../src/add";
import { addItemJsonData, addItemData, addItemResource } from "../src/add";

import { ItemSuccessResponse } from "./mocks/item";

Expand Down Expand Up @@ -172,5 +172,42 @@ describe("search", () => {
fail(e);
});
});

it("should add a binary resource to an item", done => {
fetchMock.once("*", {
success: true
});

const file = attachmentFile();

addItemResource({
id: "3ef",
// File() is only available in the browser
resource: file,
name: "thebigkahuna",
...MOCK_USER_REQOPTS
})
.then(() => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/content/users/casey/items/3ef/addResources"
);
expect(options.method).toBe("POST");
expect(options.body instanceof FormData).toBeTruthy();
const params = options.body as FormData;
if (params.get) {
expect(params.get("token")).toEqual("fake-token");
expect(params.get("f")).toEqual("json");
expect(params.get("file")).toEqual(file);
expect(params.get("fileName")).toEqual("thebigkahuna");
}

done();
})
.catch(e => {
fail(e);
});
});
}); // auth requests
});

0 comments on commit 9c63075

Please sign in to comment.