rezo 1.0.0
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/LICENSE +202 -0
- package/README.md +1507 -0
- package/assets/icon.svg +37 -0
- package/assets/logo-dark.svg +47 -0
- package/assets/logo.svg +58 -0
- package/dist/adapters/curl.cjs +1034 -0
- package/dist/adapters/curl.js +1031 -0
- package/dist/adapters/entries/curl.cjs +4 -0
- package/dist/adapters/entries/curl.d.ts +2136 -0
- package/dist/adapters/entries/curl.js +2 -0
- package/dist/adapters/entries/fetch.cjs +2 -0
- package/dist/adapters/entries/fetch.d.ts +2127 -0
- package/dist/adapters/entries/fetch.js +1 -0
- package/dist/adapters/entries/http.cjs +2 -0
- package/dist/adapters/entries/http.d.ts +2126 -0
- package/dist/adapters/entries/http.js +1 -0
- package/dist/adapters/entries/http2.cjs +4 -0
- package/dist/adapters/entries/http2.d.ts +2136 -0
- package/dist/adapters/entries/http2.js +2 -0
- package/dist/adapters/entries/react-native.cjs +2 -0
- package/dist/adapters/entries/react-native.d.ts +2126 -0
- package/dist/adapters/entries/react-native.js +1 -0
- package/dist/adapters/entries/xhr.cjs +2 -0
- package/dist/adapters/entries/xhr.d.ts +2127 -0
- package/dist/adapters/entries/xhr.js +1 -0
- package/dist/adapters/fetch.cjs +740 -0
- package/dist/adapters/fetch.js +739 -0
- package/dist/adapters/http.cjs +1153 -0
- package/dist/adapters/http.js +1151 -0
- package/dist/adapters/http2.cjs +957 -0
- package/dist/adapters/http2.js +956 -0
- package/dist/adapters/index.cjs +6 -0
- package/dist/adapters/index.js +7 -0
- package/dist/adapters/picker.cjs +342 -0
- package/dist/adapters/picker.js +331 -0
- package/dist/adapters/react-native.cjs +545 -0
- package/dist/adapters/react-native.js +544 -0
- package/dist/adapters/xhr.cjs +622 -0
- package/dist/adapters/xhr.js +621 -0
- package/dist/cache/dns-cache.cjs +118 -0
- package/dist/cache/dns-cache.js +113 -0
- package/dist/cache/file-cacher.cjs +264 -0
- package/dist/cache/file-cacher.js +261 -0
- package/dist/cache/index.cjs +13 -0
- package/dist/cache/index.js +5 -0
- package/dist/cache/lru-cache.cjs +96 -0
- package/dist/cache/lru-cache.js +93 -0
- package/dist/cache/response-cache.cjs +314 -0
- package/dist/cache/response-cache.js +310 -0
- package/dist/cache/url-store.cjs +288 -0
- package/dist/cache/url-store.js +285 -0
- package/dist/core/hooks.cjs +133 -0
- package/dist/core/hooks.js +120 -0
- package/dist/core/rezo.cjs +464 -0
- package/dist/core/rezo.js +458 -0
- package/dist/crawler.d.ts +6255 -0
- package/dist/dom/index.cjs +1 -0
- package/dist/dom/index.d.ts +23 -0
- package/dist/dom/index.js +1 -0
- package/dist/entries/crawler.cjs +5 -0
- package/dist/entries/crawler.js +2 -0
- package/dist/errors/rezo-error.cjs +722 -0
- package/dist/errors/rezo-error.js +716 -0
- package/dist/index.cjs +34 -0
- package/dist/index.d.ts +3335 -0
- package/dist/index.js +26 -0
- package/dist/platform/browser.cjs +9 -0
- package/dist/platform/browser.d.ts +3203 -0
- package/dist/platform/browser.js +7 -0
- package/dist/platform/bun.cjs +9 -0
- package/dist/platform/bun.d.ts +3203 -0
- package/dist/platform/bun.js +7 -0
- package/dist/platform/deno.cjs +9 -0
- package/dist/platform/deno.d.ts +3203 -0
- package/dist/platform/deno.js +7 -0
- package/dist/platform/node.cjs +9 -0
- package/dist/platform/node.d.ts +3203 -0
- package/dist/platform/node.js +7 -0
- package/dist/platform/react-native.cjs +9 -0
- package/dist/platform/react-native.d.ts +3203 -0
- package/dist/platform/react-native.js +7 -0
- package/dist/platform/worker.cjs +9 -0
- package/dist/platform/worker.d.ts +3203 -0
- package/dist/platform/worker.js +7 -0
- package/dist/plugin/addon/decodo/index.cjs +1 -0
- package/dist/plugin/addon/decodo/index.js +1 -0
- package/dist/plugin/addon/decodo/options.cjs +1 -0
- package/dist/plugin/addon/decodo/options.js +1 -0
- package/dist/plugin/addon/oxylabs/index.cjs +1 -0
- package/dist/plugin/addon/oxylabs/index.js +1 -0
- package/dist/plugin/addon/oxylabs/options.cjs +1 -0
- package/dist/plugin/addon/oxylabs/options.js +1 -0
- package/dist/plugin/crawler-options.cjs +1 -0
- package/dist/plugin/crawler-options.js +1 -0
- package/dist/plugin/crawler.cjs +519 -0
- package/dist/plugin/crawler.js +517 -0
- package/dist/plugin/index.cjs +36 -0
- package/dist/plugin/index.js +32 -0
- package/dist/proxy/index.cjs +142 -0
- package/dist/proxy/index.js +139 -0
- package/dist/responses/buildError.cjs +452 -0
- package/dist/responses/buildError.js +441 -0
- package/dist/responses/buildResponse.cjs +365 -0
- package/dist/responses/buildResponse.js +361 -0
- package/dist/responses/download.cjs +54 -0
- package/dist/responses/download.js +52 -0
- package/dist/responses/stream.cjs +60 -0
- package/dist/responses/stream.js +58 -0
- package/dist/responses/upload.cjs +54 -0
- package/dist/responses/upload.js +52 -0
- package/dist/types/cookies.cjs +394 -0
- package/dist/types/cookies.js +391 -0
- package/dist/types/download.cjs +10 -0
- package/dist/types/download.js +10 -0
- package/dist/types/rezo-request.cjs +131 -0
- package/dist/types/rezo-request.js +131 -0
- package/dist/utils/agent-merger.cjs +111 -0
- package/dist/utils/agent-merger.js +108 -0
- package/dist/utils/compression.cjs +84 -0
- package/dist/utils/compression.js +82 -0
- package/dist/utils/cookies.cjs +514 -0
- package/dist/utils/cookies.js +511 -0
- package/dist/utils/data-operations.cjs +75 -0
- package/dist/utils/data-operations.js +73 -0
- package/dist/utils/form-data.cjs +164 -0
- package/dist/utils/form-data.js +161 -0
- package/dist/utils/headers.cjs +162 -0
- package/dist/utils/headers.js +161 -0
- package/dist/utils/http-config.cjs +723 -0
- package/dist/utils/http-config.js +718 -0
- package/dist/utils/index.cjs +8 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/tools.cjs +18 -0
- package/dist/utils/tools.js +15 -0
- package/package.json +172 -0
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import { RezoCookieJar } from '../utils/cookies.js';
|
|
2
|
+
import { RezoHeaders } from '../utils/headers.js';
|
|
3
|
+
import { RezoFormData } from '../utils/form-data.js';
|
|
4
|
+
import PQueue from "p-queue";
|
|
5
|
+
import { RezoURLSearchParams } from '../utils/data-operations.js';
|
|
6
|
+
import packageJson from "../../package.json" with { type: 'json' };
|
|
7
|
+
import { createDefaultHooks, mergeHooks, runVoidHooksSync, runTransformHooks } from './hooks.js';
|
|
8
|
+
import { ResponseCache, DNSCache } from '../cache/index.js';
|
|
9
|
+
let globalAdapter = null;
|
|
10
|
+
export function setGlobalAdapter(adapter) {
|
|
11
|
+
globalAdapter = adapter;
|
|
12
|
+
}
|
|
13
|
+
export function getGlobalAdapter() {
|
|
14
|
+
return globalAdapter;
|
|
15
|
+
}
|
|
16
|
+
function generateInstanceSessionId() {
|
|
17
|
+
return `ses_${Date.now()}_${Math.random().toString(36).substring(2, 11)}`;
|
|
18
|
+
}
|
|
19
|
+
function parseCacheOption(option) {
|
|
20
|
+
if (!option)
|
|
21
|
+
return {};
|
|
22
|
+
if (option === true) {
|
|
23
|
+
return { response: { enable: true }, dns: true };
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
response: typeof option.response === "boolean" ? option.response ? { enable: true } : undefined : option.response,
|
|
27
|
+
dns: option.dns === true || typeof option.dns === "object" && option.dns.enable !== false
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class Rezo {
|
|
32
|
+
queue = null;
|
|
33
|
+
isQueueEnabled = false;
|
|
34
|
+
defaults;
|
|
35
|
+
hooks;
|
|
36
|
+
jar;
|
|
37
|
+
sessionId;
|
|
38
|
+
responseCache;
|
|
39
|
+
dnsCache;
|
|
40
|
+
adapter;
|
|
41
|
+
constructor(config, adapter) {
|
|
42
|
+
if (!adapter && !globalAdapter) {
|
|
43
|
+
throw new Error(`No HTTP adapter configured. Import from a platform-specific entry:
|
|
44
|
+
` + ` - Node.js/Bun/Deno: import { Rezo } from "rezo"
|
|
45
|
+
` + ` - Browser: import { Rezo } from "rezo"
|
|
46
|
+
` + " - Or provide an adapter: new Rezo(config, myAdapter)");
|
|
47
|
+
}
|
|
48
|
+
this.adapter = adapter || globalAdapter;
|
|
49
|
+
this.defaults = config || {};
|
|
50
|
+
if (config?.cookieJar instanceof RezoCookieJar) {
|
|
51
|
+
this.jar = config.cookieJar;
|
|
52
|
+
} else if (config?.cookieFile) {
|
|
53
|
+
this.jar = RezoCookieJar.fromFile(config.cookieFile);
|
|
54
|
+
} else {
|
|
55
|
+
this.jar = new RezoCookieJar;
|
|
56
|
+
}
|
|
57
|
+
this.hooks = mergeHooks(createDefaultHooks(), config?.hooks);
|
|
58
|
+
this.sessionId = generateInstanceSessionId();
|
|
59
|
+
this.isQueueEnabled = config?.queueOptions?.enable || false;
|
|
60
|
+
if (this.isQueueEnabled) {
|
|
61
|
+
this.queue = new PQueue(config?.queueOptions?.options);
|
|
62
|
+
}
|
|
63
|
+
const cacheConfig = parseCacheOption(config?.cache);
|
|
64
|
+
if (cacheConfig.response) {
|
|
65
|
+
this.responseCache = new ResponseCache(cacheConfig.response);
|
|
66
|
+
}
|
|
67
|
+
if (cacheConfig.dns) {
|
|
68
|
+
const dnsOptions = typeof config?.cache === "object" && typeof config.cache.dns === "object" ? config.cache.dns : undefined;
|
|
69
|
+
this.dnsCache = new DNSCache(dnsOptions);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
clearCache() {
|
|
73
|
+
this.responseCache?.clear();
|
|
74
|
+
this.dnsCache?.clear();
|
|
75
|
+
}
|
|
76
|
+
invalidateCache(url, method) {
|
|
77
|
+
this.responseCache?.invalidate(url, method);
|
|
78
|
+
}
|
|
79
|
+
getCacheStats() {
|
|
80
|
+
const stats = {};
|
|
81
|
+
if (this.responseCache) {
|
|
82
|
+
stats.response = {
|
|
83
|
+
size: this.responseCache.size,
|
|
84
|
+
enabled: this.responseCache.isEnabled,
|
|
85
|
+
persistent: this.responseCache.isPersistent
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (this.dnsCache) {
|
|
89
|
+
stats.dns = {
|
|
90
|
+
size: this.dnsCache.size,
|
|
91
|
+
enabled: this.dnsCache.isEnabled
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return stats;
|
|
95
|
+
}
|
|
96
|
+
get = (url, options) => {
|
|
97
|
+
return this.executeRequest({
|
|
98
|
+
...options,
|
|
99
|
+
method: "GET",
|
|
100
|
+
url
|
|
101
|
+
}, this.defaults, this.jar);
|
|
102
|
+
};
|
|
103
|
+
head = (url, options) => {
|
|
104
|
+
return this.executeRequest({
|
|
105
|
+
...options,
|
|
106
|
+
method: "HEAD",
|
|
107
|
+
url
|
|
108
|
+
}, this.defaults, this.jar);
|
|
109
|
+
};
|
|
110
|
+
options = (url, options) => {
|
|
111
|
+
return this.executeRequest({
|
|
112
|
+
...options,
|
|
113
|
+
method: "OPTIONS",
|
|
114
|
+
url
|
|
115
|
+
}, this.defaults, this.jar);
|
|
116
|
+
};
|
|
117
|
+
trace = (url, options) => {
|
|
118
|
+
return this.executeRequest({
|
|
119
|
+
...options,
|
|
120
|
+
method: "TRACE",
|
|
121
|
+
url
|
|
122
|
+
}, this.defaults, this.jar);
|
|
123
|
+
};
|
|
124
|
+
delete = (url, options) => {
|
|
125
|
+
return this.executeRequest({
|
|
126
|
+
...options,
|
|
127
|
+
method: "DELETE",
|
|
128
|
+
url
|
|
129
|
+
}, this.defaults, this.jar);
|
|
130
|
+
};
|
|
131
|
+
request = (options) => {
|
|
132
|
+
return this.executeRequest(options, this.defaults, this.jar);
|
|
133
|
+
};
|
|
134
|
+
post = (url, data, options = {}) => {
|
|
135
|
+
return this.executeRequest({
|
|
136
|
+
...options,
|
|
137
|
+
url,
|
|
138
|
+
body: data,
|
|
139
|
+
method: "POST"
|
|
140
|
+
}, this.defaults, this.jar);
|
|
141
|
+
};
|
|
142
|
+
postJson = (url, data, options = {}) => {
|
|
143
|
+
const headers = new RezoHeaders(options.headers);
|
|
144
|
+
headers.setContentType("application/json");
|
|
145
|
+
options.headers = headers;
|
|
146
|
+
data = typeof data === "string" ? data : Array.isArray(data) ? JSON.stringify(data) : Object.keys(data || {}).length ? JSON.stringify(data) : "";
|
|
147
|
+
return this.executeRequest({
|
|
148
|
+
...options,
|
|
149
|
+
url,
|
|
150
|
+
body: data,
|
|
151
|
+
method: "POST"
|
|
152
|
+
}, this.defaults, this.jar);
|
|
153
|
+
};
|
|
154
|
+
postForm = (url, data, options = {}) => {
|
|
155
|
+
const headers = new RezoHeaders(options.headers);
|
|
156
|
+
headers.setContentType("application/x-www-form-urlencoded");
|
|
157
|
+
options.headers = headers;
|
|
158
|
+
data = typeof data === "string" ? data : data instanceof URLSearchParams ? data.toString() : data instanceof RezoURLSearchParams ? data.toString() : Object.keys(data || {}).length ? new RezoURLSearchParams(data || {}).toString() : "";
|
|
159
|
+
return this.executeRequest({
|
|
160
|
+
...options,
|
|
161
|
+
url,
|
|
162
|
+
body: data,
|
|
163
|
+
method: "POST"
|
|
164
|
+
}, this.defaults, this.jar);
|
|
165
|
+
};
|
|
166
|
+
postMultipart = async (url, data, options = {}) => {
|
|
167
|
+
data = data instanceof FormData ? await RezoFormData.fromNativeFormData(data) : data;
|
|
168
|
+
return this.executeRequest({
|
|
169
|
+
...options,
|
|
170
|
+
url,
|
|
171
|
+
formData: data,
|
|
172
|
+
method: "POST"
|
|
173
|
+
}, this.defaults, this.jar);
|
|
174
|
+
};
|
|
175
|
+
put = (url, data, options = {}) => {
|
|
176
|
+
return this.executeRequest({
|
|
177
|
+
...options,
|
|
178
|
+
url,
|
|
179
|
+
body: data,
|
|
180
|
+
method: "PUT"
|
|
181
|
+
}, this.defaults, this.jar);
|
|
182
|
+
};
|
|
183
|
+
putJson = (url, data, options = {}) => {
|
|
184
|
+
const headers = new RezoHeaders(options.headers);
|
|
185
|
+
headers.setContentType("application/json");
|
|
186
|
+
options.headers = headers;
|
|
187
|
+
data = typeof data === "string" ? data : Array.isArray(data) ? JSON.stringify(data) : Object.keys(data || {}).length ? JSON.stringify(data) : "";
|
|
188
|
+
return this.executeRequest({
|
|
189
|
+
...options,
|
|
190
|
+
url,
|
|
191
|
+
body: data,
|
|
192
|
+
method: "PUT"
|
|
193
|
+
}, this.defaults, this.jar);
|
|
194
|
+
};
|
|
195
|
+
putForm = (url, data, options = {}) => {
|
|
196
|
+
const headers = new RezoHeaders(options.headers);
|
|
197
|
+
headers.setContentType("application/x-www-form-urlencoded");
|
|
198
|
+
options.headers = headers;
|
|
199
|
+
data = typeof data === "string" ? data : data instanceof URLSearchParams ? data.toString() : data instanceof RezoURLSearchParams ? data.toString() : Object.keys(data || {}).length ? new RezoURLSearchParams(data || {}).toString() : "";
|
|
200
|
+
return this.executeRequest({
|
|
201
|
+
...options,
|
|
202
|
+
url,
|
|
203
|
+
body: data,
|
|
204
|
+
method: "PUT"
|
|
205
|
+
}, this.defaults, this.jar);
|
|
206
|
+
};
|
|
207
|
+
putMultipart = async (url, data, options = {}) => {
|
|
208
|
+
data = data instanceof FormData ? await RezoFormData.fromNativeFormData(data) : data;
|
|
209
|
+
return this.executeRequest({
|
|
210
|
+
...options,
|
|
211
|
+
url,
|
|
212
|
+
formData: data,
|
|
213
|
+
method: "PUT"
|
|
214
|
+
}, this.defaults, this.jar);
|
|
215
|
+
};
|
|
216
|
+
patch = (url, data, options = {}) => {
|
|
217
|
+
return this.executeRequest({
|
|
218
|
+
...options,
|
|
219
|
+
url,
|
|
220
|
+
body: data,
|
|
221
|
+
method: "PATCH"
|
|
222
|
+
}, this.defaults, this.jar);
|
|
223
|
+
};
|
|
224
|
+
patchJson = (url, data, options = {}) => {
|
|
225
|
+
const headers = new RezoHeaders(options.headers);
|
|
226
|
+
headers.setContentType("application/json");
|
|
227
|
+
options.headers = headers;
|
|
228
|
+
data = typeof data === "string" ? data : Array.isArray(data) ? JSON.stringify(data) : Object.keys(data || {}).length ? JSON.stringify(data) : "";
|
|
229
|
+
return this.executeRequest({
|
|
230
|
+
...options,
|
|
231
|
+
url,
|
|
232
|
+
body: data,
|
|
233
|
+
method: "PATCH"
|
|
234
|
+
}, this.defaults, this.jar);
|
|
235
|
+
};
|
|
236
|
+
patchForm = (url, data, options = {}) => {
|
|
237
|
+
const headers = new RezoHeaders(options.headers);
|
|
238
|
+
headers.setContentType("application/x-www-form-urlencoded");
|
|
239
|
+
options.headers = headers;
|
|
240
|
+
data = typeof data === "string" ? data : data instanceof URLSearchParams ? data.toString() : data instanceof RezoURLSearchParams ? data.toString() : Object.keys(data || {}).length ? new RezoURLSearchParams(data || {}).toString() : "";
|
|
241
|
+
return this.executeRequest({
|
|
242
|
+
...options,
|
|
243
|
+
url,
|
|
244
|
+
body: data,
|
|
245
|
+
method: "PATCH"
|
|
246
|
+
}, this.defaults, this.jar);
|
|
247
|
+
};
|
|
248
|
+
patchMultipart = async (url, data, options = {}) => {
|
|
249
|
+
data = data instanceof FormData ? await RezoFormData.fromNativeFormData(data) : data;
|
|
250
|
+
return this.executeRequest({
|
|
251
|
+
...options,
|
|
252
|
+
url,
|
|
253
|
+
formData: data,
|
|
254
|
+
method: "PATCH"
|
|
255
|
+
}, this.defaults, this.jar);
|
|
256
|
+
};
|
|
257
|
+
async executeRequest(options, defaultOptions, jar) {
|
|
258
|
+
const requestHooks = options.hooks ? mergeHooks(this.hooks, options.hooks) : this.hooks;
|
|
259
|
+
options.sessionId = this.sessionId;
|
|
260
|
+
const plainOptions = { ...options };
|
|
261
|
+
runVoidHooksSync(requestHooks.init, plainOptions, options);
|
|
262
|
+
const headers = new RezoHeaders(options.headers);
|
|
263
|
+
headers.setUserAgent(headers.getUserAgent() || `Rezo/${packageJson.version} (+https://www.npmjs.com/package/${packageJson.name})`);
|
|
264
|
+
const fullUrl = this.buildFullUrl(options.url, defaultOptions.baseURL);
|
|
265
|
+
const method = (options.method || "GET").toUpperCase();
|
|
266
|
+
const requestHeadersRaw = headers.toObject();
|
|
267
|
+
const requestHeaders = {};
|
|
268
|
+
for (const [key, value] of Object.entries(requestHeadersRaw)) {
|
|
269
|
+
requestHeaders[key] = Array.isArray(value) ? value.join(", ") : value;
|
|
270
|
+
}
|
|
271
|
+
const cacheMode = options.cache;
|
|
272
|
+
const shouldCheckCache = this.responseCache && cacheMode !== "reload" && cacheMode !== "no-store" && !(options._isStream || options._isDownload || options._isUpload);
|
|
273
|
+
if (shouldCheckCache) {
|
|
274
|
+
const cached = this.responseCache.get(method, fullUrl, requestHeaders);
|
|
275
|
+
if (cached) {
|
|
276
|
+
return {
|
|
277
|
+
data: cached.data,
|
|
278
|
+
status: cached.status,
|
|
279
|
+
statusText: cached.statusText,
|
|
280
|
+
headers: cached.headers,
|
|
281
|
+
config: options,
|
|
282
|
+
request: undefined,
|
|
283
|
+
_fromCache: true
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
if (cacheMode === "only-if-cached") {
|
|
287
|
+
throw new Error("Response not found in cache");
|
|
288
|
+
}
|
|
289
|
+
if (cacheMode !== "force-cache") {
|
|
290
|
+
const conditionalHeaders = this.responseCache.getConditionalHeaders(method, fullUrl, requestHeaders);
|
|
291
|
+
if (conditionalHeaders) {
|
|
292
|
+
Object.assign(requestHeaders, conditionalHeaders);
|
|
293
|
+
options.headers = requestHeaders;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const executeWithHooks = async () => {
|
|
298
|
+
const response = await this.adapter(options, defaultOptions, jar);
|
|
299
|
+
if (jar.cookieFile) {
|
|
300
|
+
try {
|
|
301
|
+
jar.saveToFile();
|
|
302
|
+
} catch (e) {}
|
|
303
|
+
}
|
|
304
|
+
if (this.responseCache && cacheMode !== "no-store" && response && typeof response === "object" && "data" in response && !(options._isStream || options._isDownload || options._isUpload)) {
|
|
305
|
+
this.responseCache.set(method, fullUrl, response, requestHeaders);
|
|
306
|
+
}
|
|
307
|
+
if (response && typeof response === "object" && "data" in response && requestHooks.afterResponse.length > 0) {
|
|
308
|
+
const context = {
|
|
309
|
+
retryCount: 0,
|
|
310
|
+
retryWithMergedOptions: (mergeOptions) => {
|
|
311
|
+
throw new Error("Retry requested with options: " + JSON.stringify(mergeOptions));
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
return runTransformHooks(requestHooks.afterResponse, response, response.config, context);
|
|
315
|
+
}
|
|
316
|
+
return response;
|
|
317
|
+
};
|
|
318
|
+
if (options.queue) {
|
|
319
|
+
return await options.queue.add(() => executeWithHooks());
|
|
320
|
+
}
|
|
321
|
+
if (this.isQueueEnabled && this.queue) {
|
|
322
|
+
return await this.queue.add(() => executeWithHooks());
|
|
323
|
+
}
|
|
324
|
+
return executeWithHooks();
|
|
325
|
+
}
|
|
326
|
+
buildFullUrl(url, baseURL) {
|
|
327
|
+
if (!url)
|
|
328
|
+
return baseURL || "";
|
|
329
|
+
const urlStr = url.toString();
|
|
330
|
+
if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
|
|
331
|
+
return urlStr;
|
|
332
|
+
}
|
|
333
|
+
if (baseURL) {
|
|
334
|
+
return baseURL.endsWith("/") ? baseURL + (urlStr.startsWith("/") ? urlStr.slice(1) : urlStr) : baseURL + (urlStr.startsWith("/") ? urlStr : "/" + urlStr);
|
|
335
|
+
}
|
|
336
|
+
return urlStr;
|
|
337
|
+
}
|
|
338
|
+
isvalidJson(str) {
|
|
339
|
+
try {
|
|
340
|
+
return JSON.parse(str) ? true : false;
|
|
341
|
+
} catch {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
__create(config) {
|
|
346
|
+
return new Rezo(config, this.adapter);
|
|
347
|
+
}
|
|
348
|
+
get cookieJar() {
|
|
349
|
+
return this.jar;
|
|
350
|
+
}
|
|
351
|
+
saveCookies(filePath) {
|
|
352
|
+
if (filePath) {
|
|
353
|
+
this.jar.saveToFile(filePath);
|
|
354
|
+
} else if (this.jar.cookieFile) {
|
|
355
|
+
this.jar.saveToFile();
|
|
356
|
+
} else {
|
|
357
|
+
throw new Error("No cookie file path configured. Provide a path or configure cookieFile option.");
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
stream(url, options) {
|
|
361
|
+
return this.executeRequest({
|
|
362
|
+
...options,
|
|
363
|
+
method: options?.method || "GET",
|
|
364
|
+
url,
|
|
365
|
+
_isStream: true
|
|
366
|
+
}, this.defaults, this.jar);
|
|
367
|
+
}
|
|
368
|
+
download(url, saveTo, options) {
|
|
369
|
+
return this.executeRequest({
|
|
370
|
+
...options,
|
|
371
|
+
method: options?.method || "GET",
|
|
372
|
+
url,
|
|
373
|
+
saveTo,
|
|
374
|
+
_isDownload: true
|
|
375
|
+
}, this.defaults, this.jar);
|
|
376
|
+
}
|
|
377
|
+
upload(url, data, options) {
|
|
378
|
+
return this.executeRequest({
|
|
379
|
+
...options,
|
|
380
|
+
method: options?.method || "POST",
|
|
381
|
+
url,
|
|
382
|
+
body: data,
|
|
383
|
+
_isUpload: true
|
|
384
|
+
}, this.defaults, this.jar);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
export const defaultTransforms = {
|
|
388
|
+
request: [
|
|
389
|
+
function transformRequestData(data, headers, config) {
|
|
390
|
+
if (data === null || data === undefined) {
|
|
391
|
+
return data;
|
|
392
|
+
}
|
|
393
|
+
if (config?.isMultipart || config?.multipart) {
|
|
394
|
+
if (typeof FormData !== "undefined" && data instanceof FormData) {
|
|
395
|
+
return data;
|
|
396
|
+
}
|
|
397
|
+
if (data instanceof RezoFormData) {
|
|
398
|
+
if (headers instanceof RezoHeaders) {
|
|
399
|
+
headers.setContentType(data.getContentType());
|
|
400
|
+
}
|
|
401
|
+
return data.toBuffer();
|
|
402
|
+
}
|
|
403
|
+
if (typeof data === "object" && data.constructor === Object) {
|
|
404
|
+
const formData = RezoFormData.fromObject(data);
|
|
405
|
+
if (headers instanceof RezoHeaders) {
|
|
406
|
+
headers.setContentType(formData.getContentType());
|
|
407
|
+
}
|
|
408
|
+
return formData.toBuffer();
|
|
409
|
+
}
|
|
410
|
+
return data;
|
|
411
|
+
}
|
|
412
|
+
if (typeof FormData !== "undefined" && data instanceof FormData) {
|
|
413
|
+
return data;
|
|
414
|
+
}
|
|
415
|
+
if (typeof URLSearchParams !== "undefined" && data instanceof URLSearchParams) {
|
|
416
|
+
if (headers instanceof RezoHeaders) {
|
|
417
|
+
headers.setContentType("application/x-www-form-urlencoded;charset=utf-8");
|
|
418
|
+
} else {
|
|
419
|
+
headers["content-type"] = "application/x-www-form-urlencoded;charset=utf-8";
|
|
420
|
+
}
|
|
421
|
+
return data.toString();
|
|
422
|
+
}
|
|
423
|
+
if (typeof data === "object" && data.constructor === Object) {
|
|
424
|
+
if (headers instanceof RezoHeaders) {
|
|
425
|
+
headers.setContentType("application/json;charset=utf-8");
|
|
426
|
+
} else {
|
|
427
|
+
headers["content-type"] = "application/json;charset=utf-8";
|
|
428
|
+
}
|
|
429
|
+
return JSON.stringify(data);
|
|
430
|
+
}
|
|
431
|
+
return data;
|
|
432
|
+
}
|
|
433
|
+
],
|
|
434
|
+
response: [
|
|
435
|
+
function transformResponseData(data) {
|
|
436
|
+
if (typeof data === "string") {
|
|
437
|
+
try {
|
|
438
|
+
return JSON.parse(data);
|
|
439
|
+
} catch {}
|
|
440
|
+
}
|
|
441
|
+
return data;
|
|
442
|
+
}
|
|
443
|
+
]
|
|
444
|
+
};
|
|
445
|
+
export function createRezoInstance(adapter, config) {
|
|
446
|
+
const instance = new Rezo(config, adapter);
|
|
447
|
+
instance.create = (cfg) => new Rezo(cfg, adapter);
|
|
448
|
+
return instance;
|
|
449
|
+
}
|
|
450
|
+
export function createDefaultInstance(config) {
|
|
451
|
+
if (!globalAdapter) {
|
|
452
|
+
throw new Error(`No HTTP adapter configured. Import from a platform-specific entry:
|
|
453
|
+
` + ` - Node.js: import rezo from "rezo"
|
|
454
|
+
` + ` - Browser: import rezo from "rezo"
|
|
455
|
+
` + "The bundler will automatically select the appropriate adapter.");
|
|
456
|
+
}
|
|
457
|
+
return createRezoInstance(globalAdapter, config);
|
|
458
|
+
}
|