@wiajs/req 1.7.7

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 (78) hide show
  1. package/CHANGELOG.md +1023 -0
  2. package/LICENSE +7 -0
  3. package/MIGRATION_GUIDE.md +3 -0
  4. package/README.md +1645 -0
  5. package/SECURITY.md +6 -0
  6. package/dist/node/req.cjs +4397 -0
  7. package/dist/node/req.mjs +3551 -0
  8. package/dist/web/req.mjs +2609 -0
  9. package/index.d.cts +545 -0
  10. package/index.d.ts +565 -0
  11. package/index.js +43 -0
  12. package/lib/adapters/README.md +37 -0
  13. package/lib/adapters/adapters.js +57 -0
  14. package/lib/adapters/fetch.js +229 -0
  15. package/lib/adapters/http.js +552 -0
  16. package/lib/adapters/xhr.js +200 -0
  17. package/lib/axios.js +89 -0
  18. package/lib/cancel/CancelToken.js +106 -0
  19. package/lib/cancel/CanceledError.js +20 -0
  20. package/lib/cancel/isCancel.js +4 -0
  21. package/lib/core/Axios.js +359 -0
  22. package/lib/core/AxiosError.js +89 -0
  23. package/lib/core/AxiosHeaders.js +243 -0
  24. package/lib/core/InterceptorManager.js +59 -0
  25. package/lib/core/README.md +8 -0
  26. package/lib/core/buildFullPath.js +18 -0
  27. package/lib/core/dispatchRequest.js +72 -0
  28. package/lib/core/mergeConfig.js +98 -0
  29. package/lib/core/settle.js +21 -0
  30. package/lib/core/transformData.js +22 -0
  31. package/lib/defaults/index.js +136 -0
  32. package/lib/defaults/transitional.js +6 -0
  33. package/lib/env/README.md +3 -0
  34. package/lib/env/classes/FormData.js +2 -0
  35. package/lib/env/data.js +1 -0
  36. package/lib/helpers/AxiosTransformStream.js +116 -0
  37. package/lib/helpers/AxiosURLSearchParams.js +50 -0
  38. package/lib/helpers/HttpStatusCode.js +69 -0
  39. package/lib/helpers/README.md +7 -0
  40. package/lib/helpers/ZlibHeaderTransformStream.js +22 -0
  41. package/lib/helpers/bind.js +6 -0
  42. package/lib/helpers/buildURL.js +42 -0
  43. package/lib/helpers/callbackify.js +14 -0
  44. package/lib/helpers/combineURLs.js +11 -0
  45. package/lib/helpers/composeSignals.js +37 -0
  46. package/lib/helpers/cookies.js +29 -0
  47. package/lib/helpers/deprecatedMethod.js +18 -0
  48. package/lib/helpers/formDataToJSON.js +78 -0
  49. package/lib/helpers/formDataToStream.js +77 -0
  50. package/lib/helpers/fromDataURI.js +44 -0
  51. package/lib/helpers/isAbsoluteURL.js +13 -0
  52. package/lib/helpers/isAxiosError.js +11 -0
  53. package/lib/helpers/isURLSameOrigin.js +50 -0
  54. package/lib/helpers/null.js +2 -0
  55. package/lib/helpers/parseHeaders.js +61 -0
  56. package/lib/helpers/parseProtocol.js +5 -0
  57. package/lib/helpers/progressEventReducer.js +54 -0
  58. package/lib/helpers/readBlob.js +13 -0
  59. package/lib/helpers/resolveConfig.js +45 -0
  60. package/lib/helpers/speedometer.js +40 -0
  61. package/lib/helpers/spread.js +26 -0
  62. package/lib/helpers/throttle.js +41 -0
  63. package/lib/helpers/toFormData.js +175 -0
  64. package/lib/helpers/toURLEncodedForm.js +15 -0
  65. package/lib/helpers/trackStream.js +75 -0
  66. package/lib/helpers/validator.js +84 -0
  67. package/lib/platform/browser/classes/Blob.js +2 -0
  68. package/lib/platform/browser/classes/FormData.js +2 -0
  69. package/lib/platform/browser/classes/URLSearchParams.js +3 -0
  70. package/lib/platform/browser/index.js +19 -0
  71. package/lib/platform/common/utils.js +37 -0
  72. package/lib/platform/index.js +6 -0
  73. package/lib/platform/node/classes/FormData.js +2 -0
  74. package/lib/platform/node/classes/URLSearchParams.js +3 -0
  75. package/lib/platform/node/index.js +16 -0
  76. package/lib/req.js +67 -0
  77. package/lib/utils.js +635 -0
  78. package/package.json +214 -0
