@jokoor/sdk
Version:
Jokoor SMS API SDK for JavaScript/TypeScript
176 lines • 5.75 kB
JavaScript
"use strict";
/**
* Webhooks resource - User-friendly wrapper for webhooks API
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Webhooks = void 0;
const base_1 = require("./base");
const api_1 = require("../generated/api");
const result_1 = require("../types/result");
class Webhooks extends base_1.BaseResource {
constructor(configuration) {
super(configuration);
this.api = new api_1.WebhooksApi(configuration);
}
/**
* Create a new webhook endpoint
* @param params Webhook creation parameters
* @returns Created webhook endpoint
*/
async create(params) {
const response = await this.api.v1WebhookEndpointsPost(params);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Get a webhook endpoint by ID
* @param id Webhook endpoint ID
* @returns Webhook endpoint details
*/
async get(id) {
const response = await this.api.v1WebhookEndpointsIdGet(id);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Update a webhook endpoint
* @param id Webhook endpoint ID
* @param params Update parameters
* @returns Updated webhook endpoint
*/
async update(id, params) {
const response = await this.api.v1WebhookEndpointsIdPost(id, params);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Delete a webhook endpoint
* @param id Webhook endpoint ID
* @returns Deletion confirmation
*/
async delete(id) {
const response = await this.api.v1WebhookEndpointsIdDelete(id);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* List webhook endpoints
* @param options List options
* @returns List of webhook endpoints with cursor pagination
*/
async list(options) {
const response = await this.api.v1WebhookEndpointsGet(options?.limit, options?.startingAfter, options?.endingBefore);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Test a webhook endpoint
* @param id Webhook endpoint ID
* @returns Test result
*/
async test(id) {
const response = await this.api.v1WebhookEndpointsIdTestPost(id);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Get webhook endpoint metrics
* @param id Webhook endpoint ID
* @returns Webhook metrics
*/
async getMetrics(id) {
const response = await this.api.v1WebhookEndpointsIdMetricsGet(id);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* List webhook events
* @param options List options
* @returns List of webhook events
*/
async listEvents(options) {
const response = await this.api.v1EventsGet(options?.type, options?.limit, options?.startingAfter, options?.endingBefore);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Get a webhook event by ID
* @param id Event ID
* @returns Event details
*/
async getEvent(id) {
const response = await this.api.v1EventsIdGet(id);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Retry a webhook event
* @param id Event ID
* @returns Retry result
*/
async retryEvent(id) {
const response = await this.api.v1EventsIdRetryPost(id);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Get webhook system health
* @returns Health status
*/
async getHealth() {
const response = await this.api.v1WebhooksHealthGet();
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Get webhook system metrics
* @param since Start date for metrics
* @returns System metrics
*/
async getSystemMetrics(since) {
const response = await this.api.v1WebhooksMetricsGet(since);
const result = this.extractData(response);
return (0, result_1.unwrap)(result);
}
/**
* Verify webhook signature
* @param payload Webhook payload
* @param signature Webhook signature from headers
* @param secret Webhook secret
* @returns Whether signature is valid
*/
verifySignature(payload, signature, secret) {
try {
const crypto = require('crypto');
const expectedSignature = crypto
.createHmac('sha256', secret)
.update(payload)
.digest('hex');
return signature === expectedSignature;
}
catch (error) {
return false;
}
}
/**
* Parse and verify webhook event
* @param payload Webhook payload
* @param signature Webhook signature from headers
* @param secret Webhook secret
* @returns Parsed webhook event
* @throws Error if signature is invalid or payload cannot be parsed
*/
parseWebhookEvent(payload, signature, secret) {
if (!this.verifySignature(payload, signature, secret)) {
throw new Error('Invalid webhook signature');
}
try {
return JSON.parse(payload);
}
catch (error) {
throw new Error('Invalid webhook payload');
}
}
}
exports.Webhooks = Webhooks;
//# sourceMappingURL=webhooks.js.map