From 634f9bb66e656a7384238455af92ed2ce67afa52 Mon Sep 17 00:00:00 2001 From: Andy Shinn Date: Mon, 25 Jan 2021 15:41:03 -0600 Subject: [PATCH 1/2] initial support for CORS per-hook closes #63 --- src/config.ts | 2 +- src/options.ts | 21 +++++++++++++++------ src/webhook.ts | 3 ++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/config.ts b/src/config.ts index d125305..fff5e79 100644 --- a/src/config.ts +++ b/src/config.ts @@ -30,7 +30,7 @@ export const EVENTS = [ ]; export const DEFAULT_CONFIG = { - webhooks: Object.fromEntries(EVENTS.map((e) => [e, { url: "" }])), + webhooks: Object.fromEntries(EVENTS.map((e) => [e, { url: "", cors: false }])), } as Config; export interface Config { diff --git a/src/options.ts b/src/options.ts index d0f9f75..8390150 100644 --- a/src/options.ts +++ b/src/options.ts @@ -24,7 +24,9 @@ function saveOptions(): void { EVENTS.forEach((e: string) => { // @ts-ignore-next-line - config.webhooks[e].url = getHtmlInputElement(e).value; + config.webhooks[e].url = getHtmlInputElement(`${e}.url`).value; + // @ts-ignore-next-line + config.webhooks[e].cors = getHtmlInputElement(`${e}.cors`).checked; }); setConfig(config); @@ -35,7 +37,9 @@ async function restoreOptions(): Promise { EVENTS.forEach((e) => { // @ts-ignore-next-line - getHtmlInputElement(e).value = config.webhooks[e].url; + getHtmlInputElement(`${e}.url`).value = config.webhooks[e].url; + // @ts-ignore-next-line + getHtmlInputElement(`${e}.cors`).checked = config.webhooks[e].cors; }); } @@ -46,13 +50,18 @@ EVENTS.forEach((e) => { "beforeend", `
- - + + ${description} - + +
` ); - document.getElementById(e).addEventListener("change", () => { + document.getElementById(`${e}.url`).addEventListener("change", () => { + saveOptions(); + }); + + document.getElementById(`${e}.cors`).addEventListener("change", () => { saveOptions(); }); }); diff --git a/src/webhook.ts b/src/webhook.ts index 63f76ae..b0b49b4 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -3,6 +3,7 @@ import { State } from "./runner"; export interface Webhook { url: string; method?: string; + cors: boolean; } export const PLACEHOLDER_WEBHOOK = { url: "" }; @@ -34,7 +35,7 @@ export function send(state: State, webhook: Webhook): Promise { options = { method: "POST", body: renderBody(state), - mode: "no-cors", + mode: webhook.cors ? "cors" : "no-cors", headers: { "Content-Type": "application/json", }, From 9a853c7e5878fbb543c326d48df9d64e8a84e0b5 Mon Sep 17 00:00:00 2001 From: Andy Shinn Date: Mon, 25 Jan 2021 17:19:01 -0600 Subject: [PATCH 2/2] send form urlencoded data if not CORS closes #69 --- src/webhook.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/webhook.ts b/src/webhook.ts index b0b49b4..e440bc2 100644 --- a/src/webhook.ts +++ b/src/webhook.ts @@ -1,4 +1,4 @@ -import { State } from "./runner"; +import { State, InputState } from "./runner"; export interface Webhook { url: string; @@ -18,6 +18,10 @@ export function renderBody(state: State): string { return JSON.stringify(state); } +export function renderUrlEncoded(state: State): string { + return Object.entries(state.input).map((k, i) => `${k[0]}=${k[1]}`).join('&') +} + export function send(state: State, webhook: Webhook): Promise { let options: RequestInit; @@ -34,10 +38,10 @@ export function send(state: State, webhook: Webhook): Promise { default: options = { method: "POST", - body: renderBody(state), + body: webhook.cors ? renderBody(state) : renderUrlEncoded(state), mode: webhook.cors ? "cors" : "no-cors", headers: { - "Content-Type": "application/json", + "Content-Type": webhook.cors ? "application/json" : "application/x-www-form-urlencoded", }, }; }