chapybara 0.4.0 → 0.6.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 CHANGED
@@ -91,24 +91,6 @@ async function getWebTechInfo() {
91
91
  getWebTechInfo();
92
92
  ```
93
93
 
94
- ### Get Website Screenshot
95
-
96
- ```javascript
97
- import fs from "fs/promises";
98
-
99
- async function getWebsiteScreenshot() {
100
- try {
101
- const imageBuffer = await chapybara.screenshot.get("apple.com");
102
- await fs.writeFile("screenshot.png", imageBuffer);
103
- console.log("Screenshot saved to screenshot.png");
104
- } catch (error) {
105
- console.error(`Error fetching screenshot: ${error.message}`);
106
- }
107
- }
108
-
109
- getWebsiteScreenshot();
110
- ```
111
-
112
94
  ### Get Account Information
113
95
 
114
96
  ```javascript
@@ -116,7 +98,6 @@ async function getAccountDetails() {
116
98
  try {
117
99
  const data = await chapybara.account.getInfo();
118
100
  console.log("Domain quota remaining:", data.quotas.domain.remaining);
119
- console.log("Screenshot quota remaining:", data.quotas.screenshot.remaining);
120
101
  } catch (error) {
121
102
  console.error(`Error fetching account info: ${error.message}`);
122
103
  }
package/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { LRUCache } from "lru-cache";
2
- import type { Buffer } from "node:buffer";
3
2
 
4
3
  declare module "chapybara" {
5
4
  interface ChapybaraClientOptions {
@@ -241,7 +240,6 @@ declare module "chapybara" {
241
240
  domain: { limit: number; used: number; remaining: number };
242
241
  ip: { limit: number; used: number; remaining: number };
243
242
  webtech: { limit: number; used: number; remaining: number };
244
- screenshot: { limit: number; used: number; remaining: number };
245
243
  reset_date: string;
246
244
  };
247
245
  api_key: {
@@ -282,14 +280,12 @@ declare module "chapybara" {
282
280
  getScanner: (domain: string) => Promise<WebTechResponse>;
283
281
  };
284
282
 
285
- screenshot: {
286
- get: (domain: string) => Promise<Buffer>;
287
- };
288
-
289
283
  account: {
290
284
  getInfo: () => Promise<AccountInfoResponse>;
291
285
  };
292
286
 
293
287
  getUserIP: () => Promise<{ ip: string }>;
288
+
289
+ close(): void;
294
290
  }
295
291
  }
package/index.js CHANGED
@@ -1,11 +1,12 @@
1
1
  import { LRUCache } from "lru-cache";
2
+ import { createSession, TimeoutError as NLcURLTimeout } from "nlcurl";
2
3
  import { APIError, AuthenticationError, BadRequestError, NotFoundError, RateLimitError, ServerError } from "./lib/errors.js";
3
4
 
4
5
  const DEFAULT_BASE_URL = "https://api.chapyapi.com/api/v1";
5
6
  const DEFAULT_RETRIES = 2;
6
7
  const DEFAULT_TIMEOUT = 30000;
7
8
 
8
- const SDK_VERSION = "0.4.0";
9
+ const SDK_VERSION = "0.6.0";
9
10
 
10
11
  export class ChapybaraClient {
11
12
  constructor(options) {
@@ -18,6 +19,16 @@ export class ChapybaraClient {
18
19
  this.retries = options.retries ?? DEFAULT_RETRIES;
19
20
  this.timeout = options.timeout || DEFAULT_TIMEOUT;
20
21
 
22
+ this.session = createSession({
23
+ baseURL: this.baseUrl,
24
+ headers: {
25
+ "X-API-Key": this.apiKey,
26
+ "Content-Type": "application/json",
27
+ "User-Agent": `Chapybara-NodeJS-SDK/${SDK_VERSION}`,
28
+ },
29
+ timeout: this.timeout,
30
+ });
31
+
21
32
  if (options.cacheOptions) {
22
33
  this.cache = new LRUCache(options.cacheOptions);
23
34
  }
@@ -34,10 +45,6 @@ export class ChapybaraClient {
34
45
  getScanner: (domain) => this._request(`/webtech/${domain}`),
35
46
  };
36
47
 
37
- this.screenshot = {
38
- get: (domain) => this._request(`/screenshot/${domain}`),
39
- };
40
-
41
48
  this.account = {
42
49
  getInfo: () => this._request("/account"),
43
50
  };
@@ -46,29 +53,14 @@ export class ChapybaraClient {
46
53
  }
47
54
 
48
55
  async _request(endpoint, attempt = 1) {
49
- const url = `${this.baseUrl}${endpoint}`;
50
56
  const cacheKey = endpoint;
51
- const isScreenshotRequest = endpoint.startsWith("/screenshot");
52
57
 
53
58
  if (this.cache?.has(cacheKey)) {
54
59
  return this.cache.get(cacheKey);
55
60
  }
56
61
 
57
- const controller = new AbortController();
58
- const timeoutId = setTimeout(() => controller.abort(), this.timeout);
59
-
60
62
  try {
61
- const response = await fetch(url, {
62
- method: "GET",
63
- headers: {
64
- "X-API-Key": this.apiKey,
65
- "Content-Type": "application/json",
66
- "User-Agent": `Chapybara-NodeJS-SDK/${SDK_VERSION}`,
67
- },
68
- signal: controller.signal,
69
- });
70
-
71
- clearTimeout(timeoutId);
63
+ const response = await this.session.get(endpoint);
72
64
 
73
65
  if (!response.ok) {
74
66
  if (response.status >= 500 && attempt <= this.retries) {
@@ -76,16 +68,10 @@ export class ChapybaraClient {
76
68
  await new Promise((resolve) => setTimeout(resolve, delay));
77
69
  return this._request(endpoint, attempt + 1);
78
70
  }
79
- await this._handleError(response);
71
+ this._handleError(response);
80
72
  }
81
73
 
82
- let data;
83
- if (isScreenshotRequest) {
84
- const arrayBuffer = await response.arrayBuffer();
85
- data = Buffer.from(arrayBuffer);
86
- } else {
87
- data = await response.json();
88
- }
74
+ const data = response.json();
89
75
 
90
76
  if (this.cache) {
91
77
  this.cache.set(cacheKey, data);
@@ -93,18 +79,17 @@ export class ChapybaraClient {
93
79
 
94
80
  return data;
95
81
  } catch (error) {
96
- clearTimeout(timeoutId);
97
- if (error.name === "AbortError") {
82
+ if (error instanceof NLcURLTimeout) {
98
83
  throw new Error("Request timed out");
99
84
  }
100
85
  throw error;
101
86
  }
102
87
  }
103
88
 
104
- async _handleError(response) {
89
+ _handleError(response) {
105
90
  let errorData;
106
91
  try {
107
- errorData = await response.json();
92
+ errorData = response.json();
108
93
  } catch (e) {
109
94
  errorData = { error: `HTTP ${response.status} Error` };
110
95
  }
@@ -127,4 +112,8 @@ export class ChapybaraClient {
127
112
  throw new APIError(response.status, errorData);
128
113
  }
129
114
  }
115
+
116
+ close() {
117
+ this.session.close();
118
+ }
130
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chapybara",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Official NodeJS SDK for the Chapybara Domain & IP Intelligence API.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -15,15 +15,15 @@
15
15
  "domain",
16
16
  "ip",
17
17
  "intelligence",
18
- "security",
19
- "screenshot"
18
+ "security"
20
19
  ],
21
20
  "author": "Alpha System",
22
21
  "license": "MIT",
23
22
  "dependencies": {
24
- "lru-cache": "^11.2.2"
23
+ "lru-cache": "^11.2.6",
24
+ "nlcurl": "^0.2.0"
25
25
  },
26
26
  "engines": {
27
27
  "node": ">=18.0.0"
28
28
  }
29
- }
29
+ }