cloreai 2.0.0 → 4.0.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 +111 -75
- package/dist/Cloreai.d.ts +42 -0
- package/dist/Cloreai.js +267 -0
- package/dist/CloreaiError.d.ts +17 -0
- package/dist/CloreaiError.js +13 -0
- package/dist/Gigaspot.d.ts +14 -0
- package/dist/Gigaspot.js +74 -0
- package/dist/constants.d.ts +20 -0
- package/{build → dist}/constants.js +8 -9
- package/dist/index.d.ts +22 -0
- package/dist/index.js +5 -0
- package/{build → dist}/resources/cancelOrder.d.ts +1 -1
- package/{build → dist}/resources/cancelOrders.d.ts +1 -1
- package/{build → dist}/resources/createGigaspotOrders.d.ts +4 -11
- package/{build → dist}/resources/createOrder.d.ts +10 -10
- package/dist/resources/editGigaspotOrders.d.ts +12 -0
- package/{build → dist}/resources/getGigaspot.d.ts +27 -27
- package/{build → dist}/resources/marketplace.d.ts +5 -6
- package/{build → dist}/resources/myOrders.d.ts +6 -7
- package/dist/resources/myServers.d.ts +16 -0
- package/{build → dist}/resources/pohBalance.d.ts +18 -18
- package/dist/resources/renterFees.d.ts +22 -0
- package/{build → dist}/resources/serverConfig.d.ts +14 -11
- package/dist/resources/setServerSettings.d.ts +18 -0
- package/dist/resources/setSpotPrice.d.ts +15 -0
- package/{build → dist}/resources/spotMarketplace.d.ts +15 -15
- package/{build → dist}/resources/wallets.d.ts +5 -5
- package/dist/resources/wallets.js +1 -0
- package/{build → dist}/types.d.ts +18 -22
- package/dist/types.js +1 -0
- package/package.json +23 -24
- package/build/CloreAI.d.ts +0 -34
- package/build/CloreAI.js +0 -133
- package/build/Gigaspot.d.ts +0 -17
- package/build/Gigaspot.js +0 -46
- package/build/constants.d.ts +0 -21
- package/build/errors.d.ts +0 -22
- package/build/errors.js +0 -43
- package/build/getQueueOptions.d.ts +0 -5
- package/build/getQueueOptions.js +0 -15
- package/build/index.d.ts +0 -20
- package/build/index.js +0 -4
- package/build/resources/editGigaspotOrders.d.ts +0 -19
- package/build/resources/myServers.d.ts +0 -15
- package/build/resources/setServerSettings.d.ts +0 -11
- package/build/resources/setSpotPrice.d.ts +0 -17
- package/build/resources/setSpotPrice.js +0 -2
- /package/{build → dist}/resources/cancelOrder.js +0 -0
- /package/{build → dist}/resources/cancelOrders.js +0 -0
- /package/{build → dist}/resources/createGigaspotOrders.js +0 -0
- /package/{build → dist}/resources/createOrder.js +0 -0
- /package/{build → dist}/resources/editGigaspotOrders.js +0 -0
- /package/{build → dist}/resources/getGigaspot.js +0 -0
- /package/{build → dist}/resources/marketplace.js +0 -0
- /package/{build → dist}/resources/myOrders.js +0 -0
- /package/{build → dist}/resources/myServers.js +0 -0
- /package/{build → dist}/resources/pohBalance.js +0 -0
- /package/{build/resources/serverConfig.js → dist/resources/renterFees.js} +0 -0
- /package/{build/resources/setServerSettings.js → dist/resources/serverConfig.js} +0 -0
- /package/{build/resources/spotMarketplace.js → dist/resources/setServerSettings.js} +0 -0
- /package/{build/resources/wallets.js → dist/resources/setSpotPrice.js} +0 -0
- /package/{build/types.js → dist/resources/spotMarketplace.js} +0 -0
package/README.md
CHANGED
|
@@ -4,10 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
Features:
|
|
6
6
|
|
|
7
|
-
- Based on
|
|
8
|
-
- Protects
|
|
7
|
+
- Based on native fetch
|
|
8
|
+
- Protects against rate limit exceeded (429 errors) through
|
|
9
9
|
[p-queue](https://github.com/sindresorhus/p-queue)
|
|
10
|
-
-
|
|
10
|
+
- Convenient error handling
|
|
11
|
+
- Full type safety
|
|
11
12
|
- Supports GigaSPOT
|
|
12
13
|
|
|
13
14
|
Official documentation: [clore.ai/api-docs](https://clore.ai/api-docs)
|
|
@@ -26,13 +27,14 @@ npm install cloreai
|
|
|
26
27
|
### Creating an instance
|
|
27
28
|
|
|
28
29
|
```typescript
|
|
29
|
-
import
|
|
30
|
+
import Cloreai from 'cloreai'
|
|
30
31
|
|
|
31
|
-
const cloreai = new
|
|
32
|
-
|
|
32
|
+
const cloreai = new Cloreai('API_KEY', {
|
|
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
|
+
fetchOptions: {}, // (optional) Additional `RequestInit` options to be passed to `fetch` calls
|
|
33
36
|
queueOptions: {}, // (optional) Queue instance options https://github.com/sindresorhus/p-queue
|
|
34
|
-
|
|
35
|
-
queueOptionsGigaspot: {}, // (optional) Gigaspot queue instance options https://github.com/sindresorhus/p-queue
|
|
37
|
+
queueCreateOrderOptions: {}, // (optional) Create order queue instance options https://github.com/sindresorhus/p-queue
|
|
36
38
|
})
|
|
37
39
|
```
|
|
38
40
|
|
|
@@ -52,7 +54,7 @@ const myServers = await cloreai.myServers()
|
|
|
52
54
|
|
|
53
55
|
```typescript
|
|
54
56
|
const serverConfig = await cloreai.serverConfig({
|
|
55
|
-
server_name: '
|
|
57
|
+
server_name: 'SERVER_NAME',
|
|
56
58
|
})
|
|
57
59
|
```
|
|
58
60
|
|
|
@@ -81,13 +83,28 @@ const spotMarketplace = await cloreai.spotMarketplace({
|
|
|
81
83
|
|
|
82
84
|
```typescript
|
|
83
85
|
await cloreai.setServerSettings({
|
|
84
|
-
'name': '
|
|
86
|
+
'name': 'NAME', // Server name
|
|
85
87
|
'availability': true,
|
|
86
88
|
'mrl': 24,
|
|
87
|
-
'
|
|
88
|
-
'
|
|
89
|
-
'CLORE-Blockchain_on_demand':
|
|
90
|
-
'CLORE-Blockchain_spot':
|
|
89
|
+
'bitcoin_on_demand': 1,
|
|
90
|
+
'bitcoin_spot': 1,
|
|
91
|
+
'CLORE-Blockchain_on_demand': 1,
|
|
92
|
+
'CLORE-Blockchain_spot': 1,
|
|
93
|
+
'USD-Blockchain_on_demand': 1,
|
|
94
|
+
'USD-Blockchain_spot': 1,
|
|
95
|
+
'enabled-USD-Blockchain': true,
|
|
96
|
+
'enabled-CLORE-Blockchain': true,
|
|
97
|
+
'enabled-bitcoin': true,
|
|
98
|
+
'autoprice': {
|
|
99
|
+
'CLORE-Blockchain': 'usd',
|
|
100
|
+
'USD-Blockchain': 'usd',
|
|
101
|
+
'bitcoin': 'usd',
|
|
102
|
+
},
|
|
103
|
+
'usd_pricing': {
|
|
104
|
+
'CLORE-Blockchain': { on_demand: 1, spot: 1 },
|
|
105
|
+
'USD-Blockchain': { on_demand: 1, spot: 1 },
|
|
106
|
+
'bitcoin': { on_demand: 1, spot: 1 },
|
|
107
|
+
},
|
|
91
108
|
})
|
|
92
109
|
```
|
|
93
110
|
|
|
@@ -119,19 +136,25 @@ await cloreai.createOrder({
|
|
|
119
136
|
renting_server: 5738,
|
|
120
137
|
ports: { 22: 'tcp', 8888: 'http' },
|
|
121
138
|
env: { SSH_PASSWORD: 'SSH_PASSWORD', JUPYTER_TOKEN: 'JUPYTER_TOKEN' },
|
|
122
|
-
jupyter_token: '
|
|
123
|
-
ssh_password: '
|
|
139
|
+
jupyter_token: 'JUPYTER_TOKEN',
|
|
140
|
+
ssh_password: 'SSH_PASSWORD',
|
|
124
141
|
required_price: 50,
|
|
125
142
|
remember_password: true,
|
|
126
143
|
})
|
|
127
144
|
```
|
|
128
145
|
|
|
129
|
-
### 11.
|
|
146
|
+
### 11. poh_balance
|
|
130
147
|
|
|
131
148
|
```typescript
|
|
132
149
|
const pohBalance = await cloreai.pohBalance()
|
|
133
150
|
```
|
|
134
151
|
|
|
152
|
+
### 12. renter_fees
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
const renterFees = await cloreai.renterFees()
|
|
156
|
+
```
|
|
157
|
+
|
|
135
158
|
### GigaSPOT
|
|
136
159
|
|
|
137
160
|
#### getGigaspot
|
|
@@ -184,102 +207,115 @@ await cloreai.gigaspot.cancelOrders({
|
|
|
184
207
|
})
|
|
185
208
|
```
|
|
186
209
|
|
|
187
|
-
|
|
210
|
+
## Fetch Options
|
|
188
211
|
|
|
189
|
-
All methods accept
|
|
190
|
-
parameter, for example:
|
|
212
|
+
All methods accept fetch options as the last parameter, for example:
|
|
191
213
|
|
|
192
214
|
```typescript
|
|
193
215
|
const marketplace = await cloreai.marketplace({
|
|
194
|
-
//
|
|
216
|
+
// `RequestInit` options
|
|
195
217
|
})
|
|
196
218
|
```
|
|
197
219
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
Use the field `cloreai.axios`.
|
|
201
|
-
|
|
202
|
-
Refer to the documentation [axios](https://github.com/axios/axios).
|
|
203
|
-
|
|
204
|
-
### Access to p-queue instances
|
|
205
|
-
|
|
206
|
-
Use the fields:
|
|
207
|
-
|
|
208
|
-
- `cloreai.queue` - for main request queue
|
|
209
|
-
- `cloreai.queueCreateOrder` - for createOrder request queue
|
|
210
|
-
- `cloreai.gigaspot.queue` - for gigaspot request queue
|
|
220
|
+
## Error Handling
|
|
211
221
|
|
|
212
|
-
|
|
222
|
+
Use the `CloreaiError` class for error handling.
|
|
213
223
|
|
|
214
|
-
|
|
224
|
+
Errors have fields with information `error.statusCode` and `error.description`.
|
|
215
225
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
- `DatabaseError`
|
|
219
|
-
- `InvalidInputDataError`
|
|
220
|
-
- `InvalidApiTokenError`
|
|
221
|
-
- `InvalidEndpointError`
|
|
222
|
-
- `ExceededError`
|
|
223
|
-
- `OtherError`
|
|
224
|
-
- `UnknownError`
|
|
225
|
-
|
|
226
|
-
All these errors inherit from `AxiosError`.
|
|
227
|
-
|
|
228
|
-
Check the error type via the `instanceof` operator:
|
|
226
|
+
Use the constant `statusCodes` to check the error type.
|
|
229
227
|
|
|
230
228
|
```typescript
|
|
231
|
-
import {
|
|
232
|
-
import { ExceededError } from 'cloreai'
|
|
229
|
+
import { CloreaiError, statusCodes } from 'cloreai'
|
|
233
230
|
|
|
234
231
|
try {
|
|
235
232
|
const marketplace = await cloreai.marketplace()
|
|
236
233
|
} catch (error) {
|
|
237
|
-
if (error instanceof
|
|
238
|
-
console.log(
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
234
|
+
if (error instanceof CloreaiError) {
|
|
235
|
+
console.log(error.statusCode, error.description)
|
|
236
|
+
|
|
237
|
+
if (error.statusCode === statusCodes.EXCEEDED) {
|
|
238
|
+
console.log('Rate limit exceeded error')
|
|
239
|
+
}
|
|
240
|
+
} else {
|
|
241
|
+
throw error
|
|
243
242
|
}
|
|
244
|
-
|
|
245
|
-
throw error
|
|
246
243
|
}
|
|
247
244
|
```
|
|
248
245
|
|
|
249
|
-
|
|
246
|
+
## Rate limit
|
|
250
247
|
|
|
251
|
-
All methods are protected from rate limit exceeded
|
|
252
|
-
[p-queue](https://github.com/sindresorhus/p-queue), they automatically
|
|
253
|
-
the
|
|
248
|
+
All methods are protected from rate limit exceeded through
|
|
249
|
+
[p-queue](https://github.com/sindresorhus/p-queue), they automatically delay for
|
|
250
|
+
the needed time to avoid exceeding the limit.
|
|
254
251
|
|
|
255
252
|
Default rate limit values are available as constants:
|
|
256
253
|
|
|
257
254
|
```typescript
|
|
258
|
-
import {
|
|
259
|
-
RATE_LIMIT,
|
|
260
|
-
RATE_LIMIT_CREATE_ORDER,
|
|
261
|
-
RATE_LIMIT_GIGASPOT,
|
|
262
|
-
} from 'cloreai'
|
|
255
|
+
import { RATE_LIMIT, RATE_LIMIT_CREATE_ORDER } from 'cloreai'
|
|
263
256
|
|
|
264
257
|
console.log(RATE_LIMIT)
|
|
265
258
|
console.log(RATE_LIMIT_CREATE_ORDER)
|
|
266
|
-
console.log(RATE_LIMIT_GIGASPOT)
|
|
267
259
|
```
|
|
268
260
|
|
|
269
261
|
You can set your own rate limit values for each request queue:
|
|
270
262
|
|
|
271
263
|
```typescript
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
const cloreai = new CloreAI('<API_KEY>', {
|
|
264
|
+
const cloreai = new Cloreai('API_KEY', {
|
|
275
265
|
queueOptions: {
|
|
276
266
|
interval: 2000, // For example 2 seconds
|
|
277
267
|
},
|
|
278
|
-
|
|
268
|
+
queueCreateOrderOptions: {
|
|
279
269
|
interval: 6000, // For example 6 seconds
|
|
280
270
|
},
|
|
281
|
-
|
|
282
|
-
|
|
271
|
+
})
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Access to p-queue instances:
|
|
275
|
+
|
|
276
|
+
- `cloreai.queue` - main request queue
|
|
277
|
+
- `cloreai.queueCreateOrder` - createOrder request queue
|
|
278
|
+
|
|
279
|
+
## Retries
|
|
280
|
+
|
|
281
|
+
For retrying, it's recommended to use
|
|
282
|
+
[p-retry](https://github.com/sindresorhus/p-retry):
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
import pRetry from 'p-retry'
|
|
286
|
+
|
|
287
|
+
const marketplace = await pRetry(
|
|
288
|
+
async () => {
|
|
289
|
+
return await cloreai.marketplace()
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
retries: 5,
|
|
283
293
|
},
|
|
294
|
+
)
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
## Timeouts
|
|
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()`.
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
const cloreai = new Cloreai('API_KEY', {
|
|
307
|
+
fetchOptions: {
|
|
308
|
+
signal: AbortSignal.timeout(5000),
|
|
309
|
+
},
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
const controller = new AbortController()
|
|
313
|
+
|
|
314
|
+
setTimeout(() => {
|
|
315
|
+
controller.abort()
|
|
316
|
+
}, 10000)
|
|
317
|
+
|
|
318
|
+
const marketplace = await cloreai.marketplace({
|
|
319
|
+
signal: controller.signal, // Combined with `AbortSignal.timeout(5000)`
|
|
284
320
|
})
|
|
285
321
|
```
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import PQueue, { type Options as PQueueOptions, type Queue, type QueueAddOptions } from 'p-queue';
|
|
2
|
+
import type { CancelOrderRequestData, CancelOrderResponseData } from './resources/cancelOrder.ts';
|
|
3
|
+
import type { CreateOrderRequestData, CreateOrderResponseData } from './resources/createOrder.ts';
|
|
4
|
+
import type { MarketplaceResponseData } from './resources/marketplace.ts';
|
|
5
|
+
import type { MyOrdersRequestParams, MyOrdersResponseData } from './resources/myOrders.ts';
|
|
6
|
+
import type { MyServersResponseData } from './resources/myServers.ts';
|
|
7
|
+
import type { PohBalanceResponseData } from './resources/pohBalance.ts';
|
|
8
|
+
import type { RenterFeesResponseData } from './resources/renterFees.ts';
|
|
9
|
+
import type { ServerConfigRequestData, ServerConfigResponseData } from './resources/serverConfig.ts';
|
|
10
|
+
import type { SetServerSettingsRequestData, SetServerSettingsResponseData } from './resources/setServerSettings.ts';
|
|
11
|
+
import type { SetSpotPriceRequestData, SetSpotPriceResponseData } from './resources/setSpotPrice.ts';
|
|
12
|
+
import type { SpotMarketplaceRequestParams, SpotMarketplaceResponseData } from './resources/spotMarketplace.ts';
|
|
13
|
+
import type { WalletsResponseData } from './resources/wallets.ts';
|
|
14
|
+
import Gigaspot from './Gigaspot.ts';
|
|
15
|
+
export interface Options {
|
|
16
|
+
baseURL?: string;
|
|
17
|
+
fetch?: typeof fetch;
|
|
18
|
+
fetchOptions?: RequestInit;
|
|
19
|
+
queueOptions?: QueueOptions;
|
|
20
|
+
queueCreateOrderOptions?: QueueOptions;
|
|
21
|
+
}
|
|
22
|
+
export type QueueOptions = PQueueOptions<Queue<() => Promise<unknown>, QueueAddOptions>, QueueAddOptions>;
|
|
23
|
+
declare class Cloreai {
|
|
24
|
+
#private;
|
|
25
|
+
queue: PQueue;
|
|
26
|
+
queueCreateOrder: PQueue;
|
|
27
|
+
gigaspot: Gigaspot;
|
|
28
|
+
constructor(apiKey: string, options?: Options);
|
|
29
|
+
wallets(init?: RequestInit): Promise<WalletsResponseData>;
|
|
30
|
+
myServers(init?: RequestInit): Promise<MyServersResponseData>;
|
|
31
|
+
serverConfig(data: ServerConfigRequestData, init?: RequestInit): Promise<ServerConfigResponseData>;
|
|
32
|
+
marketplace(init?: RequestInit): Promise<MarketplaceResponseData>;
|
|
33
|
+
myOrders(params?: MyOrdersRequestParams, init?: RequestInit): Promise<MyOrdersResponseData>;
|
|
34
|
+
spotMarketplace(params: SpotMarketplaceRequestParams, init?: RequestInit): Promise<SpotMarketplaceResponseData>;
|
|
35
|
+
setServerSettings(data: SetServerSettingsRequestData, init?: RequestInit): Promise<SetServerSettingsResponseData>;
|
|
36
|
+
setSpotPrice(data: SetSpotPriceRequestData, init?: RequestInit): Promise<SetSpotPriceResponseData>;
|
|
37
|
+
cancelOrder(data: CancelOrderRequestData, init?: RequestInit): Promise<CancelOrderResponseData>;
|
|
38
|
+
createOrder(data: CreateOrderRequestData, init?: RequestInit): Promise<CreateOrderResponseData>;
|
|
39
|
+
pohBalance(init?: RequestInit): Promise<PohBalanceResponseData>;
|
|
40
|
+
renterFees(init?: RequestInit): Promise<RenterFeesResponseData>;
|
|
41
|
+
}
|
|
42
|
+
export default Cloreai;
|
package/dist/Cloreai.js
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import PQueue, {} from 'p-queue';
|
|
2
|
+
import { CloreaiError } from "./CloreaiError.js";
|
|
3
|
+
import { priorityLevels, RATE_LIMIT, RATE_LIMIT_CREATE_ORDER, statusCodes } from "./constants.js";
|
|
4
|
+
import Gigaspot from "./Gigaspot.js";
|
|
5
|
+
class Cloreai {
|
|
6
|
+
queue;
|
|
7
|
+
queueCreateOrder;
|
|
8
|
+
gigaspot;
|
|
9
|
+
#baseURL;
|
|
10
|
+
#fetch;
|
|
11
|
+
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
|
+
};
|
|
85
|
+
this.queue = new PQueue({
|
|
86
|
+
interval: RATE_LIMIT,
|
|
87
|
+
intervalCap: 1,
|
|
88
|
+
concurrency: 1,
|
|
89
|
+
...options.queueOptions,
|
|
90
|
+
});
|
|
91
|
+
this.queueCreateOrder = new PQueue({
|
|
92
|
+
interval: RATE_LIMIT_CREATE_ORDER,
|
|
93
|
+
intervalCap: 1,
|
|
94
|
+
concurrency: 1,
|
|
95
|
+
...options.queueCreateOrderOptions,
|
|
96
|
+
});
|
|
97
|
+
this.gigaspot = new Gigaspot(this.queue, this.#baseURL, this.#fetch);
|
|
98
|
+
}
|
|
99
|
+
async wallets(init = {}) {
|
|
100
|
+
const url = new URL(this.#baseURL + '/wallets');
|
|
101
|
+
const response = await this.queue.add(async () => {
|
|
102
|
+
return await this.#fetch(url, {
|
|
103
|
+
...init,
|
|
104
|
+
method: 'GET',
|
|
105
|
+
});
|
|
106
|
+
}, {
|
|
107
|
+
priority: priorityLevels.NORMAL,
|
|
108
|
+
signal: init.signal ?? undefined,
|
|
109
|
+
});
|
|
110
|
+
return await response.json();
|
|
111
|
+
}
|
|
112
|
+
async myServers(init = {}) {
|
|
113
|
+
const url = new URL(this.#baseURL + '/my_servers');
|
|
114
|
+
const response = await this.queue.add(async () => {
|
|
115
|
+
return await this.#fetch(url, {
|
|
116
|
+
...init,
|
|
117
|
+
method: 'GET',
|
|
118
|
+
});
|
|
119
|
+
}, {
|
|
120
|
+
priority: priorityLevels.NORMAL,
|
|
121
|
+
signal: init.signal ?? undefined,
|
|
122
|
+
});
|
|
123
|
+
return await response.json();
|
|
124
|
+
}
|
|
125
|
+
async serverConfig(data, init = {}) {
|
|
126
|
+
const url = new URL(this.#baseURL + '/server_config');
|
|
127
|
+
const response = await this.queue.add(async () => {
|
|
128
|
+
return await this.#fetch(url, {
|
|
129
|
+
...init,
|
|
130
|
+
method: 'GET',
|
|
131
|
+
body: JSON.stringify(data),
|
|
132
|
+
});
|
|
133
|
+
}, {
|
|
134
|
+
priority: priorityLevels.NORMAL,
|
|
135
|
+
signal: init.signal ?? undefined,
|
|
136
|
+
});
|
|
137
|
+
return await response.json();
|
|
138
|
+
}
|
|
139
|
+
async marketplace(init = {}) {
|
|
140
|
+
const url = new URL(this.#baseURL + '/marketplace');
|
|
141
|
+
const response = await this.queue.add(async () => {
|
|
142
|
+
return await this.#fetch(url, {
|
|
143
|
+
...init,
|
|
144
|
+
method: 'GET',
|
|
145
|
+
});
|
|
146
|
+
}, {
|
|
147
|
+
priority: priorityLevels.NORMAL,
|
|
148
|
+
signal: init.signal ?? undefined,
|
|
149
|
+
});
|
|
150
|
+
return await response.json();
|
|
151
|
+
}
|
|
152
|
+
async myOrders(params = {}, init = {}) {
|
|
153
|
+
const url = new URL(this.#baseURL + '/my_orders');
|
|
154
|
+
for (const [key, value] of Object.entries(params)) {
|
|
155
|
+
url.searchParams.set(key, String(value));
|
|
156
|
+
}
|
|
157
|
+
const response = await this.queue.add(async () => {
|
|
158
|
+
return await this.#fetch(url, {
|
|
159
|
+
...init,
|
|
160
|
+
method: 'GET',
|
|
161
|
+
});
|
|
162
|
+
}, {
|
|
163
|
+
priority: priorityLevels.NORMAL,
|
|
164
|
+
signal: init.signal ?? undefined,
|
|
165
|
+
});
|
|
166
|
+
return await response.json();
|
|
167
|
+
}
|
|
168
|
+
async spotMarketplace(params, init = {}) {
|
|
169
|
+
const url = new URL(this.#baseURL + '/spot_marketplace');
|
|
170
|
+
for (const [key, value] of Object.entries(params)) {
|
|
171
|
+
url.searchParams.set(key, String(value));
|
|
172
|
+
}
|
|
173
|
+
const response = await this.queue.add(async () => {
|
|
174
|
+
return await this.#fetch(url, {
|
|
175
|
+
...init,
|
|
176
|
+
method: 'GET',
|
|
177
|
+
});
|
|
178
|
+
}, {
|
|
179
|
+
priority: priorityLevels.NORMAL,
|
|
180
|
+
signal: init.signal ?? undefined,
|
|
181
|
+
});
|
|
182
|
+
return await response.json();
|
|
183
|
+
}
|
|
184
|
+
async setServerSettings(data, init = {}) {
|
|
185
|
+
const url = new URL(this.#baseURL + '/set_server_settings');
|
|
186
|
+
const response = await this.queue.add(async () => {
|
|
187
|
+
return await this.#fetch(url, {
|
|
188
|
+
...init,
|
|
189
|
+
method: 'POST',
|
|
190
|
+
body: JSON.stringify(data),
|
|
191
|
+
});
|
|
192
|
+
}, {
|
|
193
|
+
priority: priorityLevels.HIGH,
|
|
194
|
+
signal: init.signal ?? undefined,
|
|
195
|
+
});
|
|
196
|
+
return await response.json();
|
|
197
|
+
}
|
|
198
|
+
async setSpotPrice(data, init = {}) {
|
|
199
|
+
const url = new URL(this.#baseURL + '/set_spot_price');
|
|
200
|
+
const response = await this.queue.add(async () => {
|
|
201
|
+
return await this.#fetch(url, {
|
|
202
|
+
...init,
|
|
203
|
+
method: 'POST',
|
|
204
|
+
body: JSON.stringify(data),
|
|
205
|
+
});
|
|
206
|
+
}, {
|
|
207
|
+
priority: priorityLevels.HIGH,
|
|
208
|
+
signal: init.signal ?? undefined,
|
|
209
|
+
});
|
|
210
|
+
return await response.json();
|
|
211
|
+
}
|
|
212
|
+
async cancelOrder(data, init = {}) {
|
|
213
|
+
const url = new URL(this.#baseURL + '/cancel_order');
|
|
214
|
+
const response = await this.queue.add(async () => {
|
|
215
|
+
return await this.#fetch(url, {
|
|
216
|
+
...init,
|
|
217
|
+
method: 'POST',
|
|
218
|
+
body: JSON.stringify(data),
|
|
219
|
+
});
|
|
220
|
+
}, {
|
|
221
|
+
priority: priorityLevels.HIGH,
|
|
222
|
+
signal: init.signal ?? undefined,
|
|
223
|
+
});
|
|
224
|
+
return await response.json();
|
|
225
|
+
}
|
|
226
|
+
async createOrder(data, init = {}) {
|
|
227
|
+
const url = new URL(this.#baseURL + '/create_order');
|
|
228
|
+
const response = await this.queue.add(async () => {
|
|
229
|
+
return await this.#fetch(url, {
|
|
230
|
+
...init,
|
|
231
|
+
method: 'POST',
|
|
232
|
+
body: JSON.stringify(data),
|
|
233
|
+
});
|
|
234
|
+
}, {
|
|
235
|
+
priority: priorityLevels.HIGHEST,
|
|
236
|
+
signal: init.signal ?? undefined,
|
|
237
|
+
});
|
|
238
|
+
return await response.json();
|
|
239
|
+
}
|
|
240
|
+
async pohBalance(init = {}) {
|
|
241
|
+
const url = new URL(this.#baseURL + '/poh_balance');
|
|
242
|
+
const response = await this.queue.add(async () => {
|
|
243
|
+
return await this.#fetch(url, {
|
|
244
|
+
...init,
|
|
245
|
+
method: 'GET',
|
|
246
|
+
});
|
|
247
|
+
}, {
|
|
248
|
+
priority: priorityLevels.NORMAL,
|
|
249
|
+
signal: init.signal ?? undefined,
|
|
250
|
+
});
|
|
251
|
+
return await response.json();
|
|
252
|
+
}
|
|
253
|
+
async renterFees(init = {}) {
|
|
254
|
+
const url = new URL(this.#baseURL + '/renter_fees');
|
|
255
|
+
const response = await this.queue.add(async () => {
|
|
256
|
+
return await this.#fetch(url, {
|
|
257
|
+
...init,
|
|
258
|
+
method: 'GET',
|
|
259
|
+
});
|
|
260
|
+
}, {
|
|
261
|
+
priority: priorityLevels.NORMAL,
|
|
262
|
+
signal: init.signal ?? undefined,
|
|
263
|
+
});
|
|
264
|
+
return await response.json();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
export default Cloreai;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface CloreaiErrorOptions extends ErrorOptions {
|
|
2
|
+
init?: RequestInit;
|
|
3
|
+
request?: Request;
|
|
4
|
+
response?: Response;
|
|
5
|
+
statusCode?: number;
|
|
6
|
+
description?: string;
|
|
7
|
+
data?: unknown;
|
|
8
|
+
}
|
|
9
|
+
export declare class CloreaiError extends Error {
|
|
10
|
+
init?: RequestInit;
|
|
11
|
+
request?: Request;
|
|
12
|
+
response?: Response;
|
|
13
|
+
statusCode?: number;
|
|
14
|
+
description?: string;
|
|
15
|
+
data?: unknown;
|
|
16
|
+
constructor(message?: string, options?: CloreaiErrorOptions);
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type PQueue from 'p-queue';
|
|
2
|
+
import type { CancelOrdersRequestData, CancelOrdersResponseData } from './resources/cancelOrders.ts';
|
|
3
|
+
import type { CreateGigaspotOrdersRequestData, CreateGigaspotOrdersResponseData } from './resources/createGigaspotOrders.ts';
|
|
4
|
+
import type { EditGigaspotOrdersRequestData, EditGigaspotOrdersResponseData } from './resources/editGigaspotOrders.ts';
|
|
5
|
+
import type { GetGigaspotResponseData } from './resources/getGigaspot.ts';
|
|
6
|
+
declare class Gigaspot {
|
|
7
|
+
#private;
|
|
8
|
+
constructor(queue: PQueue, baseURL: string, fetchFunction: typeof fetch);
|
|
9
|
+
getGigaspot(init?: RequestInit): Promise<GetGigaspotResponseData>;
|
|
10
|
+
createGigaspotOrders(data: CreateGigaspotOrdersRequestData, init?: RequestInit): Promise<CreateGigaspotOrdersResponseData>;
|
|
11
|
+
editGigaspotOrders(data: EditGigaspotOrdersRequestData, init?: RequestInit): Promise<EditGigaspotOrdersResponseData>;
|
|
12
|
+
cancelOrders(data: CancelOrdersRequestData, init?: RequestInit): Promise<CancelOrdersResponseData>;
|
|
13
|
+
}
|
|
14
|
+
export default Gigaspot;
|