import type { OpPatch } from 'json-patch' ; import { MakeRequest } from './common-types' ; import { CreateCommentProps } from './entities/comment' ; import { Entry , EntryProps , EntryReferenceOptionsProps } from './entities/entry' ; import { CreateTaskProps } from './entities/task' ; import * as checks from './plain/checks' ; import entities from './entities' ; private export type ContentfulEntryApi = ReturnType < typeof createEntryApi > ; private export default function createEntryApi ( makeRequest ) { const { wrapEntry , wrapEntryCollection } = entities . entry ; const { wrapSnapshot , wrapSnapshotCollection } = entities . snapshot ; const { wrapTask , wrapTaskCollection } = entities . task ; const { wrapComment , wrapCommentCollection } = entities . comment ; const getParams = ( self ) => { const entry = self . toPlainObject ( ) as EntryProps ; return { params : { spaceId : entry . sys . space . sys . id , environmentId : entry . sys . environment . sys . id , entryId : entry . sys . id , } , raw : entry , } ; } ; return { return example update : function update ( ) { const { raw , params } = getParams ( this ) ; return makeRequest ( { entityType : 'Entry' , action : 'update' , params , payload : raw , } ) . then ( ( data ) => wrapEntry ( makeRequest , data ) ) ; } , return example patch : function patch ( ops ) { const { raw , params } = getParams ( this ) ; return makeRequest ( { entityType : 'Entry' , action : 'patch' , params : { ... params , version : raw . sys . version , } , payload : ops , } ) . then ( ( data ) => wrapEntry ( makeRequest , data ) ) ; } , return example delete : function del ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Entry' , action : 'delete' , params } ) ; } , return example publish : function publish ( ) { const { raw , params } = getParams ( this ) ; return makeRequest ( { entityType : 'Entry' , action : 'publish' , params , payload : raw , } ) . then ( ( data ) => wrapEntry ( makeRequest , data ) ) ; } , return example unpublish : function unpublish ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Entry' , action : 'unpublish' , params , } ) . then ( ( data ) => wrapEntry ( makeRequest , data ) ) ; } , return example archive : function archive ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Entry' , action : 'archive' , params , } ) . then ( ( data ) => wrapEntry ( makeRequest , data ) ) ; } , return example unarchive : function unarchive ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Entry' , action : 'unarchive' , params , } ) . then ( ( data ) => wrapEntry ( makeRequest , data ) ) ; } , example getSnapshots : function ( query = { } ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Snapshot' , action : 'getManyForEntry' , params : { ... params , query } , } ) . then ( ( data ) => wrapSnapshotCollection < EntryProps > ( makeRequest , data ) ) ; } , param snapshotId example getSnapshot : function ( snapshotId ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Snapshot' , action : 'getForEntry' , params : { ... params , snapshotId } , } ) . then ( ( data ) => wrapSnapshot < EntryProps > ( makeRequest , data ) ) ; } , param data returns example createComment : function ( data ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Comment' , action : 'create' , params , payload : data , } ) . then ( ( data ) => wrapComment ( makeRequest , data ) ) ; } , returns getComments : function ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Comment' , action : 'getAll' , params , } ) . then ( ( data ) => wrapCommentCollection ( makeRequest , data ) ) ; } , returns getComment : function ( id ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Comment' , action : 'get' , params : { ... params , commentId : id , } , } ) . then ( ( data ) => wrapComment ( makeRequest , data ) ) ; } , param data returns example createTask : function ( data ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Task' , action : 'create' , params , payload : data , } ) . then ( ( data ) => wrapTask ( makeRequest , data ) ) ; } , returns getTasks : function ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Task' , action : 'getAll' , params , } ) . then ( ( data ) => wrapTaskCollection ( makeRequest , data ) ) ; } , returns getTask : function ( id ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Task' , action : 'get' , params : { ... params , taskId : id , } , } ) . then ( ( data ) => wrapTask ( makeRequest , data ) ) ; } , /**
         * Checks if the entry is published. A published entry might have unpublished changes
         */ isPublished : function isPublished ( ) { const raw = this . toPlainObject ( ) as EntryProps ; return checks . isPublished ( raw ) ; } , /**
         * Checks if the entry is updated. This means the entry was previously published but has unpublished changes.
         */ isUpdated : function isUpdated ( ) { const raw = this . toPlainObject ( ) as EntryProps ; return checks . isUpdated ( raw ) ; } , /**
         * Checks if the entry is in draft mode. This means it is not published.
         */ isDraft : function isDraft ( ) { const raw = this . toPlainObject ( ) as EntryProps ; return checks . isDraft ( raw ) ; } , /**
         * Checks if entry is archived. This means it's not exposed to the Delivery/Preview APIs.
         */ isArchived : function isArchived ( ) { const raw = this . toPlainObject ( ) as EntryProps ; return checks . isArchived ( raw ) ; } , /**
         * Recursively collects references of an entry and their descendants
         */ references : function references ( options ? ) { const raw = this . toPlainObject ( ) as EntryProps ; return makeRequest ( { entityType : 'Entry' , action : 'references' , params : { spaceId : raw . sys . space . sys . id , environmentId : raw . sys . environment . sys . id , entryId : raw . sys . id , maxDepth : options ?. include || options ?. maxDepth , } , } ) . then ( ( response ) => wrapEntryCollection ( makeRequest , response ) ) ; } , } ; }