Skip to content

Commit

Permalink
fix(add resource): make resource an optional parameter
Browse files Browse the repository at this point in the history
Resources can be added with just text content, so the resource parameter should be optional.

AFFECTS PACKAGES:
@esri/arcgis-rest-items
  • Loading branch information
noahmulfinger committed Apr 10, 2019
1 parent b6c40ef commit 1db78b7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/arcgis-rest-items/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export interface IItemDataAddRequestOptions extends IItemIdRequestOptions {
data: any;
}

/**
* Deprecated. Please use `IItemResourceRequestOptions` instead.
*/
export interface IItemResourceAddRequestOptions
extends IItemResourceRequestOptions {
/**
Expand Down Expand Up @@ -136,22 +139,32 @@ export function addItemRelationship(
/**
* ```js
* import { addItemResource } from '@esri/arcgis-rest-items';
* //
*
* // Add a file resource
* addItemResource({
* id: '3ef',
* resource: file,
* name: 'bigkahuna.jpg',
* authentication
* })
* .then(response)
*
* // Add a text resource
* addItemResource({
* id: '4fg',
* content: "Text content",
* name: 'bigkahuna.txt',
* authentication
* })
* .then(response)
* ```
* Add a resource associated with an item. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/add-resources.htm) for more information.
*
* @param requestOptions - Options for the request
* @returns A Promise to add item resources.
*/
export function addItemResource(
requestOptions: IItemResourceAddRequestOptions
requestOptions: IItemResourceRequestOptions
): Promise<IItemResourceResponse> {
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
Expand Down
3 changes: 3 additions & 0 deletions packages/arcgis-rest-items/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export interface IItemResourceRequestOptions extends IItemIdRequestOptions {
* Controls whether access to the file resource is restricted to the owner or inherited from the sharing permissions set for the associated item.
*/
private?: boolean;
/**
* Object to store
*/
resource?: any;
}

Expand Down
30 changes: 30 additions & 0 deletions packages/arcgis-rest-items/test/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,5 +286,35 @@ describe("search", () => {
fail(e);
});
});

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

addItemResource({
id: "3ef",
content: "Text content",
name: "thebigkahuna.txt",
...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).toContain("f=json");
expect(options.body).toContain("text=Text%20content");
expect(options.body).toContain("fileName=thebigkahuna");
expect(options.body).toContain(encodeParam("token", "fake-token"));

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

0 comments on commit 1db78b7

Please sign in to comment.