Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
initial support for CORS per-hook
Browse files Browse the repository at this point in the history
closes #63
  • Loading branch information
andyshinn committed Jan 25, 2021
1 parent 3fb92b9 commit 634f9bb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
21 changes: 15 additions & 6 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -35,7 +37,9 @@ async function restoreOptions(): Promise<void> {

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;
});
}

Expand All @@ -46,13 +50,18 @@ EVENTS.forEach((e) => {
"beforeend",
`
<div class="form-group">
<label for="${e}">Event: <code>${e}</code></label>
<input type="url" class="form-control" id="${e}" placeholder="Webhook Url">
<label for="${e}.url">Event: <code>${e}</code></label>
<input type="url" class="form-control" id="${e}.url" placeholder="Webhook Url">
<small id="${e}.help" class="form-text text-muted">${description}</small>
<label for="${e}.cors">CORS</label>
<input type="checkbox" class="form-control" id="${e}.cors" placeholder="CORS">
</div>`
);
document.getElementById(e).addEventListener("change", () => {
document.getElementById(`${e}.url`).addEventListener("change", () => {
saveOptions();
});

document.getElementById(`${e}.cors`).addEventListener("change", () => {
saveOptions();
});
});
Expand Down
3 changes: 2 additions & 1 deletion src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { State } from "./runner";
export interface Webhook {
url: string;
method?: string;
cors: boolean;
}

export const PLACEHOLDER_WEBHOOK = { url: "" };
Expand Down Expand Up @@ -34,7 +35,7 @@ export function send(state: State, webhook: Webhook): Promise<Response> {
options = {
method: "POST",
body: renderBody(state),
mode: "no-cors",
mode: webhook.cors ? "cors" : "no-cors",
headers: {
"Content-Type": "application/json",
},
Expand Down

0 comments on commit 634f9bb

Please sign in to comment.