diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f84a7e..b817fc4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: @@ -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 @@ -22,4 +27,4 @@ jobs: - name: install run: npm ci || npm install - name: build - run: npm run build --if-present + run: npm run build diff --git a/.github/workflows/esm-lint.yml b/.github/workflows/esm-lint.yml index f1bced8..3bece05 100644 --- a/.github/workflows/esm-lint.yml +++ b/.github/workflows/esm-lint.yml @@ -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: @@ -12,6 +13,7 @@ on: push: branches: - master + - main jobs: Pack: runs-on: ubuntu-latest @@ -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 @@ -30,8 +32,8 @@ 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 @@ -39,8 +41,8 @@ jobs: 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 @@ -48,15 +50,15 @@ jobs: 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 @@ -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: @@ -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 diff --git a/index.ts b/index.ts index 5ec31f5..253181a 100644 --- a/index.ts +++ b/index.ts @@ -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 { 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; @@ -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 { @@ -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) { @@ -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}; diff --git a/license b/license index 7ebf4d0..e342b5c 100644 --- a/license +++ b/license @@ -1,21 +1,9 @@ -The MIT License (MIT) +MIT License -Copyright (c) Federico Brigante (bfred.it) +Copyright (c) Federico Brigante (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. diff --git a/package.json b/package.json index 0840026..f0bef49 100644 --- a/package.json +++ b/package.json @@ -3,14 +3,16 @@ "version": "0.8.0", "description": "Zero-effort nanomodule to submit a
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", @@ -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" } } diff --git a/readme.md b/readme.md index 4f82935..08d3ffa 100644 --- a/readme.md +++ b/readme.md @@ -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 `` 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 `` 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 @@ -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'; diff --git a/tsconfig.json b/tsconfig.json index 0545b2d..d597b0a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "@sindresorhus/tsconfig", "compilerOptions": { - "target": "es2017", + "target": "es2021", "module": "es2015" }, "files": [