fch 4.1.0 → 4.1.2
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/index.min.js +1 -0
- package/package.json +6 -5
- package/readme.md +2 -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=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},d=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 c(e),t},f=(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:()=>c(o.res),clone:()=>o.res.clone(),raw:()=>o.res.clone(),response:()=>d(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 d(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)},y=(e={})=>{var r,t,n,a,o,s,i,u,l,c,d,p;const h=(()=>{const e=new Map;return r=>({save:t=>(e.set(r,t),t),get:()=>e.get(r),clear:()=>e.delete(r)})})(),b=(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}={...b,...r};u.url=((e,r,t)=>{let[n,a={}]=e.split("?");const o=new URLSearchParams({...Object.fromEntries(new URLSearchParams(r)),...Object.fromEntries(new URLSearchParams(a))}).toString();if(o&&(n=n+"?"+o),!t)return n;return new URL(n,t).href})(u.url,{...b.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})({...b.headers,...r.headers});const l=!(!n||"get"!==u.method)&&h(u.url);var c;return"object"!=typeof(c=u.body)||c instanceof FormData||c.pipe||(u.body=JSON.stringify(u.body),u.headers["content-type"]="application/json"),u=o(u),l&&!u.signal?l.get()?l.get():l.save(f(u,{cache:l,output:a,error:i,after:s})):f(u,{output:a,error:i,after:s})};return b.url=null!==(r=e.url)&&void 0!==r?r:"/",b.method=null!==(t=e.method)&&void 0!==t?t:"get",b.query=null!==(n=e.query)&&void 0!==n?n:{},b.headers=null!==(a=e.headers)&&void 0!==a?a:{},b.baseUrl=null!==(o=null!==(s=e.baseUrl)&&void 0!==s?s:e.baseURL)&&void 0!==o?o:null,b.dedupe=null===(i=e.dedupe)||void 0===i||i,b.output=null!==(u=e.output)&&void 0!==u?u:"body",b.credentials=null!==(l=e.credentials)&&void 0!==l?l:"include",b.before=null!==(c=e.before)&&void 0!==c?c:e=>e,b.after=null!==(d=e.after)&&void 0!==d?d:e=>e,b.error=null!==(p=e.error)&&void 0!==p?p:e=>{throw e},["get","head","post","patch","put","delete","del"].map(e=>{b[e]=(r,t={})=>b(r,{...t,method:e})}),b.create=y,b};"undefined"!=typeof window&&(window.fch=y());var p=y();export default p;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fch",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.2",
|
|
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",
|
|
@@ -11,9 +11,8 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "rollup -c",
|
|
13
13
|
"size": "echo $(gzip -c index.min.js | wc -c) bytes",
|
|
14
|
-
"start": "
|
|
15
|
-
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --detectOpenHandles"
|
|
16
|
-
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --coverage --detectOpenHandles"
|
|
14
|
+
"start": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --coverage --detectOpenHandles",
|
|
15
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --detectOpenHandles"
|
|
17
16
|
},
|
|
18
17
|
"keywords": [
|
|
19
18
|
"fetch",
|
|
@@ -24,7 +23,9 @@
|
|
|
24
23
|
"ajax"
|
|
25
24
|
],
|
|
26
25
|
"main": "./index.min.js",
|
|
27
|
-
"files": [
|
|
26
|
+
"files": [
|
|
27
|
+
"./index.min.js"
|
|
28
|
+
],
|
|
28
29
|
"type": "module",
|
|
29
30
|
"engines": {
|
|
30
31
|
"node": ">=18.0.0"
|
package/readme.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Fch [](https://www.npmjs.com/package/fch) [](https://www.npmjs.com/package/fch) [](https://github.com/franciscop/fetch/blob/master/.github/workflows/tests.yml) [](https://github.com/franciscop/fetch/blob/master/index.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
|
|
|
@@ -257,7 +257,7 @@ When to use each?
|
|
|
257
257
|
|
|
258
258
|
### Output
|
|
259
259
|
|
|
260
|
-
The default output manipulation is to expect either
|
|
260
|
+
The default output manipulation is to expect either plain `TEXT` as `plain/text` or `JSON` as `application/json` from the `Content-Type`. If your API works with these (the vast majority of APIs do) then you should be fine out of the box!
|
|
261
261
|
|
|
262
262
|
```js
|
|
263
263
|
const cats = await api.get("/cats");
|