import { post } from "./httpService" ; import { HapiServer } from "../types" ; const DEFAULT_OPTIONS = { headers : { accept : "application/json" , "content-type" : "application/json" , } , timeout : 60000 , } ; export class WebhookService { logger : any ; constructor ( server ) { this . logger = server . logger ; } param url param data returns async postRequest ( url , data ) { this . logger . info ( [ "WebhookService" , "postRequest body" ] , JSON . stringify ( data ) ) ; const { payload } = await post ( url , { ... DEFAULT_OPTIONS , payload : JSON . stringify ( data ) , } ) ; if ( typeof payload === "object" && ! Buffer . isBuffer ( payload ) ) { return payload . reference ; } try { const { reference } = JSON . parse ( payload ) ; this . logger . info ( [ "WebhookService" , "postRequest" ] , `Webhook request to ${ url } submitted OK` ) ; this . logger . debug ( [ "WebhookService" , "postRequest" , `REF: ${ reference }` ] , JSON . stringify ( payload ) ) ; return reference ; } catch ( error ) { this . logger . error ( [ "WebhookService" , "postRequest" ] , error ) ; return "UNKNOWN" ; } } }