multporn-api-sdk 0.1.1 → 0.1.2

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/index.cjs CHANGED
@@ -40,7 +40,35 @@ __export(index_exports, {
40
40
  module.exports = __toCommonJS(index_exports);
41
41
 
42
42
  // src/http.ts
43
- var import_promises = require("timers/promises");
43
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
44
+ function hasAbortController() {
45
+ return typeof AbortController !== "undefined";
46
+ }
47
+ async function fetchWithTimeout(url, init, timeoutMs) {
48
+ if (hasAbortController()) {
49
+ const ctrl = new AbortController();
50
+ const timer = setTimeout(() => {
51
+ try {
52
+ ctrl.abort();
53
+ } catch {
54
+ }
55
+ }, timeoutMs);
56
+ try {
57
+ const res = await fetch(url, { ...init, signal: ctrl.signal });
58
+ clearTimeout(timer);
59
+ return res;
60
+ } catch (e) {
61
+ clearTimeout(timer);
62
+ throw e;
63
+ }
64
+ }
65
+ return await Promise.race([
66
+ fetch(url, init),
67
+ new Promise(
68
+ (_, reject) => setTimeout(() => reject(new HttpError("Request timeout")), timeoutMs)
69
+ )
70
+ ]);
71
+ }
44
72
  var defaultRetryPolicy = {
45
73
  retries: 2,
46
74
  factor: 2,
@@ -69,7 +97,7 @@ var HttpClient = class {
69
97
  this.headers = opts.headers ?? {};
70
98
  this.timeoutMs = opts.timeoutMs ?? 15e3;
71
99
  this.retry = { ...defaultRetryPolicy, ...opts.retry ?? {} };
72
- this.userAgent = opts.userAgent ?? "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118 Safari/537.36";
100
+ this.userAgent = opts.userAgent ?? "Mozilla/5.0 (Windows NT 10.0; Win32; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118 Safari/537.36";
73
101
  }
74
102
  buildURL(pathOrUrl) {
75
103
  try {
@@ -80,90 +108,86 @@ var HttpClient = class {
80
108
  }
81
109
  async getHtml(pathOrUrl, attempt = 0) {
82
110
  const url = this.buildURL(pathOrUrl);
83
- const ctrl = new AbortController();
84
- const id = setTimeout(() => ctrl.abort(), this.timeoutMs);
85
111
  try {
86
- const res = await fetch(url, {
87
- method: "GET",
88
- headers: {
89
- "User-Agent": this.userAgent,
90
- Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
91
- ...this.headers
112
+ const res = await fetchWithTimeout(
113
+ url,
114
+ {
115
+ method: "GET",
116
+ headers: {
117
+ "User-Agent": this.userAgent,
118
+ Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
119
+ ...this.headers
120
+ }
92
121
  },
93
- signal: ctrl.signal
94
- });
95
- clearTimeout(id);
122
+ this.timeoutMs
123
+ );
96
124
  if (!res.ok) {
97
125
  if (this.retry.retryOn(res.status) && attempt < this.retry.retries) {
98
126
  const delay = Math.min(
99
127
  this.retry.maxDelayMs,
100
128
  this.retry.minDelayMs * this.retry.factor ** attempt
101
129
  );
102
- await (0, import_promises.setTimeout)(delay);
130
+ await sleep(delay);
103
131
  return this.getHtml(pathOrUrl, attempt + 1);
104
132
  }
105
133
  throw new HttpError(`HTTP ${res.status}`, res.status);
106
134
  }
107
135
  return await res.text();
108
136
  } catch (e) {
109
- clearTimeout(id);
110
137
  if (attempt < this.retry.retries) {
111
138
  const delay = Math.min(
112
139
  this.retry.maxDelayMs,
113
140
  this.retry.minDelayMs * this.retry.factor ** attempt
114
141
  );
115
- await (0, import_promises.setTimeout)(delay);
142
+ await sleep(delay);
116
143
  return this.getHtml(pathOrUrl, attempt + 1);
117
144
  }
118
- if (e?.name == "AbortError") throw new HttpError("Request timeout");
145
+ if (e?.name === "AbortError") throw new HttpError("Request timeout");
119
146
  throw new HttpError(e?.message ?? "Network error");
120
147
  }
121
148
  }
122
149
  async getJson(pathOrUrl, attempt = 0) {
123
150
  const url = this.buildURL(pathOrUrl);
124
- const ctrl = new AbortController();
125
- const id = setTimeout(() => ctrl.abort(), this.timeoutMs);
126
151
  try {
127
- const res = await fetch(url, {
128
- method: "GET",
129
- headers: {
130
- "User-Agent": this.userAgent,
131
- Accept: "application/json,text/plain;q=0.9,*/*;q=0.8",
132
- ...this.headers
152
+ const res = await fetchWithTimeout(
153
+ url,
154
+ {
155
+ method: "GET",
156
+ headers: {
157
+ "User-Agent": this.userAgent,
158
+ Accept: "application/json,text/plain;q=0.9,*/*;q=0.8",
159
+ ...this.headers
160
+ }
133
161
  },
134
- signal: ctrl.signal
135
- });
136
- clearTimeout(id);
162
+ this.timeoutMs
163
+ );
137
164
  if (!res.ok) {
138
165
  if (this.retry.retryOn(res.status) && attempt < this.retry.retries) {
139
166
  const delay = Math.min(
140
167
  this.retry.maxDelayMs,
141
168
  this.retry.minDelayMs * this.retry.factor ** attempt
142
169
  );
143
- await (0, import_promises.setTimeout)(delay);
170
+ await sleep(delay);
144
171
  return this.getJson(pathOrUrl, attempt + 1);
145
172
  }
146
173
  throw new HttpError(`HTTP ${res.status}`, res.status);
147
174
  }
148
175
  return await res.json();
149
176
  } catch (e) {
150
- clearTimeout(id);
151
177
  if (attempt < this.retry.retries) {
152
178
  const delay = Math.min(
153
179
  this.retry.maxDelayMs,
154
180
  this.retry.minDelayMs * this.retry.factor ** attempt
155
181
  );
156
- await (0, import_promises.setTimeout)(delay);
182
+ await sleep(delay);
157
183
  return this.getJson(pathOrUrl, attempt + 1);
158
184
  }
159
- if (e?.name == "AbortError") throw new HttpError("Request timeout");
185
+ if (e?.name === "AbortError") throw new HttpError("Request timeout");
160
186
  throw new HttpError(e?.message ?? "Network error");
161
187
  }
162
188
  }
163
189
  async postForm(pathOrUrl, form, attempt = 0) {
164
190
  const url = this.buildURL(pathOrUrl);
165
- const ctrl = new AbortController();
166
- const id = setTimeout(() => ctrl.abort(), this.timeoutMs);
167
191
  const usp = new URLSearchParams();
168
192
  for (const [k, v] of Object.entries(form)) {
169
193
  if (Array.isArray(v)) {
@@ -174,26 +198,28 @@ var HttpClient = class {
174
198
  }
175
199
  const body = usp.toString();
176
200
  try {
177
- const res = await fetch(url, {
178
- method: "POST",
179
- headers: {
180
- "User-Agent": this.userAgent,
181
- "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
182
- "X-Requested-With": "XMLHttpRequest",
183
- Accept: "application/json, text/javascript, */*; q=0.01",
184
- ...this.headers
201
+ const res = await fetchWithTimeout(
202
+ url,
203
+ {
204
+ method: "POST",
205
+ headers: {
206
+ "User-Agent": this.userAgent,
207
+ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
208
+ "X-Requested-With": "XMLHttpRequest",
209
+ Accept: "application/json, text/javascript, */*; q=0.01",
210
+ ...this.headers
211
+ },
212
+ body
185
213
  },
186
- body,
187
- signal: ctrl.signal
188
- });
189
- clearTimeout(id);
214
+ this.timeoutMs
215
+ );
190
216
  if (!res.ok) {
191
217
  if (this.retry.retryOn(res.status) && attempt < this.retry.retries) {
192
218
  const delay = Math.min(
193
219
  this.retry.maxDelayMs,
194
220
  this.retry.minDelayMs * this.retry.factor ** attempt
195
221
  );
196
- await (0, import_promises.setTimeout)(delay);
222
+ await sleep(delay);
197
223
  return this.postForm(pathOrUrl, form, attempt + 1);
198
224
  }
199
225
  throw new HttpError(`HTTP ${res.status}`, res.status);
@@ -209,16 +235,15 @@ var HttpClient = class {
209
235
  return text;
210
236
  }
211
237
  } catch (e) {
212
- clearTimeout(id);
213
238
  if (attempt < this.retry.retries) {
214
239
  const delay = Math.min(
215
240
  this.retry.maxDelayMs,
216
241
  this.retry.minDelayMs * this.retry.factor ** attempt
217
242
  );
218
- await (0, import_promises.setTimeout)(delay);
243
+ await sleep(delay);
219
244
  return this.postForm(pathOrUrl, form, attempt + 1);
220
245
  }
221
- if (e?.name == "AbortError") throw new HttpError("Request timeout");
246
+ if (e?.name === "AbortError") throw new HttpError("Request timeout");
222
247
  throw new HttpError(e?.message ?? "Network error");
223
248
  }
224
249
  }