import copy from 'fast-copy' ; import { freezeSys , toPlainObject } from 'contentful-sdk-core' ; import { Stream } from 'stream' ; import enhanceWithMethods from '../enhance-with-methods' ; import { MetaSysProps , DefaultElements , EntityMetaSysProps , MetadataProps , MakeRequest , } from '../common-types' ; import { wrapCollection } from '../common-utils' ; import * as checks from '../plain/checks' ; export type AssetProps = { sys : EntityMetaSysProps ; fields : { /** Title for this asset */ title : { [ key ] : string ; } ; /** Description for this asset */ description ? : { [ key ] : string ; } ; /** File object for this asset */ file : { [ key ] : { fileName : string ; contentType : string ; /** Url where the file is available to be downloaded from, into the Contentful asset system. After the asset is processed this field is gone. */ upload ? : string ; /** Url where the file is available at the Contentful media asset system. This field won't be available until the asset is processed. */ url ? : string ; /** Details for the file, depending on file type (example: image size in bytes, etc) */ details ? : Record < string , any > ; uploadFrom ? : Record < string , any > ; } ; } ; } ; metadata ? : MetadataProps ; } ; export type CreateAssetProps = Omit < AssetProps , 'sys' > ; export interface AssetFileProp { sys : MetaSysProps ; fields : { title : { [ key ] : string ; } ; description : { [ key ] : string ; } ; file : { [ key ] : { file : string | ArrayBuffer | Stream ; contentType : string ; fileName : string ; } ; } ; } ; } export interface AssetProcessingForLocale { processingCheckWait ? : number ; processingCheckRetries ? : number ; } type AssetApi = { param options prop prop return throws example processForAllLocales ( options ? ) : Promise < Asset > ; param locale param options prop prop return throws example processForLocale ( locale , Options ? ) : Promise < Asset > ; return example publish ( ) : Promise < Asset > ; return example archive ( ) : Promise < Asset > ; return example delete ( ) : Promise < void > ; return example unarchive ( ) : Promise < Asset > ; return example unpublish ( ) : Promise < Asset > ; return example update ( ) : Promise < Asset > ; /**
     * Checks if the asset is published. A published asset might have unpublished changes
     */ isPublished ( ) : boolean ; /**
     * Checks if the asset is updated. This means the asset was previously published but has unpublished changes.
     */ isUpdated ( ) : boolean ; /**
     * Checks if the asset is in draft mode. This means it is not published.
     */ isDraft ( ) : boolean ; /**
     * Checks if asset is archived. This means it's not exposed to the Delivery/Preview APIs.
     */ isArchived ( ) : boolean ; } ; export interface Asset extends AssetProps , DefaultElements < AssetProps > , AssetApi { } private function createAssetApi ( makeRequest ) { const getParams = ( raw ) => { return { spaceId : raw . sys . space . sys . id , environmentId : raw . sys . environment . sys . id , assetId : raw . sys . id , } ; } ; return { processForLocale : function processForLocale ( locale , options ? ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'processForLocale' , params : { ... getParams ( raw ) , locale , options , asset : raw , } , } ) . then ( ( data ) => wrapAsset ( makeRequest , data ) ) ; } , processForAllLocales : function processForAllLocales ( options ? ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'processForAllLocales' , params : { ... getParams ( raw ) , asset : raw , options , } , } ) . then ( ( data ) => wrapAsset ( makeRequest , data ) ) ; } , update : function update ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'update' , params : getParams ( raw ) , payload : raw , headers : { } , } ) . then ( ( data ) => wrapAsset ( makeRequest , data ) ) ; } , delete : function del ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'delete' , params : getParams ( raw ) , } ) ; } , publish : function publish ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'publish' , params : getParams ( raw ) , payload : raw , } ) . then ( ( data ) => wrapAsset ( makeRequest , data ) ) ; } , unpublish : function unpublish ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'unpublish' , params : getParams ( raw ) , } ) . then ( ( data ) => wrapAsset ( makeRequest , data ) ) ; } , archive : function archive ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'archive' , params : getParams ( raw ) , } ) . then ( ( data ) => wrapAsset ( makeRequest , data ) ) ; } , unarchive : function unarchive ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return makeRequest ( { entityType : 'Asset' , action : 'unarchive' , params : getParams ( raw ) , } ) . then ( ( data ) => wrapAsset ( makeRequest , data ) ) ; } , isPublished : function isPublished ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return checks . isPublished ( raw ) ; } , isUpdated : function isUpdated ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return checks . isUpdated ( raw ) ; } , isDraft : function isDraft ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return checks . isDraft ( raw ) ; } , isArchived : function isArchived ( ) { const raw = this . toPlainObject ( ) as AssetProps ; return checks . isArchived ( raw ) ; } , } ; } private param makeRequest param data return export function wrapAsset ( makeRequest , data ) { const asset = toPlainObject ( copy ( data ) ) ; const assetWithMethods = enhanceWithMethods ( asset , createAssetApi ( makeRequest ) ) ; return freezeSys ( assetWithMethods ) ; } private export const wrapAssetCollection = wrapCollection ( wrapAsset ) ;