forked from calcom/cal.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Webhook tweaks + Support added for "Custom payload templates" / x-www…
…-form-urlencoded / json (calcom#1193) * Changed styling of webhook test & updated <Form> component * Implements custom webhook formats Co-authored-by: Bailey Pumfleet <[email protected]>
- Loading branch information
1 parent
ecc960f
commit 5b3dd02
Showing
9 changed files
with
170 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
|
||
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); | ||
data: Omit<CalendarEvent, "language">, | ||
template?: string | null | ||
) => { | ||
if (!subscriberUrl || !data) { | ||
throw new Error("Missing required elements to send webhook payload."); | ||
} | ||
|
||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
prisma/migrations/20211120211639_add_payload_template/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Webhook" ADD COLUMN "payloadTemplate" TEXT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.