import { request , IRequestOptions , appendCustomParams } from "@esri/arcgis-rest-request" ; import { IItem , IGroup } from "@esri/arcgis-rest-types" ; import { getPortalUrl } from "../util/get-portal-url" ; import { scrubControlChars } from '../util/scrub-control-chars' ; import { IItemDataOptions , IItemRelationshipOptions , IUserItemOptions , determineOwner , FetchReadMethodName } from "./helpers" ; param id param requestOptions returns export function getItem ( id , requestOptions ? ) { const url = getItemBaseUrl ( id , requestOptions ) ; const options = { ... { httpMethod : "GET" } , ... requestOptions } ; return request ( url , options ) ; } param id param portalUrlOrRequestOptions returns export const getItemBaseUrl = ( id , portalUrlOrRequestOptions ? ) => { const portalUrl = typeof portalUrlOrRequestOptions === "string" ? portalUrlOrRequestOptions : getPortalUrl ( portalUrlOrRequestOptions ) ; return `${ portalUrl }/content/items/${ id }` ; } ; param id param requestOptions returns export function getItemData ( id , requestOptions ? ) { const url = `${ getItemBaseUrl ( id , requestOptions ) }/data` ; const options = { ... { httpMethod : "GET" , params : { } } , ... requestOptions } ; if ( options . file ) { options . params . f = null ; } return request ( url , options ) . catch ( err => { const emptyResponseErr = RegExp ( /The string did not match the expected pattern|(Unexpected end of (JSON input|data at line 1 column 1))/i ) ; if ( emptyResponseErr . test ( err . message ) ) { return ; } else throw err ; } ) ; } export interface IGetRelatedItemsResponse { total : number ; relatedItems : IItem [ ] ; } param requestOptions returns export function getRelatedItems ( requestOptions ) { const url = `${ getItemBaseUrl ( requestOptions . id , requestOptions ) }/relatedItems` ; const options = { httpMethod : "GET" , params : { direction : requestOptions . direction } , ... requestOptions } ; if ( typeof requestOptions . relationshipType === "string" ) { options . params . relationshipType = requestOptions . relationshipType ; } else { options . params . relationshipTypes = requestOptions . relationshipType ; } delete options . direction ; delete options . relationshipType ; return request ( url , options ) ; } param requestOptions returns export function getItemResources ( id , requestOptions ? ) { const url = `${ getItemBaseUrl ( id , requestOptions ) }/resources` ; const options = { ... requestOptions } ; options . params = { num : 1000 , ... options . params } ; return request ( url , options ) ; } export interface IGetItemGroupsResponse { admin ? : IGroup [ ] ; member ? : IGroup [ ] ; other ? : IGroup [ ] ; } export interface IGetItemResourceOptions extends IRequestOptions { /**
     * Name of the info file, optionally including the folder path
     */ fileName : string ; /**
     * How the fetch response should be read, see:
     * https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods
     */ readAs ? : FetchReadMethodName ; } param { string } itemId param { IGetItemResourceOptions } requestOptions export function getItemResource ( itemId , requestOptions ) { const readAs = requestOptions . readAs || 'blob' ; return getItemFile ( itemId , `/resources/${ requestOptions . fileName }` , readAs , requestOptions ) ; } param id param requestOptions returns export function getItemGroups ( id , requestOptions ? ) { const url = `${ getItemBaseUrl ( id , requestOptions ) }/groups` ; return request ( url , requestOptions ) ; } export interface IItemStatusOptions extends IUserItemOptions { /**
     * The type of asynchronous job for which the status has to be checked. Default is none, which check the item's status.
     */ jobType ? : "publish" | "generateFeatures" | "export" | "createService" ; /**
     * The job ID returned during publish, generateFeatures, export, and createService calls.
     */ jobId ? : string ; /**
     * The response format. The default and the only response format for this resource is HTML.
     */ format ? : "html" ; } export interface IGetItemStatusResponse { status : "partial" | "processing" | "failed" | "completed" ; statusMessage : string ; itemId : string ; } param id param requestOptions returns export function getItemStatus ( requestOptions ) { return determineOwner ( requestOptions ) . then ( owner => { const url = `${ getPortalUrl ( requestOptions ) }/content/users/${ owner }/items/${ requestOptions . id }/status` ; const options = appendCustomParams < IItemStatusOptions > ( requestOptions , [ "jobId" , "jobType" ] , { params : { ... requestOptions . params } } ) ; return request ( url , options ) ; } ) ; } export interface IGetItemPartsResponse { parts : number [ ] ; } param id param requestOptions returns export function getItemParts ( requestOptions ) { return determineOwner ( requestOptions ) . then ( owner => { const url = `${ getPortalUrl ( requestOptions ) }/content/users/${ owner }/items/${ requestOptions . id }/parts` ; return request ( url , requestOptions ) ; } ) ; } export interface IGetItemInfoOptions extends IRequestOptions { /**
     * Name of the info file, optionally including the folder path
     */ fileName ? : string ; /**
     * How the fetch response should be read, see:
     * https://developer.mozilla.org/en-US/docs/Web/API/Body#Methods
     */ readAs ? : FetchReadMethodName ; } param id param requestOptions returns export function getItemInfo ( id , requestOptions ? ) { const { fileName = "iteminfo.xml" , readAs = "text" } = requestOptions || { } ; const options = { httpMethod : "GET" , ... requestOptions } ; return getItemFile ( id , `/info/${ fileName }` , readAs , options ) ; } param id param requestOptions returns export function getItemMetadata ( id , requestOptions ? ) { const options = { ... requestOptions , fileName : "metadata/metadata.xml" } as IGetItemInfoOptions ; return getItemInfo ( id , options ) ; } function getItemFile ( id , fileName , readMethod , requestOptions ? ) { const url = `${ getItemBaseUrl ( id , requestOptions ) }${ fileName }` ; const options = { params : { } , ... requestOptions } ; const justReturnResponse = options . rawResponse ; options . rawResponse = true ; options . params . f = null ; return request ( url , options ) . then ( response => { if ( justReturnResponse ) { return response ; } return readMethod !== 'json' ? response [ readMethod ] ( ) : response . text ( ) . then ( ( text ) => JSON . parse ( scrubControlChars ( text ) ) ) ; } ) ; }