@svkhrobbeck/fetcher
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -1,1 +0,1 @@ | ||
const e={method:"GET",timeout:0,headers:{"Content-Type":"application/json"},transformRequest:[(e,t)=>e],transformResponse:[e=>e],adapter:"fetch"};function t(e,t){return function(...r){return e.apply(t,r)}}function r(e,t){const r=t?new URLSearchParams(t).toString():"";return r?`${e}?${r}`:e}function s(e,t={}){const r={...e,...t};return e.headers&&t.headers&&(r.headers={...e.headers,...t.headers}),r}function n(e,t,r){return r.forEach((r=>{e=r(e,t)})),e}function o(e,t){if(!e)return null;if(t&&t.includes("application/json"))return JSON.stringify(e);if(t&&t.includes("multipart/form-data")){const t=new FormData;return Object.keys(e).forEach((r=>t.append(r,e[r]))),t}if(t&&t.includes("application/x-www-form-urlencoded")){const t=new URLSearchParams;return Object.keys(e).forEach((r=>t.append(r,e[r]))),t.toString()}return e}class a{constructor(t){this.defaults=s(e,t),this.interceptors={request:new h,response:new h}}async request(e){const t=Date.now(),a=s(this.defaults,e);a.url=r(a.url,a.params);const u=new i(a.headers);a.headers=u.toObject(),a.data&&(a.data=o(a.data,u.get("Content-Type")));try{const e=await function(e){const t="xhr"===e.adapter?c:d;return new Promise(((r,s)=>{e.cancelToken&&e.cancelToken.throwIfRequested(),t(e).then((t=>{t.data=n(t.data,t.headers,e.transformResponse||[]),t.status>=200&&t.status<300?r(t):s(l.from(new Error("Request failed"),e,null,t))})).catch((t=>{s(l.from(t,e,null,null))}))}))}(a),r=Date.now();return{...e,duration:r-t,statusText:p[e.status]||"Unknown Status"}}catch(e){const r=Date.now();throw e.duration=r-t,e}}get(e,t={}){return this.request({...t,method:"GET",url:e})}post(e,t,r={}){return this.request({...r,method:"POST",url:e,data:t})}put(e,t,r={}){return this.request({...r,method:"PUT",url:e,data:t})}patch(e,t,r={}){return this.request({...r,method:"PATCH",url:e,data:t})}delete(e,t={}){return this.request({...t,method:"DELETE",url:e})}}class u{constructor(e){if(this.reason=null,"function"!=typeof e)throw new TypeError("executor must be a function.");let t;this.promise=new Promise((e=>{t=e})),e((e=>{this.reason||(this.reason=new Error(e),t(this.reason))}))}throwIfRequested(){if(this.reason)throw this.reason}static source(){let e;return{token:new u((t=>{e=t})),cancel:e}}}class i{constructor(e){this.headers=e||{}}set(e,t){this.headers[e.toLowerCase()]=t}get(e){return this.headers[e.toLowerCase()]}remove(e){delete this.headers[e.toLowerCase()]}toObject(){return{...this.headers}}}function c(e){return new Promise(((t,r)=>{if("undefined"==typeof window||void 0===window.XMLHttpRequest)return void r(new Error("XMLHttpRequest is not supported in Node.js environment."));const{url:s,method:n,headers:o,data:a}=e,u=new XMLHttpRequest;u.open(n.toUpperCase(),s,!0),o&&Object.keys(o).forEach((e=>{u.setRequestHeader(e,o[e])})),u.onload=()=>{const r={data:u.response,status:u.status,statusText:u.statusText,headers:u.getAllResponseHeaders(),config:e,request:u};t(r)},u.onerror=()=>r(new Error("Network Error")),u.ontimeout=()=>r(new Error(`Timeout of ${e.timeout}ms exceeded`)),u.send(a)}))}function d(e){return new Promise(((t,r)=>{if("undefined"==typeof fetch)return void r(new Error("Fetch API not found in this environment."));const{url:s,method:n,headers:o,data:a}=e;fetch(s,{method:n,headers:o,body:a}).then((t=>t.json().then((r=>({data:r,status:t.status,statusText:t.statusText,headers:t.headers,config:e,request:t}))))).then(t).catch((e=>{r(e)}))}))}class h{constructor(){this.handlers=[]}use(e,t){return this.handlers.push({fulfilled:e,rejected:t}),this.handlers.length-1}eject(e){this.handlers[e]&&(this.handlers[e]=null)}forEach(e){this.handlers.forEach((t=>{null!==t&&e(t)}))}}class l extends Error{constructor(e,t,r,s,n){super(e),this.name="FetcherErrorHandler",this.code=t,this.config=r,this.request=s,this.response=n,Error.captureStackTrace(this,this.constructor)}static from(e,t,r,s){return new l(e.message,e.name,t,r,s)}}function f(e){const t=new a(e),r=a.prototype.request.bind(t);return Object.setPrototypeOf(r,a.prototype),Object.assign(r,t),r}const p={200:"OK",201:"Created",202:"Accepted",204:"No Content",400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout"},w=f(e);w.create=e=>f(e);export{u as CancelToken,l as FetcherError,i as FetcherHeaders,p as HTTP_STATUS_CODES,h as InterceptorManager,t as bind,r as buildURL,f as createInstance,w as default,d as fetchAdapter,o as formatRequestBody,s as mergeConfig,n as transformData,c as xhrAdapter}; | ||
const e={method:"GET",timeout:0,headers:{"Content-Type":"application/json"},transformRequest:[(e,t)=>e],transformResponse:[e=>e],adapter:"fetch"};function t(e,t){return function(...r){return e.apply(t,r)}}function r(e,t){const r=t?new URLSearchParams(t).toString():"";return r?`${e}?${r}`:e}function s(e,t={}){const r={...e,...t};return e.headers&&t.headers&&(r.headers={...e.headers,...t.headers}),r}function n(e,t,r){return r.forEach((r=>{e=r(e,t)})),e}function o(e,t){if(!e)return null;if(t&&t.includes("application/json"))return JSON.stringify(e);if(t&&t.includes("multipart/form-data")){const t=new FormData;return Object.keys(e).forEach((r=>t.append(r,e[r]))),t}if(t&&t.includes("application/x-www-form-urlencoded")){const t=new URLSearchParams;return Object.keys(e).forEach((r=>t.append(r,e[r]))),t.toString()}return e}class a{constructor(t){this.defaults=s(e,t),this.interceptors={request:new h,response:new h}}async request(e){const t=Date.now(),a=s(this.defaults,e);a.url=r(a.url,a.params);const u=new i(a.headers);a.headers=u.toObject(),a.data&&(a.data=o(a.data,u.get("Content-Type")));try{const e=await function(e){const t="xhr"===e.adapter?c:d;return new Promise(((r,s)=>{e.cancelToken&&e.cancelToken.throwIfRequested(),t(e).then((t=>{t.data=n(t.data,t.headers,e.transformResponse||[]),t.status>=200&&t.status<300?r(t):s(l.from(new Error("Request failed"),e,null,t))})).catch((t=>{s(l.from(t,e,null,null))}))}))}(a),r=Date.now();return{...e,duration:r-t,statusText:p[e.status]||"Unknown Status"}}catch(e){const r=Date.now();throw e.duration=r-t,e}}get(e,t={}){return this.request({...t,method:"GET",url:e})}post(e,t,r={}){return this.request({...r,method:"POST",url:e,data:t})}put(e,t,r={}){return this.request({...r,method:"PUT",url:e,data:t})}patch(e,t,r={}){return this.request({...r,method:"PATCH",url:e,data:t})}delete(e,t={}){return this.request({...t,method:"DELETE",url:e})}}class u{constructor(e){if(this.reason=null,"function"!=typeof e)throw new TypeError("executor must be a function.");let t;this.promise=new Promise((e=>{t=e})),e((e=>{this.reason||(this.reason=new Error(e),t(this.reason))}))}throwIfRequested(){if(this.reason)throw this.reason}static source(){let e;return{token:new u((t=>{e=t})),cancel:e}}}class i{constructor(e){this.headers=e||{}}set(e,t){this.headers[e.toLowerCase()]=t}get(e){return this.headers[e.toLowerCase()]}remove(e){delete this.headers[e.toLowerCase()]}toObject(){return{...this.headers}}}function c(e){return new Promise(((t,r)=>{if("undefined"==typeof window||void 0===window.XMLHttpRequest)return void r(new Error("XMLHttpRequest is not supported in Node.js environment."));const{url:s,method:n,headers:o,data:a}=e,u=new XMLHttpRequest;u.open(n.toUpperCase(),s,!0),o&&Object.keys(o).forEach((e=>{u.setRequestHeader(e,o[e])})),u.onload=()=>{const r={data:u.response,status:u.status,statusText:u.statusText,headers:u.getAllResponseHeaders(),config:e,request:u};t(r)},u.onerror=()=>r(new Error("Network Error")),u.ontimeout=()=>r(new Error(`Timeout of ${e.timeout}ms exceeded`)),u.send(a)}))}function d(e){return new Promise(((t,r)=>{if("undefined"==typeof fetch)return void r(new Error("Fetch API not found in this environment."));const{url:s,method:n,headers:o,data:a}=e;fetch(s,{method:n,headers:o,body:a}).then((t=>t.json().then((r=>({data:r,status:t.status,statusText:t.statusText,headers:t.headers,config:e,request:t}))))).then(t).catch((e=>{r(e)}))}))}class h{constructor(){this.handlers=[]}use(e,t){return this.handlers.push({fulfilled:e,rejected:t}),this.handlers.length-1}eject(e){this.handlers[e]&&(this.handlers[e]=null)}forEach(e){this.handlers.forEach((t=>{null!==t&&e(t)}))}}class l extends Error{constructor(e,t,r,s,n){super(e),this.name="FetcherErrorHandler",this.code=t,this.config=r,this.request=s,this.response=n,Error.captureStackTrace(this,this.constructor)}static from(e,t,r,s){return new l(e.message,e.name,t,r,s)}}function f(e){const t=new a(e),r=a.prototype.request.bind(t);return Object.setPrototypeOf(r,a.prototype),Object.assign(r,t),r.create=t=>f(s(e,t)),r}const p={200:"OK",201:"Created",202:"Accepted",204:"No Content",400:"Bad Request",401:"Unauthorized",403:"Forbidden",404:"Not Found",500:"Internal Server Error",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout"},w=f(e);w.create=e=>f(e);export{u as CancelToken,l as FetcherError,i as FetcherHeaders,p as HTTP_STATUS_CODES,h as InterceptorManager,t as bind,r as buildURL,f as createInstance,w as default,d as fetchAdapter,o as formatRequestBody,s as mergeConfig,n as transformData,c as xhrAdapter}; |
import { FetcherInstance, FetcherRequestConfig } from "./types"; | ||
declare function createInstance(config: FetcherRequestConfig): FetcherInstance; | ||
declare function createInstance(defaultConfig: FetcherRequestConfig): FetcherInstance; | ||
export default createInstance; |
@@ -26,3 +26,3 @@ import { Fetcher, CancelToken } from "../core"; | ||
export interface FetcherInstance extends Fetcher { | ||
create(config: FetcherRequestConfig): FetcherInstance; | ||
create(config: FetcherRequestConfig): Fetcher; | ||
} | ||
@@ -29,0 +29,0 @@ export interface FetcherError extends Error { |
{ | ||
"name": "@svkhrobbeck/fetcher", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "dist/index.js", |
@@ -115,5 +115,3 @@ # Fetcher Library | ||
```javascript | ||
import { Fetcher } from "@svkhrobbeck/fetcher"; | ||
const customFetcher = new Fetcher({ | ||
const customFetcher = fetcher.create({ | ||
baseURL: "https://api.example.com", | ||
@@ -145,3 +143,3 @@ headers: { | ||
- **Muallif**: [Suhrobbek Soatov](https://github.com/svkhrobbeck) | ||
- **Muallif**: [Suhrobbek Soatov](https://soatov.uz) | ||
- **GitHub**: [svkhrobbeck](https://github.com/svkhrobbeck) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16744
144