From 15d6b4c0d93011b05fb82ca24a1e699698de91a9 Mon Sep 17 00:00:00 2001 From: john gravois Date: Tue, 17 Jul 2018 11:36:17 -0700 Subject: [PATCH] fix(crud): enforce more AGOL rules in item crud operations AFFECTS PACKAGES: @esri/arcgis-rest-items ISSUES CLOSED: #246 --- packages/arcgis-rest-items/src/items.ts | 75 +++++++++++++++++-------- 1 file changed, 51 insertions(+), 24 deletions(-) diff --git a/packages/arcgis-rest-items/src/items.ts b/packages/arcgis-rest-items/src/items.ts index dad4fb7ab3..010cb36b0a 100644 --- a/packages/arcgis-rest-items/src/items.ts +++ b/packages/arcgis-rest-items/src/items.ts @@ -1,5 +1,6 @@ -/* Copyright (c) 2017 Environmental Systems Research Institute, Inc. +/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc. * Apache-2.0 */ + import { request, IRequestOptions, @@ -7,18 +8,25 @@ import { } 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. */ @@ -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`. */ @@ -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 { @@ -131,12 +144,12 @@ export function searchItems( * @param requestOptions = Options for the request */ export function createItemInFolder( - requestOptions: IItemCrudRequestOptions + requestOptions: IItemAddRequestOptions ): Promise { + 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`; @@ -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 { // delegate to createItemInFolder placing in the root of the filestore const options = { folder: null, ...requestOptions - } as IItemCrudRequestOptions; + } as IItemAddRequestOptions; return createItemInFolder(options); } @@ -177,8 +198,7 @@ export function createItem( export function addItemJsonData( requestOptions: IItemJsonDataRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; - + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/update`; @@ -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 { +export function updateItem( + requestOptions: IItemUpdateRequestOptions +): Promise { const url = `${getPortalUrl(requestOptions)}/content/users/${ requestOptions.item.owner }/items/${requestOptions.item.id}/update`; @@ -266,7 +288,7 @@ export function updateItem(requestOptions: IItemRequestOptions): Promise { export function removeItem( requestOptions: IItemIdRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/delete`; @@ -282,7 +304,7 @@ export function removeItem( export function protectItem( requestOptions: IItemIdRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/protect`; @@ -298,7 +320,7 @@ export function protectItem( export function unprotectItem( requestOptions: IItemIdRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/unprotect`; @@ -336,7 +358,7 @@ export function getItemResources( export function updateItemResource( requestOptions: IItemResourceRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/updateResources`; @@ -360,7 +382,7 @@ export function updateItemResource( export function removeItemResource( requestOptions: IItemResourceRequestOptions ): Promise { - const owner = requestOptions.owner || requestOptions.authentication.username; + const owner = determineOwner(requestOptions); const url = `${getPortalUrl(requestOptions)}/content/users/${owner}/items/${ requestOptions.id }/removeResources`; @@ -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 @@ -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; +}