Skip to content

Commit

Permalink
Add proper support for GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Nov 23, 2020
1 parent de6f55d commit 5c36e38
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@ async function pushForm(
form: HTMLFormElement,
init: RequestInit = {}
): Promise<Response> {
// TODO: drop `as` after https://github.com/microsoft/TSJS-lib-generator/issues/741
init.body = new URLSearchParams(new FormData(form) as URLSearchParams);
init.method = form.method;
const fields = new FormData(form);
const url = new URL(form.action, location.origin);
const headers = new Headers(init.headers);
headers.append('Content-Type', 'application/x-www-form-urlencoded');

This comment has been minimized.

Copy link
@fregante
if (!headers.has('Accept')) {
headers.append('Accept', 'text/html,application/xhtml+xml,application/xml');
}

init.method = form.method;
if (form.method === 'get') {
for (const [name, value] of fields) {
if (typeof value === 'string') {
url.searchParams.set(name, value);
}
}
} else {
init.body = fields;
headers.append('Cache-Control', 'max-age=0');
}

return (pushForm.fetch ?? fetch)(form.action, init);
return (pushForm.fetch ?? fetch)(url.toString(), init);
}

// Silently allow the user to override the fetch globally
Expand Down

0 comments on commit 5c36e38

Please # to comment.