Skip to content

Commit

Permalink
fix(crud): enforce more AGOL rules in item crud operations
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-items

ISSUES CLOSED: #246
  • Loading branch information
jgravois committed Jul 17, 2018
1 parent 8c40b72 commit 15d6b4c
Showing 1 changed file with 51 additions and 24 deletions.
75 changes: 51 additions & 24 deletions packages/arcgis-rest-items/src/items.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
/* Copyright (c) 2017 Environmental Systems Research Institute, Inc.
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import {
request,
IRequestOptions,
getPortalUrl
} from "@esri/arcgis-rest-request";

import { IItem, IPagingParams } from "@esri/arcgis-rest-common-types";
import { UserSession } from "@esri/arcgis-rest-auth";

interface IItemAdd extends IItem {
title: string;
type: string;
}

import { IUserRequestOptions } from "@esri/arcgis-rest-auth";
interface IItemUpdate extends IItem {
id: string;
}

export interface IItemRequestOptions extends IUserRequestOptions {
export interface IItemRequestOptions extends IRequestOptions {
item: IItem;
}

// * @param id - Item Id
// * @param owner - Item owner username
// * @param data - Javascript object to store

export interface IItemIdRequestOptions extends IUserRequestOptions {
export interface IItemIdRequestOptions extends IRequestOptions {
/**
* Unique identifier of the item.
*/
Expand Down Expand Up @@ -48,8 +56,7 @@ export interface IItemResourceRequestOptions extends IItemIdRequestOptions {
resource?: string;
}

export interface IItemCrudRequestOptions extends IUserRequestOptions {
item: IItem;
interface IItemCrudRequestOptions extends IRequestOptions {
/**
* The owner of the item. If this property is not present, `item.owner` will be passed, or lastly `authentication.username`.
*/
Expand All @@ -60,12 +67,18 @@ export interface IItemCrudRequestOptions extends IUserRequestOptions {
folder?: string;
}

export interface IItemAddRequestOptions extends IItemCrudRequestOptions {
item: IItemAdd;
}

export interface IItemUpdateRequestOptions extends IItemCrudRequestOptions {
item: IItemUpdate;
}

// this interface still needs to be docced
export interface ISearchRequest extends IPagingParams {
q: string;
[key: string]: any;
// start: number;
// num: number;
}

export interface ISearchRequestOptions extends IRequestOptions {
Expand Down Expand Up @@ -131,12 +144,12 @@ export function searchItems(
* @param requestOptions = Options for the request
*/
export function createItemInFolder(
requestOptions: IItemCrudRequestOptions
requestOptions: IItemAddRequestOptions
): Promise<any> {
const session = requestOptions.authentication as UserSession;
const owner =
requestOptions.owner ||
requestOptions.item.owner ||
requestOptions.authentication.username;
requestOptions.owner || requestOptions.item.owner || session.username;

const baseUrl = `${getPortalUrl(requestOptions)}/content/users/${owner}`;
let url = `${baseUrl}/addItem`;

Expand All @@ -156,16 +169,24 @@ export function createItemInFolder(
/**
* Create an Item in the user's root folder
*
* createItem({
* authentication: userSession,
* item: {
* title: "The Amazing Voyage",
* type: "Webmap"
* }
* })
*
* @param requestOptions - Options for the request
*/
export function createItem(
requestOptions: IItemCrudRequestOptions
requestOptions: IItemAddRequestOptions
): Promise<any> {
// delegate to createItemInFolder placing in the root of the filestore
const options = {
folder: null,
...requestOptions
} as IItemCrudRequestOptions;
} as IItemAddRequestOptions;
return createItemInFolder(options);
}

Expand All @@ -177,8 +198,7 @@ export function createItem(
export function addItemJsonData(
requestOptions: IItemJsonDataRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;

const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/update`;
Expand Down Expand Up @@ -243,7 +263,9 @@ export function getItemData(
* @param requestOptions - Options for the request.
* @returns A Promise that resolves with the status of the operation.
*/
export function updateItem(requestOptions: IItemRequestOptions): Promise<any> {
export function updateItem(
requestOptions: IItemUpdateRequestOptions
): Promise<any> {
const url = `${getPortalUrl(requestOptions)}/content/users/${
requestOptions.item.owner
}/items/${requestOptions.item.id}/update`;
Expand All @@ -266,7 +288,7 @@ export function updateItem(requestOptions: IItemRequestOptions): Promise<any> {
export function removeItem(
requestOptions: IItemIdRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/delete`;
Expand All @@ -282,7 +304,7 @@ export function removeItem(
export function protectItem(
requestOptions: IItemIdRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/protect`;
Expand All @@ -298,7 +320,7 @@ export function protectItem(
export function unprotectItem(
requestOptions: IItemIdRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/unprotect`;
Expand Down Expand Up @@ -336,7 +358,7 @@ export function getItemResources(
export function updateItemResource(
requestOptions: IItemResourceRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/updateResources`;
Expand All @@ -360,7 +382,7 @@ export function updateItemResource(
export function removeItemResource(
requestOptions: IItemResourceRequestOptions
): Promise<any> {
const owner = requestOptions.owner || requestOptions.authentication.username;
const owner = determineOwner(requestOptions);
const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${
requestOptions.id
}/removeResources`;
Expand All @@ -384,7 +406,7 @@ function serializeItem(item: IItem): any {
// create a clone so we're not messing with the original
const clone = JSON.parse(JSON.stringify(item));
// join keywords and tags...
const { typeKeywords=[], tags=[] } = item;
const { typeKeywords = [], tags = [] } = item;
clone.typeKeywords = typeKeywords.join(", ");
clone.tags = tags.join(", ");
// convert .data to .text
Expand All @@ -398,3 +420,8 @@ function serializeItem(item: IItem): any {
}
return clone;
}

function determineOwner(requestOptions: IItemIdRequestOptions): string {
const session = requestOptions.authentication as UserSession;
return requestOptions.owner || session.username;
}

0 comments on commit 15d6b4c

Please sign in to comment.