cloreai 4.0.0 → 4.0.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 CHANGED
@@ -31,8 +31,8 @@ import Cloreai from 'cloreai'
31
31
 
32
32
  const cloreai = new Cloreai('API_KEY', {
33
33
  baseURL: 'https://api.clore.ai/v1', // (optional) Override the default base URL for the API
34
- fetch: globalThis.fetch, // (optional) Specify a custom `fetch` function implementation
35
34
  fetchOptions: {}, // (optional) Additional `RequestInit` options to be passed to `fetch` calls
35
+ fetch: globalThis.fetch, // (optional) Specify a custom `fetch` function implementation
36
36
  queueOptions: {}, // (optional) Queue instance options https://github.com/sindresorhus/p-queue
37
37
  queueCreateOrderOptions: {}, // (optional) Create order queue instance options https://github.com/sindresorhus/p-queue
38
38
  })
@@ -296,26 +296,25 @@ const marketplace = await pRetry(
296
296
 
297
297
  ## Timeouts
298
298
 
299
- Use `AbortSignal.timeout`.
300
-
301
- Options signal from fetchOptions and request options are combined. In the
302
- following example, the request will be aborted after 5 seconds, but it can also
303
- be aborted earlier by calling `controller.abort()`.
299
+ Use `AbortSignal.timeout`:
304
300
 
305
301
  ```typescript
306
- const cloreai = new Cloreai('API_KEY', {
307
- fetchOptions: {
308
- signal: AbortSignal.timeout(5000),
309
- },
302
+ const marketplace = await cloreai.marketplace({
303
+ signal: AbortSignal.timeout(5000),
310
304
  })
305
+ ```
311
306
 
307
+ Combine signals using `AbortSignal.any`. In the following example, the request
308
+ will be aborted after 5 seconds, but can also be aborted earlier by calling
309
+ `controller.abort()`:
310
+
311
+ ```typescript
312
312
  const controller = new AbortController()
313
313
 
314
- setTimeout(() => {
315
- controller.abort()
316
- }, 10000)
314
+ // You can abort by event
315
+ // controller.abort()
317
316
 
318
317
  const marketplace = await cloreai.marketplace({
319
- signal: controller.signal, // Combined with `AbortSignal.timeout(5000)`
318
+ signal: AbortSignal.any([AbortSignal.timeout(5000), controller.signal]),
320
319
  })
321
320
  ```
package/dist/Cloreai.d.ts CHANGED
@@ -14,14 +14,18 @@ import type { WalletsResponseData } from './resources/wallets.ts';
14
14
  import Gigaspot from './Gigaspot.ts';
15
15
  export interface Options {
16
16
  baseURL?: string;
17
- fetch?: typeof fetch;
18
17
  fetchOptions?: RequestInit;
18
+ fetch?: typeof fetch;
19
19
  queueOptions?: QueueOptions;
20
20
  queueCreateOrderOptions?: QueueOptions;
21
21
  }
22
22
  export type QueueOptions = PQueueOptions<Queue<() => Promise<unknown>, QueueAddOptions>, QueueAddOptions>;
23
23
  declare class Cloreai {
24
24
  #private;
25
+ apiKey: string;
26
+ baseURL: string;
27
+ fetchOptions: RequestInit;
28
+ fetch: typeof fetch;
25
29
  queue: PQueue;
26
30
  queueCreateOrder: PQueue;
27
31
  gigaspot: Gigaspot;
package/dist/Cloreai.js CHANGED
@@ -3,85 +3,18 @@ import { CloreaiError } from "./CloreaiError.js";
3
3
  import { priorityLevels, RATE_LIMIT, RATE_LIMIT_CREATE_ORDER, statusCodes } from "./constants.js";
4
4
  import Gigaspot from "./Gigaspot.js";
5
5
  class Cloreai {
6
+ apiKey;
7
+ baseURL;
8
+ fetchOptions;
9
+ fetch;
6
10
  queue;
7
11
  queueCreateOrder;
8
12
  gigaspot;
9
- #baseURL;
10
- #fetch;
11
13
  constructor(apiKey, options = {}) {
12
- this.#baseURL = options.baseURL ?? 'https://api.clore.ai/v1';
13
- const fetchFunction = options.fetch ?? fetch;
14
- const fetchOptions = options.fetchOptions ?? {};
15
- this.#fetch = async (input, init = {}) => {
16
- if (input instanceof Request) {
17
- throw new TypeError('Input must be a string or URL');
18
- }
19
- const headers = new Headers({
20
- 'Content-Type': 'application/json',
21
- 'auth': apiKey,
22
- });
23
- for (const [key, value] of new Headers(fetchOptions.headers)) {
24
- headers.set(key, value);
25
- }
26
- for (const [key, value] of new Headers(init.headers)) {
27
- headers.set(key, value);
28
- }
29
- const signal = AbortSignal.any([
30
- ...(fetchOptions.signal ? [fetchOptions.signal] : []),
31
- ...(init.signal ? [init.signal] : []),
32
- ]);
33
- const mergedInit = {
34
- ...fetchOptions,
35
- ...init,
36
- headers,
37
- signal,
38
- };
39
- const url = new URL(input);
40
- const request = new Request(url, mergedInit);
41
- let response;
42
- try {
43
- response = await fetchFunction(request);
44
- }
45
- catch (error) {
46
- throw new CloreaiError(error instanceof Error ? error.message : String(error), {
47
- cause: error,
48
- init: mergedInit,
49
- request,
50
- });
51
- }
52
- const originalJson = response.json;
53
- // @ts-expect-error: json is readonly
54
- response.json = async () => {
55
- let data;
56
- try {
57
- data = await originalJson.call(response);
58
- }
59
- catch (error) {
60
- throw new CloreaiError(error instanceof Error ? error.message : String(error), {
61
- cause: error,
62
- init: mergedInit,
63
- request,
64
- response,
65
- });
66
- }
67
- if (data.code === statusCodes.NORMAL) {
68
- return data;
69
- }
70
- const hasError = Boolean(data.error);
71
- const errorMessage = hasError
72
- ? `Code "${data.code}, error "${data.error}"`
73
- : `Code "${data.code}"`;
74
- throw new CloreaiError(errorMessage, {
75
- init: mergedInit,
76
- request,
77
- response,
78
- statusCode: data.code,
79
- description: data.error ?? '',
80
- data,
81
- });
82
- };
83
- return response;
84
- };
14
+ this.apiKey = apiKey;
15
+ this.baseURL = options.baseURL ?? 'https://api.clore.ai/v1';
16
+ this.fetchOptions = options.fetchOptions ?? {};
17
+ this.fetch = options.fetch ?? fetch;
85
18
  this.queue = new PQueue({
86
19
  interval: RATE_LIMIT,
87
20
  intervalCap: 1,
@@ -94,10 +27,77 @@ class Cloreai {
94
27
  concurrency: 1,
95
28
  ...options.queueCreateOrderOptions,
96
29
  });
97
- this.gigaspot = new Gigaspot(this.queue, this.#baseURL, this.#fetch);
30
+ this.gigaspot = new Gigaspot(this, this.#fetch.bind(this));
31
+ }
32
+ async #fetch(input, init = {}) {
33
+ const headers = new Headers({
34
+ 'Content-Type': 'application/json',
35
+ 'auth': this.apiKey,
36
+ });
37
+ for (const [key, value] of new Headers(this.fetchOptions.headers)) {
38
+ headers.set(key, value);
39
+ }
40
+ for (const [key, value] of new Headers(init.headers)) {
41
+ headers.set(key, value);
42
+ }
43
+ const signal = AbortSignal.any([
44
+ ...(this.fetchOptions.signal ? [this.fetchOptions.signal] : []),
45
+ ...(init.signal ? [init.signal] : []),
46
+ ]);
47
+ const mergedInit = {
48
+ ...this.fetchOptions,
49
+ ...init,
50
+ headers,
51
+ signal,
52
+ };
53
+ const url = new URL(input);
54
+ const request = new Request(url, mergedInit);
55
+ let response;
56
+ try {
57
+ response = await this.fetch(request);
58
+ }
59
+ catch (error) {
60
+ throw new CloreaiError(error instanceof Error ? error.message : String(error), {
61
+ cause: error,
62
+ init: mergedInit,
63
+ request,
64
+ });
65
+ }
66
+ const originalJson = response.json;
67
+ // @ts-expect-error: json is readonly
68
+ response.json = async () => {
69
+ let data;
70
+ try {
71
+ data = await originalJson.call(response);
72
+ }
73
+ catch (error) {
74
+ throw new CloreaiError(error instanceof Error ? error.message : String(error), {
75
+ cause: error,
76
+ init: mergedInit,
77
+ request,
78
+ response,
79
+ });
80
+ }
81
+ if (data.code === statusCodes.NORMAL) {
82
+ return data;
83
+ }
84
+ const hasError = Boolean(data.error);
85
+ const errorMessage = hasError
86
+ ? `Code "${data.code}, error "${data.error}"`
87
+ : `Code "${data.code}"`;
88
+ throw new CloreaiError(errorMessage, {
89
+ init: mergedInit,
90
+ request,
91
+ response,
92
+ statusCode: data.code,
93
+ description: data.error ?? '',
94
+ data,
95
+ });
96
+ };
97
+ return response;
98
98
  }
99
99
  async wallets(init = {}) {
100
- const url = new URL(this.#baseURL + '/wallets');
100
+ const url = new URL(this.baseURL + '/wallets');
101
101
  const response = await this.queue.add(async () => {
102
102
  return await this.#fetch(url, {
103
103
  ...init,
@@ -110,7 +110,7 @@ class Cloreai {
110
110
  return await response.json();
111
111
  }
112
112
  async myServers(init = {}) {
113
- const url = new URL(this.#baseURL + '/my_servers');
113
+ const url = new URL(this.baseURL + '/my_servers');
114
114
  const response = await this.queue.add(async () => {
115
115
  return await this.#fetch(url, {
116
116
  ...init,
@@ -123,7 +123,7 @@ class Cloreai {
123
123
  return await response.json();
124
124
  }
125
125
  async serverConfig(data, init = {}) {
126
- const url = new URL(this.#baseURL + '/server_config');
126
+ const url = new URL(this.baseURL + '/server_config');
127
127
  const response = await this.queue.add(async () => {
128
128
  return await this.#fetch(url, {
129
129
  ...init,
@@ -137,7 +137,7 @@ class Cloreai {
137
137
  return await response.json();
138
138
  }
139
139
  async marketplace(init = {}) {
140
- const url = new URL(this.#baseURL + '/marketplace');
140
+ const url = new URL(this.baseURL + '/marketplace');
141
141
  const response = await this.queue.add(async () => {
142
142
  return await this.#fetch(url, {
143
143
  ...init,
@@ -150,7 +150,7 @@ class Cloreai {
150
150
  return await response.json();
151
151
  }
152
152
  async myOrders(params = {}, init = {}) {
153
- const url = new URL(this.#baseURL + '/my_orders');
153
+ const url = new URL(this.baseURL + '/my_orders');
154
154
  for (const [key, value] of Object.entries(params)) {
155
155
  url.searchParams.set(key, String(value));
156
156
  }
@@ -166,7 +166,7 @@ class Cloreai {
166
166
  return await response.json();
167
167
  }
168
168
  async spotMarketplace(params, init = {}) {
169
- const url = new URL(this.#baseURL + '/spot_marketplace');
169
+ const url = new URL(this.baseURL + '/spot_marketplace');
170
170
  for (const [key, value] of Object.entries(params)) {
171
171
  url.searchParams.set(key, String(value));
172
172
  }
@@ -182,7 +182,7 @@ class Cloreai {
182
182
  return await response.json();
183
183
  }
184
184
  async setServerSettings(data, init = {}) {
185
- const url = new URL(this.#baseURL + '/set_server_settings');
185
+ const url = new URL(this.baseURL + '/set_server_settings');
186
186
  const response = await this.queue.add(async () => {
187
187
  return await this.#fetch(url, {
188
188
  ...init,
@@ -196,7 +196,7 @@ class Cloreai {
196
196
  return await response.json();
197
197
  }
198
198
  async setSpotPrice(data, init = {}) {
199
- const url = new URL(this.#baseURL + '/set_spot_price');
199
+ const url = new URL(this.baseURL + '/set_spot_price');
200
200
  const response = await this.queue.add(async () => {
201
201
  return await this.#fetch(url, {
202
202
  ...init,
@@ -210,7 +210,7 @@ class Cloreai {
210
210
  return await response.json();
211
211
  }
212
212
  async cancelOrder(data, init = {}) {
213
- const url = new URL(this.#baseURL + '/cancel_order');
213
+ const url = new URL(this.baseURL + '/cancel_order');
214
214
  const response = await this.queue.add(async () => {
215
215
  return await this.#fetch(url, {
216
216
  ...init,
@@ -224,7 +224,7 @@ class Cloreai {
224
224
  return await response.json();
225
225
  }
226
226
  async createOrder(data, init = {}) {
227
- const url = new URL(this.#baseURL + '/create_order');
227
+ const url = new URL(this.baseURL + '/create_order');
228
228
  const response = await this.queue.add(async () => {
229
229
  return await this.#fetch(url, {
230
230
  ...init,
@@ -238,7 +238,7 @@ class Cloreai {
238
238
  return await response.json();
239
239
  }
240
240
  async pohBalance(init = {}) {
241
- const url = new URL(this.#baseURL + '/poh_balance');
241
+ const url = new URL(this.baseURL + '/poh_balance');
242
242
  const response = await this.queue.add(async () => {
243
243
  return await this.#fetch(url, {
244
244
  ...init,
@@ -251,7 +251,7 @@ class Cloreai {
251
251
  return await response.json();
252
252
  }
253
253
  async renterFees(init = {}) {
254
- const url = new URL(this.#baseURL + '/renter_fees');
254
+ const url = new URL(this.baseURL + '/renter_fees');
255
255
  const response = await this.queue.add(async () => {
256
256
  return await this.#fetch(url, {
257
257
  ...init,
@@ -1,11 +1,12 @@
1
- import type PQueue from 'p-queue';
1
+ import type Cloreai from './Cloreai.ts';
2
2
  import type { CancelOrdersRequestData, CancelOrdersResponseData } from './resources/cancelOrders.ts';
3
3
  import type { CreateGigaspotOrdersRequestData, CreateGigaspotOrdersResponseData } from './resources/createGigaspotOrders.ts';
4
4
  import type { EditGigaspotOrdersRequestData, EditGigaspotOrdersResponseData } from './resources/editGigaspotOrders.ts';
5
5
  import type { GetGigaspotResponseData } from './resources/getGigaspot.ts';
6
+ type Fetch = (input: string | URL, init?: RequestInit) => Promise<Response>;
6
7
  declare class Gigaspot {
7
8
  #private;
8
- constructor(queue: PQueue, baseURL: string, fetchFunction: typeof fetch);
9
+ constructor(cloreai: Cloreai, fetch: Fetch);
9
10
  getGigaspot(init?: RequestInit): Promise<GetGigaspotResponseData>;
10
11
  createGigaspotOrders(data: CreateGigaspotOrdersRequestData, init?: RequestInit): Promise<CreateGigaspotOrdersResponseData>;
11
12
  editGigaspotOrders(data: EditGigaspotOrdersRequestData, init?: RequestInit): Promise<EditGigaspotOrdersResponseData>;
package/dist/Gigaspot.js CHANGED
@@ -1,17 +1,14 @@
1
1
  import { priorityLevels } from "./constants.js";
2
- /* eslint-disable @typescript-eslint/no-unsafe-type-assertion */
3
2
  class Gigaspot {
4
- #queue;
5
- #baseURL;
3
+ #cloreai;
6
4
  #fetch;
7
- constructor(queue, baseURL, fetchFunction) {
8
- this.#queue = queue;
9
- this.#baseURL = baseURL;
10
- this.#fetch = fetchFunction;
5
+ constructor(cloreai, fetch) {
6
+ this.#cloreai = cloreai;
7
+ this.#fetch = fetch;
11
8
  }
12
9
  async getGigaspot(init = {}) {
13
- const url = new URL(this.#baseURL + '/get_gigaspot');
14
- const response = await this.#queue.add(async () => {
10
+ const url = new URL(this.#cloreai.baseURL + '/get_gigaspot');
11
+ const response = await this.#cloreai.queue.add(async () => {
15
12
  return await this.#fetch(url, {
16
13
  ...init,
17
14
  method: 'GET',
@@ -29,8 +26,8 @@ class Gigaspot {
29
26
  };
30
27
  }
31
28
  async createGigaspotOrders(data, init = {}) {
32
- const url = new URL(this.#baseURL + '/create_gigaspot_orders');
33
- const response = await this.#queue.add(async () => {
29
+ const url = new URL(this.#cloreai.baseURL + '/create_gigaspot_orders');
30
+ const response = await this.#cloreai.queue.add(async () => {
34
31
  return await this.#fetch(url, {
35
32
  ...init,
36
33
  method: 'POST',
@@ -43,8 +40,8 @@ class Gigaspot {
43
40
  return await response.json();
44
41
  }
45
42
  async editGigaspotOrders(data, init = {}) {
46
- const url = new URL(this.#baseURL + '/edit_gigaspot_orders');
47
- const response = await this.#queue.add(async () => {
43
+ const url = new URL(this.#cloreai.baseURL + '/edit_gigaspot_orders');
44
+ const response = await this.#cloreai.queue.add(async () => {
48
45
  return await this.#fetch(url, {
49
46
  ...init,
50
47
  method: 'POST',
@@ -57,8 +54,8 @@ class Gigaspot {
57
54
  return await response.json();
58
55
  }
59
56
  async cancelOrders(data, init = {}) {
60
- const url = new URL(this.#baseURL + '/cancel_orders');
61
- const response = await this.#queue.add(async () => {
57
+ const url = new URL(this.#cloreai.baseURL + '/cancel_orders');
58
+ const response = await this.#cloreai.queue.add(async () => {
62
59
  return await this.#fetch(url, {
63
60
  ...init,
64
61
  method: 'POST',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloreai",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "description": "CLORE.AI API client",
5
5
  "keywords": [
6
6
  "cloreai",
@@ -16,6 +16,9 @@
16
16
  "type": "module",
17
17
  "main": "dist/index.js",
18
18
  "types": "dist/index.d.ts",
19
+ "files": [
20
+ "dist"
21
+ ],
19
22
  "scripts": {
20
23
  "build": "rimraf dist && tsc --project tsconfig.build.json",
21
24
  "check": "npm run typecheck && npm run lint && npm run format",
@@ -26,19 +29,19 @@
26
29
  "typecheck": "tsc --noEmit"
27
30
  },
28
31
  "dependencies": {
29
- "p-queue": "^9.1.2"
32
+ "p-queue": "^9.3.0"
30
33
  },
31
34
  "devDependencies": {
32
35
  "@tsconfig/node-lts": "^24.0.0",
33
36
  "@tsconfig/node-ts": "^23.6.4",
34
37
  "@tsconfig/strictest": "^2.0.8",
35
- "@types/node": "^25.6.0",
36
- "eslint": "^10.2.0",
38
+ "@types/node": "^25.9.1",
39
+ "eslint": "^10.4.1",
37
40
  "eslint-config-vladpuz": "^4.0.1",
38
41
  "husky": "^9.1.7",
39
- "prettier": "^3.8.2",
42
+ "prettier": "^3.8.3",
40
43
  "prettier-config-vladpuz": "^3.0.0",
41
44
  "rimraf": "^6.1.3",
42
- "typescript": "^6.0.2"
45
+ "typescript": "^6.0.3"
43
46
  }
44
47
  }