87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
|
|
import { apiClient } from '@/services/api/client';
|
||
|
|
import { Webhook, WebhookFailure } from '@/types/api';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Webhook API
|
||
|
|
* Implémente les endpoints webhooks selon le backend
|
||
|
|
* Endpoints:
|
||
|
|
* - POST /webhooks (protected) - Enregistrer un webhook
|
||
|
|
* - GET /webhooks (protected) - Lister les webhooks de l'utilisateur
|
||
|
|
* - DELETE /webhooks/:id (protected) - Supprimer un webhook
|
||
|
|
* - GET /webhooks/stats (protected) - Statistiques des webhooks
|
||
|
|
* - POST /webhooks/:id/test (protected) - Tester un webhook
|
||
|
|
*/
|
||
|
|
|
||
|
|
export interface RegisterWebhookRequest {
|
||
|
|
url: string;
|
||
|
|
events: string[]; // Array of event types (e.g., ['track.uploaded', 'user.created'])
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface WebhookStats {
|
||
|
|
queue_size: number;
|
||
|
|
workers: number;
|
||
|
|
max_retries: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enregistre un nouveau webhook
|
||
|
|
* @param data Données du webhook (URL et événements)
|
||
|
|
* @returns Le webhook créé
|
||
|
|
*/
|
||
|
|
export async function registerWebhook(
|
||
|
|
data: RegisterWebhookRequest,
|
||
|
|
): Promise<Webhook> {
|
||
|
|
const response = await apiClient.post<Webhook>('/webhooks', data);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Liste les webhooks de l'utilisateur authentifié
|
||
|
|
* @returns Liste des webhooks
|
||
|
|
*/
|
||
|
|
export async function listWebhooks(): Promise<Webhook[]> {
|
||
|
|
const response = await apiClient.get<Webhook[]>('/webhooks');
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Supprime un webhook
|
||
|
|
* @param id ID du webhook à supprimer
|
||
|
|
* @returns Message de confirmation
|
||
|
|
*/
|
||
|
|
export async function deleteWebhook(
|
||
|
|
id: string,
|
||
|
|
): Promise<{ message: string }> {
|
||
|
|
const response = await apiClient.delete<{ message: string }>(
|
||
|
|
`/webhooks/${id}`,
|
||
|
|
);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Récupère les statistiques des webhooks
|
||
|
|
* @returns Statistiques du worker de webhooks
|
||
|
|
*/
|
||
|
|
export async function getWebhookStats(): Promise<WebhookStats> {
|
||
|
|
const response = await apiClient.get<{ stats: WebhookStats }>(
|
||
|
|
'/webhooks/stats',
|
||
|
|
);
|
||
|
|
return response.data.stats;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Teste un webhook en envoyant un événement de test
|
||
|
|
* @param id ID du webhook à tester
|
||
|
|
* @returns Message de confirmation
|
||
|
|
*/
|
||
|
|
export async function testWebhook(
|
||
|
|
id: string,
|
||
|
|
): Promise<{ message: string }> {
|
||
|
|
const response = await apiClient.post<{ message: string }>(
|
||
|
|
`/webhooks/${id}/test`,
|
||
|
|
);
|
||
|
|
return response.data;
|
||
|
|
}
|
||
|
|
|
||
|
|
|