enterprise-fetch 1.0.1 → 2.0.1
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/dist/client/enterprise-fetch.cjs.js +1136 -1449
- package/dist/client/enterprise-fetch.cjs.min.js +3 -1
- package/dist/client/enterprise-fetch.esm.js +607 -611
- package/dist/client/enterprise-fetch.esm.min.js +1 -1
- package/dist/enterprise-fetch.cjs.js +128 -138
- package/dist/enterprise-fetch.d.ts +5 -6
- package/dist/enterprise-fetch.esm.js +128 -112
- package/dist/enterprise-fetch.esm.js.map +1 -0
- package/dist/fetch-defaults.d.ts +3 -3
- package/dist/models/AppError.d.ts +27 -27
- package/dist/models/EnterpriseFetch.d.ts +15 -15
- package/dist/models/index.d.ts +2 -2
- package/dist/util/client/is-client.d.ts +2 -2
- package/dist/util/parsejson.d.ts +3 -3
- package/dist/util/resolve.d.ts +1 -0
- package/dist/util/server/fetch.d.ts +2 -2
- package/dist/util/server/is-node.d.ts +2 -0
- package/dist/util/server/proxy-agent.d.ts +5 -5
- package/dist/util/typeguards.d.ts +2 -2
- package/package.json +55 -49
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["enterprise-fetch"]={})}(this,function(t){"use strict";function e(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var r,o={exports:{}};var i,n,s=function(){if(r)return o.exports;r=1;const{AbortController:t,AbortSignal:e}="undefined"!=typeof self?self:"undefined"!=typeof window?window:void 0;return o.exports=t,o.exports.AbortSignal=e,o.exports.default=t,o.exports}(),u=e(s);var a,c,f,p,h,m,l,y={};function _(){return f||(f=1,function(t){var e=function(){if(c)return a;function t(t,e){"boolean"==typeof e&&(e={forever:e}),this._originalTimeouts=JSON.parse(JSON.stringify(t)),this._timeouts=t,this._options=e||{},this._maxRetryTime=e&&e.maxRetryTime||1/0,this._fn=null,this._errors=[],this._attempts=1,this._operationTimeout=null,this._operationTimeoutCb=null,this._timeout=null,this._operationStart=null,this._options.forever&&(this._cachedTimeouts=this._timeouts.slice(0))}return c=1,a=t,t.prototype.reset=function(){this._attempts=1,this._timeouts=this._originalTimeouts},t.prototype.stop=function(){this._timeout&&clearTimeout(this._timeout),this._timeouts=[],this._cachedTimeouts=null},t.prototype.retry=function(t){if(this._timeout&&clearTimeout(this._timeout),!t)return!1;var e=(new Date).getTime();if(t&&e-this._operationStart>=this._maxRetryTime)return this._errors.unshift(new Error("RetryOperation timeout occurred")),!1;this._errors.push(t);var r=this._timeouts.shift();if(void 0===r){if(!this._cachedTimeouts)return!1;this._errors.splice(this._errors.length-1,this._errors.length),this._timeouts=this._cachedTimeouts.slice(0),r=this._timeouts.shift()}var o=this,i=setTimeout(function(){o._attempts++,o._operationTimeoutCb&&(o._timeout=setTimeout(function(){o._operationTimeoutCb(o._attempts)},o._operationTimeout),o._options.unref&&o._timeout.unref()),o._fn(o._attempts)},r);return this._options.unref&&i.unref(),!0},t.prototype.attempt=function(t,e){this._fn=t,e&&(e.timeout&&(this._operationTimeout=e.timeout),e.cb&&(this._operationTimeoutCb=e.cb));var r=this;this._operationTimeoutCb&&(this._timeout=setTimeout(function(){r._operationTimeoutCb()},r._operationTimeout)),this._operationStart=(new Date).getTime(),this._fn(this._attempts)},t.prototype.try=function(t){console.log("Using RetryOperation.try() is deprecated"),this.attempt(t)},t.prototype.start=function(t){console.log("Using RetryOperation.start() is deprecated"),this.attempt(t)},t.prototype.start=t.prototype.try,t.prototype.errors=function(){return this._errors},t.prototype.attempts=function(){return this._attempts},t.prototype.mainError=function(){if(0===this._errors.length)return null;for(var t={},e=null,r=0,o=0;o<this._errors.length;o++){var i=this._errors[o],n=i.message,s=(t[n]||0)+1;t[n]=s,s>=r&&(e=i,r=s)}return e},a}();t.operation=function(r){var o=t.timeouts(r);return new e(o,{forever:r&&r.forever,unref:r&&r.unref,maxRetryTime:r&&r.maxRetryTime})},t.timeouts=function(t){if(t instanceof Array)return[].concat(t);var e={retries:10,factor:2,minTimeout:1e3,maxTimeout:1/0,randomize:!1};for(var r in t)e[r]=t[r];if(e.minTimeout>e.maxTimeout)throw new Error("minTimeout is greater than maxTimeout");for(var o=[],i=0;i<e.retries;i++)o.push(this.createTimeout(i,e));return t&&t.forever&&!o.length&&o.push(this.createTimeout(i,e)),o.sort(function(t,e){return t-e}),o},t.createTimeout=function(t,e){var r=e.randomize?Math.random()+1:1,o=Math.round(r*e.minTimeout*Math.pow(e.factor,t));return o=Math.min(o,e.maxTimeout)},t.wrap=function(e,r,o){if(r instanceof Array&&(o=r,r=null),!o)for(var i in o=[],e)"function"==typeof e[i]&&o.push(i);for(var n=0;n<o.length;n++){var s=o[n],u=e[s];e[s]=function(o){var i=t.operation(r),n=Array.prototype.slice.call(arguments,1),s=n.pop();n.push(function(t){i.retry(t)||(t&&(arguments[0]=i.mainError()),s.apply(this,arguments))}),i.attempt(function(){o.apply(e,n)})}.bind(e,u),e[s].options=r}}}(y)),y}function d(){return h?p:(h=1,p=_())}var T=function(){if(l)return m;l=1;var t=function(){if(n)return i;function t(t,e){for(const r in e)Object.defineProperty(t,r,{value:e[r],enumerable:!0,configurable:!0});return t}return n=1,i=function(e,r,o){if(!e||"string"==typeof e)throw new TypeError("Please pass an Error to err-code");o||(o={}),"object"==typeof r&&(o=r,r=void 0),null!=r&&(o.code=r);try{return t(e,o)}catch(r){o.message=e.message,o.stack=e.stack;const i=function(){};return i.prototype=Object.create(Object.getPrototypeOf(e)),t(new i,o)}}}(),e=d(),r=Object.prototype.hasOwnProperty;function o(t){return t&&"EPROMISERETRY"===t.code&&r.call(t,"retried")}return m=function(r,i){var n,s;return"object"==typeof r&&"function"==typeof i&&(n=i,i=r,r=n),s=e.operation(i),new Promise(function(e,i){s.attempt(function(n){Promise.resolve().then(function(){return r(function(e){throw o(e)&&(e=e.retried),t(new Error("Retrying"),"EPROMISERETRY",{retried:e})},n)}).then(e,function(t){o(t)&&(t=t.retried,s.retry(t||new Error))||i(t)})})})}}(),g=e(T);const w={timeout:6e4,retry:{retries:3,minTimeout:400,factor:2},doRetry:async(t,e,{url:r,options:o})=>{const{retry:i=w.retry}=o;if(t<=i.retries){const o=`${t}/${i.retries}`;if(!e.status||e.status>=400&&404!==e.status)return"message"in e?console.warn(`[fetch] Attempt ${o} ${e.name}: ${e.message} ${r}`):console.warn(`[fetch] Attempt ${o} ${e.status}: ${e.statusText} ${r}`),!0}return!1}},v=()=>"undefined"!=typeof window,b=async()=>{const t=!v()&&(globalThis.fallbackFetch||"function"!=typeof globalThis.fetch?"node-fetch":"node");return t?("node"!==t&&(globalThis.fallbackFetch=!0,globalThis.fetch=(await"node-fetch").default),globalThis.fetch):window.fetch},x=async()=>{const t=!v()&&process.env.http_proxy;if(t){if(process.env.NODE_TLS_REJECT_UNAUTHORIZED="0",globalThis.fallbackFetch){const{HttpsProxyAgent:e}=await"https-proxy-agent";return{agent:new e(t)}}{const{EnvHttpProxyAgent:t}=await"undici";return{dispatcher:new t}}}return{}},R=t=>"function"==typeof t,E=(t=w)=>async(e,r={})=>{const o=await b(),i=await g(async(i,n)=>{const{retry:s=t.retry,timeout:a=t.timeout,doRetry:c=t.doRetry||w.doRetry,...f}=r,p=a||t.timeout||6e4,h=new u,m=setTimeout(()=>{h.abort()},p),[l,y]=await(_=o(e,{...f,...await x(),signal:h.signal}),_.then(function(t){return[null,t]}).catch(function(t){return[t,void 0]}));var _;if(l)throw clearTimeout(m),R(c)&&await c(n,l,{url:e.toString(),options:r})&&i(l),l;return y.ok?(clearTimeout(m),y):(clearTimeout(m),R(c)&&await c(n,y,{url:e.toString(),options:r})&&i(y),y)},r.retry||t.retry);return i},O=E(w);t.default=O,t.fetch=O,t.fetchWithDefaults=E,Object.defineProperty(t,"__esModule",{value:!0})});
|
|
@@ -2,158 +2,148 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var fetch = require('cross-fetch');
|
|
6
|
-
var promiseRetry = require('promise-retry');
|
|
7
5
|
var AbortController = require('abort-controller');
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
-
|
|
12
|
-
function _interopNamespace(e) {
|
|
13
|
-
if (e && e.__esModule) return e;
|
|
14
|
-
var n = Object.create(null);
|
|
15
|
-
if (e) {
|
|
16
|
-
Object.keys(e).forEach(function (k) {
|
|
17
|
-
if (k !== 'default') {
|
|
18
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () {
|
|
22
|
-
return e[k];
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
n['default'] = e;
|
|
29
|
-
return Object.freeze(n);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
|
|
33
|
-
var promiseRetry__default = /*#__PURE__*/_interopDefaultLegacy(promiseRetry);
|
|
34
|
-
var AbortController__default = /*#__PURE__*/_interopDefaultLegacy(AbortController);
|
|
35
|
-
var to__default = /*#__PURE__*/_interopDefaultLegacy(to);
|
|
6
|
+
var awaitToJs = require('await-to-js');
|
|
7
|
+
var promiseRetry = require('promise-retry');
|
|
36
8
|
|
|
37
9
|
const fetchDefaults = {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
10
|
+
// The timeout to apply to requests that do not supply a timeout option
|
|
11
|
+
timeout: 60 * 1000,
|
|
12
|
+
// Retry policy for all fetch requests
|
|
13
|
+
retry: {
|
|
14
|
+
retries: 3,
|
|
15
|
+
minTimeout: 400,
|
|
16
|
+
factor: 2,
|
|
17
|
+
},
|
|
18
|
+
// Do retry function to examine failures and apply custom retry logic
|
|
19
|
+
// return true to retry the fetch call
|
|
20
|
+
doRetry: async (attempt, res, { url, options }) => {
|
|
21
|
+
// Get the retry policy from options or fetchDefaults
|
|
22
|
+
const { retry = fetchDefaults.retry } = options;
|
|
23
|
+
if (attempt <= retry.retries) {
|
|
24
|
+
const counter = `${attempt}/${retry.retries}`;
|
|
25
|
+
// Retry request on any network error,
|
|
26
|
+
// or 4xx or 5xx status codes. No retry on 404
|
|
27
|
+
if (!res.status || (res.status >= 400 && res.status !== 404)) {
|
|
28
|
+
if ('message' in res)
|
|
29
|
+
console.warn(`[fetch] Attempt ${counter} ${res.name}: ${res.message} ${url}`);
|
|
30
|
+
else
|
|
31
|
+
console.warn(`[fetch] Attempt ${counter} ${res.status}: ${res.statusText} ${url}`);
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
56
38
|
|
|
57
|
-
|
|
58
|
-
const counter = `${attempt}/${retry.retries}`; // Retry request on any network error,
|
|
59
|
-
// or 4xx or 5xx status codes. No retry on 404
|
|
39
|
+
const isClient = () => typeof window !== 'undefined';
|
|
60
40
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return
|
|
64
|
-
|
|
65
|
-
|
|
41
|
+
const isNode = () => {
|
|
42
|
+
if (isClient())
|
|
43
|
+
return false;
|
|
44
|
+
if (!globalThis.fallbackFetch &&
|
|
45
|
+
typeof globalThis.fetch === 'function')
|
|
46
|
+
return 'node';
|
|
47
|
+
return 'node-fetch';
|
|
48
|
+
};
|
|
66
49
|
|
|
67
|
-
|
|
68
|
-
|
|
50
|
+
const resolveFetch = async () => {
|
|
51
|
+
const node = isNode();
|
|
52
|
+
if (!node)
|
|
53
|
+
return window.fetch;
|
|
54
|
+
if (node !== 'node') {
|
|
55
|
+
globalThis.fallbackFetch = true;
|
|
56
|
+
globalThis.fetch = (await import('node-fetch')).default;
|
|
57
|
+
}
|
|
58
|
+
return globalThis.fetch;
|
|
69
59
|
};
|
|
70
60
|
|
|
71
61
|
// Setting http_proxy in env will inject a proxy agent header and disable certificate verification
|
|
72
62
|
const proxyAgent = async () => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
agent: new HttpsProxyAgent(proxyUri)
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return {};
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const isFunc = func => typeof func === 'function';
|
|
90
|
-
|
|
91
|
-
const isClient = () => typeof window !== 'undefined';
|
|
92
|
-
|
|
93
|
-
const thisFetch = isClient() ? window.fetch : fetch__default['default'];
|
|
94
|
-
|
|
95
|
-
const fetchWithDefaults = (fetchDefaults$1 = fetchDefaults) => {
|
|
96
|
-
// Fetch function using hoisted defaults wrapped in a promise
|
|
97
|
-
// retry function adhering to a default or supplied policy calling
|
|
98
|
-
// a doRetry function on each failure that requires a boolean return
|
|
99
|
-
const enterpriseFetch = async (url, init = {}) => {
|
|
100
|
-
const fetchResponse = await promiseRetry__default['default'](async (retry, attempt) => {
|
|
101
|
-
const {
|
|
102
|
-
retry: retryPolicy = fetchDefaults$1.retry,
|
|
103
|
-
timeout: rtimeout = fetchDefaults$1.timeout,
|
|
104
|
-
doRetry = fetchDefaults$1.doRetry || fetchDefaults.doRetry,
|
|
105
|
-
...options
|
|
106
|
-
} = init;
|
|
107
|
-
const timeoutMs = rtimeout || fetchDefaults$1.timeout || 60 * 1000; // Abort signal pattern
|
|
108
|
-
|
|
109
|
-
const controller = new AbortController__default['default']();
|
|
110
|
-
const timeoutId = setTimeout(() => {
|
|
111
|
-
controller.abort();
|
|
112
|
-
}, timeoutMs); // Try fetching, wait for error or response
|
|
113
|
-
|
|
114
|
-
const [error, response] = await to__default['default'](thisFetch(url, { // Standard fetch options
|
|
115
|
-
...options,
|
|
116
|
-
// Conditionally add a proxy agent to debug requests
|
|
117
|
-
...(await proxyAgent()),
|
|
118
|
-
// The signal will recieve abort() after timeout elapsed
|
|
119
|
-
signal: controller.signal
|
|
120
|
-
}));
|
|
121
|
-
|
|
122
|
-
if (error) {
|
|
123
|
-
clearTimeout(timeoutId);
|
|
124
|
-
|
|
125
|
-
if (isFunc(doRetry) && (await doRetry(attempt, error, {
|
|
126
|
-
url,
|
|
127
|
-
options: init
|
|
128
|
-
}))) {
|
|
129
|
-
retry(error);
|
|
63
|
+
const proxyUri = !isClient() && process.env.http_proxy;
|
|
64
|
+
if (proxyUri) {
|
|
65
|
+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
66
|
+
if (globalThis.fallbackFetch) {
|
|
67
|
+
// https://www.npmjs.com/package/node-fetch#custom-agent
|
|
68
|
+
// NOTE: This uses the HttpsProxyAgent class from https-proxy-agent v7.x
|
|
69
|
+
// Will break in v8+ where only a default factory function is exported
|
|
70
|
+
const { HttpsProxyAgent } = await import('https-proxy-agent');
|
|
71
|
+
return { agent: new HttpsProxyAgent(proxyUri) };
|
|
130
72
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}))) {
|
|
142
|
-
retry(response);
|
|
73
|
+
else {
|
|
74
|
+
// EnvHttpProxyAgent automatically reads the proxy configuration from the
|
|
75
|
+
// environment variables http_proxy, https_proxy, and no_proxy and sets up
|
|
76
|
+
// the proxy agents accordingly. When http_proxy and https_proxy are set,
|
|
77
|
+
// http_proxy is used for HTTP requests and https_proxy is used for HTTPS requests.
|
|
78
|
+
// If only http_proxy is set, http_proxy is used for both HTTP and HTTPS requests.
|
|
79
|
+
// If only https_proxy is set, it is only used for HTTPS requests.
|
|
80
|
+
// https://undici.nodejs.org/#/docs/api/EnvHttpProxyAgent.md
|
|
81
|
+
const { EnvHttpProxyAgent } = await import('undici');
|
|
82
|
+
return { dispatcher: new EnvHttpProxyAgent() };
|
|
143
83
|
}
|
|
84
|
+
}
|
|
85
|
+
return {};
|
|
86
|
+
};
|
|
144
87
|
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
clearTimeout(timeoutId);
|
|
149
|
-
return response;
|
|
150
|
-
}, init.retry || fetchDefaults$1.retry);
|
|
151
|
-
return fetchResponse;
|
|
152
|
-
};
|
|
88
|
+
const isFunc = (func) => typeof func === 'function';
|
|
153
89
|
|
|
154
|
-
|
|
90
|
+
const fetchWithDefaults = (fetchDefaults$1 = fetchDefaults) => {
|
|
91
|
+
// Fetch function using hoisted defaults wrapped in a promise
|
|
92
|
+
// retry function adhering to a default or supplied policy calling
|
|
93
|
+
// a doRetry function on each failure that requires a boolean return
|
|
94
|
+
const enterpriseFetch = async (url, init = {}) => {
|
|
95
|
+
const thisFetch = await resolveFetch();
|
|
96
|
+
const fetchResponse = await promiseRetry(async (retry, attempt) => {
|
|
97
|
+
const {
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
99
|
+
retry: retryPolicy = fetchDefaults$1.retry, timeout: rtimeout = fetchDefaults$1.timeout, doRetry = fetchDefaults$1.doRetry || fetchDefaults.doRetry, ...options } = init;
|
|
100
|
+
const timeoutMs = rtimeout || fetchDefaults$1.timeout || 60 * 1000;
|
|
101
|
+
// Abort signal pattern
|
|
102
|
+
// Since v15.4.0 Node.js comes with AbortController out of the box
|
|
103
|
+
const controller = new AbortController();
|
|
104
|
+
const timeoutId = setTimeout(() => {
|
|
105
|
+
controller.abort();
|
|
106
|
+
}, timeoutMs);
|
|
107
|
+
// Try fetching, wait for error or response
|
|
108
|
+
const [error, response] = await awaitToJs.to(thisFetch(url, {
|
|
109
|
+
// Standard fetch options
|
|
110
|
+
...options,
|
|
111
|
+
// Conditionally add a proxy agent to debug requests
|
|
112
|
+
...(await proxyAgent()),
|
|
113
|
+
// The signal will recieve abort() after timeout elapsed
|
|
114
|
+
signal: controller.signal,
|
|
115
|
+
}));
|
|
116
|
+
if (error) {
|
|
117
|
+
clearTimeout(timeoutId);
|
|
118
|
+
if (isFunc(doRetry) &&
|
|
119
|
+
(await doRetry(attempt, error, {
|
|
120
|
+
url: url.toString(),
|
|
121
|
+
options: init,
|
|
122
|
+
}))) {
|
|
123
|
+
retry(error);
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
if (!response.ok) {
|
|
128
|
+
clearTimeout(timeoutId);
|
|
129
|
+
if (isFunc(doRetry) &&
|
|
130
|
+
(await doRetry(attempt, response, {
|
|
131
|
+
url: url.toString(),
|
|
132
|
+
options: init,
|
|
133
|
+
}))) {
|
|
134
|
+
retry(response);
|
|
135
|
+
}
|
|
136
|
+
return response;
|
|
137
|
+
}
|
|
138
|
+
clearTimeout(timeoutId);
|
|
139
|
+
return response;
|
|
140
|
+
}, init.retry || fetchDefaults$1.retry);
|
|
141
|
+
return fetchResponse;
|
|
142
|
+
};
|
|
143
|
+
return enterpriseFetch;
|
|
155
144
|
};
|
|
156
|
-
|
|
145
|
+
const fetch = fetchWithDefaults(fetchDefaults);
|
|
157
146
|
|
|
158
|
-
exports.default =
|
|
147
|
+
exports.default = fetch;
|
|
148
|
+
exports.fetch = fetch;
|
|
159
149
|
exports.fetchWithDefaults = fetchWithDefaults;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { FetchInit, Fetch } from './models';
|
|
2
|
-
export * from './models';
|
|
3
|
-
declare const fetchWithDefaults: (fetchDefaults?: FetchInit) => Fetch;
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
export default _default;
|
|
1
|
+
import { FetchInit, Fetch } from './models';
|
|
2
|
+
export * from './models';
|
|
3
|
+
export declare const fetchWithDefaults: (fetchDefaults?: FetchInit) => Fetch;
|
|
4
|
+
export declare const fetch: Fetch;
|
|
5
|
+
export default fetch;
|
|
@@ -1,128 +1,144 @@
|
|
|
1
|
-
import fetch from 'cross-fetch';
|
|
2
|
-
import promiseRetry from 'promise-retry';
|
|
3
1
|
import AbortController from 'abort-controller';
|
|
4
|
-
import to from 'await-to-js';
|
|
2
|
+
import { to } from 'await-to-js';
|
|
3
|
+
import promiseRetry from 'promise-retry';
|
|
5
4
|
|
|
6
5
|
const fetchDefaults = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
6
|
+
// The timeout to apply to requests that do not supply a timeout option
|
|
7
|
+
timeout: 60 * 1000,
|
|
8
|
+
// Retry policy for all fetch requests
|
|
9
|
+
retry: {
|
|
10
|
+
retries: 3,
|
|
11
|
+
minTimeout: 400,
|
|
12
|
+
factor: 2,
|
|
13
|
+
},
|
|
14
|
+
// Do retry function to examine failures and apply custom retry logic
|
|
15
|
+
// return true to retry the fetch call
|
|
16
|
+
doRetry: async (attempt, res, { url, options }) => {
|
|
17
|
+
// Get the retry policy from options or fetchDefaults
|
|
18
|
+
const { retry = fetchDefaults.retry } = options;
|
|
19
|
+
if (attempt <= retry.retries) {
|
|
20
|
+
const counter = `${attempt}/${retry.retries}`;
|
|
21
|
+
// Retry request on any network error,
|
|
22
|
+
// or 4xx or 5xx status codes. No retry on 404
|
|
23
|
+
if (!res.status || (res.status >= 400 && res.status !== 404)) {
|
|
24
|
+
if ('message' in res)
|
|
25
|
+
console.warn(`[fetch] Attempt ${counter} ${res.name}: ${res.message} ${url}`);
|
|
26
|
+
else
|
|
27
|
+
console.warn(`[fetch] Attempt ${counter} ${res.status}: ${res.statusText} ${url}`);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
25
34
|
|
|
26
|
-
|
|
27
|
-
const counter = `${attempt}/${retry.retries}`; // Retry request on any network error,
|
|
28
|
-
// or 4xx or 5xx status codes. No retry on 404
|
|
35
|
+
const isClient = () => typeof window !== 'undefined';
|
|
29
36
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
const isNode = () => {
|
|
38
|
+
if (isClient())
|
|
39
|
+
return false;
|
|
40
|
+
if (!globalThis.fallbackFetch &&
|
|
41
|
+
typeof globalThis.fetch === 'function')
|
|
42
|
+
return 'node';
|
|
43
|
+
return 'node-fetch';
|
|
44
|
+
};
|
|
35
45
|
|
|
36
|
-
|
|
37
|
-
|
|
46
|
+
const resolveFetch = async () => {
|
|
47
|
+
const node = isNode();
|
|
48
|
+
if (!node)
|
|
49
|
+
return window.fetch;
|
|
50
|
+
if (node !== 'node') {
|
|
51
|
+
globalThis.fallbackFetch = true;
|
|
52
|
+
globalThis.fetch = (await import('node-fetch')).default;
|
|
53
|
+
}
|
|
54
|
+
return globalThis.fetch;
|
|
38
55
|
};
|
|
39
56
|
|
|
40
57
|
// Setting http_proxy in env will inject a proxy agent header and disable certificate verification
|
|
41
58
|
const proxyAgent = async () => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
agent: new HttpsProxyAgent(proxyUri)
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return {};
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const isFunc = func => typeof func === 'function';
|
|
59
|
-
|
|
60
|
-
const isClient = () => typeof window !== 'undefined';
|
|
61
|
-
|
|
62
|
-
const thisFetch = isClient() ? window.fetch : fetch;
|
|
63
|
-
|
|
64
|
-
const fetchWithDefaults = (fetchDefaults$1 = fetchDefaults) => {
|
|
65
|
-
// Fetch function using hoisted defaults wrapped in a promise
|
|
66
|
-
// retry function adhering to a default or supplied policy calling
|
|
67
|
-
// a doRetry function on each failure that requires a boolean return
|
|
68
|
-
const enterpriseFetch = async (url, init = {}) => {
|
|
69
|
-
const fetchResponse = await promiseRetry(async (retry, attempt) => {
|
|
70
|
-
const {
|
|
71
|
-
retry: retryPolicy = fetchDefaults$1.retry,
|
|
72
|
-
timeout: rtimeout = fetchDefaults$1.timeout,
|
|
73
|
-
doRetry = fetchDefaults$1.doRetry || fetchDefaults.doRetry,
|
|
74
|
-
...options
|
|
75
|
-
} = init;
|
|
76
|
-
const timeoutMs = rtimeout || fetchDefaults$1.timeout || 60 * 1000; // Abort signal pattern
|
|
77
|
-
|
|
78
|
-
const controller = new AbortController();
|
|
79
|
-
const timeoutId = setTimeout(() => {
|
|
80
|
-
controller.abort();
|
|
81
|
-
}, timeoutMs); // Try fetching, wait for error or response
|
|
82
|
-
|
|
83
|
-
const [error, response] = await to(thisFetch(url, { // Standard fetch options
|
|
84
|
-
...options,
|
|
85
|
-
// Conditionally add a proxy agent to debug requests
|
|
86
|
-
...(await proxyAgent()),
|
|
87
|
-
// The signal will recieve abort() after timeout elapsed
|
|
88
|
-
signal: controller.signal
|
|
89
|
-
}));
|
|
90
|
-
|
|
91
|
-
if (error) {
|
|
92
|
-
clearTimeout(timeoutId);
|
|
93
|
-
|
|
94
|
-
if (isFunc(doRetry) && (await doRetry(attempt, error, {
|
|
95
|
-
url,
|
|
96
|
-
options: init
|
|
97
|
-
}))) {
|
|
98
|
-
retry(error);
|
|
59
|
+
const proxyUri = !isClient() && process.env.http_proxy;
|
|
60
|
+
if (proxyUri) {
|
|
61
|
+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
62
|
+
if (globalThis.fallbackFetch) {
|
|
63
|
+
// https://www.npmjs.com/package/node-fetch#custom-agent
|
|
64
|
+
// NOTE: This uses the HttpsProxyAgent class from https-proxy-agent v7.x
|
|
65
|
+
// Will break in v8+ where only a default factory function is exported
|
|
66
|
+
const { HttpsProxyAgent } = await import('https-proxy-agent');
|
|
67
|
+
return { agent: new HttpsProxyAgent(proxyUri) };
|
|
99
68
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}))) {
|
|
111
|
-
retry(response);
|
|
69
|
+
else {
|
|
70
|
+
// EnvHttpProxyAgent automatically reads the proxy configuration from the
|
|
71
|
+
// environment variables http_proxy, https_proxy, and no_proxy and sets up
|
|
72
|
+
// the proxy agents accordingly. When http_proxy and https_proxy are set,
|
|
73
|
+
// http_proxy is used for HTTP requests and https_proxy is used for HTTPS requests.
|
|
74
|
+
// If only http_proxy is set, http_proxy is used for both HTTP and HTTPS requests.
|
|
75
|
+
// If only https_proxy is set, it is only used for HTTPS requests.
|
|
76
|
+
// https://undici.nodejs.org/#/docs/api/EnvHttpProxyAgent.md
|
|
77
|
+
const { EnvHttpProxyAgent } = await import('undici');
|
|
78
|
+
return { dispatcher: new EnvHttpProxyAgent() };
|
|
112
79
|
}
|
|
80
|
+
}
|
|
81
|
+
return {};
|
|
82
|
+
};
|
|
113
83
|
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
clearTimeout(timeoutId);
|
|
118
|
-
return response;
|
|
119
|
-
}, init.retry || fetchDefaults$1.retry);
|
|
120
|
-
return fetchResponse;
|
|
121
|
-
};
|
|
84
|
+
const isFunc = (func) => typeof func === 'function';
|
|
122
85
|
|
|
123
|
-
|
|
86
|
+
const fetchWithDefaults = (fetchDefaults$1 = fetchDefaults) => {
|
|
87
|
+
// Fetch function using hoisted defaults wrapped in a promise
|
|
88
|
+
// retry function adhering to a default or supplied policy calling
|
|
89
|
+
// a doRetry function on each failure that requires a boolean return
|
|
90
|
+
const enterpriseFetch = async (url, init = {}) => {
|
|
91
|
+
const thisFetch = await resolveFetch();
|
|
92
|
+
const fetchResponse = await promiseRetry(async (retry, attempt) => {
|
|
93
|
+
const {
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
95
|
+
retry: retryPolicy = fetchDefaults$1.retry, timeout: rtimeout = fetchDefaults$1.timeout, doRetry = fetchDefaults$1.doRetry || fetchDefaults.doRetry, ...options } = init;
|
|
96
|
+
const timeoutMs = rtimeout || fetchDefaults$1.timeout || 60 * 1000;
|
|
97
|
+
// Abort signal pattern
|
|
98
|
+
// Since v15.4.0 Node.js comes with AbortController out of the box
|
|
99
|
+
const controller = new AbortController();
|
|
100
|
+
const timeoutId = setTimeout(() => {
|
|
101
|
+
controller.abort();
|
|
102
|
+
}, timeoutMs);
|
|
103
|
+
// Try fetching, wait for error or response
|
|
104
|
+
const [error, response] = await to(thisFetch(url, {
|
|
105
|
+
// Standard fetch options
|
|
106
|
+
...options,
|
|
107
|
+
// Conditionally add a proxy agent to debug requests
|
|
108
|
+
...(await proxyAgent()),
|
|
109
|
+
// The signal will recieve abort() after timeout elapsed
|
|
110
|
+
signal: controller.signal,
|
|
111
|
+
}));
|
|
112
|
+
if (error) {
|
|
113
|
+
clearTimeout(timeoutId);
|
|
114
|
+
if (isFunc(doRetry) &&
|
|
115
|
+
(await doRetry(attempt, error, {
|
|
116
|
+
url: url.toString(),
|
|
117
|
+
options: init,
|
|
118
|
+
}))) {
|
|
119
|
+
retry(error);
|
|
120
|
+
}
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
if (!response.ok) {
|
|
124
|
+
clearTimeout(timeoutId);
|
|
125
|
+
if (isFunc(doRetry) &&
|
|
126
|
+
(await doRetry(attempt, response, {
|
|
127
|
+
url: url.toString(),
|
|
128
|
+
options: init,
|
|
129
|
+
}))) {
|
|
130
|
+
retry(response);
|
|
131
|
+
}
|
|
132
|
+
return response;
|
|
133
|
+
}
|
|
134
|
+
clearTimeout(timeoutId);
|
|
135
|
+
return response;
|
|
136
|
+
}, init.retry || fetchDefaults$1.retry);
|
|
137
|
+
return fetchResponse;
|
|
138
|
+
};
|
|
139
|
+
return enterpriseFetch;
|
|
124
140
|
};
|
|
125
|
-
|
|
141
|
+
const fetch = fetchWithDefaults(fetchDefaults);
|
|
126
142
|
|
|
127
|
-
export default
|
|
128
|
-
|
|
143
|
+
export { fetch as default, fetch, fetchWithDefaults };
|
|
144
|
+
//# sourceMappingURL=enterprise-fetch.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enterprise-fetch.esm.js","sources":["../src/fetch-defaults.ts","../src/util/client/is-client.ts","../src/util/server/is-node.ts","../src/util/resolve.ts","../src/util/server/proxy-agent.ts","../src/util/typeguards.ts","../src/enterprise-fetch.ts"],"sourcesContent":[null,null,null,null,null,null,null],"names":["fetchDefaults","FetchDefaults"],"mappings":";;;;AAEA,MAAM,aAAa,GAAG;;IAEpB,OAAO,EAAE,EAAE,GAAG,IAAI;;AAElB,IAAA,KAAK,EAAE;AACL,QAAA,OAAO,EAAE,CAAC;AACV,QAAA,UAAU,EAAE,GAAG;AACf,QAAA,MAAM,EAAE,CAAC;AACV,KAAA;;;AAGD,IAAA,OAAO,EAAE,OAAO,OAAO,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,OAAO,EAAE,KAAI;;QAEhD,MAAM,EAAE,KAAK,GAAG,aAAa,CAAC,KAAK,EAAE,GAAG,OAAO;AAC/C,QAAA,IAAI,OAAO,IAAI,KAAK,CAAC,OAAO,EAAE;YAC5B,MAAM,OAAO,GAAG,CAAA,EAAG,OAAO,IAAI,KAAK,CAAC,OAAO,CAAA,CAAE;;;AAI7C,YA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
|
|
1
|
-
import { FetchInit } from './models/EnterpriseFetch';
|
|
2
|
-
declare const fetchDefaults: FetchInit;
|
|
3
|
-
export default fetchDefaults;
|
|
1
|
+
import { FetchInit } from './models/EnterpriseFetch';
|
|
2
|
+
declare const fetchDefaults: FetchInit;
|
|
3
|
+
export default fetchDefaults;
|