@hyperbrowser/sdk 0.2.0 → 0.4.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/README.md +48 -0
- package/dist/client.d.ts +6 -2
- package/dist/client.js +31 -2
- package/dist/types/constants.d.ts +5 -0
- package/dist/types/constants.js +2 -0
- package/dist/types/scrape.d.ts +28 -0
- package/dist/types/scrape.js +2 -0
- package/dist/types/session.d.ts +18 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Hyperbrowser SDK
|
|
2
|
+
|
|
3
|
+
A TypeScript/JavaScript SDK for interacting with the Hyperbrowser API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Hyperbrowser can be installed via npm by running:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @hyperbrowser/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { connect } from "puppeteer-core";
|
|
17
|
+
import Hyperbrowser from "hyperbrowser";
|
|
18
|
+
import dotenv from "dotenv";
|
|
19
|
+
|
|
20
|
+
dotenv.config();
|
|
21
|
+
|
|
22
|
+
const client = new Hyperbrowser({
|
|
23
|
+
apiKey: process.env.HYPERBROWSER_API_KEY,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
(async () => {
|
|
27
|
+
const session = await client.createSession();
|
|
28
|
+
|
|
29
|
+
const browser = await connect({
|
|
30
|
+
browserWSEndpoint: session.wsEndpoint,
|
|
31
|
+
defaultViewport: null,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Create a new page
|
|
35
|
+
const [page] = await browser.pages();
|
|
36
|
+
|
|
37
|
+
// Navigate to a website
|
|
38
|
+
console.log("Navigating to Hacker News...");
|
|
39
|
+
await page.goto("https://news.ycombinator.com/");
|
|
40
|
+
const pageTitle = await page.title();
|
|
41
|
+
console.log("Page title:", pageTitle);
|
|
42
|
+
|
|
43
|
+
await page.close();
|
|
44
|
+
await browser.close();
|
|
45
|
+
console.log("Session completed!");
|
|
46
|
+
await client.stopSession(session.id);
|
|
47
|
+
})().catch((error) => console.error(error.message));
|
|
48
|
+
```
|
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Response } from "node-fetch";
|
|
2
2
|
import { HyperbrowserConfig } from "./types/config";
|
|
3
|
-
import { BasicResponse, SessionDetail, SessionListParams, SessionListResponse } from "./types/session";
|
|
3
|
+
import { BasicResponse, CreateSessionParams, SessionDetail, SessionListParams, SessionListResponse } from "./types/session";
|
|
4
|
+
import { ScrapeJobResponse, StartScrapeJobParams, StartScrapeJobResponse } from "./types/scrape";
|
|
4
5
|
export declare class HyperbrowserError extends Error {
|
|
5
6
|
statusCode?: number | undefined;
|
|
6
7
|
response?: Response | undefined;
|
|
@@ -14,8 +15,9 @@ export declare class HyperbrowserClient {
|
|
|
14
15
|
private request;
|
|
15
16
|
/**
|
|
16
17
|
* Create a new browser session
|
|
18
|
+
* @param params Configuration parameters for the new session
|
|
17
19
|
*/
|
|
18
|
-
createSession(): Promise<SessionDetail>;
|
|
20
|
+
createSession(params?: CreateSessionParams): Promise<SessionDetail>;
|
|
19
21
|
/**
|
|
20
22
|
* Get details of an existing session
|
|
21
23
|
*/
|
|
@@ -28,4 +30,6 @@ export declare class HyperbrowserClient {
|
|
|
28
30
|
* List all sessions with optional filtering
|
|
29
31
|
*/
|
|
30
32
|
listSessions(params?: SessionListParams): Promise<SessionListResponse>;
|
|
33
|
+
startScrapeJob(params: StartScrapeJobParams): Promise<StartScrapeJobResponse>;
|
|
34
|
+
getScrapeJob(id: string): Promise<ScrapeJobResponse>;
|
|
31
35
|
}
|
package/dist/client.js
CHANGED
|
@@ -74,10 +74,14 @@ class HyperbrowserClient {
|
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
76
|
* Create a new browser session
|
|
77
|
+
* @param params Configuration parameters for the new session
|
|
77
78
|
*/
|
|
78
|
-
async createSession() {
|
|
79
|
+
async createSession(params) {
|
|
79
80
|
try {
|
|
80
|
-
return await this.request("/session", {
|
|
81
|
+
return await this.request("/session", {
|
|
82
|
+
method: "POST",
|
|
83
|
+
body: params ? JSON.stringify(params) : undefined,
|
|
84
|
+
});
|
|
81
85
|
}
|
|
82
86
|
catch (error) {
|
|
83
87
|
if (error instanceof HyperbrowserError) {
|
|
@@ -133,5 +137,30 @@ class HyperbrowserClient {
|
|
|
133
137
|
throw new HyperbrowserError("Failed to list sessions", undefined, undefined, error instanceof Error ? error : undefined);
|
|
134
138
|
}
|
|
135
139
|
}
|
|
140
|
+
async startScrapeJob(params) {
|
|
141
|
+
try {
|
|
142
|
+
return await this.request("/scrape", {
|
|
143
|
+
method: "POST",
|
|
144
|
+
body: JSON.stringify(params),
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
if (error instanceof HyperbrowserError) {
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
throw new HyperbrowserError("Failed to start scrape job", undefined, undefined, error instanceof Error ? error : undefined);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async getScrapeJob(id) {
|
|
155
|
+
try {
|
|
156
|
+
return await this.request(`/scrape/${id}`);
|
|
157
|
+
}
|
|
158
|
+
catch (error) {
|
|
159
|
+
if (error instanceof HyperbrowserError) {
|
|
160
|
+
throw error;
|
|
161
|
+
}
|
|
162
|
+
throw new HyperbrowserError(`Failed to get scrape job ${id}`, undefined, undefined, error instanceof Error ? error : undefined);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
136
165
|
}
|
|
137
166
|
exports.HyperbrowserClient = HyperbrowserClient;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type ScrapeJobStatus = "pending" | "running" | "completed" | "failed";
|
|
2
|
+
export type Country = "AD" | "AE" | "AF" | "AL" | "AM" | "AO" | "AR" | "AT" | "AU" | "AW" | "AZ" | "BA" | "BD" | "BE" | "BG" | "BH" | "BJ" | "BO" | "BR" | "BS" | "BT" | "BY" | "BZ" | "CA" | "CF" | "CH" | "CI" | "CL" | "CM" | "CN" | "CO" | "CR" | "CU" | "CY" | "CZ" | "DE" | "DJ" | "DK" | "DM" | "EC" | "EE" | "EG" | "ES" | "ET" | "EU" | "FI" | "FJ" | "FR" | "GB" | "GE" | "GH" | "GM" | "GR" | "HK" | "HN" | "HR" | "HT" | "HU" | "ID" | "IE" | "IL" | "IN" | "IQ" | "IR" | "IS" | "IT" | "JM" | "JO" | "JP" | "KE" | "KH" | "KR" | "KW" | "KZ" | "LB" | "LI" | "LR" | "LT" | "LU" | "LV" | "MA" | "MC" | "MD" | "ME" | "MG" | "MK" | "ML" | "MM" | "MN" | "MR" | "MT" | "MU" | "MV" | "MX" | "MY" | "MZ" | "NG" | "NL" | "NO" | "NZ" | "OM" | "PA" | "PE" | "PH" | "PK" | "PL" | "PR" | "PT" | "PY" | "QA" | "RANDOM_COUNTRY" | "RO" | "RS" | "RU" | "SA" | "SC" | "SD" | "SE" | "SG" | "SI" | "SK" | "SN" | "SS" | "TD" | "TG" | "TH" | "TM" | "TN" | "TR" | "TT" | "TW" | "UA" | "UG" | "US" | "UY" | "UZ" | "VE" | "VG" | "VN" | "YE" | "ZA" | "ZM" | "ZW" | "ad" | "ae" | "af" | "al" | "am" | "ao" | "ar" | "at" | "au" | "aw" | "az" | "ba" | "bd" | "be" | "bg" | "bh" | "bj" | "bo" | "br" | "bs" | "bt" | "by" | "bz" | "ca" | "cf" | "ch" | "ci" | "cl" | "cm" | "cn" | "co" | "cr" | "cu" | "cy" | "cz" | "de" | "dj" | "dk" | "dm" | "ec" | "ee" | "eg" | "es" | "et" | "eu" | "fi" | "fj" | "fr" | "gb" | "ge" | "gh" | "gm" | "gr" | "hk" | "hn" | "hr" | "ht" | "hu" | "id" | "ie" | "il" | "in" | "iq" | "ir" | "is" | "it" | "jm" | "jo" | "jp" | "ke" | "kh" | "kr" | "kw" | "kz" | "lb" | "li" | "lr" | "lt" | "lu" | "lv" | "ma" | "mc" | "md" | "me" | "mg" | "mk" | "ml" | "mm" | "mn" | "mr" | "mt" | "mu" | "mv" | "mx" | "my" | "mz" | "ng" | "nl" | "no" | "nz" | "om" | "pa" | "pe" | "ph" | "pk" | "pl" | "pr" | "pt" | "py" | "qa" | "ro" | "rs" | "ru" | "sa" | "sc" | "sd" | "se" | "sg" | "si" | "sk" | "sn" | "ss" | "td" | "tg" | "th" | "tm" | "tn" | "tr" | "tt" | "tw" | "ua" | "ug" | "us" | "uy" | "uz" | "ve" | "vg" | "vn" | "ye" | "za" | "zm" | "zw";
|
|
3
|
+
export type OperatingSystem = "windows" | "android" | "macos" | "linux" | "ios";
|
|
4
|
+
export type Platform = "chrome" | "firefox" | "safari" | "edge";
|
|
5
|
+
export type ISO639_1 = "aa" | "ab" | "ae" | "af" | "ak" | "am" | "an" | "ar" | "as" | "av" | "ay" | "az" | "ba" | "be" | "bg" | "bh" | "bi" | "bm" | "bn" | "bo" | "br" | "bs" | "ca" | "ce" | "ch" | "co" | "cr" | "cs" | "cu" | "cv" | "cy" | "da" | "de" | "dv" | "dz" | "ee" | "el" | "en" | "eo" | "es" | "et" | "eu" | "fa" | "ff" | "fi" | "fj" | "fo" | "fr" | "fy" | "ga" | "gd" | "gl" | "gn" | "gu" | "gv" | "ha" | "he" | "hi" | "ho" | "hr" | "ht" | "hu" | "hy" | "hz" | "ia" | "id" | "ie" | "ig" | "ii" | "ik" | "io" | "is" | "it" | "iu" | "ja" | "jv" | "ka" | "kg" | "ki" | "kj" | "kk" | "kl" | "km" | "kn" | "ko" | "kr" | "ks" | "ku" | "kv" | "kw" | "ky" | "la" | "lb" | "lg" | "li" | "ln" | "lo" | "lt" | "lu" | "lv" | "mg" | "mh" | "mi" | "mk" | "ml" | "mn" | "mo" | "mr" | "ms" | "mt" | "my" | "na" | "nb" | "nd" | "ne" | "ng" | "nl" | "nn" | "no" | "nr" | "nv" | "ny" | "oc" | "oj" | "om" | "or" | "os" | "pa" | "pi" | "pl" | "ps" | "pt" | "qu" | "rm" | "rn" | "ro" | "ru" | "rw" | "sa" | "sc" | "sd" | "se" | "sg" | "si" | "sk" | "sl" | "sm" | "sn" | "so" | "sq" | "sr" | "ss" | "st" | "su" | "sv" | "sw" | "ta" | "te" | "tg" | "th" | "ti" | "tk" | "tl" | "tn" | "to" | "tr" | "ts" | "tt" | "tw" | "ty" | "ug" | "uk" | "ur" | "uz" | "ve" | "vi" | "vo" | "wa" | "wo" | "xh" | "yi" | "yo" | "za" | "zh" | "zu";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ScrapeJobStatus } from "./constants";
|
|
2
|
+
export interface StartScrapeJobParams {
|
|
3
|
+
url: string;
|
|
4
|
+
}
|
|
5
|
+
export interface StartScrapeJobResponse {
|
|
6
|
+
jobId: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ScrapeJobMetadata {
|
|
9
|
+
title: string;
|
|
10
|
+
description: string;
|
|
11
|
+
robots: string;
|
|
12
|
+
ogTitle: string;
|
|
13
|
+
ogDescription: string;
|
|
14
|
+
ogUrl: string;
|
|
15
|
+
ogImage: string;
|
|
16
|
+
ogLocaleAlternate: string[];
|
|
17
|
+
ogSiteName: string;
|
|
18
|
+
sourceURL: string;
|
|
19
|
+
}
|
|
20
|
+
export interface ScrapeJobData {
|
|
21
|
+
metadata: ScrapeJobMetadata;
|
|
22
|
+
markdown: string;
|
|
23
|
+
}
|
|
24
|
+
export interface ScrapeJobResponse {
|
|
25
|
+
status: ScrapeJobStatus;
|
|
26
|
+
data?: ScrapeJobData;
|
|
27
|
+
error?: string;
|
|
28
|
+
}
|
package/dist/types/session.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Country, ISO639_1, OperatingSystem, Platform } from "./constants";
|
|
1
2
|
export type SessionStatus = "active" | "closed" | "error";
|
|
2
3
|
export interface BasicResponse {
|
|
3
4
|
success: boolean;
|
|
@@ -25,3 +26,20 @@ export interface SessionListResponse {
|
|
|
25
26
|
page: number;
|
|
26
27
|
perPage: number;
|
|
27
28
|
}
|
|
29
|
+
export interface ScreenConfig {
|
|
30
|
+
maxWidth: number;
|
|
31
|
+
maxHeight: number;
|
|
32
|
+
minWidth: number;
|
|
33
|
+
minHeight: number;
|
|
34
|
+
}
|
|
35
|
+
export interface CreateSessionParams {
|
|
36
|
+
proxyServer?: string;
|
|
37
|
+
proxyServerPassword?: string;
|
|
38
|
+
proxyServerUsername?: string;
|
|
39
|
+
proxyCountry?: Country;
|
|
40
|
+
operatingSystems?: OperatingSystem[];
|
|
41
|
+
device?: ("desktop" | "mobile")[];
|
|
42
|
+
platform?: Platform[];
|
|
43
|
+
locales?: ISO639_1[];
|
|
44
|
+
screen?: ScreenConfig;
|
|
45
|
+
}
|