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
@@ -1,38 +1,63 @@
|
||||
import { compile } from "handlebars";
|
||||
|
||||
import { CalendarEvent } from "@lib/calendarClient";
|
||||
|
||||
const sendPayload = (
|
||||
type ContentType = "application/json" | "application/x-www-form-urlencoded";
|
||||
|
||||
function applyTemplate(template: string, data: Omit<CalendarEvent, "language">, contentType: ContentType) {
|
||||
const compiled = compile(template)(data);
|
||||
if (contentType === "application/json") {
|
||||
return jsonParse(compiled);
|
||||
}
|
||||
return compiled;
|
||||
}
|
||||
|
||||
function jsonParse(jsonString: string) {
|
||||
try {
|
||||
return JSON.parse(jsonString);
|
||||
} catch (e) {
|
||||
// don't do anything.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const sendPayload = async (
|
||||
triggerEvent: string,
|
||||
createdAt: string,
|
||||
subscriberUrl: string,
|
||||
payload: CalendarEvent
|
||||
): Promise<string | Response> =>
|
||||
new Promise((resolve, reject) => {
|
||||
if (!subscriberUrl || !payload) {
|
||||
return reject(new Error("Missing required elements to send webhook payload."));
|
||||
}
|
||||
const body = {
|
||||
triggerEvent: triggerEvent,
|
||||
createdAt: createdAt,
|
||||
payload: payload,
|
||||
};
|
||||
data: Omit<CalendarEvent, "language">,
|
||||
template?: string | null
|
||||
) => {
|
||||
if (!subscriberUrl || !data) {
|
||||
throw new Error("Missing required elements to send webhook payload.");
|
||||
}
|
||||
|
||||
fetch(subscriberUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
reject(new Error(`Response code ${response.status}`));
|
||||
return;
|
||||
}
|
||||
resolve(response);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
const contentType =
|
||||
!template || jsonParse(template) ? "application/json" : "application/x-www-form-urlencoded";
|
||||
|
||||
const body = template
|
||||
? applyTemplate(template, data, contentType)
|
||||
: JSON.stringify({
|
||||
triggerEvent: triggerEvent,
|
||||
createdAt: createdAt,
|
||||
payload: data,
|
||||
});
|
||||
|
||||
const response = await fetch(subscriberUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": contentType,
|
||||
},
|
||||
body,
|
||||
});
|
||||
|
||||
const text = await response.text();
|
||||
|
||||
return {
|
||||
ok: response.ok,
|
||||
status: response.status,
|
||||
message: text,
|
||||
};
|
||||
};
|
||||
|
||||
export default sendPayload;
|
||||
@@ -2,7 +2,7 @@ import { WebhookTriggerEvents } from "@prisma/client";
|
||||
|
||||
import prisma from "@lib/prisma";
|
||||
|
||||
const getSubscriberUrls = async (userId: number, triggerEvent: WebhookTriggerEvents): Promise<string[]> => {
|
||||
const getSubscribers = async (userId: number, triggerEvent: WebhookTriggerEvents) => {
|
||||
const allWebhooks = await prisma.webhook.findMany({
|
||||
where: {
|
||||
userId: userId,
|
||||
@@ -17,11 +17,11 @@ const getSubscriberUrls = async (userId: number, triggerEvent: WebhookTriggerEve
|
||||
},
|
||||
select: {
|
||||
subscriberUrl: true,
|
||||
payloadTemplate: true,
|
||||
},
|
||||
});
|
||||
const subscriberUrls = allWebhooks.map(({ subscriberUrl }) => subscriberUrl);
|
||||
|
||||
return subscriberUrls;
|
||||
return allWebhooks;
|
||||
};
|
||||
|
||||
export default getSubscriberUrls;
|
||||
export default getSubscribers;
|
||||
Reference in new issue
Block a user