import { getItem } from "@esri/arcgis-rest-portal" ; import { IHubContent , cloneObject , datasetToContent , hubApiRequest , mergeObjects , } from "@esri/hub-common" ; import { isSlug , addContextToSlug , parseDatasetId } from "./slugs" ; import { enrichContent , IEnrichContentOptions } from "./enrichments" ; export { datasetToContent , datasetToItem , DatasetResource , } from "@esri/hub-common" ; export interface IGetContentOptions extends IEnrichContentOptions { siteOrgKey ? : string ; } const itemOverrides = [ "contentStatus" , "spatialReference" , "accessInformation" , "proxyFilter" , "appCategories" , "industries" , "languages" , "largeThumbnail" , "banner" , "screenshots" , "listed" , "ownerFolder" , "protected" , "commentsEnabled" , "numComments" , "numRatings" , "avgRating" , "numViews" , "itemControl" , "scoreCompleteness" , ] ; param identifier param options export export function getContentFromHub ( identifier , options ? ) { let request ; if ( isSlug ( identifier ) ) { const slug = addContextToSlug ( identifier , options && options . siteOrgKey ) ; const opts = cloneObject ( options ) ; opts . params = { ... opts . params , "filter[slug]" : slug } ; request = hubApiRequest ( `/datasets` , opts ) . then ( ( resp ) => resp && resp . data [ 0 ] ) ; } else { request = hubApiRequest ( `/datasets/${ identifier }` , options ) . then ( ( resp ) => resp && resp . data ) ; } return request . then ( ( dataset ) => { if ( dataset && options . authentication ) { return getItem ( parseDatasetId ( dataset . id ) . itemId , options ) . then ( ( item ) => { dataset . attributes = mergeObjects ( item , dataset . attributes , itemOverrides ) ; return dataset ; } ) ; } else { return dataset ; } } ) . then ( ( dataset ) => { const content = dataset && datasetToContent ( dataset ) ; return content && enrichContent ( content , options ) ; } ) ; }