@@ -0,0 +1,200 @@
1
+ import utils from '../utils.js';
2
+ import settle from '../core/settle.js';
3
+ import cookies from '../helpers/cookies.js';
4
+ import buildURL from '../helpers/buildURL.js';
5
+ import buildFullPath from '../core/buildFullPath.js';
6
+ import isURLSameOrigin from '../helpers/isURLSameOrigin.js';
7
+ import transitionalDefaults from '../defaults/transitional.js';
8
+ import AxiosError from '../core/AxiosError.js';
9
+ import CanceledError from '../cancel/CanceledError.js';
10
+ import parseProtocol from '../helpers/parseProtocol.js';
11
+ import platform from '../platform/index.js';
12
+ import AxiosHeaders from '../core/AxiosHeaders.js';
13
+ import speedometer from '../helpers/speedometer.js';
14
+ function progressEventReducer(listener, isDownloadStream) {
15
+ let bytesNotified = 0;
16
+ const _speedometer = speedometer(50, 250);
17
+ return (e)=>{
18
+ const { loaded } = e;
19
+ const total = e.lengthComputable ? e.total : undefined;
20
+ const progressBytes = loaded - bytesNotified;
21
+ const rate = _speedometer(progressBytes);
22
+ const inRange = loaded <= total;
23
+ bytesNotified = loaded;
24
+ const data = {
25
+ loaded,
26
+ total,
27
+ progress: total ? loaded / total : undefined,
28
+ bytes: progressBytes,
29
+ rate: rate ? rate : undefined,
30
+ estimated: rate && total && inRange ? (total - loaded) / rate : undefined,
31
+ event: e
32
+ };
33
+ data[isDownloadStream ? 'download' : 'upload'] = true;
34
+ listener(data);
35
+ };
36
+ }
37
+ const isXHRAdapterSupported = typeof XMLHttpRequest !== 'undefined';
38
+ let XhrAdapter = class XhrAdapter {
39
+ constructor(config){
40
+ this.init(config);
41
+ }
42
+ init(config) {}
43
+ request() {}
44
+ stream() {}
45
+ };
46
+ export default isXHRAdapterSupported && XhrAdapter;
47
+ function req(config) {
48
+ return new Promise((resolve, reject)=>{
49
+ const { data: requestData, responseType } = config;
50
+ const requestHeaders = AxiosHeaders.from(config.headers).normalize();
51
+ let onCanceled;
52
+ function done() {
53
+ if (config.cancelToken) config.cancelToken.unsubscribe(onCanceled);
54
+ if (config.signal) config.signal.removeEventListener('abort', onCanceled);
55
+ }
56
+ if (utils.isFormData(requestData) && platform.isStandardBrowserEnv) requestHeaders.setContentType(false); // Let the browser set it
57
+ let request = new XMLHttpRequest();
58
+ // HTTP basic authentication
59
+ if (config.auth) {
60
+ const username = config.auth.username || '';
61
+ const password = config.auth.password ? unescape(encodeURIComponent(config.auth.password)) : '';
62
+ requestHeaders.set('Authorization', 'Basic ' + btoa(username + ':' + password));
63
+ }
64
+ const fullPath = buildFullPath(config.baseURL, config.url);
65
+ request.open(config.method.toUpperCase(), buildURL(fullPath, config.params, config.paramsSerializer), true);
66
+ // Set the request timeout in MS
67
+ request.timeout = config.timeout;
68
+ function onloadend() {
69
+ if (!request) {
70
+ return;
71
+ }
72
+ // Prepare the response
73
+ const responseHeaders = AxiosHeaders.from('getAllResponseHeaders' in request && request.getAllResponseHeaders());
74
+ const responseData = !responseType || responseType === 'text' || responseType === 'json' ? request.responseText : request.response;
75
+ const response = {
76
+ data: responseData,
77
+ status: request.status,
78
+ statusText: request.statusText,
79
+ headers: responseHeaders,
80
+ config,
81
+ request
82
+ };
83
+ settle(function _resolve(value) {
84
+ resolve(value);
85
+ done();
86
+ }, function _reject(err) {
87
+ reject(err);
88
+ done();
89
+ }, response);
90
+ // Clean up request
91
+ request = null;
92
+ }
93
+ if ('onloadend' in request) {
94
+ // Use onloadend if available
95
+ request.onloadend = onloadend;
96
+ } else {
97
+ // Listen for ready state to emulate onloadend
98
+ request.onreadystatechange = function handleLoad() {
99
+ if (!request || request.readyState !== 4) {
100
+ return;
101
+ }
102
+ // The request errored out and we didn't get a response, this will be
103
+ // handled by onerror instead
104
+ // With one exception: request that using file: protocol, most browsers
105
+ // will return status as 0 even though it's a successful request
106
+ if (request.status === 0 && !(request.responseURL && request.responseURL.indexOf('file:') === 0)) {
107
+ return;
108
+ }
109
+ // readystate handler is calling before onerror or ontimeout handlers,
110
+ // so we should call onloadend on the next 'tick'
111
+ setTimeout(onloadend);
112
+ };
113
+ }
114
+ // Handle browser request cancellation (as opposed to a manual cancellation)
115
+ request.onabort = function handleAbort() {
116
+ if (!request) {
117
+ return;
118
+ }
119
+ reject(new AxiosError('Request aborted', AxiosError.ECONNABORTED, config, request));
120
+ // Clean up request
121
+ request = null;
122
+ };
123
+ // Handle low level network errors
124
+ request.onerror = function handleError() {
125
+ // Real errors are hidden from us by the browser
126
+ // onerror should only fire if it's a network error
127
+ reject(new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request));
128
+ // Clean up request
129
+ request = null;
130
+ };
131
+ // Handle timeout
132
+ request.ontimeout = function handleTimeout() {
133
+ let timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';
134
+ const transitional = config.transitional || transitionalDefaults;
135
+ if (config.timeoutErrorMessage) {
136
+ timeoutErrorMessage = config.timeoutErrorMessage;
137
+ }
138
+ reject(new AxiosError(timeoutErrorMessage, transitional.clarifyTimeoutError ? AxiosError.ETIMEDOUT : AxiosError.ECONNABORTED, config, request));
139
+ // Clean up request
140
+ request = null;
141
+ };
142
+ // Add xsrf header
143
+ // This is only done if running in a standard browser environment.
144
+ // Specifically not if we're in a web worker, or react-native.
145
+ if (platform.isStandardBrowserEnv) {
146
+ // Add xsrf header
147
+ const xsrfValue = (config.withCredentials || isURLSameOrigin(fullPath)) && config.xsrfCookieName && cookies.read(config.xsrfCookieName);
148
+ if (xsrfValue) {
149
+ requestHeaders.set(config.xsrfHeaderName, xsrfValue);
150
+ }
151
+ }
152
+ // Remove Content-Type if data is undefined
153
+ requestData === undefined && requestHeaders.setContentType(null);
154
+ // Add headers to the request
155
+ if ('setRequestHeader' in request) {
156
+ utils.forEach(requestHeaders.toJSON(), function setRequestHeader(val, key) {
157
+ request.setRequestHeader(key, val);
158
+ });
159
+ }
160
+ // Add withCredentials to request if needed
161
+ if (!utils.isUndefined(config.withCredentials)) {
162
+ request.withCredentials = !!config.withCredentials;
163
+ }
164
+ // Add responseType to request if needed
165
+ if (responseType && responseType !== 'json') {
166
+ request.responseType = config.responseType;
167
+ }
168
+ // Handle progress if needed
169
+ if (typeof config.onDownloadProgress === 'function') {
170
+ request.addEventListener('progress', progressEventReducer(config.onDownloadProgress, true));
171
+ }
172
+ // Not all browsers support upload events
173
+ if (typeof config.onUploadProgress === 'function' && request.upload) {
174
+ request.upload.addEventListener('progress', progressEventReducer(config.onUploadProgress));
175
+ }
176
+ if (config.cancelToken || config.signal) {
177
+ // Handle cancellation
178
+ // eslint-disable-next-line func-names
179
+ onCanceled = (cancel)=>{
180
+ if (!request) {
181
+ return;
182
+ }
183
+ reject(!cancel || cancel.type ? new CanceledError(null, config, request) : cancel);
184
+ request.abort();
185
+ request = null;
186
+ };
187
+ config.cancelToken && config.cancelToken.subscribe(onCanceled);
188
+ if (config.signal) {
189
+ config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
190
+ }
191
+ }
192
+ const protocol = parseProtocol(fullPath);
193
+ if (protocol && platform.protocols.indexOf(protocol) === -1) {
194
+ reject(new AxiosError('Unsupported protocol ' + protocol + ':', AxiosError.ERR_BAD_REQUEST, config));
195
+ return;
196
+ }
197
+ // Send the request
198
+ request.send(requestData || null);
199
+ });
200
+ }
package/lib/axios.js ADDED
@@ -0,0 +1,89 @@
1
+ 'use strict';
2
+
3
+ import utils from './utils.js';
4
+ import bind from './helpers/bind.js';
5
+ import Axios from './core/Axios.js';
6
+ import mergeConfig from './core/mergeConfig.js';
7
+ import defaults from './defaults/index.js';
8
+ import formDataToJSON from './helpers/formDataToJSON.js';
9
+ import CanceledError from './cancel/CanceledError.js';
10
+ import CancelToken from './cancel/CancelToken.js';
11
+ import isCancel from './cancel/isCancel.js';
12
+ import {VERSION} from './env/data.js';
13
+ import toFormData from './helpers/toFormData.js';
14
+ import AxiosError from './core/AxiosError.js';
15
+ import spread from './helpers/spread.js';
16
+ import isAxiosError from './helpers/isAxiosError.js';
17
+ import AxiosHeaders from "./core/AxiosHeaders.js";
18
+ import adapters from './adapters/adapters.js';
19
+ import HttpStatusCode from './helpers/HttpStatusCode.js';
20
+
21
+ /**
22
+ * Create an instance of Axios
23
+ *
24
+ * @param {Object} defaultConfig The default config for the instance
25
+ *
26
+ * @returns {Axios} A new instance of Axios
27
+ */
28
+ function createInstance(defaultConfig) {
29
+ const context = new Axios(defaultConfig);
30
+ const instance = bind(Axios.prototype.request, context);
31
+
32
+ // Copy axios.prototype to instance
33
+ utils.extend(instance, Axios.prototype, context, {allOwnKeys: true});
34
+
35
+ // Copy context to instance
36
+ utils.extend(instance, context, null, {allOwnKeys: true});
37
+
38
+ // Factory for creating new instances
39
+ instance.create = function create(instanceConfig) {
40
+ return createInstance(mergeConfig(defaultConfig, instanceConfig));
41
+ };
42
+
43
+ return instance;
44
+ }
45
+
46
+ // Create the default instance to be exported
47
+ const axios = createInstance(defaults);
48
+
49
+ // Expose Axios class to allow class inheritance
50
+ axios.Axios = Axios;
51
+
52
+ // Expose Cancel & CancelToken
53
+ axios.CanceledError = CanceledError;
54
+ axios.CancelToken = CancelToken;
55
+ axios.isCancel = isCancel;
56
+ axios.VERSION = VERSION;
57
+ axios.toFormData = toFormData;
58
+
59
+ // Expose AxiosError class
60
+ axios.AxiosError = AxiosError;
61
+
62
+ // alias for CanceledError for backward compatibility
63
+ axios.Cancel = axios.CanceledError;
64
+
65
+ // Expose all/spread
66
+ axios.all = function all(promises) {
67
+ return Promise.all(promises);
68
+ };
69
+
70
+ axios.spread = spread;
71
+
72
+ // Expose isAxiosError
73
+ axios.isAxiosError = isAxiosError;
74
+
75
+ // Expose mergeConfig
76
+ axios.mergeConfig = mergeConfig;
77
+
78
+ axios.AxiosHeaders = AxiosHeaders;
79
+
80
+ axios.formToJSON = thing => formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);
81
+
82
+ axios.getAdapter = adapters.getAdapter;
83
+
84
+ axios.HttpStatusCode = HttpStatusCode;
85
+
86
+ axios.default = axios;
87
+
88
+ // this module should only have a default export
89
+ export default axios
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+ import CanceledError from './CanceledError.js';
3
+ /**
4
+ * A `CancelToken` is an object that can be used to request cancellation of an operation.
5
+ *
6
+ * @param {Function} executor The executor function.
7
+ *
8
+ * @returns {CancelToken}
9
+ */ let CancelToken = class CancelToken {
10
+ constructor(executor){
11
+ if (typeof executor !== 'function') {
12
+ throw new TypeError('executor must be a function.');
13
+ }
14
+ let resolvePromise;
15
+ this.promise = new Promise(function promiseExecutor(resolve) {
16
+ resolvePromise = resolve;
17
+ });
18
+ const token = this;
19
+ // eslint-disable-next-line func-names
20
+ this.promise.then((cancel)=>{
21
+ if (!token._listeners) return;
22
+ let i = token._listeners.length;
23
+ while(i-- > 0){
24
+ token._listeners[i](cancel);
25
+ }
26
+ token._listeners = null;
27
+ });
28
+ // eslint-disable-next-line func-names
29
+ this.promise.then = (onfulfilled)=>{
30
+ let _resolve;
31
+ // eslint-disable-next-line func-names
32
+ const promise = new Promise((resolve)=>{
33
+ token.subscribe(resolve);
34
+ _resolve = resolve;
35
+ }).then(onfulfilled);
36
+ promise.cancel = function reject() {
37
+ token.unsubscribe(_resolve);
38
+ };
39
+ return promise;
40
+ };
41
+ executor(function cancel(message, config, request) {
42
+ if (token.reason) {
43
+ // Cancellation has already been requested
44
+ return;
45
+ }
46
+ token.reason = new CanceledError(message, config, request);
47
+ resolvePromise(token.reason);
48
+ });
49
+ }
50
+ /**
51
+ * Throws a `CanceledError` if cancellation has been requested.
52
+ */ throwIfRequested() {
53
+ if (this.reason) {
54
+ throw this.reason;
55
+ }
56
+ }
57
+ /**
58
+ * Subscribe to the cancel signal
59
+ */ subscribe(listener) {
60
+ if (this.reason) {
61
+ listener(this.reason);
62
+ return;
63
+ }
64
+ if (this._listeners) {
65
+ this._listeners.push(listener);
66
+ } else {
67
+ this._listeners = [
68
+ listener
69
+ ];
70
+ }
71
+ }
72
+ /**
73
+ * Unsubscribe from the cancel signal
74
+ */ unsubscribe(listener) {
75
+ if (!this._listeners) {
76
+ return;
77
+ }
78
+ const index = this._listeners.indexOf(listener);
79
+ if (index !== -1) {
80
+ this._listeners.splice(index, 1);
81
+ }
82
+ }
83
+ toAbortSignal() {
84
+ const controller = new AbortController();
85
+ const abort = (err)=>{
86
+ controller.abort(err);
87
+ };
88
+ this.subscribe(abort);
89
+ controller.signal.unsubscribe = ()=>this.unsubscribe(abort);
90
+ return controller.signal;
91
+ }
92
+ /**
93
+ * Returns an object that contains a new `CancelToken` and a function that, when called,
94
+ * cancels the `CancelToken`.
95
+ */ static source() {
96
+ let cancel;
97
+ const token = new CancelToken(function executor(c) {
98
+ cancel = c;
99
+ });
100
+ return {
101
+ token,
102
+ cancel
103
+ };
104
+ }
105
+ };
106
+ export default CancelToken;
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+ import AxiosError from '../core/AxiosError.js';
3
+ import utils from '../utils.js';
4
+ /**
5
+ * A `CanceledError` is an object that is thrown when an operation is canceled.
6
+ *
7
+ * @param {string=} message The message.
8
+ * @param {Object=} config The config.
9
+ * @param {Object=} request The request.
10
+ *
11
+ * @returns {CanceledError} The created error.
12
+ */ function CanceledError(message, config, request) {
13
+ // eslint-disable-next-line no-eq-null,eqeqeq
14
+ AxiosError.call(this, message == null ? 'canceled' : message, AxiosError.ERR_CANCELED, config, request);
15
+ this.name = 'CanceledError';
16
+ }
17
+ utils.inherits(CanceledError, AxiosError, {
18
+ __CANCEL__: true
19
+ });
20
+ export default CanceledError;
@@ -0,0 +1,4 @@
1
+ 'use strict';
2
+ export default function isCancel(value) {
3
+ return !!(value && value.__CANCEL__);
4
+ }