// Operations for webhook management
import { z } from "zod";
import { getWebhooksApi } from "../common/client";
import {
CurvegridResourceNotFoundError,
CurvegridError,
} from "../common/errors";
import { WebhookEventsType } from "@curvegrid/multibaas-sdk";
// Schema for webhook events
export const ListWebhookEventsSchema = z.object({
webhookId: z.number().describe("Webhook ID"),
limit: z.number().optional().describe("Maximum number of events to return"),
offset: z.number().optional().describe("Offset for pagination"),
});
// Schema for listing webhooks
export const ListWebhooksSchema = z.object({
limit: z.number().optional().describe("Maximum number of webhooks to return"),
offset: z.number().optional().describe("Offset for pagination"),
});
// Schema for getting webhook details
export const GetWebhookSchema = z.object({
id: z.number().describe("Webhook ID"),
});
// Schema for creating a webhook
export const CreateWebhookSchema = z.object({
label: z.string().describe("Label for the webhook"),
url: z.string().describe("URL to call when the webhook is triggered"),
subscriptions: z
.array(
z.enum([
WebhookEventsType.TransactionIncluded,
WebhookEventsType.EventEmitted,
]),
)
.describe(
'Events to trigger the webhook (e.g., ["transaction.included", "event.emitted"])',
),
});
// Schema for updating a webhook
export const UpdateWebhookSchema = z.object({
id: z.number().describe("Webhook ID to update"),
label: z.string().optional().describe("New label for the webhook"),
url: z
.string()
.optional()
.describe("New URL to call when the webhook is triggered"),
subscriptions: z
.array(
z.enum([
WebhookEventsType.TransactionIncluded,
WebhookEventsType.EventEmitted,
]),
)
.optional()
.describe("New events to trigger the webhook"),
});
// Schema for deleting a webhook
export const DeleteWebhookSchema = z.object({
id: z.number().describe("Webhook ID to delete"),
});
// List webhooks
export async function listWebhooks(
options: z.infer<typeof ListWebhooksSchema> = {},
) {
try {
const webhooksApi = getWebhooksApi();
const response = await webhooksApi.listWebhooks(
options.limit,
options.offset,
);
return response.data.result;
} catch (error: any) {
throw new CurvegridError(`Failed to list webhooks: ${error.message}`);
}
}
// Get webhook details
export async function getWebhook(options: z.infer<typeof GetWebhookSchema>) {
try {
const webhooksApi = getWebhooksApi();
const response = await webhooksApi.getWebhook(options.id);
return response.data.result;
} catch (error: any) {
if (error.response?.status === 404) {
throw new CurvegridResourceNotFoundError(
`Webhook with ID ${options.id} not found`,
);
}
throw new CurvegridError(`Failed to get webhook: ${error.message}`);
}
}
// Create webhook
export async function createWebhook(
options: z.infer<typeof CreateWebhookSchema>,
) {
try {
const webhooksApi = getWebhooksApi();
const response = await webhooksApi.createWebhook({
label: options.label,
url: options.url,
subscriptions: options.subscriptions,
});
return response.data.result;
} catch (error: any) {
throw new CurvegridError(`Failed to create webhook: ${error.message}`);
}
}
// Update webhook
export async function updateWebhook(
options: z.infer<typeof UpdateWebhookSchema>,
) {
try {
const webhooksApi = getWebhooksApi();
const response = await webhooksApi.updateWebhook(options.id, {
label: options.label || "",
url: options.url || "",
subscriptions: options.subscriptions || [],
});
return response.data.result;
} catch (error: any) {
if (error.response?.status === 404) {
throw new CurvegridResourceNotFoundError(
`Webhook with ID ${options.id} not found`,
);
}
throw new CurvegridError(`Failed to update webhook: ${error.message}`);
}
}
// Delete webhook
export async function deleteWebhook(
options: z.infer<typeof DeleteWebhookSchema>,
) {
try {
const webhooksApi = getWebhooksApi();
await webhooksApi.deleteWebhook(options.id);
// Return true to indicate success (deleteWebhook returns BaseResponse which has no result)
return true;
} catch (error: any) {
if (error.response?.status === 404) {
throw new CurvegridResourceNotFoundError(
`Webhook with ID ${options.id} not found`,
);
}
throw new CurvegridError(`Failed to delete webhook: ${error.message}`);
}
}
// Count webhooks
export async function countWebhooks() {
try {
const webhooksApi = getWebhooksApi();
const response = await webhooksApi.countWebhooks();
return response.data.result;
} catch (error: any) {
throw new CurvegridError(`Failed to count webhooks: ${error.message}`);
}
}
// List webhook events
export async function listWebhookEvents(
options: z.infer<typeof ListWebhookEventsSchema>,
) {
try {
const webhooksApi = getWebhooksApi();
const response = await webhooksApi.listWebhookEvents(
options.webhookId,
options.limit,
options.offset,
);
return response.data.result;
} catch (error: any) {
if (error.response?.status === 404) {
throw new CurvegridResourceNotFoundError(
`Webhook with ID ${options.webhookId} not found`,
);
}
throw new CurvegridError(`Failed to list webhook events: ${error.message}`);
}
}
// Count webhook events
export async function countWebhookEvents(
options: z.infer<typeof ListWebhookEventsSchema>,
) {
try {
const webhooksApi = getWebhooksApi();
const response = await webhooksApi.countWebhookEvents(options.webhookId);
return response.data.result;
} catch (error: any) {
if (error.response?.status === 404) {
throw new CurvegridResourceNotFoundError(
`Webhook with ID ${options.webhookId} not found`,
);
}
throw new CurvegridError(
`Failed to count webhook events: ${error.message}`,
);
}
}