From 634f9bb66e656a7384238455af92ed2ce67afa52 Mon Sep 17 00:00:00 2001 From: Andy Shinn Date: Mon, 25 Jan 2021 15:41:03 -0600 Subject: [PATCH] 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", },