Files
directus/api/src/webhooks.ts
Nicola Krumschmidt d64ca14348 Explicitly set catch parameters to any type (#7654)
This fixes not being able to build the repo due to type issues
introduced by the Typescript 4.4 option "useUnknownInCatchVariables",
which is enabled by default in strict mode.
2021-08-27 10:33:30 -04:00

70 lines
1.7 KiB
TypeScript

import axios from 'axios';
import { ListenerFn } from 'eventemitter2';
import getDatabase from './database';
import emitter from './emitter';
import logger from './logger';
import { Webhook } from './types';
import { pick } from 'lodash';
let registered: { event: string; handler: ListenerFn }[] = [];
export async function register(): Promise<void> {
unregister();
const database = getDatabase();
const webhooks = await database.select<Webhook[]>('*').from('directus_webhooks').where({ status: 'active' });
for (const webhook of webhooks) {
if (webhook.actions === '*') {
const event = 'items.*';
const handler = createHandler(webhook);
emitter.on(event, handler);
registered.push({ event, handler });
} else {
for (const action of webhook.actions.split(',')) {
const event = `items.${action}`;
const handler = createHandler(webhook);
emitter.on(event, handler);
registered.push({ event, handler });
}
}
}
}
export function unregister(): void {
for (const { event, handler } of registered) {
emitter.off(event, handler);
}
registered = [];
}
function createHandler(webhook: Webhook): ListenerFn {
return async (data) => {
const collectionAllowList = webhook.collections.split(',');
if (collectionAllowList.includes('*') === false && collectionAllowList.includes(data.collection) === false) return;
const webhookPayload = pick(data, [
'event',
'accountability.user',
'accountability.role',
'collection',
'item',
'action',
'payload',
]);
try {
await axios({
url: webhook.url,
method: webhook.method,
data: webhook.data ? webhookPayload : null,
});
} catch (error: any) {
logger.warn(`Webhook "${webhook.name}" (id: ${webhook.id}) failed`);
logger.warn(error);
}
};
}