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

default to urlencoded data if not CORS #71

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 9 additions & 4 deletions src/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { State } from "./runner";
import { State, InputState } from "./runner";

export interface Webhook {
url: string;
method?: string;
cors: boolean;
}

export const PLACEHOLDER_WEBHOOK = { url: "" };
Expand All @@ -17,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<Response> {
let options: RequestInit;

Expand All @@ -33,10 +38,10 @@ export function send(state: State, webhook: Webhook): Promise<Response> {
default:
options = {
method: "POST",
body: renderBody(state),
mode: "no-cors",
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",
},
};
}
Expand Down