2025-12-22 21:00:50 +00:00
|
|
|
import { apiClient } from '@/services/api/client';
|
2025-12-22 21:56:37 +00:00
|
|
|
import { Webhook } from '@/types/webhook';
|
2025-12-22 21:00:50 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
2025-12-25 10:20:45 +00:00
|
|
|
* - POST /webhooks/:id/regenerate-key (protected) - Régénérer la clé API
|
2025-12-22 21:00:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-25 10:20:45 +00:00
|
|
|
export interface WebhookStatsResponse {
|
|
|
|
|
user_id: string;
|
|
|
|
|
stats: WebhookStats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface RegenerateAPIKeyResponse {
|
|
|
|
|
api_key: string;
|
|
|
|
|
message: string;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-22 21:00:50 +00:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2025-12-25 10:20:45 +00:00
|
|
|
export async function getWebhookStats(): Promise<WebhookStatsResponse> {
|
|
|
|
|
const response = await apiClient.get<WebhookStatsResponse>(
|
2025-12-22 21:00:50 +00:00
|
|
|
'/webhooks/stats',
|
|
|
|
|
);
|
2025-12-25 10:20:45 +00:00
|
|
|
return response.data;
|
2025-12-22 21:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-25 10:20:45 +00:00
|
|
|
/**
|
|
|
|
|
* Régénère la clé API d'un webhook
|
|
|
|
|
* @param id ID du webhook
|
|
|
|
|
* @returns Nouvelle clé API et message de confirmation
|
|
|
|
|
*/
|
|
|
|
|
export async function regenerateWebhookAPIKey(
|
|
|
|
|
id: string,
|
|
|
|
|
): Promise<RegenerateAPIKeyResponse> {
|
|
|
|
|
const response = await apiClient.post<RegenerateAPIKeyResponse>(
|
|
|
|
|
`/webhooks/${id}/regenerate-key`,
|
|
|
|
|
);
|
|
|
|
|
return response.data;
|
|
|
|
|
}
|
2025-12-22 21:00:50 +00:00
|
|
|
|