import { freezeSys , toPlainObject } from 'contentful-sdk-core' ; import copy from 'fast-copy' ; import { Except , SetOptional } from 'type-fest' ; import { BasicMetaSysProps , Collection , DefaultElements , MakeRequest , QueryOptions , SysLink , } from '../common-types' ; import { wrapCollection } from '../common-utils' ; import enhanceWithMethods from '../enhance-with-methods' ; import { isDraft , isPublished , isUpdated } from '../plain/checks' ; import { ContentFields } from './content-type-fields' ; import { EditorInterface , wrapEditorInterface } from './editor-interface' ; import { Snapshot , SnapshotProps , wrapSnapshot , wrapSnapshotCollection } from './snapshot' ; import { omitAndDeleteField } from '../methods/content-type' ; export type ContentTypeProps = { sys : BasicMetaSysProps & { space : SysLink ; environment : SysLink ; firstPublishedAt ? : string ; publishedCounter ? : number ; publishedVersion ? : number ; } ; name : string ; description : string ; /** Field used as the main display field for Entries */ displayField : string ; /** All the fields contained in this Content Type */ fields : ContentFields [ ] ; } ; export type CreateContentTypeProps = SetOptional < Except < ContentTypeProps , 'sys' > , 'description' | 'displayField' > ; type ContentTypeApi = { return example update ( ) : Promise < ContentType > ; return example delete ( ) : Promise < void > ; return example publish ( ) : Promise < ContentType > ; return example unpublish ( ) : Promise < ContentType > ; return example getEditorInterface ( ) : Promise < EditorInterface > ; /**
     * Checks if the contentType is in draft mode. This means it is not published.
     */ isDraft ( ) : boolean ; /**
     * Checks if the contentType is published. A published contentType might have unpublished changes (@see {ContentType.isUpdated})
     */ isPublished ( ) : boolean ; /**
     * Checks if the contentType is updated. This means the contentType was previously published but has unpublished changes.
     */ isUpdated ( ) : boolean ; return omitAndDeleteField ( id ) : Promise < ContentType > ; param snapshotId example getSnapshot ( snapshotId ) : Promise < SnapshotProps < ContentTypeProps > > ; example getSnapshots ( ) : Promise < Collection < Snapshot < ContentTypeProps > , SnapshotProps < ContentTypeProps > > > ; } ; export interface ContentType extends ContentTypeProps , DefaultElements < ContentTypeProps > , ContentTypeApi { } private function createContentTypeApi ( makeRequest ) { const getParams = ( self ) => { const contentType = self . toPlainObject ( ) as ContentTypeProps ; return { raw : contentType , params : { spaceId : contentType . sys . space . sys . id , environmentId : contentType . sys . environment . sys . id , contentTypeId : contentType . sys . id , } , } ; } ; return { update : function ( ) { const { raw , params } = getParams ( this ) ; return makeRequest ( { entityType : 'ContentType' , action : 'update' , params , payload : raw , } ) . then ( ( data ) => wrapContentType ( makeRequest , data ) ) ; } , delete : function ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'ContentType' , action : 'delete' , params , } ) . then ( ( ) => { } ) ; } , publish : function ( ) { const { raw , params } = getParams ( this ) ; return makeRequest ( { entityType : 'ContentType' , action : 'publish' , params , payload : raw , } ) . then ( ( data ) => wrapContentType ( makeRequest , data ) ) ; } , unpublish : function ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'ContentType' , action : 'unpublish' , params , } ) . then ( ( data ) => wrapContentType ( makeRequest , data ) ) ; } , getEditorInterface : function ( ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'EditorInterface' , action : 'get' , params , } ) . then ( ( data ) => wrapEditorInterface ( makeRequest , data ) ) ; } , getSnapshots : function ( query = { } ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Snapshot' , action : 'getManyForContentType' , params : { ... params , query } , } ) . then ( ( data ) => wrapSnapshotCollection < ContentTypeProps > ( makeRequest , data ) ) ; } , getSnapshot : function ( snapshotId ) { const { params } = getParams ( this ) ; return makeRequest ( { entityType : 'Snapshot' , action : 'getForContentType' , params : { ... params , snapshotId } , } ) . then ( ( data ) => wrapSnapshot < ContentTypeProps > ( makeRequest , data ) ) ; } , isPublished : function ( ) { return isPublished ( this ) ; } , isUpdated : function ( ) { return isUpdated ( this ) ; } , isDraft : function ( ) { return isDraft ( this ) ; } , omitAndDeleteField : function ( fieldId ) { const { raw , params } = getParams ( this ) ; return omitAndDeleteField ( makeRequest , { ... params , fieldId } , raw ) . then ( ( data ) => wrapContentType ( makeRequest , data ) ) ; } , } ; } private param makeRequest param data return export function wrapContentType ( makeRequest , data ) { const contentType = toPlainObject ( copy ( data ) ) ; const contentTypeWithMethods = enhanceWithMethods ( contentType , createContentTypeApi ( makeRequest ) ) ; return freezeSys ( contentTypeWithMethods ) ; } private export const wrapContentTypeCollection = wrapCollection ( wrapContentType ) ;