stophy 1.2.2 → 1.3.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/dist/index.js CHANGED
@@ -1,3 +1,108 @@
1
+ // src/bind.ts
2
+ function post(path, call) {
3
+ return (body, options) => call({ method: "POST", path, body, options });
4
+ }
5
+ function postOptional(path, call) {
6
+ return (body, options) => call({ method: "POST", path, body: body ?? {}, options });
7
+ }
8
+ function send(method, path, call) {
9
+ return (input, options) => call({ method, ...fillPath(path, input), options });
10
+ }
11
+ function sendOptional(method, path, call) {
12
+ return (input, options) => call({ method, ...fillPath(path, input ?? {}), options });
13
+ }
14
+ function sendEmpty(method, path, call) {
15
+ return (options) => call({ method, path, options });
16
+ }
17
+ function fillPath(template, input) {
18
+ const used = /* @__PURE__ */ new Set();
19
+ const path = template.replaceAll(/\{(\w+)\}/g, (_, name) => {
20
+ const value = input[name];
21
+ if (value === void 0) throw new TypeError(`Missing ${name} for ${template}`);
22
+ used.add(name);
23
+ return encodeURIComponent(String(value));
24
+ });
25
+ const query = Object.fromEntries(Object.entries(input).filter(([key]) => !used.has(key)));
26
+ return { path, query };
27
+ }
28
+
29
+ // src/generated/surface.gen.ts
30
+ function bindSurface(call) {
31
+ return {
32
+ endpoints: sendEmpty("GET", "/v1/endpoints", call),
33
+ parsers: sendEmpty("GET", "/v1/parsers", call),
34
+ extract: Object.assign(post("/v1/extract", call), {
35
+ result: send("GET", "/v1/extract/{id}", call)
36
+ }),
37
+ signup: Object.assign(postOptional("/v1/signup", call), {
38
+ poll: post("/v1/signup/poll", call)
39
+ }),
40
+ limits: sendEmpty("GET", "/v1/limits", call),
41
+ usage: sendEmpty("GET", "/v1/usage", call),
42
+ logs: sendOptional("GET", "/v1/logs", call),
43
+ key: {
44
+ revoke: sendEmpty("DELETE", "/v1/key", call)
45
+ },
46
+ search: post("/v1/search", call),
47
+ scrape: post("/v1/scrape", call),
48
+ browser: {
49
+ task: Object.assign(post("/v1/browser/task", call), {
50
+ status: post("/v1/browser/task/status", call)
51
+ })
52
+ },
53
+ parse: post("/v1/parse", call),
54
+ map: post("/v1/map", call),
55
+ crawl: Object.assign(post("/v1/crawl", call), {
56
+ status: send("GET", "/v1/crawl/{id}", call),
57
+ cancel: send("DELETE", "/v1/crawl/{id}", call)
58
+ }),
59
+ batch: {
60
+ scrape: Object.assign(post("/v1/batch/scrape", call), {
61
+ status: send("GET", "/v1/batch/scrape/{id}", call),
62
+ cancel: send("DELETE", "/v1/batch/scrape/{id}", call)
63
+ })
64
+ }
65
+ };
66
+ }
67
+ var Surface = class {
68
+ endpoints;
69
+ parsers;
70
+ extract;
71
+ signup;
72
+ limits;
73
+ usage;
74
+ logs;
75
+ key;
76
+ search;
77
+ scrape;
78
+ browser;
79
+ parse;
80
+ map;
81
+ crawl;
82
+ batch;
83
+ constructor(call) {
84
+ const bound = bindSurface(call);
85
+ this.endpoints = bound.endpoints;
86
+ this.parsers = bound.parsers;
87
+ this.extract = bound.extract;
88
+ this.signup = bound.signup;
89
+ this.limits = bound.limits;
90
+ this.usage = bound.usage;
91
+ this.logs = bound.logs;
92
+ this.key = bound.key;
93
+ this.search = bound.search;
94
+ this.scrape = bound.scrape;
95
+ this.browser = bound.browser;
96
+ this.parse = bound.parse;
97
+ this.map = bound.map;
98
+ this.crawl = bound.crawl;
99
+ this.batch = bound.batch;
100
+ }
101
+ };
102
+
103
+ // package.json
104
+ var version = "1.3.0";
105
+
1
106
  // src/errors.ts
