xpeditis2.0/apps/frontend/src/lib/api/webhooks.ts
David e6b9b42f6c
Some checks failed
CI/CD Pipeline - Xpeditis PreProd / Backend - Build & Test (push) Failing after 5m51s
CI/CD Pipeline - Xpeditis PreProd / Backend - Docker Build & Push (push) Has been skipped
CI/CD Pipeline - Xpeditis PreProd / Frontend - Build & Test (push) Successful in 10m57s
CI/CD Pipeline - Xpeditis PreProd / Frontend - Docker Build & Push (push) Failing after 12m28s
CI/CD Pipeline - Xpeditis PreProd / Deploy to PreProd Server (push) Has been skipped
CI/CD Pipeline - Xpeditis PreProd / Run Smoke Tests (push) Has been skipped
fix
2025-11-13 00:15:45 +01:00

147 lines
3.8 KiB
TypeScript

/**
* Webhooks API
*
* Endpoints for managing webhook subscriptions (admin only)
*/
import { get, post, patch, del } from './client';
import type {
WebhookResponse,
SuccessResponse,
} from '@/types/api';
// TODO: These types should be moved to @/types/api.ts
export interface WebhookListResponse {
webhooks: WebhookResponse[];
total: number;
page: number;
pageSize: number;
}
export interface CreateWebhookRequest {
url: string;
eventTypes: string[];
secret?: string;
isActive?: boolean;
description?: string;
}
export interface UpdateWebhookRequest {
url?: string;
eventTypes?: string[];
secret?: string;
isActive?: boolean;
description?: string;
}
export interface WebhookEventListResponse {
events: Array<{
id: string;
webhookId: string;
eventType: string;
status: 'success' | 'failure' | 'pending';
requestBody: any;
responseStatus?: number;
responseBody?: any;
error?: string;
createdAt: string;
}>;
total: number;
page: number;
pageSize: number;
}
export interface TestWebhookRequest {
eventType?: string;
payload?: any;
}
/**
* List webhooks with pagination
* GET /api/v1/webhooks?page=1&limit=20&isActive=true&eventType=booking.created
* Requires: ADMIN role
*/
export async function listWebhooks(params?: {
page?: number;
limit?: number;
isActive?: boolean;
eventType?: string;
}): Promise<WebhookListResponse> {
const queryParams = new URLSearchParams();
if (params?.page) queryParams.append('page', params.page.toString());
if (params?.limit) queryParams.append('limit', params.limit.toString());
if (params?.isActive !== undefined) queryParams.append('isActive', params.isActive.toString());
if (params?.eventType) queryParams.append('eventType', params.eventType);
const queryString = queryParams.toString();
return get<WebhookListResponse>(`/api/v1/webhooks${queryString ? `?${queryString}` : ''}`);
}
/**
* Get webhook by ID
* GET /api/v1/webhooks/:id
* Requires: ADMIN role
*/
export async function getWebhook(id: string): Promise<WebhookResponse> {
return get<WebhookResponse>(`/api/v1/webhooks/${id}`);
}
/**
* Create webhook subscription
* POST /api/v1/webhooks
* Requires: ADMIN role
*/
export async function createWebhook(data: CreateWebhookRequest): Promise<WebhookResponse> {
return post<WebhookResponse>('/api/v1/webhooks', data);
}
/**
* Update webhook
* PATCH /api/v1/webhooks/:id
* Requires: ADMIN role
*/
export async function updateWebhook(
id: string,
data: UpdateWebhookRequest
): Promise<WebhookResponse> {
return patch<WebhookResponse>(`/api/v1/webhooks/${id}`, data);
}
/**
* Delete webhook
* DELETE /api/v1/webhooks/:id
* Requires: ADMIN role
*/
export async function deleteWebhook(id: string): Promise<SuccessResponse> {
return del<SuccessResponse>(`/api/v1/webhooks/${id}`);
}
/**
* Test webhook (send test event)
* POST /api/v1/webhooks/:id/test
* Requires: ADMIN role
*/
export async function testWebhook(id: string, data?: TestWebhookRequest): Promise<SuccessResponse> {
return post<SuccessResponse>(`/api/v1/webhooks/${id}/test`, data);
}
/**
* List webhook events (delivery history)
* GET /api/v1/webhooks/:id/events?page=1&limit=50&status=success
* Requires: ADMIN role
*/
export async function listWebhookEvents(
id: string,
params?: { page?: number; limit?: number; status?: string }
): Promise<WebhookEventListResponse> {
const queryParams = new URLSearchParams();
if (params?.page) queryParams.append('page', params.page.toString());
if (params?.limit) queryParams.append('limit', params.limit.toString());
if (params?.status) queryParams.append('status', params.status);
const queryString = queryParams.toString();
return get<WebhookEventListResponse>(
`/api/v1/webhooks/${id}/events${queryString ? `?${queryString}` : ''}`
);
}