chapybara 0.5.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.
Files changed (3) hide show
  1. package/index.d.ts +2 -0
  2. package/index.js +22 -22
  3. package/package.json +4 -3
package/index.d.ts CHANGED
@@ -285,5 +285,7 @@ declare module "chapybara" {
285
285
  };
286
286
 
287
287
  getUserIP: () => Promise<{ ip: string }>;
288
+
289
+ close(): void;
288
290
  }
289
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
  }
@@ -42,28 +53,14 @@ export class ChapybaraClient {
42
53
  }
43
54
 
44
55
  async _request(endpoint, attempt = 1) {
45
- const url = `${this.baseUrl}${endpoint}`;
46
56
  const cacheKey = endpoint;
47
57
 
48
58
  if (this.cache?.has(cacheKey)) {
49
59
  return this.cache.get(cacheKey);
50
60
  }
51
61
 
52
- const controller = new AbortController();
53
- const timeoutId = setTimeout(() => controller.abort(), this.timeout);
54
-
55
62
  try {
56
- const response = await fetch(url, {
57
- method: "GET",
58
- headers: {
59
- "X-API-Key": this.apiKey,
60
- "Content-Type": "application/json",
61
- "User-Agent": `Chapybara-NodeJS-SDK/${SDK_VERSION}`,
62
- },
63
- signal: controller.signal,
64
- });
65
-
66
- clearTimeout(timeoutId);
63
+ const response = await this.session.get(endpoint);
67
64
 
68
65
  if (!response.ok) {
69
66
  if (response.status >= 500 && attempt <= this.retries) {
@@ -71,10 +68,10 @@ export class ChapybaraClient {
71
68
  await new Promise((resolve) => setTimeout(resolve, delay));
72
69
  return this._request(endpoint, attempt + 1);
73
70
  }
74
- await this._handleError(response);
71
+ this._handleError(response);
75
72
  }
76
73
 
77
- const data = await response.json();
74
+ const data = response.json();
78
75
 
79
76
  if (this.cache) {
80
77
  this.cache.set(cacheKey, data);
@@ -82,18 +79,17 @@ export class ChapybaraClient {
82
79
 
83
80
  return data;
84
81
  } catch (error) {
85
- clearTimeout(timeoutId);
86
- if (error.name === "AbortError") {
82
+ if (error instanceof NLcURLTimeout) {
87
83
  throw new Error("Request timed out");
88
84
  }
89
85
  throw error;
90
86
  }
91
87
  }
92
88
 
93
- async _handleError(response) {
89
+ _handleError(response) {
94
90
  let errorData;
95
91
  try {
96
- errorData = await response.json();
92
+ errorData = response.json();
97
93
  } catch (e) {
98
94
  errorData = { error: `HTTP ${response.status} Error` };
99
95
  }
@@ -116,4 +112,8 @@ export class ChapybaraClient {
116
112
  throw new APIError(response.status, errorData);
117
113
  }
118
114
  }
115
+
116
+ close() {
117
+ this.session.close();
118
+ }
119
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chapybara",
3
- "version": "0.5.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",
@@ -20,9 +20,10 @@
20
20
  "author": "Alpha System",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "lru-cache": "^11.2.6"
23
+ "lru-cache": "^11.2.6",
24
+ "nlcurl": "^0.2.0"
24
25
  },
25
26
  "engines": {
26
27
  "node": ">=18.0.0"
27
28
  }
28
- }
29
+ }