2
107
  var StophyError = class extends Error {
3
108
  code;
@@ -26,417 +131,10 @@ function isString(value) {
26
131
  function isNumber(value) {
27
132
  return tagOf(value) === "[object Number]";
28
133
  }
29
- function isBoolean(value) {
30
- return value === true || value === false;
31
- }
32
134
  function isRecord(value) {
33
135
  return tagOf(value) === "[object Object]";
34
136
  }
35
137
 
36
- // src/account.ts
37
- function usage(call) {
38
- return call({ method: "GET", path: "/v1/usage" }).then(parseUsage);
39
- }
40
- function logs(call, query) {
41
- const params = {};
42
- if (query?.apiKeyId !== void 0) params.apiKeyId = query.apiKeyId;
43
- if (query?.days !== void 0) params.days = query.days;
44
- if (query?.endpoint !== void 0) params.endpoint = query.endpoint;
45
- if (query?.page !== void 0) params.page = query.page;
46
- return call({ method: "GET", path: "/v1/logs", query: params }).then(parseLogs);
47
- }
48
- function parseUsage(value) {
49
- if (!isRecord(value)) return unexpected("usage");
50
- const balanceMicros = numberField(value, "balanceMicros");
51
- const creditsUsed = numberField(value, "creditsUsed");
52
- const requestCount = numberField(value, "requestCount");
53
- return { balanceMicros, creditsUsed, requestCount };
54
- }
55
- function parseLogs(value) {
56
- if (!isRecord(value) || !Array.isArray(value.logs) || !Array.isArray(value.endpoints)) {
57
- return unexpected("logs");
58
- }
59
- return {
60
- endpoints: value.endpoints.map((item) => {
61
- if (!isString(item)) return unexpected("logs.endpoints");
62
- return item;
63
- }),
64
- logs: value.logs.map(parseLog),
65
- page: numberField(value, "page"),
66
- pageSize: numberField(value, "pageSize"),
67
- total: numberField(value, "total"),
68
- totalPages: numberField(value, "totalPages")
69
- };
70
- }
71
- function parseLog(value) {
72
- if (!isRecord(value)) return unexpected("logs.logs");
73
- return {
74
- apiKeyId: stringOrNull(value, "apiKeyId"),
75
- apiKeyName: stringOrNull(value, "apiKeyName"),
76
- createdAt: stringField(value, "createdAt"),
77
- credits: numberField(value, "credits"),
78
- durationMs: numberOrNull(value, "durationMs"),
79
- endpoint: stringField(value, "endpoint"),
80
- id: stringField(value, "id"),
81
- method: stringField(value, "method"),
82
- response: stringField(value, "response"),
83
- status: numberField(value, "status")
84
- };
85
- }
86
- function numberField(record, field) {
87
- const value = record[field];
88
- if (!isNumber(value)) return unexpected(field);
89
- return value;
90
- }
91
- function numberOrNull(record, field) {
92
- const value = record[field];
93
- if (value === null) return null;
94
- if (!isNumber(value)) return unexpected(field);
95
- return value;
96
- }
97
- function stringField(record, field) {
98
- const value = record[field];
99
- if (!isString(value)) return unexpected(field);
100
- return value;
101
- }
102
- function stringOrNull(record, field) {
103
- const value = record[field];
104
- if (value === null) return null;
105
- if (!isString(value)) return unexpected(field);
106
- return value;
107
- }
108
- function unexpected(field) {
109
- throw new StophyError(`Stophy returned an unexpected ${field}`, {
110
- status: 200,
111
- retryable: false
112
- });
113
- }
114
-
115
- // src/bind.ts
116
- function post(path, call) {
117
- return (body, options) => call({ method: "POST", path, body, options });
118
- }
119
- function postOptional(path, call) {
120
- return (body, options) => call({
121
- method: "POST",
122
- path,
123
- body: body ?? {},
124
- options
125
- });
126
- }
127
- function get(path, call) {
128
- return (options) => call({ method: "GET", path, options });
129
- }
130
-
131
- // src/generated/surface.gen.ts
132
- function bindSurface(call) {
133
- return {
134
- endpoints: get("/v1/endpoints", call),
135
- google: {
136
- search: post("/v1/google/search", call),
137
- news: postOptional("/v1/google/news", call),
138
- images: post("/v1/google/images", call),
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
- })
183
- },
184
- youtube: {
185
- suggest: post("/v1/youtube/suggest", call),
186
- search: post("/v1/youtube/search", call),
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)
193
- },
194
- reddit: {
195
- search: post("/v1/reddit/search", call),
196
- post: postOptional("/v1/reddit/post", call),
197
- subreddit: post("/v1/reddit/subreddit", call),
198
- user: postOptional("/v1/reddit/user", call),
199
- discussions: postOptional("/v1/reddit/discussions", call),
200
- subreddits: postOptional("/v1/reddit/subreddits", call)
201
- },
202
- instagram: {
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)
209
- },
210
- tiktok: {
211
- profile: postOptional("/v1/tiktok/profile", call),
212
- video: postOptional("/v1/tiktok/video", call),
213
- hashtag: post("/v1/tiktok/hashtag", call),
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
- }
228
- },
229
- meta: {
230
- ads: {
231
- page: post("/v1/meta/ads/page", call),
232
- search: post("/v1/meta/ads/search", call),
233
- ad: postOptional("/v1/meta/ads/ad", call)
234
- }
235
- },
236
- linkedin: {
237
- jobs: {
238
- search: postOptional("/v1/linkedin/jobs/search", call),
239
- job: postOptional("/v1/linkedin/jobs/job", call)
240
- },
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
- }
248
- },
249
- zillow: {
250
- search: post("/v1/zillow/search", call),
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)
263
- },
264
- upwork: {
265
- search: postOptional("/v1/upwork/search", call),
266
- job: postOptional("/v1/upwork/job", call)
267
- },
268
- indeed: {
269
- search: postOptional("/v1/indeed/search", call),
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
- })
281
- },
282
- tripadvisor: {
283
- search: post("/v1/tripadvisor/search", call),
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
- })
298
- },
299
- expedia: {
300
- hotels: post("/v1/expedia/hotels", call)
301
- },
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)
308
- },
309
- appstore: {
310
- app: postOptional("/v1/appstore/app", call),
311
- search: post("/v1/appstore/search", call),
312
- reviews: postOptional("/v1/appstore/reviews", call),
313
- top: postOptional("/v1/appstore/top", call)
314
- },
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)
320
- },
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)
326
- },
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)
332
- },
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)
338
- },
339
- ebay: {
340
- search: post("/v1/ebay/search", call),
341
- item: postOptional("/v1/ebay/item", call)
342
- },
343
- airbnb: {
344
- search: post("/v1/airbnb/search", call),
345
- listing: postOptional("/v1/airbnb/listing", call),
346
- reviews: postOptional("/v1/airbnb/reviews", call)
347
- },
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
- }
358
- },
359
- chatgpt: {
360
- search: post("/v1/chatgpt/search", call)
361
- },
362
- gemini: {
363
- search: post("/v1/gemini/search", call)
364
- }
365
- };
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
- };
436
-
437
- // package.json
438
- var version = "1.2.2";
439
-
440
138
  // src/transport.ts
