feat: improve webhook integration (customized body and headers)
This commit is contained in:
@@ -40,6 +40,7 @@ COPY packages/payments/package.json ./packages/payments/
|
||||
COPY packages/constants/package.json ./packages/constants/
|
||||
COPY packages/validation/package.json ./packages/validation/
|
||||
COPY packages/integrations/package.json ./packages/integrations/
|
||||
COPY packages/js-runtime/package.json ./packages/js-runtime/
|
||||
COPY patches ./patches
|
||||
|
||||
# BUILD
|
||||
@@ -90,6 +91,7 @@ COPY --from=build /app/packages/payments ./packages/payments
|
||||
COPY --from=build /app/packages/constants ./packages/constants
|
||||
COPY --from=build /app/packages/validation ./packages/validation
|
||||
COPY --from=build /app/packages/integrations ./packages/integrations
|
||||
COPY --from=build /app/packages/js-runtime ./packages/js-runtime
|
||||
COPY --from=build /app/tooling/typescript ./tooling/typescript
|
||||
RUN pnpm db:codegen
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
"@openpanel/db": "workspace:*",
|
||||
"@openpanel/email": "workspace:*",
|
||||
"@openpanel/integrations": "workspace:^",
|
||||
"@openpanel/js-runtime": "workspace:*",
|
||||
"@openpanel/json": "workspace:*",
|
||||
"@openpanel/logger": "workspace:*",
|
||||
"@openpanel/importer": "workspace:*",
|
||||
|
||||
@@ -1,11 +1,22 @@
|
||||
import type { Job } from 'bullmq';
|
||||
|
||||
import { db } from '@openpanel/db';
|
||||
import { Prisma, db } from '@openpanel/db';
|
||||
import { sendDiscordNotification } from '@openpanel/integrations/src/discord';
|
||||
import { sendSlackNotification } from '@openpanel/integrations/src/slack';
|
||||
import { setSuperJson } from '@openpanel/json';
|
||||
import { execute as executeJavaScriptTemplate } from '@openpanel/js-runtime';
|
||||
import type { NotificationQueuePayload } from '@openpanel/queue';
|
||||
import { getRedisPub, publishEvent } from '@openpanel/redis';
|
||||
import { publishEvent } from '@openpanel/redis';
|
||||
|
||||
function isValidJson<T>(
|
||||
value: T | Prisma.NullableJsonNullValueInput | null | undefined,
|
||||
): value is T {
|
||||
return (
|
||||
value !== null &&
|
||||
value !== undefined &&
|
||||
value !== Prisma.JsonNull &&
|
||||
value !== Prisma.DbNull
|
||||
);
|
||||
}
|
||||
|
||||
export async function notificationJob(job: Job<NotificationQueuePayload>) {
|
||||
switch (job.data.type) {
|
||||
@@ -14,12 +25,10 @@ export async function notificationJob(job: Job<NotificationQueuePayload>) {
|
||||
|
||||
if (notification.sendToApp) {
|
||||
publishEvent('notification', 'created', notification);
|
||||
// empty for now
|
||||
return;
|
||||
}
|
||||
|
||||
if (notification.sendToEmail) {
|
||||
// empty for now
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -33,18 +42,44 @@ export async function notificationJob(job: Job<NotificationQueuePayload>) {
|
||||
},
|
||||
});
|
||||
|
||||
const payload = notification.payload;
|
||||
|
||||
if (!isValidJson(payload)) {
|
||||
return new Error('Invalid payload');
|
||||
}
|
||||
|
||||
switch (integration.config.type) {
|
||||
case 'webhook': {
|
||||
let body: unknown;
|
||||
|
||||
if (integration.config.mode === 'javascript') {
|
||||
// We only transform event payloads for now (not funnel)
|
||||
if (
|
||||
integration.config.javascriptTemplate &&
|
||||
payload.type === 'event'
|
||||
) {
|
||||
const result = executeJavaScriptTemplate(
|
||||
integration.config.javascriptTemplate,
|
||||
payload.event,
|
||||
);
|
||||
body = result;
|
||||
} else {
|
||||
body = payload;
|
||||
}
|
||||
} else {
|
||||
body = {
|
||||
title: notification.title,
|
||||
message: notification.message,
|
||||
};
|
||||
}
|
||||
|
||||
return fetch(integration.config.url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...(integration.config.headers ?? {}),
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
title: notification.title,
|
||||
message: notification.message,
|
||||
}),
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
case 'discord': {
|
||||
|
||||
Reference in New Issue
Block a user