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