simple-typed-fetch
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
import"isomorphic-unfetch";import{err as s,fromPromise as g,fromThrowable as I,ok as l}from"neverthrow";async function y(t,i,n,c){let p={...n,headers:n?.headers??{}},m={requestOptions:p,url:t},E=await g(fetch(t,p),r=>r instanceof Error?s({type:"fetchError",...m,message:`Fetch error: ${r.message}. URL: ${t}`,error:r}):s({type:"unknownFetchThrow",...m,message:`Unknown fetch error. URL: ${t}`,error:r}));if(E.isErr())return E.error;let e=E.value,$={...m,response:e},d=await g(e.text(),r=>r instanceof Error?s({type:"unknownGetTextError",...$,message:`Can't get response content: ${r.message}. URL: ${t}. HTTP ${e.status}`,error:r}):s({type:"unknownGetTextUnknownError",...$,message:`Can't get response content: Unknown error. URL: ${t}. HTTP ${e.status}`,error:r}));if(d.isErr())return d.error;let a=d.value,o={...$,text:a},u=I(JSON.parse,r=>r instanceof Error?s({type:"jsonParseError",...o,message:`Can't parse response as JSON: ${r.message}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,error:r}):s({type:"jsonParseUnknownError",...o,message:`Unknown JSON parse error. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,error:r}))(a);if(u.isErr()){let r=i.safeParse(a);return r.success?l({...o,data:r.data}):s({type:"jsonParseError",...o,message:`Can't parse response as JSON: ${u.error.error.message}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,error:u.error.error})}let O=u.value,f;if(c){let r=c.safeParse(O);r.success&&(f=r.data)}if(e.status>=500)return s({type:"serverError",...o,message:`Server error. URL: ${t}. HTTP ${e.status} ${e.statusText}. Original response: ${a}. ${f?`Data: ${JSON.stringify(f)}`:""}`});if(e.status>=400)return s({type:"clientError",...o,message:`Error: ${e.status} ${e.statusText}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,errorMetadata:f});let h=i.safeParse(O);if(!h.success){let r=h.error.issues.map(T=>`[${T.path.join(".")}] ${T.message}`).join(", ");return s({type:"payloadParseError",...o,message:`Can't recognize response payload: ${r}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`})}return l({...o,data:h.data})}function P(t){return async(...i)=>{let n=await t(...i);if(n.isErr()){let{message:c,url:p}=n.error;throw new Error(`${c} (${p})`)}return n.value.data}}export{y as fetchWithValidation,P as simpleFetch}; | ||
import{err as s,fromPromise as g,fromThrowable as I,ok as l}from"neverthrow";async function y(t,i,n,c){let p={...n,headers:n?.headers??{}},m={requestOptions:p,url:t},E=await g(fetch(t,p),r=>r instanceof Error?s({type:"fetchError",...m,message:`Fetch error: ${r.message}. URL: ${t}`,error:r}):s({type:"unknownFetchThrow",...m,message:`Unknown fetch error. URL: ${t}`,error:r}));if(E.isErr())return E.error;let e=E.value,$={...m,response:e},d=await g(e.text(),r=>r instanceof Error?s({type:"unknownGetTextError",...$,message:`Can't get response content: ${r.message}. URL: ${t}. HTTP ${e.status}`,error:r}):s({type:"unknownGetTextUnknownError",...$,message:`Can't get response content: Unknown error. URL: ${t}. HTTP ${e.status}`,error:r}));if(d.isErr())return d.error;let a=d.value,o={...$,text:a},u=I(JSON.parse,r=>r instanceof Error?s({type:"jsonParseError",...o,message:`Can't parse response as JSON: ${r.message}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,error:r}):s({type:"jsonParseUnknownError",...o,message:`Unknown JSON parse error. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,error:r}))(a);if(u.isErr()){let r=i.safeParse(a);return r.success?l({...o,data:r.data}):s({type:"jsonParseError",...o,message:`Can't parse response as JSON: ${u.error.error.message}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,error:u.error.error})}let O=u.value,f;if(c){let r=c.safeParse(O);r.success&&(f=r.data)}if(e.status>=500)return s({type:"serverError",...o,message:`Server error. URL: ${t}. HTTP ${e.status} ${e.statusText}. Original response: ${a}. ${f?`Data: ${JSON.stringify(f)}`:""}`});if(e.status>=400)return s({type:"clientError",...o,message:`Error: ${e.status} ${e.statusText}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`,errorMetadata:f});let h=i.safeParse(O);if(!h.success){let r=h.error.issues.map(T=>`[${T.path.join(".")}] ${T.message}`).join(", ");return s({type:"payloadParseError",...o,message:`Can't recognize response payload: ${r}. URL: ${t}. HTTP ${e.status}. Original response: ${a}`})}return l({...o,data:h.data})}function P(t){return async(...i)=>{let n=await t(...i);if(n.isErr()){let{message:c,url:p}=n.error;throw new Error(`${c} (${p})`)}return n.value.data}}export{y as fetchWithValidation,P as simpleFetch}; | ||
//# sourceMappingURL=index.js.map |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "0.2.17", | ||
"version": "0.3.0", | ||
"description": "Making HTTP requests human way", | ||
@@ -33,3 +33,2 @@ "main": "./dist/index.cjs", | ||
"fetch", | ||
"unfetch", | ||
"zod", | ||
@@ -41,3 +40,2 @@ "typed" | ||
"dependencies": { | ||
"isomorphic-unfetch": "^4.0.2", | ||
"neverthrow": "^8.1.1", | ||
@@ -52,2 +50,3 @@ "patch-package": "^8.0.0", | ||
"devDependencies": { | ||
"@types/node": "^22.10.1", | ||
"@typescript-eslint/eslint-plugin": ">=8.17.0", | ||
@@ -61,8 +60,3 @@ "@typescript-eslint/parser": ">=8.17.0", | ||
"typescript": "^5.7.2" | ||
}, | ||
"pnpm": { | ||
"patchedDependencies": { | ||
"unfetch@5.0.0": "patches/unfetch@5.0.0.patch" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3
-25%123622
-0.39%9
12.5%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed