Skip to content

Commit

Permalink
Export ES2021 code
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Jun 29, 2022
1 parent 17d161e commit 780ddf5
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 69 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
env: {}

# DO NOT EDIT BELOW, USE: npx ghat fregante/ghatemplates/node --exclude 'jobs.Test'
# FILE GENERATED WITH: npx ghat fregante/ghatemplates/node
# SOURCE: /~https://github.com/fregante/ghatemplates
# OPTIONS: {"exclude":["jobs.Test"]}

name: CI
on:
Expand All @@ -11,6 +13,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- name: install
run: npm ci || npm install
- name: XO
Expand All @@ -22,4 +27,4 @@ jobs:
- name: install
run: npm ci || npm install
- name: build
run: npm run build --if-present
run: npm run build
26 changes: 14 additions & 12 deletions .github/workflows/esm-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ env:
IMPORT_TEXT: import pushForm from
NPM_MODULE_NAME: push-form

# DO NOT EDIT BELOW, USE: npx ghat fregante/ghatemplates/esm-lint
# FILE GENERATED WITH: npx ghat fregante/ghatemplates/esm-lint
# SOURCE: /~https://github.com/fregante/ghatemplates

name: ESM
on:
Expand All @@ -12,6 +13,7 @@ on:
push:
branches:
- master
- main
jobs:
Pack:
runs-on: ubuntu-latest
Expand All @@ -20,7 +22,7 @@ jobs:
- run: npm install
- run: npm run build --if-present
- run: npm pack --dry-run
- run: npm pack --silent 2>/dev/null | xargs cat | tar -xz
- run: npm pack | tail -1 | xargs -n1 tar -xzf
- uses: actions/upload-artifact@v2
with:
path: package
Expand All @@ -30,33 +32,33 @@ jobs:
steps:
- uses: actions/download-artifact@v2
- run: npm install ./artifact
- run: 'echo "${{ env.IMPORT_TEXT }} ''${{ env.NPM_MODULE_NAME }}''" > index.js'
- run: webpack ./index.js
- run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js
- run: webpack --entry ./index.js
- run: cat dist/main.js
Parcel:
runs-on: ubuntu-latest
needs: Pack
steps:
- uses: actions/download-artifact@v2
- run: npm install ./artifact
- run: 'echo "${{ env.IMPORT_TEXT }} ''${{ env.NPM_MODULE_NAME }}''" > index.js'
- run: npx parcel@1 build index.js
- run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js
- run: npx parcel@2 build index.js
- run: cat dist/index.js
Rollup:
runs-on: ubuntu-latest
needs: Pack
steps:
- uses: actions/download-artifact@v2
- run: npm install ./artifact rollup@2 @rollup/plugin-node-resolve
- run: 'echo "${{ env.IMPORT_TEXT }} ''${{ env.NPM_MODULE_NAME }}''" > index.js'
- run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js
- run: npx rollup -p node-resolve index.js
Snowpack:
runs-on: ubuntu-latest
needs: Pack
steps:
- uses: actions/download-artifact@v2
- run: 'echo ''{}'' > package.json'
- run: 'echo "${{ env.IMPORT_TEXT }} ''${{ env.NPM_MODULE_NAME }}''" > index.js'
- run: echo '{}' > package.json
- run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.js
- run: npm install ./artifact
- run: npx snowpack@2 build
- run: cat build/web_modules/$NPM_MODULE_NAME.js
Expand All @@ -66,7 +68,7 @@ jobs:
steps:
- uses: actions/download-artifact@v2
- run: npm install ./artifact
- run: 'echo "${{ env.IMPORT_TEXT }} ''${{ env.NPM_MODULE_NAME }}''" > index.ts'
- run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.ts
- run: tsc index.ts
- run: cat index.js
Node:
Expand All @@ -76,7 +78,7 @@ jobs:
- uses: actions/download-artifact@v2
- uses: actions/setup-node@v1
with:
node-version: 13.x
- run: 'echo "${{ env.IMPORT_TEXT }} ''${{ env.NPM_MODULE_NAME }}''" > index.mjs'
node-version: 14.x
- run: echo "${{ env.IMPORT_TEXT }} '${{ env.NPM_MODULE_NAME }}'" > index.mjs
- run: npm install ./artifact
- run: node index.mjs
32 changes: 16 additions & 16 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
async function pushForm(
let localFetch = globalThis.fetch;

export function setFetch(fetch: typeof localFetch): void {
localFetch = fetch;
}

export default async function pushForm(
form: HTMLFormElement,
init: RequestInit = {}
init: RequestInit = {},
): Promise<Response> {
const fields = new FormData(form);
const url = new URL(form.getAttribute('action')!, location.origin);
init.headers = new Headers(init.headers);
if (!init.headers.has('Accept')) {
init.headers.append('Accept', 'text/html,application/xhtml+xml,application/xml');
init.headers.append(
'Accept',
'text/html,application/xhtml+xml,application/xml',
);
}

init.method = form.method;
Expand All @@ -21,7 +30,7 @@ async function pushForm(
init.headers.append('Cache-Control', 'max-age=0');
}

return (pushForm.fetch ?? fetch)(url.toString(), init);
return localFetch(url.href, init);
}

interface Options {
Expand All @@ -39,17 +48,16 @@ function onSuccessDefault(): void {
alert('Thanks for your submission');
}

function ajaxifyForm(
export function ajaxifyForm(
form: HTMLFormElement,
{
onSuccess = onSuccessDefault,
onError = onErrorDefault,
request = {}
}: Options = {}
request = {},
}: Options = {},
): () => void {
const submitHandler = async (event: Event) => {
event.preventDefault();
form.disable = true;
try {
const response = await pushForm(form, request);
if (!response.ok) {
Expand All @@ -68,11 +76,3 @@ function ajaxifyForm(
form.removeEventListener('submit', submitHandler);
};
}

// Allow the user to override the fetch globally
namespace pushForm {
export let fetch: typeof window.fetch;
}

export default pushForm;
export {ajaxifyForm};
22 changes: 5 additions & 17 deletions license
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License

Copyright (c) Federico Brigante <opensource@bfred.it> (bfred.it)
Copyright (c) Federico Brigante <me@fregante.com> (https://fregante.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"version": "0.8.0",
"description": "Zero-effort nanomodule to submit a <form> element via `fetch` and receive the response.",
"keywords": [
"fetch",
"form",
"submit",
"fetch",
"post",
"form",
"js",
"xhr",
"ajax",
"ajaxify",
"ajaxed",
"file",
"js",
"xhr",
"upload"
],
"repository": "fregante/push-form",
Expand Down Expand Up @@ -39,8 +41,8 @@
}
},
"devDependencies": {
"@sindresorhus/tsconfig": "^0.8.0",
"typescript": "^4.1.2",
"xo": "^0.35.0"
"@sindresorhus/tsconfig": "^3.0.1",
"typescript": "^4.7.4",
"xo": "^0.50.0"
}
}
24 changes: 10 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,9 @@
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/push-form.svg?label=gzipped
[link-bundlephobia]: https://bundlephobia.com/result?p=push-form

> Zero-effort nanomodule to submit a `<form>` element via `fetch` and receive the response.
> Like `form.submit()`, but ajaxed. Lightweight, modern, promisified, uses `fetch`
Sit back and relax ’cuz `push-form` will take care of all your form-submitting needs. — As long as all you need is to submit a `<form>` element as-is.

You'll just need:

```js
pushForm(document.querySelector('form')).then(response => {
if (response.ok) {
alert('Thanks for your submission!');
}
});
```
Instead of causing a page load like `form.submit()` does, you can use `pushForm(form)` to send a form via `fetch`. It automatically reads all the fields using modern APIs and performs the request exactly as described by the `form` attributes.

## Install

Expand Down Expand Up @@ -47,10 +37,16 @@ You can post it via `fetch` with:
import pushForm from 'push-form';

const form = document.querySelector('form');
await pushForm(form);
form.addEventListener('submit', async event => {
event.preventDefault();
const response = await pushForm();
if (response.ok) {
alert('Thanks for your submission!');
}
});
```

Or you can handle the submissing with:
Or use `ajaxifyForm` to have it handle the user submission automatically:

```js
import {ajaxifyForm} from 'push-form';
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "@sindresorhus/tsconfig",
"compilerOptions": {
"target": "es2017",
"target": "es2021",
"module": "es2015"
},
"files": [
Expand Down

0 comments on commit 780ddf5

Please sign in to comment.