fch 4.1.1 → 4.1.3

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.
Files changed (2) hide show
  1. package/index.min.js +1 -0
  2. package/package.json +4 -2
package/index.min.js ADDED
@@ -0,0 +1 @@
1
+ const e=async r=>(r=await r,Array.isArray(r)?await Promise.all(r.map(e)):r),r=(e,r)=>(...t)=>(e=>e instanceof RegExp?e.test.bind(e):e)(e).call(r,...t),t=(e,t)=>async(n,a,o)=>({value:n,extra:await r(e,t)(n,a,o)}),n=({extra:e})=>e,a=({value:e})=>e,o={every:async(e,t,n)=>{for(let a=0;a<e.length;a++)if(!await r(t,n)(e[a],a,e))return!1;return!0},filter:async(r,o,s)=>(await e(r.map(t(o,s)))).filter(n).map(a),find:async(e,t,n)=>{for(let a=0;a<e.length;a++)if(await r(t,n)(e[a],a,e))return e[a]},findIndex:async(e,t,n)=>{for(let a=0;a<e.length;a++)if(await r(t,n)(e[a],a,e))return a;return-1},forEach:async(r,n,a)=>(await e(r.map(t(n,a))),r),reduce:async(e,t,n)=>{const a=void 0!==n;a||(n=e[0]);for(let o=a?0:1;o<e.length;o++)n=await r(t)(n,e[o],o,e);return n},reduceRight:async(e,t,n)=>{const a=void 0!==n;a||(n=e[e.length-1]);for(let o=e.length-(a?1:2);o>=0;o--)n=await r(t)(n,e[o],o,e);return n},some:async(e,t,n)=>{for(let a=0;a<e.length;a++)if(await r(t,n)(e[a],a,e))return!0;return!1}},s=(r,t)=>(n,a)=>"then"===a?(...t)=>e(r).then(...t):"catch"===a?(...t)=>l(e(r).catch(...t)):u(e(r).then(e=>"symbol"==typeof a?e[a]:a in t?u((...r)=>t[a](e,...r),t):"number"==typeof e&&a in t.number?u((...r)=>t.number[a](e,...r),t):"string"==typeof e&&a in t.string?u((...r)=>t.string[a](e,...r),t):Array.isArray(e)&&a in t.array?u((...r)=>t.array[a](e,...r),t):e[a]&&e[a].bind?u(e[a].bind(e),t):u(e[a],t)),t),i=(r,t)=>(n,a,o)=>u(e(r).then(e=>{return"function"!=typeof e?(r=`You tried to call "${JSON.stringify(e)}" (${typeof e}) as a function, but it is not.`,Promise.reject(new Error(r))):e(...o);var r}),t),u=(e,r)=>new Proxy(()=>{},{get:s(e,r),apply:i(e,r)});function l(e,{number:r,string:t,array:n,...a}={}){return"function"==typeof e?(...o)=>l(Promise.all(o).then(r=>e(...r)),{number:r,string:t,array:n,...a}):new Proxy({},{get:s(e,{number:{...r},string:{...t},array:{...o,...n},...a})})}const c=e=>{if("object"!=typeof e)return e;for(let r in e)void 0===e[r]&&delete e[r];return e},d=async e=>{const r=e.headers.get("content-type"),t=r&&r.includes("application/json"),n=await e.clone().text();return t?JSON.parse(n):n},f=async(e,r)=>{let t={status:e.status,statusText:e.statusText,headers:{}};for(let r of e.headers.keys())t.headers[r.toLowerCase()]=e.headers.get(r);if(!e.ok){const n=new Error(e.statusText);throw n.response=t,r(n)}return t.body=await d(e),t},y=(e,{after:r,cache:t,error:n,output:a})=>{const o={},s={text:()=>o.res.text(),json:()=>o.res.json(),blob:()=>o.res.blob(),arrayBuffer:()=>o.res.arrayBuffer(),formData:()=>o.res.formData(),body:()=>d(o.res),clone:()=>o.res.clone(),raw:()=>o.res.clone(),response:()=>f(o.res.clone(),n)};return l(fetch(e.url,e).then(async e=>{if(o.res=e,t&&t.clear(),e.ok&&"stream"===a)return e.body;if(e.ok&&e[a]&&"function"==typeof e[a])return e[a]();const s=r(await f(e,n));if("body"===a)return s.body;if("response"===a)return s;if("raw"===a)return e.clone();throw new Error(`Invalid option output="${a}"`)}),s)},p=(e={})=>{var r,t,n,a,o,s,i,u,l,d,f,h;const b=(()=>{const e=new Map;return r=>({save:t=>(e.set(r,t),t),get:()=>e.get(r),clear:()=>e.delete(r)})})(),w=(e="/",r={})=>{var t;"object"!=typeof r&&(r={}),r="string"==typeof e?{url:e,...r}:e;let{dedupe:n,output:a,before:o,after:s,error:i,...u}={...w,...r};u.url=((e,r,t)=>{let[n,a={}]=e.split("?");const o=new URLSearchParams(Object.fromEntries([...new URLSearchParams(c(r)),...new URLSearchParams(c(a))])).toString();if(o&&(n=n+"?"+o),!t)return n;return new URL(n.replace(/^\//,""),t).href})(u.url,{...w.query,...r.query},null!==(t=u.baseUrl)&&void 0!==t?t:u.baseURL),u.method=u.method.toLowerCase(),u.headers=(e=>{const r={};for(let[t,n]of Object.entries(e))r[t.toLowerCase()]=n;return r})({...w.headers,...r.headers});const l=!(!n||"get"!==u.method)&&b(u.url);var d;return"object"!=typeof(d=u.body)||d instanceof FormData||d.pipe||(u.body=JSON.stringify(c(u.body)),u.headers["content-type"]="application/json"),u=o(u),l&&!u.signal?l.get()?l.get():l.save(y(u,{cache:l,output:a,error:i,after:s})):y(u,{output:a,error:i,after:s})};return w.url=null!==(r=e.url)&&void 0!==r?r:"/",w.method=null!==(t=e.method)&&void 0!==t?t:"get",w.query=null!==(n=e.query)&&void 0!==n?n:{},w.headers=null!==(a=e.headers)&&void 0!==a?a:{},w.baseUrl=null!==(o=null!==(s=e.baseUrl)&&void 0!==s?s:e.baseURL)&&void 0!==o?o:null,w.dedupe=null===(i=e.dedupe)||void 0===i||i,w.output=null!==(u=e.output)&&void 0!==u?u:"body",w.credentials=null!==(l=e.credentials)&&void 0!==l?l:"include",w.before=null!==(d=e.before)&&void 0!==d?d:e=>e,w.after=null!==(f=e.after)&&void 0!==f?f:e=>e,w.error=null!==(h=e.error)&&void 0!==h?h:e=>{throw e},["get","head","post","patch","put","delete","del"].map(e=>{w[e]=(r,t={})=>w(r,{...t,method:e})}),w.create=p,w};"undefined"!=typeof window&&(window.fch=p());var h=p();export default h;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fch",
3
- "version": "4.1.1",
3
+ "version": "4.1.3",
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",
@@ -23,7 +23,9 @@
23
23
  "ajax"
24
24
  ],
25
25
  "main": "./index.min.js",
26
- "files": [],
26
+ "files": [
27
+ "./index.min.js"
28
+ ],
27
29
  "type": "module",
28
30
  "engines": {
29
31
  "node": ">=18.0.0"