stophy 1.0.7 → 1.2.1
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/README.md +22 -21
- package/dist/index.cjs +305 -135
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12487 -4851
- package/dist/index.d.ts +12487 -4851
- package/dist/index.js +305 -135
- package/dist/index.js.map +1 -1
- package/package.json +23 -22
package/dist/index.js
CHANGED
|
@@ -16,6 +16,23 @@ var StophyError = class extends Error {
|
|
|
16
16
|
}
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
+
// src/json.ts
|
|
20
|
+
function tagOf(value) {
|
|
21
|
+
return Object.prototype.toString.call(value);
|
|
22
|
+
}
|
|
23
|
+
function isString(value) {
|
|
24
|
+
return tagOf(value) === "[object String]";
|
|
25
|
+
}
|
|
26
|
+
function isNumber(value) {
|
|
27
|
+
return tagOf(value) === "[object Number]";
|
|
28
|
+
}
|
|
29
|
+
function isBoolean(value) {
|
|
30
|
+
return value === true || value === false;
|
|
31
|
+
}
|
|
32
|
+
function isRecord(value) {
|
|
33
|
+
return tagOf(value) === "[object Object]";
|
|
34
|
+
}
|
|
35
|
+
|
|
19
36
|
// src/account.ts
|
|
20
37
|
function usage(call) {
|
|
21
38
|
return call({ method: "GET", path: "/v1/usage" }).then(parseUsage);
|
|
@@ -26,9 +43,7 @@ function logs(call, query) {
|
|
|
26
43
|
if (query?.days !== void 0) params.days = query.days;
|
|
27
44
|
if (query?.endpoint !== void 0) params.endpoint = query.endpoint;
|
|
28
45
|
if (query?.page !== void 0) params.page = query.page;
|
|
29
|
-
return call({ method: "GET", path: "/v1/logs", query: params }).then(
|
|
30
|
-
parseLogs
|
|
31
|
-
);
|
|
46
|
+
return call({ method: "GET", path: "/v1/logs", query: params }).then(parseLogs);
|
|
32
47
|
}
|
|
33
48
|
function parseUsage(value) {
|
|
34
49
|
if (!isRecord(value)) return unexpected("usage");
|
|
@@ -43,7 +58,7 @@ function parseLogs(value) {
|
|
|
43
58
|
}
|
|
44
59
|
return {
|
|
45
60
|
endpoints: value.endpoints.map((item) => {
|
|
46
|
-
if (
|
|
61
|
+
if (!isString(item)) return unexpected("logs.endpoints");
|
|
47
62
|
return item;
|
|
48
63
|
}),
|
|
49
64
|
logs: value.logs.map(parseLog),
|
|
@@ -70,24 +85,24 @@ function parseLog(value) {
|
|
|
70
85
|
}
|
|
71
86
|
function numberField(record, field) {
|
|
72
87
|
const value = record[field];
|
|
73
|
-
if (
|
|
88
|
+
if (!isNumber(value)) return unexpected(field);
|
|
74
89
|
return value;
|
|
75
90
|
}
|
|
76
91
|
function numberOrNull(record, field) {
|
|
77
92
|
const value = record[field];
|
|
78
93
|
if (value === null) return null;
|
|
79
|
-
if (
|
|
94
|
+
if (!isNumber(value)) return unexpected(field);
|
|
80
95
|
return value;
|
|
81
96
|
}
|
|
82
97
|
function stringField(record, field) {
|
|
83
98
|
const value = record[field];
|
|
84
|
-
if (
|
|
99
|
+
if (!isString(value)) return unexpected(field);
|
|
85
100
|
return value;
|
|
86
101
|
}
|
|
87
102
|
function stringOrNull(record, field) {
|
|
88
103
|
const value = record[field];
|
|
89
104
|
if (value === null) return null;
|
|
90
|
-
if (
|
|
105
|
+
if (!isString(value)) return unexpected(field);
|
|
91
106
|
return value;
|
|
92
107
|
}
|
|
93
108
|
function unexpected(field) {
|
|
@@ -96,9 +111,6 @@ function unexpected(field) {
|
|
|
96
111
|
retryable: false
|
|
97
112
|
});
|
|
98
113
|
}
|
|
99
|
-
function isRecord(value) {
|
|
100
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
101
|
-
}
|
|
102
114
|
|
|
103
115
|
// src/bind.ts
|
|
104
116
|
function post(path, call) {
|
|
@@ -124,137 +136,310 @@ function bindSurface(call) {
|
|
|
124
136
|
search: post("/v1/google/search", call),
|
|
125
137
|
news: postOptional("/v1/google/news", call),
|
|
126
138
|
images: post("/v1/google/images", call),
|
|
127
|
-
|
|
128
|
-
|
|
139
|
+
suggest: post("/v1/google/suggest", call),
|
|
140
|
+
ads: {
|
|
141
|
+
search: postOptional("/v1/google/ads/search", call),
|
|
142
|
+
ad: postOptional("/v1/google/ads/ad", call),
|
|
143
|
+
advertisers: post("/v1/google/ads/advertisers", call)
|
|
144
|
+
},
|
|
145
|
+
scholar: postOptional("/v1/google/scholar", call),
|
|
146
|
+
patents: postOptional("/v1/google/patents", call),
|
|
147
|
+
maps: {
|
|
148
|
+
search: post("/v1/google/maps/search", call),
|
|
149
|
+
place: postOptional("/v1/google/maps/place", call),
|
|
150
|
+
reviews: postOptional("/v1/google/maps/reviews", call)
|
|
151
|
+
},
|
|
152
|
+
trends: {
|
|
153
|
+
related: post("/v1/google/trends/related", call),
|
|
154
|
+
trending: postOptional("/v1/google/trends/trending", call),
|
|
155
|
+
interest: post("/v1/google/trends/interest", call),
|
|
156
|
+
regions: post("/v1/google/trends/regions", call)
|
|
157
|
+
},
|
|
158
|
+
flights: post("/v1/google/flights", call),
|
|
159
|
+
hotels: post("/v1/google/hotels", call),
|
|
160
|
+
play: {
|
|
161
|
+
app: postOptional("/v1/google/play/app", call),
|
|
162
|
+
search: post("/v1/google/play/search", call),
|
|
163
|
+
reviews: postOptional("/v1/google/play/reviews", call)
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
web: {
|
|
167
|
+
search: post("/v1/web/search", call),
|
|
168
|
+
news: post("/v1/web/news", call),
|
|
169
|
+
images: post("/v1/web/images", call),
|
|
170
|
+
scrape: postOptional("/v1/web/scrape", call),
|
|
171
|
+
parse: post("/v1/web/parse", call),
|
|
172
|
+
map: post("/v1/web/map", call),
|
|
173
|
+
contacts: post("/v1/web/contacts", call),
|
|
174
|
+
crawl: Object.assign(post("/v1/web/crawl", call), {
|
|
175
|
+
start: post("/v1/web/crawl/start", call),
|
|
176
|
+
status: post("/v1/web/crawl/status", call),
|
|
177
|
+
cancel: post("/v1/web/crawl/cancel", call)
|
|
178
|
+
}),
|
|
179
|
+
batch: Object.assign(post("/v1/web/batch", call), {
|
|
180
|
+
status: post("/v1/web/batch/status", call),
|
|
181
|
+
cancel: post("/v1/web/batch/cancel", call)
|
|
182
|
+
})
|
|
129
183
|
},
|
|
130
184
|
youtube: {
|
|
185
|
+
suggest: post("/v1/youtube/suggest", call),
|
|
131
186
|
search: post("/v1/youtube/search", call),
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
187
|
+
transcript: postOptional("/v1/youtube/transcript", call),
|
|
188
|
+
video: postOptional("/v1/youtube/video", call),
|
|
189
|
+
comments: postOptional("/v1/youtube/comments", call),
|
|
190
|
+
channel: postOptional("/v1/youtube/channel", call),
|
|
191
|
+
post: postOptional("/v1/youtube/post", call),
|
|
192
|
+
playlist: postOptional("/v1/youtube/playlist", call)
|
|
136
193
|
},
|
|
137
|
-
transcript: post("/v1/transcript", call),
|
|
138
194
|
reddit: {
|
|
139
195
|
search: post("/v1/reddit/search", call),
|
|
140
|
-
post:
|
|
196
|
+
post: postOptional("/v1/reddit/post", call),
|
|
141
197
|
subreddit: post("/v1/reddit/subreddit", call),
|
|
142
|
-
user:
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
maps: {
|
|
146
|
-
search: post("/v1/maps/search", call),
|
|
147
|
-
place: post("/v1/maps/place", call),
|
|
148
|
-
reviews: post("/v1/maps/reviews", call)
|
|
198
|
+
user: postOptional("/v1/reddit/user", call),
|
|
199
|
+
discussions: postOptional("/v1/reddit/discussions", call),
|
|
200
|
+
subreddits: postOptional("/v1/reddit/subreddits", call)
|
|
149
201
|
},
|
|
150
202
|
instagram: {
|
|
151
|
-
|
|
152
|
-
post:
|
|
153
|
-
|
|
203
|
+
comments: postOptional("/v1/instagram/comments", call),
|
|
204
|
+
post: postOptional("/v1/instagram/post", call),
|
|
205
|
+
profile: Object.assign(postOptional("/v1/instagram/profile", call), {
|
|
206
|
+
reels: postOptional("/v1/instagram/profile/reels", call)
|
|
207
|
+
}),
|
|
208
|
+
transcript: postOptional("/v1/instagram/transcript", call)
|
|
154
209
|
},
|
|
155
210
|
tiktok: {
|
|
156
|
-
profile:
|
|
157
|
-
video:
|
|
211
|
+
profile: postOptional("/v1/tiktok/profile", call),
|
|
212
|
+
video: postOptional("/v1/tiktok/video", call),
|
|
158
213
|
hashtag: post("/v1/tiktok/hashtag", call),
|
|
159
|
-
|
|
160
|
-
|
|
214
|
+
sound: post("/v1/tiktok/sound", call),
|
|
215
|
+
comments: postOptional("/v1/tiktok/comments", call),
|
|
216
|
+
search: post("/v1/tiktok/search", call),
|
|
217
|
+
transcript: postOptional("/v1/tiktok/transcript", call),
|
|
218
|
+
ads: {
|
|
219
|
+
search: postOptional("/v1/tiktok/ads/search", call),
|
|
220
|
+
ad: postOptional("/v1/tiktok/ads/ad", call)
|
|
221
|
+
},
|
|
222
|
+
shop: {
|
|
223
|
+
search: post("/v1/tiktok/shop/search", call),
|
|
224
|
+
products: postOptional("/v1/tiktok/shop/products", call),
|
|
225
|
+
product: postOptional("/v1/tiktok/shop/product", call),
|
|
226
|
+
reviews: postOptional("/v1/tiktok/shop/reviews", call)
|
|
227
|
+
}
|
|
161
228
|
},
|
|
162
229
|
meta: {
|
|
163
230
|
ads: {
|
|
164
|
-
page: post("/v1/meta/ads/page", call)
|
|
231
|
+
page: post("/v1/meta/ads/page", call),
|
|
232
|
+
search: post("/v1/meta/ads/search", call),
|
|
233
|
+
ad: postOptional("/v1/meta/ads/ad", call)
|
|
165
234
|
}
|
|
166
235
|
},
|
|
167
236
|
linkedin: {
|
|
168
237
|
jobs: {
|
|
169
238
|
search: postOptional("/v1/linkedin/jobs/search", call),
|
|
170
|
-
job:
|
|
171
|
-
},
|
|
172
|
-
people: {
|
|
173
|
-
search: postOptional("/v1/linkedin/people/search", call)
|
|
239
|
+
job: postOptional("/v1/linkedin/jobs/job", call)
|
|
174
240
|
},
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
241
|
+
company: postOptional("/v1/linkedin/company", call),
|
|
242
|
+
profile: postOptional("/v1/linkedin/profile", call),
|
|
243
|
+
posts: postOptional("/v1/linkedin/posts", call),
|
|
244
|
+
ads: {
|
|
245
|
+
search: postOptional("/v1/linkedin/ads/search", call),
|
|
246
|
+
ad: postOptional("/v1/linkedin/ads/ad", call)
|
|
247
|
+
}
|
|
181
248
|
},
|
|
182
249
|
zillow: {
|
|
183
250
|
search: post("/v1/zillow/search", call),
|
|
184
|
-
property:
|
|
251
|
+
property: postOptional("/v1/zillow/property", call)
|
|
252
|
+
},
|
|
253
|
+
realtor: {
|
|
254
|
+
search: post("/v1/realtor/search", call),
|
|
255
|
+
property: postOptional("/v1/realtor/property", call)
|
|
256
|
+
},
|
|
257
|
+
craigslist: {
|
|
258
|
+
search: post("/v1/craigslist/search", call)
|
|
259
|
+
},
|
|
260
|
+
luma: {
|
|
261
|
+
events: post("/v1/luma/events", call),
|
|
262
|
+
event: post("/v1/luma/event", call)
|
|
185
263
|
},
|
|
186
264
|
upwork: {
|
|
187
265
|
search: postOptional("/v1/upwork/search", call),
|
|
188
|
-
job:
|
|
266
|
+
job: postOptional("/v1/upwork/job", call)
|
|
189
267
|
},
|
|
190
268
|
indeed: {
|
|
191
269
|
search: postOptional("/v1/indeed/search", call),
|
|
192
|
-
job:
|
|
270
|
+
job: postOptional("/v1/indeed/job", call)
|
|
271
|
+
},
|
|
272
|
+
careers: {
|
|
273
|
+
jobs: post("/v1/careers/jobs", call),
|
|
274
|
+
job: post("/v1/careers/job", call)
|
|
275
|
+
},
|
|
276
|
+
glassdoor: {
|
|
277
|
+
company: Object.assign(postOptional("/v1/glassdoor/company", call), {
|
|
278
|
+
reviews: postOptional("/v1/glassdoor/company/reviews", call),
|
|
279
|
+
salaries: postOptional("/v1/glassdoor/company/salaries", call)
|
|
280
|
+
})
|
|
193
281
|
},
|
|
194
282
|
tripadvisor: {
|
|
195
283
|
search: post("/v1/tripadvisor/search", call),
|
|
196
|
-
place:
|
|
197
|
-
reviews:
|
|
284
|
+
place: postOptional("/v1/tripadvisor/place", call),
|
|
285
|
+
reviews: postOptional("/v1/tripadvisor/reviews", call)
|
|
286
|
+
},
|
|
287
|
+
yelp: {
|
|
288
|
+
search: post("/v1/yelp/search", call),
|
|
289
|
+
business: Object.assign(postOptional("/v1/yelp/business", call), {
|
|
290
|
+
reviews: postOptional("/v1/yelp/business/reviews", call)
|
|
291
|
+
})
|
|
292
|
+
},
|
|
293
|
+
booking: {
|
|
294
|
+
search: post("/v1/booking/search", call),
|
|
295
|
+
hotel: Object.assign(postOptional("/v1/booking/hotel", call), {
|
|
296
|
+
reviews: postOptional("/v1/booking/hotel/reviews", call)
|
|
297
|
+
})
|
|
198
298
|
},
|
|
199
|
-
|
|
200
|
-
|
|
299
|
+
expedia: {
|
|
300
|
+
hotels: post("/v1/expedia/hotels", call)
|
|
201
301
|
},
|
|
202
|
-
|
|
203
|
-
search:
|
|
204
|
-
product:
|
|
302
|
+
amazon: {
|
|
303
|
+
search: postOptional("/v1/amazon/search", call),
|
|
304
|
+
product: postOptional("/v1/amazon/product", call),
|
|
305
|
+
reviews: postOptional("/v1/amazon/reviews", call),
|
|
306
|
+
bestsellers: post("/v1/amazon/bestsellers", call),
|
|
307
|
+
suggest: post("/v1/amazon/suggest", call)
|
|
205
308
|
},
|
|
206
309
|
appstore: {
|
|
207
|
-
app:
|
|
310
|
+
app: postOptional("/v1/appstore/app", call),
|
|
208
311
|
search: post("/v1/appstore/search", call),
|
|
209
|
-
reviews:
|
|
312
|
+
reviews: postOptional("/v1/appstore/reviews", call),
|
|
210
313
|
top: postOptional("/v1/appstore/top", call)
|
|
211
314
|
},
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
315
|
+
pinterest: {
|
|
316
|
+
search: post("/v1/pinterest/search", call),
|
|
317
|
+
pin: postOptional("/v1/pinterest/pin", call),
|
|
318
|
+
board: post("/v1/pinterest/board", call),
|
|
319
|
+
profile: postOptional("/v1/pinterest/profile", call)
|
|
216
320
|
},
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
321
|
+
x: {
|
|
322
|
+
profile: Object.assign(postOptional("/v1/x/profile", call), {
|
|
323
|
+
posts: postOptional("/v1/x/profile/posts", call)
|
|
324
|
+
}),
|
|
325
|
+
post: postOptional("/v1/x/post", call)
|
|
222
326
|
},
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
327
|
+
threads: {
|
|
328
|
+
profile: Object.assign(postOptional("/v1/threads/profile", call), {
|
|
329
|
+
posts: postOptional("/v1/threads/profile/posts", call)
|
|
330
|
+
}),
|
|
331
|
+
post: postOptional("/v1/threads/post", call)
|
|
226
332
|
},
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
333
|
+
trustpilot: {
|
|
334
|
+
company: Object.assign(postOptional("/v1/trustpilot/company", call), {
|
|
335
|
+
reviews: postOptional("/v1/trustpilot/company/reviews", call)
|
|
336
|
+
}),
|
|
337
|
+
search: post("/v1/trustpilot/search", call)
|
|
230
338
|
},
|
|
231
|
-
|
|
232
|
-
search: post("/v1/
|
|
233
|
-
|
|
234
|
-
board: post("/v1/pinterest/board", call),
|
|
235
|
-
user: post("/v1/pinterest/user", call)
|
|
339
|
+
ebay: {
|
|
340
|
+
search: post("/v1/ebay/search", call),
|
|
341
|
+
item: postOptional("/v1/ebay/item", call)
|
|
236
342
|
},
|
|
237
|
-
|
|
238
|
-
search: post("/v1/
|
|
239
|
-
|
|
240
|
-
|
|
343
|
+
airbnb: {
|
|
344
|
+
search: post("/v1/airbnb/search", call),
|
|
345
|
+
listing: postOptional("/v1/airbnb/listing", call),
|
|
346
|
+
reviews: postOptional("/v1/airbnb/reviews", call)
|
|
241
347
|
},
|
|
242
|
-
|
|
243
|
-
|
|
348
|
+
facebook: {
|
|
349
|
+
page: Object.assign(postOptional("/v1/facebook/page", call), {
|
|
350
|
+
posts: postOptional("/v1/facebook/page/posts", call)
|
|
351
|
+
}),
|
|
352
|
+
post: Object.assign(postOptional("/v1/facebook/post", call), {
|
|
353
|
+
comments: postOptional("/v1/facebook/post/comments", call)
|
|
354
|
+
}),
|
|
355
|
+
marketplace: {
|
|
356
|
+
search: post("/v1/facebook/marketplace/search", call)
|
|
357
|
+
}
|
|
244
358
|
},
|
|
245
|
-
|
|
246
|
-
search: post("/v1/
|
|
247
|
-
|
|
359
|
+
chatgpt: {
|
|
360
|
+
search: post("/v1/chatgpt/search", call)
|
|
361
|
+
},
|
|
362
|
+
gemini: {
|
|
363
|
+
search: post("/v1/gemini/search", call)
|
|
248
364
|
}
|
|
249
365
|
};
|
|
250
366
|
}
|
|
367
|
+
var Surface = class {
|
|
368
|
+
endpoints;
|
|
369
|
+
google;
|
|
370
|
+
web;
|
|
371
|
+
youtube;
|
|
372
|
+
reddit;
|
|
373
|
+
instagram;
|
|
374
|
+
tiktok;
|
|
375
|
+
meta;
|
|
376
|
+
linkedin;
|
|
377
|
+
zillow;
|
|
378
|
+
realtor;
|
|
379
|
+
craigslist;
|
|
380
|
+
luma;
|
|
381
|
+
upwork;
|
|
382
|
+
indeed;
|
|
383
|
+
careers;
|
|
384
|
+
glassdoor;
|
|
385
|
+
tripadvisor;
|
|
386
|
+
yelp;
|
|
387
|
+
booking;
|
|
388
|
+
expedia;
|
|
389
|
+
amazon;
|
|
390
|
+
appstore;
|
|
391
|
+
pinterest;
|
|
392
|
+
x;
|
|
393
|
+
threads;
|
|
394
|
+
trustpilot;
|
|
395
|
+
ebay;
|
|
396
|
+
airbnb;
|
|
397
|
+
facebook;
|
|
398
|
+
chatgpt;
|
|
399
|
+
gemini;
|
|
400
|
+
constructor(call) {
|
|
401
|
+
const bound = bindSurface(call);
|
|
402
|
+
this.endpoints = bound.endpoints;
|
|
403
|
+
this.google = bound.google;
|
|
404
|
+
this.web = bound.web;
|
|
405
|
+
this.youtube = bound.youtube;
|
|
406
|
+
this.reddit = bound.reddit;
|
|
407
|
+
this.instagram = bound.instagram;
|
|
408
|
+
this.tiktok = bound.tiktok;
|
|
409
|
+
this.meta = bound.meta;
|
|
410
|
+
this.linkedin = bound.linkedin;
|
|
411
|
+
this.zillow = bound.zillow;
|
|
412
|
+
this.realtor = bound.realtor;
|
|
413
|
+
this.craigslist = bound.craigslist;
|
|
414
|
+
this.luma = bound.luma;
|
|
415
|
+
this.upwork = bound.upwork;
|
|
416
|
+
this.indeed = bound.indeed;
|
|
417
|
+
this.careers = bound.careers;
|
|
418
|
+
this.glassdoor = bound.glassdoor;
|
|
419
|
+
this.tripadvisor = bound.tripadvisor;
|
|
420
|
+
this.yelp = bound.yelp;
|
|
421
|
+
this.booking = bound.booking;
|
|
422
|
+
this.expedia = bound.expedia;
|
|
423
|
+
this.amazon = bound.amazon;
|
|
424
|
+
this.appstore = bound.appstore;
|
|
425
|
+
this.pinterest = bound.pinterest;
|
|
426
|
+
this.x = bound.x;
|
|
427
|
+
this.threads = bound.threads;
|
|
428
|
+
this.trustpilot = bound.trustpilot;
|
|
429
|
+
this.ebay = bound.ebay;
|
|
430
|
+
this.airbnb = bound.airbnb;
|
|
431
|
+
this.facebook = bound.facebook;
|
|
432
|
+
this.chatgpt = bound.chatgpt;
|
|
433
|
+
this.gemini = bound.gemini;
|
|
434
|
+
}
|
|
435
|
+
};
|
|
251
436
|
|
|
252
437
|
// package.json
|
|
253
|
-
var version = "1.
|
|
438
|
+
var version = "1.2.1";
|
|
254
439
|
|
|
255
440
|
// src/transport.ts
|
|
256
441
|
var DEFAULT_BASE_URL = "https://api.stophy.dev";
|
|
257
|
-
var DEFAULT_TIMEOUT_MS =
|
|
442
|
+
var DEFAULT_TIMEOUT_MS = 7e4;
|
|
258
443
|
var MAX_RETRY_WAIT_SECONDS = 60;
|
|
259
444
|
var RETRYABLE_STATUS = /* @__PURE__ */ new Set([429, 500, 502, 503, 504]);
|
|
260
445
|
var USER_AGENT = `stophy-typescript/${version}`;
|
|
@@ -268,15 +453,14 @@ function createCaller(options) {
|
|
|
268
453
|
};
|
|
269
454
|
return async (spec) => {
|
|
270
455
|
const headers = new Headers(options.headers);
|
|
271
|
-
if (
|
|
456
|
+
if (!("document" in globalThis) && !headers.has("user-agent")) {
|
|
272
457
|
headers.set("user-agent", USER_AGENT);
|
|
273
458
|
}
|
|
274
459
|
if (options.apiKey) {
|
|
275
460
|
headers.set("authorization", `Bearer ${options.apiKey}`);
|
|
276
461
|
}
|
|
277
462
|
headers.set("accept", "application/json");
|
|
278
|
-
if (spec.body !== void 0)
|
|
279
|
-
headers.set("content-type", "application/json");
|
|
463
|
+
if (spec.body !== void 0) headers.set("content-type", "application/json");
|
|
280
464
|
const url = withQuery(`${baseUrl}${spec.path}`, spec.query);
|
|
281
465
|
return send(
|
|
282
466
|
baseFetch,
|
|
@@ -297,12 +481,10 @@ async function send(baseFetch, url, init, signal, retry) {
|
|
|
297
481
|
const deadline = withDeadline(signal, retry.timeoutMs);
|
|
298
482
|
let waitMs;
|
|
299
483
|
try {
|
|
300
|
-
const response = await baseFetch(url, init(deadline.signal)).catch(
|
|
301
|
-
(
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
);
|
|
484
|
+
const response = await baseFetch(url, init(deadline.signal)).catch((error) => {
|
|
485
|
+
if (deadline.signal.aborted || n >= retry.maxRetries) throw error;
|
|
486
|
+
return void 0;
|
|
487
|
+
});
|
|
306
488
|
if (response === void 0) {
|
|
307
489
|
waitMs = backoffMs(n, retry.initialDelayMs);
|
|
308
490
|
} else if (n < retry.maxRetries && RETRYABLE_STATUS.has(response.status)) {
|
|
@@ -328,10 +510,7 @@ function withDeadline(signal, timeoutMs) {
|
|
|
328
510
|
else signal?.addEventListener("abort", abort, { once: true });
|
|
329
511
|
const timer = setTimeout(
|
|
330
512
|
() => controller.abort(
|
|
331
|
-
new DOMException(
|
|
332
|
-
`Stophy request timed out after ${timeoutMs} ms`,
|
|
333
|
-
"TimeoutError"
|
|
334
|
-
)
|
|
513
|
+
new DOMException(`Stophy request timed out after ${timeoutMs} ms`, "TimeoutError")
|
|
335
514
|
),
|
|
336
515
|
timeoutMs
|
|
337
516
|
);
|
|
@@ -357,26 +536,14 @@ async function readBody(response) {
|
|
|
357
536
|
}
|
|
358
537
|
async function errorFrom(response) {
|
|
359
538
|
const text = await response.text();
|
|
360
|
-
|
|
361
|
-
if (text) {
|
|
362
|
-
try {
|
|
363
|
-
body = JSON.parse(text);
|
|
364
|
-
} catch {
|
|
365
|
-
body = void 0;
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
const error = readError(body);
|
|
539
|
+
const error = readError(text ? parseJson(text) : null);
|
|
369
540
|
const retryAfterSeconds = error.retryAfterSeconds ?? headerRetryAfter(response);
|
|
370
|
-
return failed(
|
|
371
|
-
|
|
372
|
-
error.
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
retryAfterSeconds,
|
|
377
|
-
requestId: error.requestId ?? headerRequestId(response)
|
|
378
|
-
}
|
|
379
|
-
);
|
|
541
|
+
return failed(response, error.message ?? `Stophy request failed with status ${response.status}`, {
|
|
542
|
+
code: error.code,
|
|
543
|
+
retryable: error.retryable ?? RETRYABLE_STATUS.has(response.status),
|
|
544
|
+
retryAfterSeconds,
|
|
545
|
+
requestId: error.requestId ?? headerRequestId(response)
|
|
546
|
+
});
|
|
380
547
|
}
|
|
381
548
|
function failed(response, message, options) {
|
|
382
549
|
return new StophyError(message, {
|
|
@@ -387,20 +554,24 @@ function failed(response, message, options) {
|
|
|
387
554
|
requestId: options.requestId ?? headerRequestId(response)
|
|
388
555
|
});
|
|
389
556
|
}
|
|
557
|
+
function parseJson(text) {
|
|
558
|
+
try {
|
|
559
|
+
return JSON.parse(text);
|
|
560
|
+
} catch {
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
390
564
|
function readError(body) {
|
|
391
|
-
if (!
|
|
565
|
+
if (!isRecord(body) || !isRecord(body.error)) return {};
|
|
392
566
|
const error = body.error;
|
|
393
567
|
return {
|
|
394
|
-
code:
|
|
395
|
-
message:
|
|
396
|
-
retryable:
|
|
397
|
-
retryAfterSeconds:
|
|
398
|
-
requestId:
|
|
568
|
+
code: isString(error.code) ? error.code : void 0,
|
|
569
|
+
message: isString(error.message) ? error.message : void 0,
|
|
570
|
+
retryable: isBoolean(error.retryable) ? error.retryable : void 0,
|
|
571
|
+
retryAfterSeconds: isNumber(error.retryAfterSeconds) ? error.retryAfterSeconds : void 0,
|
|
572
|
+
requestId: isString(error.requestId) ? error.requestId : void 0
|
|
399
573
|
};
|
|
400
574
|
}
|
|
401
|
-
function isRecord2(value) {
|
|
402
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
403
|
-
}
|
|
404
575
|
function headerRequestId(response) {
|
|
405
576
|
return response.headers.get("x-request-id") ?? void 0;
|
|
406
577
|
}
|
|
@@ -432,25 +603,24 @@ function sleep(ms) {
|
|
|
432
603
|
|
|
433
604
|
// src/client.ts
|
|
434
605
|
function env(name) {
|
|
435
|
-
return
|
|
606
|
+
return globalThis.process?.env[name];
|
|
436
607
|
}
|
|
437
|
-
var Stophy = class {
|
|
608
|
+
var Stophy = class extends Surface {
|
|
438
609
|
#call;
|
|
439
610
|
constructor(input = {}) {
|
|
440
|
-
const options =
|
|
611
|
+
const options = input instanceof Object ? input : { apiKey: input };
|
|
441
612
|
const apiKey = options.apiKey?.trim() || env("STOPHY_API_KEY")?.trim();
|
|
442
|
-
|
|
613
|
+
const call = createCaller({
|
|
443
614
|
...options,
|
|
444
615
|
apiKey,
|
|
445
616
|
baseUrl: options.baseUrl ?? env("STOPHY_BASE_URL")
|
|
446
617
|
});
|
|
447
|
-
|
|
618
|
+
super(call);
|
|
619
|
+
this.#call = call;
|
|
448
620
|
}
|
|
449
|
-
/** Current balance and lifetime usage. Requires an API key. */
|
|
450
621
|
usage() {
|
|
451
622
|
return usage(this.#call);
|
|
452
623
|
}
|
|
453
|
-
/** Recent metered requests. Requires an API key. */
|
|
454
624
|
logs(query) {
|
|
455
625
|
return logs(this.#call, query);
|
|
456
626
|
}
|