stophy 1.2.0 → 1.2.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/README.md +12 -9
- package/dist/index.cjs +126 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +131 -13
- package/dist/index.d.ts +131 -13
- package/dist/index.js +126 -60
- package/dist/index.js.map +1 -1
- package/package.json +23 -22
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Stophy for TypeScript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Give your agents the live web from TypeScript. Every method and result is typed.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -17,7 +17,7 @@ const result = await new Stophy().google.search({ query: "bun runtime" });
|
|
|
17
17
|
console.log(result.data.results);
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
Some searches and lookups work without a key, within a small free allowance. The [endpoint list](https://api.stophy.dev/v1/endpoints) marks them with `keyless: true`. Every other method throws a `StophyError` with the code `
|
|
20
|
+
Some searches and lookups work without a key, within a small free allowance. The [endpoint list](https://api.stophy.dev/v1/endpoints) marks them with `keyless: true`. Every other method throws a `StophyError` with the code `apiKeyRequired`.
|
|
21
21
|
|
|
22
22
|
## Use an API key
|
|
23
23
|
|
|
@@ -45,7 +45,10 @@ A YouTube transcript, Reddit posts, Google Maps reviews, a TikTok profile and Me
|
|
|
45
45
|
```ts
|
|
46
46
|
const stophy = new Stophy({ apiKey: "st_..." });
|
|
47
47
|
|
|
48
|
-
const transcript = await stophy.youtube.transcript({
|
|
48
|
+
const transcript = await stophy.youtube.transcript({
|
|
49
|
+
videoUrl: "https://youtu.be/dQw4w9WgXcQ",
|
|
50
|
+
includeTimestamps: true,
|
|
51
|
+
});
|
|
49
52
|
console.log(transcript.data.text, transcript.data.segments);
|
|
50
53
|
|
|
51
54
|
const posts = await stophy.reddit.search({ query: "bun runtime", sort: "top", within: "month" });
|
|
@@ -105,12 +108,12 @@ The SDK retries network errors and the HTTP statuses 429, 500, 502, 503, and 504
|
|
|
105
108
|
|
|
106
109
|
## Options
|
|
107
110
|
|
|
108
|
-
| Option
|
|
109
|
-
|
|
|
110
|
-
| `apiKey`
|
|
111
|
-
| `baseUrl`
|
|
112
|
-
| `maxRetries` | `2`
|
|
113
|
-
| `timeoutMs`
|
|
111
|
+
| Option | Default | What it does |
|
|
112
|
+
| ------------ | ------------------------------------ | ---------------------------------------------------------------- |
|
|
113
|
+
| `apiKey` | `STOPHY_API_KEY` | Your API key |
|
|
114
|
+
| `baseUrl` | `STOPHY_BASE_URL`, or the Stophy API | The server to call |
|
|
115
|
+
| `maxRetries` | `2` | Retries per request. `0` turns retries off. |
|
|
116
|
+
| `timeoutMs` | `70000` | Time limit for each attempt. A timed-out attempt is not retried. |
|
|
114
117
|
|
|
115
118
|
Each method also takes `signal` to cancel the request.
|
|
116
119
|
|
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) {
|
|
@@ -356,9 +368,78 @@ function bindSurface(call) {
|
|
|
356
368
|
}
|
|
357
369
|
};
|
|
358
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
|
+
};
|
|
359
440
|
|
|
360
441
|
// package.json
|
|
361
|
-
var version = "1.2.
|
|
442
|
+
var version = "1.2.2";
|
|
362
443
|
|
|
363
444
|
// src/transport.ts
|
|
364
445
|
var DEFAULT_BASE_URL = "https://api.stophy.dev";
|
|
@@ -376,15 +457,14 @@ function createCaller(options) {
|
|
|
376
457
|
};
|
|
377
458
|
return async (spec) => {
|
|
378
459
|
const headers = new Headers(options.headers);
|
|
379
|
-
if (
|
|
460
|
+
if (!("document" in globalThis) && !headers.has("user-agent")) {
|
|
380
461
|
headers.set("user-agent", USER_AGENT);
|
|
381
462
|
}
|
|
382
463
|
if (options.apiKey) {
|
|
383
464
|
headers.set("authorization", `Bearer ${options.apiKey}`);
|
|
384
465
|
}
|
|
385
466
|
headers.set("accept", "application/json");
|
|
386
|
-
if (spec.body !== void 0)
|
|
387
|
-
headers.set("content-type", "application/json");
|
|
467
|
+
if (spec.body !== void 0) headers.set("content-type", "application/json");
|
|
388
468
|
const url = withQuery(`${baseUrl}${spec.path}`, spec.query);
|
|
389
469
|
return send(
|
|
390
470
|
baseFetch,
|
|
@@ -405,12 +485,10 @@ async function send(baseFetch, url, init, signal, retry) {
|
|
|
405
485
|
const deadline = withDeadline(signal, retry.timeoutMs);
|
|
406
486
|
let waitMs;
|
|
407
487
|
try {
|
|
408
|
-
const response = await baseFetch(url, init(deadline.signal)).catch(
|
|
409
|
-
(
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
413
|
-
);
|
|
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
|
+
});
|
|
414
492
|
if (response === void 0) {
|
|
415
493
|
waitMs = backoffMs(n, retry.initialDelayMs);
|
|
416
494
|
} else if (n < retry.maxRetries && RETRYABLE_STATUS.has(response.status)) {
|
|
@@ -436,10 +514,7 @@ function withDeadline(signal, timeoutMs) {
|
|
|
436
514
|
else signal?.addEventListener("abort", abort, { once: true });
|
|
437
515
|
const timer = setTimeout(
|
|
438
516
|
() => controller.abort(
|
|
439
|
-
new DOMException(
|
|
440
|
-
`Stophy request timed out after ${timeoutMs} ms`,
|
|
441
|
-
"TimeoutError"
|
|
442
|
-
)
|
|
517
|
+
new DOMException(`Stophy request timed out after ${timeoutMs} ms`, "TimeoutError")
|
|
443
518
|
),
|
|
444
519
|
timeoutMs
|
|
445
520
|
);
|
|
@@ -465,26 +540,14 @@ async function readBody(response) {
|
|
|
465
540
|
}
|
|
466
541
|
async function errorFrom(response) {
|
|
467
542
|
const text = await response.text();
|
|
468
|
-
|
|
469
|
-
if (text) {
|
|
470
|
-
try {
|
|
471
|
-
body = JSON.parse(text);
|
|
472
|
-
} catch {
|
|
473
|
-
body = void 0;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
const error = readError(body);
|
|
543
|
+
const error = readError(text ? parseJson(text) : null);
|
|
477
544
|
const retryAfterSeconds = error.retryAfterSeconds ?? headerRetryAfter(response);
|
|
478
|
-
return failed(
|
|
479
|
-
|
|
480
|
-
error.
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
retryAfterSeconds,
|
|
485
|
-
requestId: error.requestId ?? headerRequestId(response)
|
|
486
|
-
}
|
|
487
|
-
);
|
|
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
|
+
});
|
|
488
551
|
}
|
|
489
552
|
function failed(response, message, options) {
|
|
490
553
|
return new StophyError(message, {
|
|
@@ -495,20 +558,24 @@ function failed(response, message, options) {
|
|
|
495
558
|
requestId: options.requestId ?? headerRequestId(response)
|
|
496
559
|
});
|
|
497
560
|
}
|
|
561
|
+
function parseJson(text) {
|
|
562
|
+
try {
|
|
563
|
+
return JSON.parse(text);
|
|
564
|
+
} catch {
|
|
565
|
+
return null;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
498
568
|
function readError(body) {
|
|
499
|
-
if (!
|
|
569
|
+
if (!isRecord(body) || !isRecord(body.error)) return {};
|
|
500
570
|
const error = body.error;
|
|
501
571
|
return {
|
|
502
|
-
code:
|
|
503
|
-
message:
|
|
504
|
-
retryable:
|
|
505
|
-
retryAfterSeconds:
|
|
506
|
-
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
|
|
507
577
|
};
|
|
508
578
|
}
|
|
509
|
-
function isRecord2(value) {
|
|
510
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
511
|
-
}
|
|
512
579
|
function headerRequestId(response) {
|
|
513
580
|
return response.headers.get("x-request-id") ?? void 0;
|
|
514
581
|
}
|
|
@@ -540,25 +607,24 @@ function sleep(ms) {
|
|
|
540
607
|
|
|
541
608
|
// src/client.ts
|
|
542
609
|
function env(name) {
|
|
543
|
-
return
|
|
610
|
+
return globalThis.process?.env[name];
|
|
544
611
|
}
|
|
545
|
-
var Stophy = class {
|
|
612
|
+
var Stophy = class extends Surface {
|
|
546
613
|
#call;
|
|
547
614
|
constructor(input = {}) {
|
|
548
|
-
const options =
|
|
615
|
+
const options = input instanceof Object ? input : { apiKey: input };
|
|
549
616
|
const apiKey = options.apiKey?.trim() || env("STOPHY_API_KEY")?.trim();
|
|
550
|
-
|
|
617
|
+
const call = createCaller({
|
|
551
618
|
...options,
|
|
552
619
|
apiKey,
|
|
553
620
|
baseUrl: options.baseUrl ?? env("STOPHY_BASE_URL")
|
|
554
621
|
});
|
|
555
|
-
|
|
622
|
+
super(call);
|
|
623
|
+
this.#call = call;
|
|
556
624
|
}
|
|
557
|
-
/** Current balance and lifetime usage. Requires an API key. */
|
|
558
625
|
usage() {
|
|
559
626
|
return usage(this.#call);
|
|
560
627
|
}
|
|
561
|
-
/** Recent metered requests. Requires an API key. */
|
|
562
628
|
logs(query) {
|
|
563
629
|
return logs(this.#call, query);
|
|
564
630
|
}
|