Skip to content

Commit

Permalink
fix(createItem): owner > item.owner > authentication.username
Browse files Browse the repository at this point in the history
previously we looked to an explicit owner constructor option and authentication.owner only

AFFECTS PACKAGES:
@esri/arcgis-rest-items

💅

doc improvements
  • Loading branch information
jgravois committed May 10, 2018
1 parent 8df9278 commit 76680a1
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 12 deletions.
15 changes: 7 additions & 8 deletions packages/arcgis-rest-items/src/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface IItemIdRequestOptions extends IUserRequestOptions {
*/
id: string;
/**
* Item owner username (by default authentication session will be used).
* Item owner username. If not present, `authentication.username` is utilized.
*/
owner?: string;
}
Expand All @@ -34,10 +34,6 @@ export interface IItemJsonDataRequestOptions extends IItemIdRequestOptions {
* JSON object to store
*/
data: any;
/**
* Item owner username (by default authentication session will be used).
*/
owner?: string;
}

export interface IItemResourceRequestOptions extends IItemIdRequestOptions {
Expand All @@ -55,11 +51,11 @@ export interface IItemResourceRequestOptions extends IItemIdRequestOptions {
export interface IItemCrudRequestOptions extends IUserRequestOptions {
item: IItem;
/**
* Item owner username (by default authentication session will be used).
* The owner of the item. If this property is not present, `item.owner` will be passed, or lastly `authentication.username`.
*/
owner?: string;
/**
* Optional folder to house the item
* Folder to house the item.
*/
folder?: string;
}
Expand Down Expand Up @@ -135,7 +131,10 @@ export function searchItems(
export function createItemInFolder(
requestOptions: IItemCrudRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const owner =
requestOptions.owner ||
requestOptions.item.owner ||
requestOptions.authentication.username;
const baseUrl = `${getPortalUrl(requestOptions)}/content/users/${owner}`;
let url = `${baseUrl}/addItem`;

Expand Down
93 changes: 92 additions & 1 deletion packages/arcgis-rest-items/test/items.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ describe("search", () => {
}
}
};
// why not just use item.owner??
createItem({
item: fakeItem,
owner: "dbouwman",
Expand Down Expand Up @@ -249,6 +248,98 @@ describe("search", () => {
});
});

it("should create an item without an explicit owner", done => {
fetchMock.once("*", ItemSuccessResponse);
const fakeItem = {
title: "my fake item",
owner: "dbouwman",
description: "yep its fake",
snipped: "so very fake",
type: "Web Mapping Application",
typeKeywords: ["fake", "kwds"],
tags: ["fakey", "mcfakepants"],
properties: {
key: "somevalue"
}
};
createItem({
item: fakeItem,
...MOCK_USER_REQOPTS
})
.then(response => {
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://myorg.maps.arcgis.com/sharing/rest/content/users/dbouwman/addItem"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
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("typeKeywords", "fake, kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
);
expect(options.body).toContain(
encodeParam("properties", JSON.stringify(fakeItem.properties))
);

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

it("should create an item with only a username from auth", done => {
fetchMock.once("*", ItemSuccessResponse);
const fakeItem = {
title: "my fake item",
description: "yep its fake",
snipped: "so very fake",
type: "Web Mapping Application",
typeKeywords: ["fake", "kwds"],
tags: ["fakey", "mcfakepants"],
properties: {
key: "somevalue"
}
};
// why not just use item.owner??
createItem({
item: fakeItem,
...MOCK_USER_REQOPTS
})
.then(response => {
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/addItem"
);
expect(options.method).toBe("POST");
expect(options.body).toContain("f=json");
expect(options.body).toContain(encodeParam("token", "fake-token"));
// expect(options.body).toContain("owner=casey");
// ensure the array props are serialized into strings
expect(options.body).toContain(
encodeParam("typeKeywords", "fake, kwds")
);
expect(options.body).toContain(
encodeParam("tags", "fakey, mcfakepants")
);
expect(options.body).toContain(
encodeParam("properties", JSON.stringify(fakeItem.properties))
);

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

it("should create an item in a folder", done => {
fetchMock.once("*", ItemSuccessResponse);
const fakeItem = {
Expand Down
6 changes: 3 additions & 3 deletions packages/arcgis-rest-request/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ export interface IRequestOptions {
authentication?: IAuthenticationManager;

/**
* Base url for the portal you want to make the request to. Defaults to 'https://www.arcgis.com/sharing/rest'
* Base url for the portal you want to make the request to. Defaults to 'https://www.arcgis.com/sharing/rest'.
*/
portal?: string;

/**
* The implementation of `fetch` to use. Defaults to a global `fetch`
* The implementation of `fetch` to use. Defaults to a global `fetch`.
*/
fetch?: (input: RequestInfo, init?: RequestInit) => Promise<Response>;

/**
* If the length of a GET request's URL exceeds `maxUrlLength` the request will use POST instead
* If the length of a GET request's URL exceeds `maxUrlLength` the request will use POST instead.
*/
maxUrlLength?: number;
}
Expand Down

0 comments on commit 76680a1

Please sign in to comment.