import type { AxiosInstance } from 'contentful-sdk-core' ; import { errorHandler } from 'contentful-sdk-core' ; import copy from 'fast-copy' ; import { SetOptional } from 'type-fest' ; import { CollectionProp , GetSpaceEnvironmentParams , QueryParams } from '../../../common-types' ; import { AssetFileProp , AssetProcessingForLocale , AssetProps , CreateAssetProps , } from '../../../entities/asset' ; import { getUploadHttpClient } from '../../../upload-http-client' ; import { RestEndpoint } from '../types' ; import * as raw from './raw' ; import { create as createUpload } from './upload' ; import { normalizeSelect } from './utils' ; export const get = ( http , params , rawData ? , headers ? ) => { return raw . get < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }` , { params : normalizeSelect ( params . query ) , headers : { ... headers } , } ) ; } ; export const getMany = ( http , params ) => { return raw . get < CollectionProp < AssetProps > > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets` , { params : normalizeSelect ( params . query ) , } ) ; } ; export const update = ( http , params , rawData , headers ? ) => { const data = copy ( rawData ) ; delete data . sys ; return raw . put < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }` , data , { headers : { 'X-Contentful-Version' : rawData . sys . version ?? 0 , ... headers , } , } ) ; } ; export const del = ( http , params ) => { return raw . del ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }` ) ; } ; export const publish = ( http , params , rawData ) => { return raw . put < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }/published` , null , { headers : { 'X-Contentful-Version' : rawData . sys . version ?? 0 , } , } ) ; } ; export const unpublish = ( http , params ) => { return raw . del < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }/published` ) ; } ; export const archive = ( http , params ) => { return raw . put < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }/archived` ) ; } ; export const unarchive = ( http , params ) => { return raw . del < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }/archived` ) ; } ; export const create = ( http , params , rawData ) => { const data = copy ( rawData ) ; return raw . post < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets` , data ) ; } ; export const createWithId = ( http , params , rawData ) => { const data = copy ( rawData ) ; return raw . put < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ params . assetId }` , data ) ; } ; export const createFromFiles = ( http , params , data ) => { const httpUpload = getUploadHttpClient ( http ) ; const { file } = data . fields ; return Promise . all ( Object . keys ( file ) . map ( ( locale ) => { const { contentType , fileName } = file [ locale ] ; return createUpload ( httpUpload , params , file [ locale ] ) . then ( ( upload ) => { return { [ locale ] : { contentType , fileName , uploadFrom : { sys : { type : 'Link' , linkType : 'Upload' , id : upload . sys . id , } , } , } , } ; } ) ; } ) ) . then ( ( uploads ) => { const file = uploads . reduce ( ( fieldsData , upload ) => ( { ... fieldsData , ... upload } ) , { } ) ; const asset = { ... data , fields : { ... data . fields , file , } , } ; return create ( http , params , asset ) ; } ) . catch ( errorHandler ) ; } ; /**
 * Asset processing
 */ const ASSET_PROCESSING_CHECK_WAIT = 3000 ; const ASSET_PROCESSING_CHECK_RETRIES = 10 ; function checkIfAssetHasUrl ( http , params , { resolve , reject , locale , processingCheckWait = ASSET_PROCESSING_CHECK_WAIT , processingCheckRetries = ASSET_PROCESSING_CHECK_RETRIES , checkCount = 0 , } ) { return get ( http , params ) . then ( ( asset ) => { if ( asset . fields . file [ locale ] . url ) { resolve ( asset ) ; } else if ( checkCount === processingCheckRetries ) { const error = new Error ( ) ; error . name = 'AssetProcessingTimeout' ; error . message = 'Asset is taking longer then expected to process.' ; reject ( error ) ; } else { checkCount ++ ; setTimeout ( ( ) => checkIfAssetHasUrl ( http , params , { resolve : resolve , reject : reject , locale : locale , checkCount : checkCount , processingCheckWait , processingCheckRetries , } ) , processingCheckWait ) ; } } ) ; } export const processForLocale = ( http , { asset , locale , options : { processingCheckRetries , processingCheckWait } = { } , ... params } ) => { return raw . put < AssetProps > ( http , `/spaces/${ params . spaceId }/environments/${ params . environmentId }/assets/${ asset . sys . id }/files/${ locale }/process` , null , { headers : { 'X-Contentful-Version' : asset . sys . version , } , } ) . then ( ( ) => { return new Promise < AssetProps > ( ( resolve , reject ) => checkIfAssetHasUrl ( http , { spaceId : params . spaceId , environmentId : params . environmentId , assetId : asset . sys . id , } , { resolve , reject , locale , processingCheckWait , processingCheckRetries , } ) ) ; } ) ; } ; export const processForAllLocales = ( http , { asset , options = { } , ... params } ) => { const locales = Object . keys ( asset . fields . file || { } ) ; let mostUpToDateAssetVersion = asset ; const allProcessingLocales = locales . map ( ( locale ) => processForLocale ( http , { ... params , asset , locale , options } ) . then ( ( result ) => { mostUpToDateAssetVersion = result ; } ) ) ; return Promise . all ( allProcessingLocales ) . then ( ( ) => mostUpToDateAssetVersion ) ; } ;