import { freezeSys , toPlainObject } from 'contentful-sdk-core' ; import copy from 'fast-copy' ; import { Except , JsonValue , SetOptional } from 'type-fest' ; import { BasicMetaSysProps , CollectionProp , DefaultElements , MakeRequest , MetaLinkProps , SysLink , } from '../common-types' ; import { wrapCollection } from '../common-utils' ; import enhanceWithMethods from '../enhance-with-methods' ; interface EqualityConstraint { equals : [ Doc , string ] ; } interface Doc { doc : 'sys.id' | 'sys.contentType.sys.id' | 'sys.environment.sys.id' ; } interface InConstraint { in : [ Doc , [ string , ... string [ ] ] ] ; } interface RegexpConstraint { regexp : [ Doc , Pattern ] ; } interface Pattern { pattern : string ; } interface NotConstraint { not : EqualityConstraint | InConstraint | RegexpConstraint ; } export type WebhookCalls = { total : number ; healthy : number ; } ; export type WebhookCallRequest = { url : string ; method : string ; headers : { [ key ] : string ; } ; body : string ; } ; export type WebhookCallResponse = WebhookCallRequest & { statusCode : number ; } ; export type WebhookHealthSys = Except < BasicMetaSysProps , 'version' | 'updatedAt' | 'updatedBy' | 'createdAt' > ; export type WebhookCallDetailsSys = Except < BasicMetaSysProps , 'version' | 'updatedAt' | 'updatedBy' > ; export type WebhookHeader = { key : string ; value : string ; secret ? : boolean ; } ; export type WebhookFilter = EqualityConstraint | InConstraint | RegexpConstraint | NotConstraint ; export type WebhookTransformation = { method ? : null | 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE' ; contentType ? : null | 'application/vnd.contentful.management.v1+json' | 'application/vnd.contentful.management.v1+json; charset=utf-8' | 'application/json' | 'application/json; charset=utf-8' | 'application/x-www-form-urlencoded' | 'application/x-www-form-urlencoded; charset=utf-8' ; includeContentLength ? : boolean | null ; body ? : JsonValue ; } ; export type CreateWebhooksProps = SetOptional < Except < WebhookProps , 'sys' > , 'headers' | 'active' > ; export type UpdateWebhookProps = SetOptional < Except < WebhookProps , 'sys' > , 'headers' | 'name' | 'topics' | 'url' | 'active' > ; export type WebhookCallDetailsProps = { /**
     * System metadata
     */ sys : WebhookCallDetailsSys ; /**
     * Request object
     */ request : WebhookCallRequest ; /**
     * Request object
     */ response : WebhookCallResponse ; /**
     * Status code of the request
     */ statusCode : number ; /**
     * Errors
     */ errors : any [ ] ; /**
     * Type of the webhook
     */ eventType : string ; /**
     * Url of the request
     */ url : string ; /**
     * Timestamp of the request
     */ requestAt : string ; /**
     * Timestamp of the response
     */ responseAt : string ; } ; export type WebhookCallOverviewProps = Except < WebhookCallDetailsProps , 'request' | 'response' > ; export type WebhookHealthProps = { /**
     * System metadata
     */ sys : WebhookHealthSys & { space : { sys : MetaLinkProps ; } ; } ; /**
     * Webhook call statistics
     */ calls : WebhookCalls ; } ; export type WebhookProps = { /**
     * System metadata
     */ sys : BasicMetaSysProps & { space : SysLink ; } ; /**
     * Webhook name
     */ name : string ; /**
     * Webhook url
     */ url : string ; /**
     * Topics the webhook wants to subscribe to
     */ topics : string [ ] ; /**
     * Username for basic http auth
     */ httpBasicUsername ? : string ; /**
     * Password for basic http auth
     */ httpBasicPassword ? : string ; /**
     * Headers that should be appended to the webhook request
     */ headers : Array < WebhookHeader > ; /**
     * Webhook filters
     */ filters ? : WebhookFilter [ ] ; /**
     * Transformation to apply
     */ transformation ? : WebhookTransformation ; /**
     * Whether the Webhook is active. If set to false, no calls will be made
     */ active : boolean ; } ; export interface WebHooks extends WebhookProps , DefaultElements < WebhookProps > { return update ( ) : Promise < WebHooks > ; return delete ( ) : Promise < void > ; return getCalls ( ) : Promise < CollectionProp < WebhookCallOverviewProps > > ; return getCall ( id ) : Promise < WebhookCallDetailsProps > ; return getHealth ( ) : Promise < WebhookHealthProps > ; } private function createWebhookApi ( makeRequest ) { const getParams = ( data ) => ( { spaceId : data . sys . space . sys . id , webhookDefinitionId : data . sys . id , } ) ; return { update : function update ( ) { const data = this . toPlainObject ( ) as WebhookProps ; return makeRequest ( { entityType : 'Webhook' , action : 'update' , params : getParams ( data ) , payload : data , } ) . then ( ( data ) => wrapWebhook ( makeRequest , data ) ) ; } , delete : function del ( ) { const data = this . toPlainObject ( ) as WebhookProps ; return makeRequest ( { entityType : 'Webhook' , action : 'delete' , params : getParams ( data ) , } ) ; } , getCalls : function getCalls ( ) { const data = this . toPlainObject ( ) as WebhookProps ; return makeRequest ( { entityType : 'Webhook' , action : 'getManyCallDetails' , params : getParams ( data ) , } ) ; } , getCall : function getCall ( id ) { const data = this . toPlainObject ( ) as WebhookProps ; return makeRequest ( { entityType : 'Webhook' , action : 'getCallDetails' , params : { ... getParams ( data ) , callId : id } , } ) ; } , getHealth : function getHealth ( ) { const data = this . toPlainObject ( ) as WebhookProps ; return makeRequest ( { entityType : 'Webhook' , action : 'getHealthStatus' , params : getParams ( data ) , } ) ; } , } ; } private param makeRequest param data return export function wrapWebhook ( makeRequest , data ) { const webhook = toPlainObject ( copy ( data ) ) ; const webhookWithMethods = enhanceWithMethods ( webhook , createWebhookApi ( makeRequest ) ) ; return freezeSys ( webhookWithMethods ) ; } private export const wrapWebhookCollection = wrapCollection ( wrapWebhook ) ;