import { request , appendCustomParams } from "@esri/arcgis-rest-request" ; import { IItemAdd } from "@esri/arcgis-rest-types" ; import { getPortalUrl } from "../util/get-portal-url" ; import { IAddFolderResponse , IUpdateItemResponse , ICreateUpdateItemOptions , serializeItem , determineOwner } from "./helpers" ; export interface ICreateFolderOptions extends ICreateUpdateItemOptions { /**
     * Name of the folder to create.
     */ title : string ; } export interface ICreateItemOptions extends ICreateUpdateItemOptions { item : IItemAdd ; } export interface ICreateItemResponse extends IUpdateItemResponse { folder : string ; } param requestOptions returns export function createFolder ( requestOptions ) { return determineOwner ( requestOptions ) . then ( owner => { const baseUrl = `${ getPortalUrl ( requestOptions ) }/content/users/${ owner }` ; const url = `${ baseUrl }/createFolder` ; requestOptions . params = { title : requestOptions . title , ... requestOptions . params } ; return request ( url , requestOptions ) ; } ) ; } param requestOptions export function createItemInFolder ( requestOptions ) { if ( requestOptions . multipart && ! requestOptions . filename ) { return Promise . reject ( new Error ( "The filename is required for a multipart request." ) ) ; } return determineOwner ( requestOptions ) . then ( owner => { const baseUrl = `${ getPortalUrl ( requestOptions ) }/content/users/${ owner }` ; let url = `${ baseUrl }/addItem` ; if ( requestOptions . folderId ) { url = `${ baseUrl }/${ requestOptions . folderId }/addItem` ; } requestOptions . params = { ... requestOptions . params , ... serializeItem ( requestOptions . item ) } ; const options = appendCustomParams < ICreateItemOptions > ( requestOptions , [ "owner" , "folderId" , "file" , "dataUrl" , "text" , "async" , "multipart" , "filename" , "overwrite" ] , { params : { ... requestOptions . params } } ) ; return request ( url , options ) ; } ) ; } param requestOptions returns export function createItem ( requestOptions ) { const options = { folderId : null , ... requestOptions } as ICreateItemOptions ; return createItemInFolder ( options ) ; }