multporn-api-sdk 0.1.1 → 0.1.3

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.js CHANGED
@@ -5,7 +5,35 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/http.ts
8
- import { setTimeout as sleep } from "timers/promises";
8
+ var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
9
+ function hasAbortController() {
10
+ return typeof AbortController !== "undefined";
11
+ }
12
+ async function fetchWithTimeout(url, init, timeoutMs) {
13
+ if (hasAbortController()) {
14
+ const ctrl = new AbortController();
15
+ const timer = setTimeout(() => {
16
+ try {
17
+ ctrl.abort();
18
+ } catch {
19
+ }
20
+ }, timeoutMs);
21
+ try {
22
+ const res = await fetch(url, { ...init, signal: ctrl.signal });
23
+ clearTimeout(timer);
24
+ return res;
25
+ } catch (e) {
26
+ clearTimeout(timer);
27
+ throw e;
28
+ }
29
+ }
30
+ return await Promise.race([
31
+ fetch(url, init),
32
+ new Promise(
33
+ (_, reject) => setTimeout(() => reject(new HttpError("Request timeout")), timeoutMs)
34
+ )
35
+ ]);
36
+ }
9
37
  var defaultRetryPolicy = {
10
38
  retries: 2,
11
39
  factor: 2,
@@ -34,7 +62,7 @@ var HttpClient = class {
34
62
  this.headers = opts.headers ?? {};
35
63
  this.timeoutMs = opts.timeoutMs ?? 15e3;
36
64
  this.retry = { ...defaultRetryPolicy, ...opts.retry ?? {} };
37
- this.userAgent = opts.userAgent ?? "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118 Safari/537.36";
65
+ this.userAgent = opts.userAgent ?? "Mozilla/5.0 (Windows NT 10.0; Win32; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118 Safari/537.36";
38
66
  }
39
67
  buildURL(pathOrUrl) {
40
68
  try {
@@ -45,19 +73,19 @@ var HttpClient = class {
45
73
  }
46
74
  async getHtml(pathOrUrl, attempt = 0) {
47
75
  const url = this.buildURL(pathOrUrl);
48
- const ctrl = new AbortController();
49
- const id = setTimeout(() => ctrl.abort(), this.timeoutMs);
50
76
  try {
51
- const res = await fetch(url, {
52
- method: "GET",
53
- headers: {
54
- "User-Agent": this.userAgent,
55
- Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
56
- ...this.headers
77
+ const res = await fetchWithTimeout(
78
+ url,
79
+ {
80
+ method: "GET",
81
+ headers: {
82
+ "User-Agent": this.userAgent,
83
+ Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
84
+ ...this.headers
85
+ }
57
86
  },
58
- signal: ctrl.signal
59
- });
60
- clearTimeout(id);
87
+ this.timeoutMs
88
+ );
61
89
  if (!res.ok) {
62
90
  if (this.retry.retryOn(res.status) && attempt < this.retry.retries) {
63
91
  const delay = Math.min(
@@ -71,7 +99,6 @@ var HttpClient = class {
71
99
  }
72
100
  return await res.text();
73
101
  } catch (e) {
74
- clearTimeout(id);
75
102
  if (attempt < this.retry.retries) {
76
103
  const delay = Math.min(
77
104
  this.retry.maxDelayMs,
@@ -80,25 +107,25 @@ var HttpClient = class {
80
107
  await sleep(delay);
81
108
  return this.getHtml(pathOrUrl, attempt + 1);
82
109
  }
83
- if (e?.name == "AbortError") throw new HttpError("Request timeout");
110
+ if (e?.name === "AbortError") throw new HttpError("Request timeout");
84
111
  throw new HttpError(e?.message ?? "Network error");
85
112
  }
86
113
  }
87
114
  async getJson(pathOrUrl, attempt = 0) {
88
115
  const url = this.buildURL(pathOrUrl);
89
- const ctrl = new AbortController();
90
- const id = setTimeout(() => ctrl.abort(), this.timeoutMs);
91
116
  try {
92
- const res = await fetch(url, {
93
- method: "GET",
94
- headers: {
95
- "User-Agent": this.userAgent,
96
- Accept: "application/json,text/plain;q=0.9,*/*;q=0.8",
97
- ...this.headers
117
+ const res = await fetchWithTimeout(
118
+ url,
119
+ {
120
+ method: "GET",
121
+ headers: {
122
+ "User-Agent": this.userAgent,
123
+ Accept: "application/json,text/plain;q=0.9,*/*;q=0.8",
124
+ ...this.headers
125
+ }
98
126
  },
99
- signal: ctrl.signal
100
- });
101
- clearTimeout(id);
127
+ this.timeoutMs
128
+ );
102
129
  if (!res.ok) {
103
130
  if (this.retry.retryOn(res.status) && attempt < this.retry.retries) {
104
131
  const delay = Math.min(
@@ -112,7 +139,6 @@ var HttpClient = class {
112
139
  }
113
140
  return await res.json();
114
141
  } catch (e) {
115
- clearTimeout(id);
116
142
  if (attempt < this.retry.retries) {
117
143
  const delay = Math.min(
118
144
  this.retry.maxDelayMs,
@@ -121,14 +147,12 @@ var HttpClient = class {
121
147
  await sleep(delay);
122
148
  return this.getJson(pathOrUrl, attempt + 1);
123
149
  }
124
- if (e?.name == "AbortError") throw new HttpError("Request timeout");
150
+ if (e?.name === "AbortError") throw new HttpError("Request timeout");
125
151
  throw new HttpError(e?.message ?? "Network error");
126
152
  }
127
153
  }
128
154
  async postForm(pathOrUrl, form, attempt = 0) {
129
155
  const url = this.buildURL(pathOrUrl);
130
- const ctrl = new AbortController();
131
- const id = setTimeout(() => ctrl.abort(), this.timeoutMs);
132
156
  const usp = new URLSearchParams();
133
157
  for (const [k, v] of Object.entries(form)) {
134
158
  if (Array.isArray(v)) {
@@ -139,19 +163,21 @@ var HttpClient = class {
139
163
  }
140
164
  const body = usp.toString();
141
165
  try {
142
- const res = await fetch(url, {
143
- method: "POST",
144
- headers: {
145
- "User-Agent": this.userAgent,
146
- "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
147
- "X-Requested-With": "XMLHttpRequest",
148
- Accept: "application/json, text/javascript, */*; q=0.01",
149
- ...this.headers
166
+ const res = await fetchWithTimeout(
167
+ url,
168
+ {
169
+ method: "POST",
170
+ headers: {
171
+ "User-Agent": this.userAgent,
172
+ "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
173
+ "X-Requested-With": "XMLHttpRequest",
174
+ Accept: "application/json, text/javascript, */*; q=0.01",
175
+ ...this.headers
176
+ },
177
+ body
150
178
  },
151
- body,
152
- signal: ctrl.signal
153
- });
154
- clearTimeout(id);
179
+ this.timeoutMs
180
+ );
155
181
  if (!res.ok) {
156
182
  if (this.retry.retryOn(res.status) && attempt < this.retry.retries) {
157
183
  const delay = Math.min(
@@ -174,7 +200,6 @@ var HttpClient = class {
174
200
  return text;
175
201
  }
176
202
  } catch (e) {
177
- clearTimeout(id);
178
203
  if (attempt < this.retry.retries) {
179
204
  const delay = Math.min(
180
205
  this.retry.maxDelayMs,
@@ -183,7 +208,7 @@ var HttpClient = class {
183
208
  await sleep(delay);
184
209
  return this.postForm(pathOrUrl, form, attempt + 1);
185
210
  }
186
- if (e?.name == "AbortError") throw new HttpError("Request timeout");
211
+ if (e?.name === "AbortError") throw new HttpError("Request timeout");
187
212
  throw new HttpError(e?.message ?? "Network error");
188
213
  }
189
214
  }