cloreai 0.1.9 → 0.1.10

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.
@@ -1,4 +1,5 @@
1
1
  import { type AxiosInstance, type AxiosRequestConfig } from 'axios';
2
+ import PQueue, { type QueueAddOptions } from 'p-queue';
2
3
  import type { CancelOrderBody, CancelOrderOutput } from './endpoints/cancelOrder.js';
3
4
  import type { CreateOrderBody, CreateOrderOutput } from './endpoints/createOrder.js';
4
5
  import type { MarketplaceOutput } from './endpoints/marketplace.js';
@@ -10,17 +11,20 @@ import type { SetSpotPriceBody, SetSpotPriceOutput } from './endpoints/setSpotPr
10
11
  import type { SpotMarketplaceOutput, SpotMarketplaceParams } from './endpoints/spotMarketplace.js';
11
12
  import type { WalletsOutput } from './endpoints/wallets.js';
12
13
  import type { Config } from './types.js';
13
- export declare class CloreAI {
14
+ declare class CloreAI {
14
15
  readonly axios: AxiosInstance;
16
+ readonly rateLimitQueue: PQueue;
17
+ readonly rateLimitCreateOrderQueue: PQueue;
15
18
  constructor(config: Config);
16
- wallets(config?: AxiosRequestConfig): Promise<WalletsOutput>;
17
- myServers(config?: AxiosRequestConfig): Promise<MyServersOutput>;
18
- serverConfig(body: ServerConfigBody, config?: AxiosRequestConfig<ServerConfigBody>): Promise<ServerConfigOutput>;
19
- marketplace(config?: AxiosRequestConfig): Promise<MarketplaceOutput>;
20
- myOrders(params?: MyOrdersParams, config?: AxiosRequestConfig): Promise<MyOrdersOutput>;
21
- spotMarketplace(params: SpotMarketplaceParams, config?: AxiosRequestConfig): Promise<SpotMarketplaceOutput>;
22
- setServerSettings(body: SetServerSettingsBody, config?: AxiosRequestConfig<SetServerSettingsBody>): Promise<SetServerSettingsOutput>;
23
- setSpotPrice(body: SetSpotPriceBody, config?: AxiosRequestConfig<SetSpotPriceBody>): Promise<SetSpotPriceOutput>;
24
- cancelOrder(body: CancelOrderBody, config?: AxiosRequestConfig<CancelOrderBody>): Promise<CancelOrderOutput>;
25
- createOrder(body: CreateOrderBody, config?: AxiosRequestConfig<CreateOrderBody>): Promise<CreateOrderOutput>;
19
+ wallets(axiosConfig?: AxiosRequestConfig, queueOptions?: QueueAddOptions): Promise<WalletsOutput>;
20
+ myServers(axiosConfig?: AxiosRequestConfig, queueOptions?: QueueAddOptions): Promise<MyServersOutput>;
21
+ serverConfig(body: ServerConfigBody, axiosConfig?: AxiosRequestConfig<ServerConfigBody>, queueOptions?: QueueAddOptions): Promise<ServerConfigOutput>;
22
+ marketplace(axiosConfig?: AxiosRequestConfig, queueOptions?: QueueAddOptions): Promise<MarketplaceOutput>;
23
+ myOrders(params?: MyOrdersParams, axiosConfig?: AxiosRequestConfig, queueOptions?: QueueAddOptions): Promise<MyOrdersOutput>;
24
+ spotMarketplace(params: SpotMarketplaceParams, axiosConfig?: AxiosRequestConfig, queueOptions?: QueueAddOptions): Promise<SpotMarketplaceOutput>;
25
+ setServerSettings(body: SetServerSettingsBody, axiosConfig?: AxiosRequestConfig<SetServerSettingsBody>, queueOptions?: QueueAddOptions): Promise<SetServerSettingsOutput>;
26
+ setSpotPrice(body: SetSpotPriceBody, axiosConfig?: AxiosRequestConfig<SetSpotPriceBody>, queueOptions?: QueueAddOptions): Promise<SetSpotPriceOutput>;
27
+ cancelOrder(body: CancelOrderBody, axiosConfig?: AxiosRequestConfig<CancelOrderBody>, queueOptions?: QueueAddOptions): Promise<CancelOrderOutput>;
28
+ createOrder(body: CreateOrderBody, axiosConfig?: AxiosRequestConfig<CreateOrderBody>, queueOptions?: QueueAddOptions): Promise<CreateOrderOutput>;
26
29
  }
30
+ export default CloreAI;
@@ -1,8 +1,19 @@
1
1
  import axios from 'axios';
2
+ import PQueue from 'p-queue';
2
3
  import { CustomError, DatabaseError, ExceededError, InvalidApiTokenError, InvalidEndpointError, InvalidInputDataError, UnknownError } from '../common/errors.js';
3
- import { getErrorMessage, statusCodes } from '../common/index.js';
4
- export class CloreAI {
4
+ import { getErrorMessage, RATE_LIMIT, RATE_LIMIT_CREATE_ORDER, statusCodes } from '../common/index.js';
5
+ class CloreAI {
5
6
  axios;
7
+ rateLimitQueue = new PQueue({
8
+ interval: RATE_LIMIT,
9
+ intervalCap: 1,
10
+ concurrency: 1,
11
+ });
12
+ rateLimitCreateOrderQueue = new PQueue({
13
+ interval: RATE_LIMIT_CREATE_ORDER,
14
+ intervalCap: 1,
15
+ concurrency: 1,
16
+ });
6
17
  constructor(config) {
7
18
  const { apiKey, axiosConfig = {} } = config;
8
19
  this.axios = axios.create({
@@ -43,44 +54,75 @@ export class CloreAI {
43
54
  }
44
55
  });
45
56
  }
46
- async wallets(config) {
47
- const response = await this.axios.get('/wallets', config);
57
+ async wallets(axiosConfig, queueOptions) {
58
+ const response = await this.rateLimitQueue.add(async () => {
59
+ return await this.axios.get('/wallets', axiosConfig);
60
+ }, { ...queueOptions, throwOnTimeout: true });
48
61
  return response.data;
49
62
  }
50
- async myServers(config) {
51
- const response = await this.axios.get('/my_servers', config);
63
+ async myServers(axiosConfig, queueOptions) {
64
+ const response = await this.rateLimitQueue.add(async () => {
65
+ return await this.axios.get('/my_servers', axiosConfig);
66
+ }, { ...queueOptions, throwOnTimeout: true });
52
67
  return response.data;
53
68
  }
54
- async serverConfig(body, config) {
55
- const response = await this.axios.get('/server_config', { ...config, data: body });
69
+ async serverConfig(body, axiosConfig, queueOptions) {
70
+ const response = await this.rateLimitQueue.add(async () => {
71
+ return await this.axios.get('/server_config', { ...axiosConfig, data: body });
72
+ }, { ...queueOptions, throwOnTimeout: true });
56
73
  return response.data;
57
74
  }
58
- async marketplace(config) {
59
- const response = await this.axios.get('/marketplace', config);
75
+ async marketplace(axiosConfig, queueOptions) {
76
+ const response = await this.rateLimitQueue.add(async () => {
77
+ return await this.axios.get('/marketplace', axiosConfig);
78
+ }, { ...queueOptions, throwOnTimeout: true });
60
79
  return response.data;
61
80
  }
62
- async myOrders(params, config) {
63
- const response = await this.axios.get('/my_orders', { ...config, params });
81
+ async myOrders(params, axiosConfig, queueOptions) {
82
+ const response = await this.rateLimitQueue.add(async () => {
83
+ return await this.axios.get('/my_orders', { ...axiosConfig, params });
84
+ }, { ...queueOptions, throwOnTimeout: true });
64
85
  return response.data;
65
86
  }
66
- async spotMarketplace(params, config) {
67
- const response = await this.axios.get('/spot_marketplace', { ...config, params });
87
+ async spotMarketplace(params, axiosConfig, queueOptions) {
88
+ const response = await this.rateLimitQueue.add(async () => {
89
+ return await this.axios.get('/spot_marketplace', { ...axiosConfig, params });
90
+ }, { ...queueOptions, throwOnTimeout: true });
68
91
  return response.data;
69
92
  }
70
- async setServerSettings(body, config) {
71
- const response = await this.axios.post('/set_server_settings', body, config);
93
+ async setServerSettings(body, axiosConfig, queueOptions) {
94
+ const response = await this.rateLimitQueue.add(async () => {
95
+ return await this.axios.post('/set_server_settings', body, axiosConfig);
96
+ }, { ...queueOptions, throwOnTimeout: true });
72
97
  return response.data;
73
98
  }
74
- async setSpotPrice(body, config) {
75
- const response = await this.axios.post('/set_spot_price', body, config);
99
+ async setSpotPrice(body, axiosConfig, queueOptions) {
100
+ const response = await this.rateLimitQueue.add(async () => {
101
+ return await this.axios.post('/set_spot_price', body, axiosConfig);
102
+ }, { ...queueOptions, throwOnTimeout: true });
76
103
  return response.data;
77
104
  }
78
- async cancelOrder(body, config) {
79
- const response = await this.axios.post('/cancel_order', body, config);
105
+ async cancelOrder(body, axiosConfig, queueOptions) {
106
+ const response = await this.rateLimitQueue.add(async () => {
107
+ return await this.axios.post('/cancel_order', body, axiosConfig);
108
+ }, { ...queueOptions, throwOnTimeout: true });
80
109
  return response.data;
81
110
  }
82
- async createOrder(body, config) {
83
- const response = await this.axios.post('/create_order', body, config);
111
+ async createOrder(body, axiosConfig, queueOptions) {
112
+ const response = await this.rateLimitQueue.add(async () => {
113
+ this.rateLimitQueue.pause();
114
+ try {
115
+ return await this.rateLimitCreateOrderQueue.add(async () => {
116
+ return await this.axios.post('/create_order', body, axiosConfig);
117
+ }, { ...queueOptions, throwOnTimeout: true });
118
+ }
119
+ finally {
120
+ setTimeout(() => {
121
+ this.rateLimitQueue.start();
122
+ }, RATE_LIMIT);
123
+ }
124
+ }, { ...queueOptions, throwOnTimeout: true });
84
125
  return response.data;
85
126
  }
86
127
  }
128
+ export default CloreAI;
@@ -1,4 +1,4 @@
1
- export { CloreAI as default } from './CloreAI.js';
1
+ import CloreAI from './CloreAI.js';
2
2
  export * from './endpoints/cancelOrder.js';
3
3
  export * from './endpoints/createOrder.js';
4
4
  export * from './endpoints/marketplace.js';
@@ -10,3 +10,4 @@ export * from './endpoints/setSpotPrice.js';
10
10
  export * from './endpoints/spotMarketplace.js';
11
11
  export * from './endpoints/wallets.js';
12
12
  export * from './types.js';
13
+ export default CloreAI;
@@ -1,4 +1,4 @@
1
- export { CloreAI as default } from './CloreAI.js';
1
+ import CloreAI from './CloreAI.js';
2
2
  export * from './endpoints/cancelOrder.js';
3
3
  export * from './endpoints/createOrder.js';
4
4
  export * from './endpoints/marketplace.js';
@@ -10,3 +10,4 @@ export * from './endpoints/setSpotPrice.js';
10
10
  export * from './endpoints/spotMarketplace.js';
11
11
  export * from './endpoints/wallets.js';
12
12
  export * from './types.js';
13
+ export default CloreAI;
@@ -1,5 +1,5 @@
1
- export declare const GLOBAL_RATE_LIMIT = 1000;
2
- export declare const CREATE_ORDER_RATE_LIMIT = 5000;
1
+ export declare const RATE_LIMIT = 1000;
2
+ export declare const RATE_LIMIT_CREATE_ORDER = 5000;
3
3
  export declare const statusCodes: {
4
4
  NORMAL: number;
5
5
  DATABASE_ERROR: number;
@@ -1,5 +1,5 @@
1
- export const GLOBAL_RATE_LIMIT = 1_000;
2
- export const CREATE_ORDER_RATE_LIMIT = 5_000;
1
+ export const RATE_LIMIT = 1_000;
2
+ export const RATE_LIMIT_CREATE_ORDER = 5_000;
3
3
  export const statusCodes = {
4
4
  NORMAL: 0,
5
5
  DATABASE_ERROR: 1,
@@ -1,4 +1,5 @@
1
1
  import { type AxiosInstance, type AxiosRequestConfig } from 'axios';
2
+ import PQueue, { type QueueAddOptions } from 'p-queue';
2
3
  import type { CancelOrderBody, CancelOrderOutput } from './endpoints/cancelOrder.js';
3
4
  import type { CreateOrderBody, CreateOrderOutput } from './endpoints/createOrder.js';
4
5
  import type { GetSpotBody, GetSpotOutput } from './endpoints/getSpot.js';
@@ -6,13 +7,15 @@ import type { OrdersBody, OrdersOutput } from './endpoints/orders.js';
6
7
  import type { ServersOutput } from './endpoints/servers.js';
7
8
  import type { SetSpotPriceBody, SetSpotPriceOutput } from './endpoints/setSpotPrice.js';
8
9
  import type { Config } from './types.js';
9
- export declare class WebAPI {
10
+ declare class WebAPI {
10
11
  readonly axios: AxiosInstance;
12
+ readonly rateLimitCreateOrderQueue: PQueue;
11
13
  constructor(config: Config);
12
- servers(config?: AxiosRequestConfig): Promise<ServersOutput>;
13
- orders(body: OrdersBody, config?: AxiosRequestConfig<OrdersBody>): Promise<OrdersOutput>;
14
- getSpot(body: GetSpotBody, config?: AxiosRequestConfig<GetSpotBody>): Promise<GetSpotOutput>;
15
- setSpotPrice(body: SetSpotPriceBody, config?: AxiosRequestConfig<SetSpotPriceBody>): Promise<SetSpotPriceOutput>;
16
- cancelOrder(body: CancelOrderBody, config?: AxiosRequestConfig<CancelOrderBody>): Promise<CancelOrderOutput>;
17
- createOrder(body: CreateOrderBody, config?: AxiosRequestConfig<CreateOrderBody>): Promise<CreateOrderOutput>;
14
+ servers(axiosConfig?: AxiosRequestConfig): Promise<ServersOutput>;
15
+ orders(body: OrdersBody, axiosConfig?: AxiosRequestConfig<OrdersBody>): Promise<OrdersOutput>;
16
+ getSpot(body: GetSpotBody, axiosConfig?: AxiosRequestConfig<GetSpotBody>): Promise<GetSpotOutput>;
17
+ setSpotPrice(body: SetSpotPriceBody, axiosConfig?: AxiosRequestConfig<SetSpotPriceBody>): Promise<SetSpotPriceOutput>;
18
+ cancelOrder(body: CancelOrderBody, axiosConfig?: AxiosRequestConfig<CancelOrderBody>): Promise<CancelOrderOutput>;
19
+ createOrder(body: CreateOrderBody, axiosConfig?: AxiosRequestConfig<CreateOrderBody>, queueOptions?: QueueAddOptions): Promise<CreateOrderOutput>;
18
20
  }
21
+ export default WebAPI;
@@ -1,8 +1,14 @@
1
1
  import axios from 'axios';
2
+ import PQueue from 'p-queue';
2
3
  import { UnknownError } from '../common/errors.js';
3
- import { getErrorMessage } from '../common/index.js';
4
- export class WebAPI {
4
+ import { getErrorMessage, RATE_LIMIT_CREATE_ORDER } from '../common/index.js';
5
+ class WebAPI {
5
6
  axios;
7
+ rateLimitCreateOrderQueue = new PQueue({
8
+ interval: RATE_LIMIT_CREATE_ORDER,
9
+ intervalCap: 1,
10
+ concurrency: 1,
11
+ });
6
12
  constructor(config) {
7
13
  const { token, axiosConfig = {} } = config;
8
14
  this.axios = axios.create({
@@ -25,28 +31,31 @@ export class WebAPI {
25
31
  throw new UnknownError(errorMessage, undefined, response.config, response.request, response);
26
32
  });
27
33
  }
28
- async servers(config) {
29
- const response = await this.axios.post('/marketplace/servers', null, config);
34
+ async servers(axiosConfig) {
35
+ const response = await this.axios.post('/marketplace/servers', null, axiosConfig);
30
36
  return response.data;
31
37
  }
32
- async orders(body, config) {
33
- const response = await this.axios.post('/marketplace/orders', body, config);
38
+ async orders(body, axiosConfig) {
39
+ const response = await this.axios.post('/marketplace/orders', body, axiosConfig);
34
40
  return response.data;
35
41
  }
36
- async getSpot(body, config) {
37
- const response = await this.axios.post('/marketplace/get_spot', body, config);
42
+ async getSpot(body, axiosConfig) {
43
+ const response = await this.axios.post('/marketplace/get_spot', body, axiosConfig);
38
44
  return response.data;
39
45
  }
40
- async setSpotPrice(body, config) {
41
- const response = await this.axios.post('/marketplace/set_spot_price', body, config);
46
+ async setSpotPrice(body, axiosConfig) {
47
+ const response = await this.axios.post('/marketplace/set_spot_price', body, axiosConfig);
42
48
  return response.data;
43
49
  }
44
- async cancelOrder(body, config) {
45
- const response = await this.axios.post('/marketplace/cancel_order', body, config);
50
+ async cancelOrder(body, axiosConfig) {
51
+ const response = await this.axios.post('/marketplace/cancel_order', body, axiosConfig);
46
52
  return response.data;
47
53
  }
48
- async createOrder(body, config) {
49
- const response = await this.axios.post('/create_order', body, config);
54
+ async createOrder(body, axiosConfig, queueOptions) {
55
+ const response = await this.rateLimitCreateOrderQueue.add(async () => {
56
+ return await this.axios.post('/create_order', body, axiosConfig);
57
+ }, { ...queueOptions, throwOnTimeout: true });
50
58
  return response.data;
51
59
  }
52
60
  }
61
+ export default WebAPI;
@@ -1,3 +1,4 @@
1
+ import WebAPI from './WebAPI.js';
1
2
  export * from './endpoints/cancelOrder.js';
2
3
  export * from './endpoints/createOrder.js';
3
4
  export * from './endpoints/getSpot.js';
@@ -5,4 +6,4 @@ export * from './endpoints/orders.js';
5
6
  export * from './endpoints/servers.js';
6
7
  export * from './endpoints/setSpotPrice.js';
7
8
  export * from './types.js';
8
- export { WebAPI as default } from './WebAPI.js';
9
+ export default WebAPI;
@@ -1,3 +1,4 @@
1
+ import WebAPI from './WebAPI.js';
1
2
  export * from './endpoints/cancelOrder.js';
2
3
  export * from './endpoints/createOrder.js';
3
4
  export * from './endpoints/getSpot.js';
@@ -5,4 +6,4 @@ export * from './endpoints/orders.js';
5
6
  export * from './endpoints/servers.js';
6
7
  export * from './endpoints/setSpotPrice.js';
7
8
  export * from './types.js';
8
- export { WebAPI as default } from './WebAPI.js';
9
+ export default WebAPI;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloreai",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "description": "CLORE.AI API client",
6
6
  "repository": {
@@ -30,24 +30,26 @@
30
30
  "clean": "rimraf build",
31
31
  "format": "prettier --check .",
32
32
  "lint": "eslint .",
33
+ "playground": "jiti playground/index.ts",
33
34
  "prepack": "npm run clean && npm run build && npm run check",
34
35
  "prepare": "husky",
35
- "start-playground": "node playground/index.js",
36
36
  "typecheck": "tsc --noEmit",
37
- "update-latest": "rimraf package-lock.json && npm-check-updates --peer --upgrade && npm install"
37
+ "update-latest": "npm-check-updates --peer --upgrade && npm update"
38
38
  },
39
39
  "dependencies": {
40
- "axios": "^1.7.5"
40
+ "axios": "^1.7.7",
41
+ "p-queue": "^8.0.1"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@tsconfig/strictest": "^2.0.5",
44
45
  "eslint": "^8.57.0",
45
- "eslint-config-vladpuz": "^0.11.0",
46
- "husky": "^9.1.5",
47
- "npm-check-updates": "^17.1.0",
46
+ "eslint-config-vladpuz": "^0.15.0",
47
+ "husky": "^9.1.6",
48
+ "jiti": "^1.21.6",
49
+ "npm-check-updates": "^17.1.1",
48
50
  "prettier": "^3.3.3",
49
51
  "prettier-config-vladpuz": "^0.8.0",
50
52
  "rimraf": "^6.0.1",
51
- "typescript": "^5.5.4"
53
+ "typescript": "^5.6.2"
52
54
  }
53
55
  }