Refactor webhook handling and router services
Webhook handling has been made more flexible with an optional custom event handler in the 'handleWebhook' function. A new 'DatabaseChangePayload' type has been created to define the payload for database change events, aiding custom webhook handling. The 'DatabaseWebhookRouterService' has been changed to a factory function. This transformation improves the service initialization, making it more readable and testing friendly. Some potential noisy logs in the default case are also removed.
This commit is contained in:
@@ -4,13 +4,23 @@ import { getLogger } from '@kit/shared/logger';
|
||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
||||
|
||||
import { RecordChange, Tables } from '../record-change.type';
|
||||
import { DatabaseWebhookRouterService } from './database-webhook-router.service';
|
||||
import { createDatabaseWebhookRouterService } from './database-webhook-router.service';
|
||||
import { getDatabaseWebhookVerifier } from './verifier';
|
||||
|
||||
/**
|
||||
* @name DatabaseChangePayload
|
||||
* @description Payload for the database change event. Useful for handling custom webhooks.
|
||||
*/
|
||||
export type DatabaseChangePayload = RecordChange<keyof Tables>;
|
||||
|
||||
export function getDatabaseWebhookHandlerService() {
|
||||
return new DatabaseWebhookHandlerService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @name getDatabaseWebhookHandlerService
|
||||
* @description Get the database webhook handler service
|
||||
*/
|
||||
class DatabaseWebhookHandlerService {
|
||||
private readonly namespace = 'database-webhook-handler';
|
||||
|
||||
@@ -18,8 +28,14 @@ class DatabaseWebhookHandlerService {
|
||||
* @name handleWebhook
|
||||
* @description Handle the webhook event
|
||||
* @param request
|
||||
* @param params
|
||||
*/
|
||||
async handleWebhook(request: Request) {
|
||||
async handleWebhook(
|
||||
request: Request,
|
||||
params?: {
|
||||
handleEvent(payload: DatabaseChangePayload): unknown;
|
||||
},
|
||||
) {
|
||||
const logger = await getLogger();
|
||||
|
||||
const json = await request.clone().json();
|
||||
@@ -47,12 +63,17 @@ class DatabaseWebhookHandlerService {
|
||||
});
|
||||
|
||||
// handle the webhook
|
||||
const service = new DatabaseWebhookRouterService(client);
|
||||
const service = createDatabaseWebhookRouterService(client);
|
||||
|
||||
try {
|
||||
// handle the webhook event based on the table
|
||||
await service.handleWebhook(json);
|
||||
|
||||
// if a custom handler is provided, call it
|
||||
if (params?.handleEvent) {
|
||||
await params.handleEvent(json);
|
||||
}
|
||||
|
||||
logger.info(ctx, 'Webhook processed successfully');
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { getLogger } from '@kit/shared/logger';
|
||||
import { Database } from '@kit/supabase/database';
|
||||
|
||||
import { RecordChange, Tables } from '../record-change.type';
|
||||
|
||||
export class DatabaseWebhookRouterService {
|
||||
export function createDatabaseWebhookRouterService(
|
||||
adminClient: SupabaseClient<Database>,
|
||||
) {
|
||||
return new DatabaseWebhookRouterService(adminClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name DatabaseWebhookRouterService
|
||||
* @description Service that routes the webhook event to the appropriate service
|
||||
*/
|
||||
class DatabaseWebhookRouterService {
|
||||
constructor(private readonly adminClient: SupabaseClient<Database>) {}
|
||||
|
||||
/**
|
||||
* @name handleWebhook
|
||||
* @description Handle the webhook event
|
||||
* @param body
|
||||
*/
|
||||
async handleWebhook(body: RecordChange<keyof Tables>) {
|
||||
switch (body.table) {
|
||||
case 'invitations': {
|
||||
@@ -29,14 +43,7 @@ export class DatabaseWebhookRouterService {
|
||||
}
|
||||
|
||||
default: {
|
||||
const logger = await getLogger();
|
||||
|
||||
logger.warn(
|
||||
{
|
||||
table: body.table,
|
||||
},
|
||||
'No handler found for table',
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user