441
139
  var DEFAULT_BASE_URL = "https://api.stophy.dev";
442
140
  var DEFAULT_TIMEOUT_MS = 7e4;
@@ -462,7 +160,7 @@ function createCaller(options) {
462
160
  headers.set("accept", "application/json");
463
161
  if (spec.body !== void 0) headers.set("content-type", "application/json");
464
162
  const url = withQuery(`${baseUrl}${spec.path}`, spec.query);
465
- return send(
163
+ return send2(
466
164
  baseFetch,
467
165
  url,
468
166
  (signal) => ({
@@ -472,11 +170,11 @@ function createCaller(options) {
472
170
  signal
473
171
  }),
474
172
  spec.options?.signal,
475
- retry
173
+ spec.method === "DELETE" ? { ...retry, maxRetries: 0 } : retry
476
174
  );
477
175
  };
478
176
  }
479
- async function send(baseFetch, url, init, signal, retry) {
177
+ async function send2(baseFetch, url, init, signal, retry) {
480
178
  for (let n = 0; ; n += 1) {
481
179
  const deadline = withDeadline(signal, retry.timeoutMs);
482
180
  let waitMs;
@@ -540,9 +238,8 @@ async function errorFrom(response) {
540
238
  const retryAfterSeconds = error.retryAfterSeconds ?? headerRetryAfter(response);
541
239
  return failed(response, error.message ?? `Stophy request failed with status ${response.status}`, {
542
240
  code: error.code,
543
- retryable: error.retryable ?? RETRYABLE_STATUS.has(response.status),
544
- retryAfterSeconds,
545
- requestId: error.requestId ?? headerRequestId(response)
241
+ retryable: RETRYABLE_STATUS.has(response.status),
242
+ retryAfterSeconds
546
243
  });
547
244
  }
548
245
  function failed(response, message, options) {
@@ -562,14 +259,11 @@ function parseJson(text) {
562
259
  }
563
260
  }
564
261
  function readError(body) {
565
- if (!isRecord(body) || !isRecord(body.error)) return {};
566
- const error = body.error;
262
+ if (!isRecord(body)) return {};
567
263
  return {
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
264
+ code: isString(body.code) ? body.code : void 0,
265
+ message: isString(body.error) ? body.error : void 0,
266
+ retryAfterSeconds: isNumber(body.retryAfterSeconds) ? body.retryAfterSeconds : void 0
573
267
  };
574
268
  }
575
269
  function headerRequestId(response) {
@@ -606,7 +300,6 @@ function env(name) {
606
300
  return globalThis.process?.env[name];
607
301
  }
608
302
  var Stophy = class extends Surface {
609
- #call;
610
303
  constructor(input = {}) {
611
304
  const options = input instanceof Object ? input : { apiKey: input };
612
305
  const apiKey = options.apiKey?.trim() || env("STOPHY_API_KEY")?.trim();
@@ -616,13 +309,9 @@ var Stophy = class extends Surface {
616
309
  baseUrl: options.baseUrl ?? env("STOPHY_BASE_URL")
617
310
  });
618
311
  super(call);
619
- this.#call = call;
620
- }
621
- usage() {
622
- return usage(this.#call);
623
312
  }
624
- logs(query) {
625
- return logs(this.#call, query);
313
+ parsed(parser, url, options, request) {
314
+ return this.scrape({ url, formats: [{ type: "json", parser, options }] }, request);
626
315
  }
627
316
  };
628
317