Webhook tweaks + Support added for "Custom payload templates" / x-www-form-urlencoded / json (#1193)
* Changed styling of webhook test & updated <Form> component * Implements custom webhook formats Co-authored-by: Bailey Pumfleet <[email protected]>
This commit is contained in:
1 parent
ecc960f0a3
commit
5b3dd02747
9 files changed
+169
-124
No files matched your search
@@ -3,6 +3,7 @@ import { z } from "zod";
|
||||
|
||||
import { getErrorFromUnknown } from "@lib/errors";
|
||||
import { WEBHOOK_TRIGGER_EVENTS } from "@lib/webhooks/constants";
|
||||
import sendPayload from "@lib/webhooks/sendPayload";
|
||||
|
||||
import { createProtectedRouter } from "@server/createRouter";
|
||||
|
||||
@@ -38,6 +39,7 @@ export const webhookRouter = createProtectedRouter()
|
||||
subscriberUrl: z.string().url().optional(),
|
||||
eventTriggers: z.enum(WEBHOOK_TRIGGER_EVENTS).array().optional(),
|
||||
active: z.boolean().optional(),
|
||||
payloadTemplate: z.string().nullable(),
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
const { id, ...data } = input;
|
||||
@@ -88,50 +90,39 @@ export const webhookRouter = createProtectedRouter()
|
||||
input: z.object({
|
||||
url: z.string().url(),
|
||||
type: z.string(),
|
||||
payloadTemplate: z.string().optional().nullable(),
|
||||
}),
|
||||
async resolve({ input }) {
|
||||
const { url, type } = input;
|
||||
const { url, type, payloadTemplate } = input;
|
||||
|
||||
const responseBodyMocks: Record<"PING", unknown> = {
|
||||
PING: {
|
||||
triggerEvent: "PING",
|
||||
createdAt: new Date().toISOString(),
|
||||
payload: {
|
||||
type: "Test",
|
||||
title: "Test trigger event",
|
||||
description: "",
|
||||
startTime: new Date().toISOString(),
|
||||
endTime: new Date().toISOString(),
|
||||
organizer: {
|
||||
name: "Cal",
|
||||
email: "",
|
||||
timeZone: "Europe/London",
|
||||
},
|
||||
const data = {
|
||||
type: "Test",
|
||||
title: "Test trigger event",
|
||||
description: "",
|
||||
startTime: new Date().toISOString(),
|
||||
endTime: new Date().toISOString(),
|
||||
attendees: [
|
||||
{
|
||||
email: "[email protected]",
|
||||
name: "John Doe",
|
||||
timeZone: "Europe/London",
|
||||
},
|
||||
],
|
||||
organizer: {
|
||||
name: "Cal",
|
||||
email: "",
|
||||
timeZone: "Europe/London",
|
||||
},
|
||||
};
|
||||
|
||||
const body = responseBodyMocks[type as "PING"];
|
||||
if (!body) {
|
||||
throw new Error(`Unknown type '${type}'`);
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
// [...]
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const text = await res.text();
|
||||
return {
|
||||
status: res.status,
|
||||
message: text,
|
||||
};
|
||||
return await sendPayload(type, new Date().toISOString(), url, data, payloadTemplate);
|
||||
} catch (_err) {
|
||||
const err = getErrorFromUnknown(_err);
|
||||
const error = getErrorFromUnknown(_err);
|
||||
return {
|
||||
ok: false,
|
||||
status: 500,
|
||||
message: err.message,
|
||||
message: error.message,
|
||||
};
|
||||
}
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user