fch 3.0.2 → 3.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/fetch.js CHANGED
@@ -165,6 +165,7 @@ const create = (defaults = {}) => {
165
165
  fch.method = defaults.method ?? "get";
166
166
  fch.query = defaults.query ?? {};
167
167
  fch.headers = defaults.headers ?? {};
168
+ fch.baseUrl = defaults.baseUrl ?? defaults.baseURL ?? null;
168
169
 
169
170
  // Default options
170
171
  fch.dedupe = defaults.dedupe ?? true;
package/fetch.min.js ADDED
@@ -0,0 +1 @@
1
+ const hasPlainBody=e=>{if(!e.headers["content-type"]&&["object","array"].includes(typeof e.body)&&!(e.body instanceof FormData))return!0},createUrl=(e,t,r)=>{let[o,a={}]=e.split("?");const s=new URLSearchParams({...Object.fromEntries(new URLSearchParams(t)),...Object.fromEntries(new URLSearchParams(a))}).toString();if(s&&(o=o+"?"+s),!r)return o;return new URL(o,r).href},createHeaders=(e,t)=>{const r={...t,...e};for(let e in r){const t=r[e];delete r[e],r[e.toLowerCase()]=t}return r},createDedupe=(e,t)=>({save:r=>(e.set(t,r),r),get:()=>e.get(t),clear:()=>e.delete(t)}),createFetch=(e,{after:t,dedupe:r,error:o,output:a})=>fetch(e.url,e).then((async e=>{r&&r.clear();let s={status:e.status,statusText:e.statusText,headers:{}};for(let t of e.headers.keys())s.headers[t.toLowerCase()]=e.headers.get(t);if(!e.ok){const t=new Error(e.statusText);return t.response=s,o(t)}const d=e.headers.get("content-type").includes("application/json");return s.body=await(d?e.json():e.text()),t&&(s=t(s)),"body"===a?s.body:s})),create=(e={})=>{const t=new Map,r=async(e,o={})=>{"object"!=typeof o&&(o={}),o="string"==typeof e?{url:e,...o}:e||{};let{dedupe:a=r.dedupe,output:s=r.output,baseURL:d=r.baseURL,baseUrl:n=d||r.baseUrl,query:u={},before:c=r.before,after:h=r.after,error:l=r.error,...p}=o;if(u={...r.query,...u},p.url=createUrl(p.url??r.url,u,n),p.method=(p.method??r.method).toLowerCase(),p.headers=createHeaders(p.headers,r.headers),"get"!==p.method&&(a=!1),a&&(a=createDedupe(t,p.url)),!["body","response"].includes(s)){throw new Error(`options.output needs to be either "body" (default) or "response", not "${s}"`)}return hasPlainBody(p)&&(p.body=JSON.stringify(p.body),p.headers["content-type"]="application/json; charset=utf-8"),c&&(p=c(p)),a&&!p.signal?a.get()?a.get():a.save(createFetch(p,{dedupe:a,output:s,error:l,after:h})):createFetch(p,{output:s,error:l,after:h})};r.url=e.url??"/",r.method=e.method??"get",r.query=e.query??{},r.headers=e.headers??{},r.baseUrl=e.baseUrl??e.baseURL??null,r.dedupe=e.dedupe??!0,r.output=e.output??"body",r.credentials=e.credentials??"include",r.before=e.before??(e=>e),r.after=e.after??(e=>e),r.error=e.error??(e=>Promise.reject(e));return r.get=(e,t={})=>r(e,{...t}),r.head=(e,t={})=>r(e,{...t,method:"head"}),r.post=(e,t={})=>r(e,{...t,method:"post"}),r.patch=(e,t={})=>r(e,{...t,method:"patch"}),r.put=(e,t={})=>r(e,{...t,method:"put"}),r.del=(e,t={})=>r(e,{...t,method:"delete"}),r.create=create,r},fch=create();window.fch=fch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fch",
3
- "version": "3.0.2",
3
+ "version": "3.0.5",
4
4
  "description": "Fetch interface with better promises, deduplication, defaults, etc.",
5
5
  "homepage": "https://github.com/franciscop/fetch",
6
6
  "repository": "https://github.com/franciscop/fetch.git",
@@ -9,7 +9,7 @@
9
9
  "author": "Francisco Presencia <public@francisco.io> (https://francisco.io/)",
10
10
  "license": "MIT",
11
11
  "scripts": {
12
- "build": "terser --compress --mangle -- ./fetch.js | sed 's/export default fch;/window.fch=fch;/g' > ./index.js",
12
+ "build": "terser --compress --mangle -- ./fetch.js | sed 's/export default fch;/window.fch=fch;/g' > ./fetch.min.js",
13
13
  "start": "npm run watch # Start ~= Start dev",
14
14
  "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --detectOpenHandles",
15
15
  "watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --coverage --detectOpenHandles"
@@ -23,9 +23,11 @@
23
23
  "ajax"
24
24
  ],
25
25
  "main": "./fetch.js",
26
- "browser": "./index.js",
26
+ "module": "./fetch.js",
27
+ "browser": "./fetch.min.js",
27
28
  "files": [
28
- "fetch.js"
29
+ "fetch.js",
30
+ "fetch.min.js"
29
31
  ],
30
32
  "type": "module",
31
33
  "engines": {
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # Fch [![npm install fch](https://img.shields.io/badge/npm%20install-fch-blue.svg)](https://www.npmjs.com/package/fch) [![gzip size](https://img.badgesize.io/franciscop/fetch/master/index.js.svg?compression=gzip)](https://github.com/franciscop/fetch/blob/master/index.js)
1
+ # Fch [![npm install fch](https://img.shields.io/badge/npm%20install-fch-blue.svg)](https://www.npmjs.com/package/fch) [![gzip size](https://img.badgesize.io/franciscop/fetch/master/fetch.min.js.svg?compression=gzip)](https://github.com/franciscop/fetch/blob/master/fetch.min.js)
2
2
 
3
3
  A tiny library to make API calls easier. Similar to Axios, but tiny size and simpler API:
4
4