@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.
- package/CHANGELOG.md +1023 -0
- package/LICENSE +7 -0
- package/MIGRATION_GUIDE.md +3 -0
- package/README.md +1645 -0
- package/SECURITY.md +6 -0
- package/dist/node/req.cjs +4397 -0
- package/dist/node/req.mjs +3551 -0
- package/dist/web/req.mjs +2609 -0
- package/index.d.cts +545 -0
- package/index.d.ts +565 -0
- package/index.js +43 -0
- package/lib/adapters/README.md +37 -0
- package/lib/adapters/adapters.js +57 -0
- package/lib/adapters/fetch.js +229 -0
- package/lib/adapters/http.js +552 -0
- package/lib/adapters/xhr.js +200 -0
- package/lib/axios.js +89 -0
- package/lib/cancel/CancelToken.js +106 -0
- package/lib/cancel/CanceledError.js +20 -0
- package/lib/cancel/isCancel.js +4 -0
- package/lib/core/Axios.js +359 -0
- package/lib/core/AxiosError.js +89 -0
- package/lib/core/AxiosHeaders.js +243 -0
- package/lib/core/InterceptorManager.js +59 -0
- package/lib/core/README.md +8 -0
- package/lib/core/buildFullPath.js +18 -0
- package/lib/core/dispatchRequest.js +72 -0
- package/lib/core/mergeConfig.js +98 -0
- package/lib/core/settle.js +21 -0
- package/lib/core/transformData.js +22 -0
- package/lib/defaults/index.js +136 -0
- package/lib/defaults/transitional.js +6 -0
- package/lib/env/README.md +3 -0
- package/lib/env/classes/FormData.js +2 -0
- package/lib/env/data.js +1 -0
- package/lib/helpers/AxiosTransformStream.js +116 -0
- package/lib/helpers/AxiosURLSearchParams.js +50 -0
- package/lib/helpers/HttpStatusCode.js +69 -0
- package/lib/helpers/README.md +7 -0
- package/lib/helpers/ZlibHeaderTransformStream.js +22 -0
- package/lib/helpers/bind.js +6 -0
- package/lib/helpers/buildURL.js +42 -0
- package/lib/helpers/callbackify.js +14 -0
- package/lib/helpers/combineURLs.js +11 -0
- package/lib/helpers/composeSignals.js +37 -0
- package/lib/helpers/cookies.js +29 -0
- package/lib/helpers/deprecatedMethod.js +18 -0
- package/lib/helpers/formDataToJSON.js +78 -0
- package/lib/helpers/formDataToStream.js +77 -0
- package/lib/helpers/fromDataURI.js +44 -0
- package/lib/helpers/isAbsoluteURL.js +13 -0
- package/lib/helpers/isAxiosError.js +11 -0
- package/lib/helpers/isURLSameOrigin.js +50 -0
- package/lib/helpers/null.js +2 -0
- package/lib/helpers/parseHeaders.js +61 -0
- package/lib/helpers/parseProtocol.js +5 -0
- package/lib/helpers/progressEventReducer.js +54 -0
- package/lib/helpers/readBlob.js +13 -0
- package/lib/helpers/resolveConfig.js +45 -0
- package/lib/helpers/speedometer.js +40 -0
- package/lib/helpers/spread.js +26 -0
- package/lib/helpers/throttle.js +41 -0
- package/lib/helpers/toFormData.js +175 -0
- package/lib/helpers/toURLEncodedForm.js +15 -0
- package/lib/helpers/trackStream.js +75 -0
- package/lib/helpers/validator.js +84 -0
- package/lib/platform/browser/classes/Blob.js +2 -0
- package/lib/platform/browser/classes/FormData.js +2 -0
- package/lib/platform/browser/classes/URLSearchParams.js +3 -0
- package/lib/platform/browser/index.js +19 -0
- package/lib/platform/common/utils.js +37 -0
- package/lib/platform/index.js +6 -0
- package/lib/platform/node/classes/FormData.js +2 -0
- package/lib/platform/node/classes/URLSearchParams.js +3 -0
- package/lib/platform/node/index.js +16 -0
- package/lib/req.js +67 -0
- package/lib/utils.js +635 -0
- package/package.json +214 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import platform from "../platform/index.js";
|
|
2
|
+
import utils from "../utils.js";
|
|
3
|
+
import AxiosError from "../core/AxiosError.js";
|
|
4
|
+
import composeSignals from "../helpers/composeSignals.js";
|
|
5
|
+
import {trackStream} from "../helpers/trackStream.js";
|
|
6
|
+
import AxiosHeaders from "../core/AxiosHeaders.js";
|
|
7
|
+
import {progressEventReducer, progressEventDecorator, asyncDecorator} from "../helpers/progressEventReducer.js";
|
|
8
|
+
import resolveConfig from "../helpers/resolveConfig.js";
|
|
9
|
+
import settle from "../core/settle.js";
|
|
10
|
+
|
|
11
|
+
const isFetchSupported = typeof fetch === 'function' && typeof Request === 'function' && typeof Response === 'function';
|
|
12
|
+
const isReadableStreamSupported = isFetchSupported && typeof ReadableStream === 'function';
|
|
13
|
+
|
|
14
|
+
// used only inside the fetch adapter
|
|
15
|
+
const encodeText = isFetchSupported && (typeof TextEncoder === 'function' ?
|
|
16
|
+
((encoder) => (str) => encoder.encode(str))(new TextEncoder()) :
|
|
17
|
+
async (str) => new Uint8Array(await new Response(str).arrayBuffer())
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const test = (fn, ...args) => {
|
|
21
|
+
try {
|
|
22
|
+
return !!fn(...args);
|
|
23
|
+
} catch (e) {
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const supportsRequestStream = isReadableStreamSupported && test(() => {
|
|
29
|
+
let duplexAccessed = false;
|
|
30
|
+
|
|
31
|
+
const hasContentType = new Request(platform.origin, {
|
|
32
|
+
body: new ReadableStream(),
|
|
33
|
+
method: 'POST',
|
|
34
|
+
get duplex() {
|
|
35
|
+
duplexAccessed = true;
|
|
36
|
+
return 'half';
|
|
37
|
+
},
|
|
38
|
+
}).headers.has('Content-Type');
|
|
39
|
+
|
|
40
|
+
return duplexAccessed && !hasContentType;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const DEFAULT_CHUNK_SIZE = 64 * 1024;
|
|
44
|
+
|
|
45
|
+
const supportsResponseStream = isReadableStreamSupported &&
|
|
46
|
+
test(() => utils.isReadableStream(new Response('').body));
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
const resolvers = {
|
|
50
|
+
stream: supportsResponseStream && ((res) => res.body)
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
isFetchSupported && (((res) => {
|
|
54
|
+
['text', 'arrayBuffer', 'blob', 'formData', 'stream'].forEach(type => {
|
|
55
|
+
!resolvers[type] && (resolvers[type] = utils.isFunction(res[type]) ? (res) => res[type]() :
|
|
56
|
+
(_, config) => {
|
|
57
|
+
throw new AxiosError(`Response type '${type}' is not supported`, AxiosError.ERR_NOT_SUPPORT, config);
|
|
58
|
+
})
|
|
59
|
+
});
|
|
60
|
+
})(new Response));
|
|
61
|
+
|
|
62
|
+
const getBodyLength = async (body) => {
|
|
63
|
+
if (body == null) {
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if(utils.isBlob(body)) {
|
|
68
|
+
return body.size;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if(utils.isSpecCompliantForm(body)) {
|
|
72
|
+
const _request = new Request(platform.origin, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
body,
|
|
75
|
+
});
|
|
76
|
+
return (await _request.arrayBuffer()).byteLength;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if(utils.isArrayBufferView(body) || utils.isArrayBuffer(body)) {
|
|
80
|
+
return body.byteLength;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if(utils.isURLSearchParams(body)) {
|
|
84
|
+
body = body + '';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if(utils.isString(body)) {
|
|
88
|
+
return (await encodeText(body)).byteLength;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const resolveBodyLength = async (headers, body) => {
|
|
93
|
+
const length = utils.toFiniteNumber(headers.getContentLength());
|
|
94
|
+
|
|
95
|
+
return length == null ? getBodyLength(body) : length;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export default isFetchSupported && (async (config) => {
|
|
99
|
+
let {
|
|
100
|
+
url,
|
|
101
|
+
method,
|
|
102
|
+
data,
|
|
103
|
+
signal,
|
|
104
|
+
cancelToken,
|
|
105
|
+
timeout,
|
|
106
|
+
onDownloadProgress,
|
|
107
|
+
onUploadProgress,
|
|
108
|
+
responseType,
|
|
109
|
+
headers,
|
|
110
|
+
withCredentials = 'same-origin',
|
|
111
|
+
fetchOptions
|
|
112
|
+
} = resolveConfig(config);
|
|
113
|
+
|
|
114
|
+
responseType = responseType ? (responseType + '').toLowerCase() : 'text';
|
|
115
|
+
|
|
116
|
+
let composedSignal = composeSignals([signal, cancelToken && cancelToken.toAbortSignal()], timeout);
|
|
117
|
+
|
|
118
|
+
let request;
|
|
119
|
+
|
|
120
|
+
const unsubscribe = composedSignal && composedSignal.unsubscribe && (() => {
|
|
121
|
+
composedSignal.unsubscribe();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
let requestContentLength;
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
if (
|
|
128
|
+
onUploadProgress && supportsRequestStream && method !== 'get' && method !== 'head' &&
|
|
129
|
+
(requestContentLength = await resolveBodyLength(headers, data)) !== 0
|
|
130
|
+
) {
|
|
131
|
+
let _request = new Request(url, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
body: data,
|
|
134
|
+
duplex: "half"
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
let contentTypeHeader;
|
|
138
|
+
|
|
139
|
+
if (utils.isFormData(data) && (contentTypeHeader = _request.headers.get('content-type'))) {
|
|
140
|
+
headers.setContentType(contentTypeHeader)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (_request.body) {
|
|
144
|
+
const [onProgress, flush] = progressEventDecorator(
|
|
145
|
+
requestContentLength,
|
|
146
|
+
progressEventReducer(asyncDecorator(onUploadProgress))
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
data = trackStream(_request.body, DEFAULT_CHUNK_SIZE, onProgress, flush);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!utils.isString(withCredentials)) {
|
|
154
|
+
withCredentials = withCredentials ? 'include' : 'omit';
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Cloudflare Workers throws when credentials are defined
|
|
158
|
+
// see https://github.com/cloudflare/workerd/issues/902
|
|
159
|
+
const isCredentialsSupported = "credentials" in Request.prototype;
|
|
160
|
+
request = new Request(url, {
|
|
161
|
+
...fetchOptions,
|
|
162
|
+
signal: composedSignal,
|
|
163
|
+
method: method.toUpperCase(),
|
|
164
|
+
headers: headers.normalize().toJSON(),
|
|
165
|
+
body: data,
|
|
166
|
+
duplex: "half",
|
|
167
|
+
credentials: isCredentialsSupported ? withCredentials : undefined
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
let response = await fetch(request);
|
|
171
|
+
|
|
172
|
+
const isStreamResponse = supportsResponseStream && (responseType === 'stream' || responseType === 'response');
|
|
173
|
+
|
|
174
|
+
if (supportsResponseStream && (onDownloadProgress || (isStreamResponse && unsubscribe))) {
|
|
175
|
+
const options = {};
|
|
176
|
+
|
|
177
|
+
['status', 'statusText', 'headers'].forEach(prop => {
|
|
178
|
+
options[prop] = response[prop];
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const responseContentLength = utils.toFiniteNumber(response.headers.get('content-length'));
|
|
182
|
+
|
|
183
|
+
const [onProgress, flush] = onDownloadProgress && progressEventDecorator(
|
|
184
|
+
responseContentLength,
|
|
185
|
+
progressEventReducer(asyncDecorator(onDownloadProgress), true)
|
|
186
|
+
) || [];
|
|
187
|
+
|
|
188
|
+
response = new Response(
|
|
189
|
+
trackStream(response.body, DEFAULT_CHUNK_SIZE, onProgress, () => {
|
|
190
|
+
flush && flush();
|
|
191
|
+
unsubscribe && unsubscribe();
|
|
192
|
+
}),
|
|
193
|
+
options
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
responseType = responseType || 'text';
|
|
198
|
+
|
|
199
|
+
let responseData = await resolvers[utils.findKey(resolvers, responseType) || 'text'](response, config);
|
|
200
|
+
|
|
201
|
+
!isStreamResponse && unsubscribe && unsubscribe();
|
|
202
|
+
|
|
203
|
+
return await new Promise((resolve, reject) => {
|
|
204
|
+
settle(resolve, reject, {
|
|
205
|
+
data: responseData,
|
|
206
|
+
headers: AxiosHeaders.from(response.headers),
|
|
207
|
+
status: response.status,
|
|
208
|
+
statusText: response.statusText,
|
|
209
|
+
config,
|
|
210
|
+
request
|
|
211
|
+
})
|
|
212
|
+
})
|
|
213
|
+
} catch (err) {
|
|
214
|
+
unsubscribe && unsubscribe();
|
|
215
|
+
|
|
216
|
+
if (err && err.name === 'TypeError' && /fetch/i.test(err.message)) {
|
|
217
|
+
throw Object.assign(
|
|
218
|
+
new AxiosError('Network Error', AxiosError.ERR_NETWORK, config, request),
|
|
219
|
+
{
|
|
220
|
+
cause: err.cause || err
|
|
221
|
+
}
|
|
222
|
+
)
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
throw AxiosError.from(err, err && err.code, config, request);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
|