@sonoransoftware/sonoran.js 1.0.19 → 1.0.20
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/.eslintrc.js +10 -10
- package/.github/workflows/npm-publish.yml +34 -34
- package/.prettierrc.js +6 -6
- package/dist/builders/cad/DispatchCall.d.ts +92 -92
- package/dist/builders/cad/DispatchCall.js +144 -144
- package/dist/builders/cad/index.d.ts +1 -1
- package/dist/builders/cad/index.js +17 -17
- package/dist/builders/index.d.ts +1 -1
- package/dist/builders/index.js +19 -19
- package/dist/constants.d.ts +230 -230
- package/dist/constants.js +27 -27
- package/dist/errors/LibraryErrors.d.ts +19 -19
- package/dist/errors/LibraryErrors.js +47 -47
- package/dist/errors/Messages.d.ts +1 -1
- package/dist/errors/Messages.js +8 -8
- package/dist/errors/index.d.ts +2 -2
- package/dist/errors/index.js +18 -18
- package/dist/index.d.ts +4 -4
- package/dist/index.js +24 -24
- package/dist/instance/Instance.d.ts +24 -24
- package/dist/instance/Instance.js +139 -139
- package/dist/instance/instance.types.d.ts +17 -17
- package/dist/instance/instance.types.js +2 -2
- package/dist/libs/rest/src/index.d.ts +6 -6
- package/dist/libs/rest/src/index.js +22 -22
- package/dist/libs/rest/src/lib/REST.d.ts +101 -101
- package/dist/libs/rest/src/lib/REST.js +136 -129
- package/dist/libs/rest/src/lib/RequestManager.d.ts +59 -59
- package/dist/libs/rest/src/lib/RequestManager.js +191 -191
- package/dist/libs/rest/src/lib/errors/APIError.d.ts +9 -9
- package/dist/libs/rest/src/lib/errors/APIError.js +17 -17
- package/dist/libs/rest/src/lib/errors/HTTPError.d.ts +17 -17
- package/dist/libs/rest/src/lib/errors/HTTPError.js +23 -23
- package/dist/libs/rest/src/lib/errors/RateLimitError.d.ts +13 -13
- package/dist/libs/rest/src/lib/errors/RateLimitError.js +19 -19
- package/dist/libs/rest/src/lib/errors/index.d.ts +4 -4
- package/dist/libs/rest/src/lib/errors/index.js +20 -20
- package/dist/libs/rest/src/lib/handlers/IHandler.d.ts +7 -7
- package/dist/libs/rest/src/lib/handlers/IHandler.js +2 -2
- package/dist/libs/rest/src/lib/handlers/SequentialHandler.d.ts +45 -45
- package/dist/libs/rest/src/lib/handlers/SequentialHandler.js +143 -143
- package/dist/libs/rest/src/lib/utils/constants.d.ts +533 -527
- package/dist/libs/rest/src/lib/utils/constants.js +463 -455
- package/dist/libs/rest/src/lib/utils/utils.d.ts +1 -1
- package/dist/libs/rest/src/lib/utils/utils.js +22 -22
- package/dist/managers/BaseManager.d.ts +14 -14
- package/dist/managers/BaseManager.js +18 -18
- package/dist/managers/CADActiveUnitsManager.d.ts +15 -15
- package/dist/managers/CADActiveUnitsManager.js +38 -38
- package/dist/managers/CADManager.d.ts +29 -29
- package/dist/managers/CADManager.js +86 -86
- package/dist/managers/CADServerManager.d.ts +8 -8
- package/dist/managers/CADServerManager.js +28 -28
- package/dist/managers/CMSManager.d.ts +101 -101
- package/dist/managers/CMSManager.js +266 -266
- package/dist/managers/CMSServerManager.d.ts +8 -8
- package/dist/managers/CMSServerManager.js +34 -34
- package/dist/managers/CacheManager.d.ts +10 -10
- package/dist/managers/CacheManager.js +36 -36
- package/dist/managers/DataManager.d.ts +31 -31
- package/dist/managers/DataManager.js +58 -58
- package/dist/structures/Base.d.ts +9 -9
- package/dist/structures/Base.js +24 -24
- package/dist/structures/CADActiveUnit.d.ts +47 -47
- package/dist/structures/CADActiveUnit.js +66 -66
- package/dist/structures/CADServer.d.ts +26 -26
- package/dist/structures/CADServer.js +15 -15
- package/dist/structures/CMSServer.d.ts +18 -18
- package/dist/structures/CMSServer.js +12 -12
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +17 -17
- package/dist/utils/utils.d.ts +13 -13
- package/dist/utils/utils.js +79 -79
- package/docs/CAD-Methods-and-Usage.md +58 -58
- package/docs/CMS-Methods-and-Usage.md +211 -211
- package/docs/REST-Methods-and-Usage.md +46 -46
- package/package.json +48 -48
- package/readme.md +53 -53
- package/src/constants.ts +248 -248
- package/src/instance/Instance.ts +121 -121
- package/src/instance/instance.types.ts +17 -17
- package/src/libs/rest/src/lib/REST.ts +241 -234
- package/src/libs/rest/src/lib/RequestManager.ts +260 -260
- package/src/libs/rest/src/lib/handlers/SequentialHandler.ts +160 -160
- package/src/libs/rest/src/lib/utils/constants.ts +1012 -997
- package/src/managers/CADManager.ts +63 -63
- package/src/managers/CMSManager.ts +226 -226
- package/src/managers/CMSServerManager.ts +32 -32
- package/tsconfig.json +71 -71
|
@@ -1,161 +1,161 @@
|
|
|
1
|
-
// import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
-
import { AsyncQueue } from '@sapphire/async-queue';
|
|
3
|
-
import fetch, { RequestInit, Response } from 'node-fetch';
|
|
4
|
-
import { AbortController } from "node-abort-controller";
|
|
5
|
-
// import { DiscordAPIError, DiscordErrorData, OAuthErrorData } from '../errors/DiscordAPIError';
|
|
6
|
-
import { APIError } from '../errors';
|
|
7
|
-
import { HTTPError } from '../errors/HTTPError';
|
|
8
|
-
// import { RateLimitError } from '../errors/RateLimitError';
|
|
9
|
-
import type { RequestManager, APIData, /**RequestData*/ } from '../RequestManager';
|
|
10
|
-
import { RateLimitData } from '../REST';
|
|
11
|
-
// import { RESTEvents } from '../utils/constants';
|
|
12
|
-
// import type { RateLimitData } from '../REST';
|
|
13
|
-
import type { IHandler } from './IHandler';
|
|
14
|
-
|
|
15
|
-
export class SequentialHandler implements IHandler {
|
|
16
|
-
/**
|
|
17
|
-
* The unique id of the handler
|
|
18
|
-
*/
|
|
19
|
-
public readonly id: string;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The total number of requests that can be made before we are rate limited
|
|
23
|
-
*/
|
|
24
|
-
// private limit = Infinity;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* The interface used to sequence async requests sequentially
|
|
28
|
-
*/
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
|
|
30
|
-
#asyncQueue = new AsyncQueue();
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @param manager The request manager
|
|
34
|
-
* @param hash The hash that this RequestHandler handles
|
|
35
|
-
* @param majorParameter The major parameter for this handler
|
|
36
|
-
*/
|
|
37
|
-
public constructor(
|
|
38
|
-
private readonly manager: RequestManager,
|
|
39
|
-
private readonly data: APIData,
|
|
40
|
-
) {
|
|
41
|
-
this.id = `${this.data.typePath}:${String(this.data.product)}`;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* If the bucket is currently inactive (no pending requests)
|
|
46
|
-
*/
|
|
47
|
-
public get inactive(): boolean {
|
|
48
|
-
return (
|
|
49
|
-
this.#asyncQueue.remaining === 0
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public getMang(): RequestManager {
|
|
54
|
-
return this.manager;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Emits a debug message
|
|
59
|
-
* @param message The message to debug
|
|
60
|
-
*/
|
|
61
|
-
// private debug(message: string) {
|
|
62
|
-
// this.manager.emit(RESTEvents.Debug, `[REST ${this.id}] ${message}`);
|
|
63
|
-
// }
|
|
64
|
-
|
|
65
|
-
/*
|
|
66
|
-
* Determines whether the request should be queued or whether a RateLimitError should be thrown
|
|
67
|
-
*/
|
|
68
|
-
// private async onRateLimit(rateLimitData: RateLimitData) {
|
|
69
|
-
// const { options } = this.manager;
|
|
70
|
-
// if (options.rejectOnRateLimit) {
|
|
71
|
-
// throw new RateLimitError(rateLimitData);
|
|
72
|
-
// }
|
|
73
|
-
// }
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Queues a request to be sent
|
|
77
|
-
* @param routeId The generalized api route with literal ids for major parameters
|
|
78
|
-
* @param url The url to do the request on
|
|
79
|
-
* @param options All the information needed to make a request
|
|
80
|
-
* @param requestData Extra data from the user's request needed for errors and additional processing
|
|
81
|
-
*/
|
|
82
|
-
public async queueRequest(
|
|
83
|
-
url: string,
|
|
84
|
-
options: RequestInit,
|
|
85
|
-
data: APIData
|
|
86
|
-
): Promise<unknown> {
|
|
87
|
-
let queue = this.#asyncQueue;
|
|
88
|
-
// Wait for any previous requests to be completed before this one is run
|
|
89
|
-
await queue.wait();
|
|
90
|
-
try {
|
|
91
|
-
// Make the request, and return the results
|
|
92
|
-
return await this.runRequest(url, options, data);
|
|
93
|
-
} finally {
|
|
94
|
-
// Allow the next request to fire
|
|
95
|
-
queue.shift();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* The method that actually makes the request to the api, and updates info about the bucket accordingly
|
|
101
|
-
* @param routeId The generalized api route with literal ids for major parameters
|
|
102
|
-
* @param url The fully resolved url to make the request to
|
|
103
|
-
* @param options The node-fetch options needed to make the request
|
|
104
|
-
* @param requestData Extra data from the user's request needed for errors and additional processing
|
|
105
|
-
* @param retries The number of retries this request has already attempted (recursion)
|
|
106
|
-
*/
|
|
107
|
-
private async runRequest(
|
|
108
|
-
url: string,
|
|
109
|
-
options: RequestInit,
|
|
110
|
-
data: APIData,
|
|
111
|
-
// retries = 0,
|
|
112
|
-
): Promise<unknown> {
|
|
113
|
-
const controller = new AbortController();
|
|
114
|
-
const timeout = setTimeout(() => controller.abort(), 30000).unref();
|
|
115
|
-
let res: Response;
|
|
116
|
-
|
|
117
|
-
void this.manager.debug(`[${url} Request] - ${JSON.stringify({ url, options, data })}`);
|
|
118
|
-
|
|
119
|
-
try {
|
|
120
|
-
// node-fetch typings are a bit weird, so we have to cast to any to get the correct signature
|
|
121
|
-
// Type 'AbortSignal' is not assignable to type 'import('discord.js-modules/node_modules/@types/node-fetch/externals').AbortSignal'
|
|
122
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
123
|
-
res = await fetch(url, { ...options, signal: controller.signal as any });
|
|
124
|
-
} catch (error: unknown) {
|
|
125
|
-
throw error;
|
|
126
|
-
} finally {
|
|
127
|
-
clearTimeout(timeout);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const parsedRes = await SequentialHandler.parseResponse(res);
|
|
131
|
-
|
|
132
|
-
void this.manager.debug(`[${url} Response] - ${JSON.stringify({ body: parsedRes, res, status: res.status, headers: res.headers })}`);
|
|
133
|
-
|
|
134
|
-
if (res.ok) {
|
|
135
|
-
return parsedRes;
|
|
136
|
-
} else if (res.status === 400 || res.status === 401 || res.status === 404) {
|
|
137
|
-
throw new APIError(parsedRes as string, data.type, data.fullUrl, res.status, data);
|
|
138
|
-
} else if (res.status === 429) {
|
|
139
|
-
const timeout = setTimeout(() => {
|
|
140
|
-
this.manager.removeRateLimit(data.requestTypeId);
|
|
141
|
-
}, 60 * 1000);
|
|
142
|
-
const ratelimitData: RateLimitData = {
|
|
143
|
-
product: data.product,
|
|
144
|
-
type: data.type,
|
|
145
|
-
timeTill: timeout
|
|
146
|
-
};
|
|
147
|
-
this.manager.onRateLimit(data.requestTypeId, ratelimitData);
|
|
148
|
-
} else if (res.status >= 500 && res.status < 600) {
|
|
149
|
-
throw new HTTPError(res.statusText, res.constructor.name, res.status, data.method, url);
|
|
150
|
-
}
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
private static parseResponse(res: Response): Promise<unknown> {
|
|
155
|
-
if (res.headers.get('Content-Type')?.startsWith('application/json')) {
|
|
156
|
-
return res.json();
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
return res.text();
|
|
160
|
-
}
|
|
1
|
+
// import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
|
+
import { AsyncQueue } from '@sapphire/async-queue';
|
|
3
|
+
import fetch, { RequestInit, Response } from 'node-fetch';
|
|
4
|
+
import { AbortController } from "node-abort-controller";
|
|
5
|
+
// import { DiscordAPIError, DiscordErrorData, OAuthErrorData } from '../errors/DiscordAPIError';
|
|
6
|
+
import { APIError } from '../errors';
|
|
7
|
+
import { HTTPError } from '../errors/HTTPError';
|
|
8
|
+
// import { RateLimitError } from '../errors/RateLimitError';
|
|
9
|
+
import type { RequestManager, APIData, /**RequestData*/ } from '../RequestManager';
|
|
10
|
+
import { RateLimitData } from '../REST';
|
|
11
|
+
// import { RESTEvents } from '../utils/constants';
|
|
12
|
+
// import type { RateLimitData } from '../REST';
|
|
13
|
+
import type { IHandler } from './IHandler';
|
|
14
|
+
|
|
15
|
+
export class SequentialHandler implements IHandler {
|
|
16
|
+
/**
|
|
17
|
+
* The unique id of the handler
|
|
18
|
+
*/
|
|
19
|
+
public readonly id: string;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The total number of requests that can be made before we are rate limited
|
|
23
|
+
*/
|
|
24
|
+
// private limit = Infinity;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The interface used to sequence async requests sequentially
|
|
28
|
+
*/
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
|
|
30
|
+
#asyncQueue = new AsyncQueue();
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param manager The request manager
|
|
34
|
+
* @param hash The hash that this RequestHandler handles
|
|
35
|
+
* @param majorParameter The major parameter for this handler
|
|
36
|
+
*/
|
|
37
|
+
public constructor(
|
|
38
|
+
private readonly manager: RequestManager,
|
|
39
|
+
private readonly data: APIData,
|
|
40
|
+
) {
|
|
41
|
+
this.id = `${this.data.typePath}:${String(this.data.product)}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* If the bucket is currently inactive (no pending requests)
|
|
46
|
+
*/
|
|
47
|
+
public get inactive(): boolean {
|
|
48
|
+
return (
|
|
49
|
+
this.#asyncQueue.remaining === 0
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public getMang(): RequestManager {
|
|
54
|
+
return this.manager;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Emits a debug message
|
|
59
|
+
* @param message The message to debug
|
|
60
|
+
*/
|
|
61
|
+
// private debug(message: string) {
|
|
62
|
+
// this.manager.emit(RESTEvents.Debug, `[REST ${this.id}] ${message}`);
|
|
63
|
+
// }
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* Determines whether the request should be queued or whether a RateLimitError should be thrown
|
|
67
|
+
*/
|
|
68
|
+
// private async onRateLimit(rateLimitData: RateLimitData) {
|
|
69
|
+
// const { options } = this.manager;
|
|
70
|
+
// if (options.rejectOnRateLimit) {
|
|
71
|
+
// throw new RateLimitError(rateLimitData);
|
|
72
|
+
// }
|
|
73
|
+
// }
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Queues a request to be sent
|
|
77
|
+
* @param routeId The generalized api route with literal ids for major parameters
|
|
78
|
+
* @param url The url to do the request on
|
|
79
|
+
* @param options All the information needed to make a request
|
|
80
|
+
* @param requestData Extra data from the user's request needed for errors and additional processing
|
|
81
|
+
*/
|
|
82
|
+
public async queueRequest(
|
|
83
|
+
url: string,
|
|
84
|
+
options: RequestInit,
|
|
85
|
+
data: APIData
|
|
86
|
+
): Promise<unknown> {
|
|
87
|
+
let queue = this.#asyncQueue;
|
|
88
|
+
// Wait for any previous requests to be completed before this one is run
|
|
89
|
+
await queue.wait();
|
|
90
|
+
try {
|
|
91
|
+
// Make the request, and return the results
|
|
92
|
+
return await this.runRequest(url, options, data);
|
|
93
|
+
} finally {
|
|
94
|
+
// Allow the next request to fire
|
|
95
|
+
queue.shift();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The method that actually makes the request to the api, and updates info about the bucket accordingly
|
|
101
|
+
* @param routeId The generalized api route with literal ids for major parameters
|
|
102
|
+
* @param url The fully resolved url to make the request to
|
|
103
|
+
* @param options The node-fetch options needed to make the request
|
|
104
|
+
* @param requestData Extra data from the user's request needed for errors and additional processing
|
|
105
|
+
* @param retries The number of retries this request has already attempted (recursion)
|
|
106
|
+
*/
|
|
107
|
+
private async runRequest(
|
|
108
|
+
url: string,
|
|
109
|
+
options: RequestInit,
|
|
110
|
+
data: APIData,
|
|
111
|
+
// retries = 0,
|
|
112
|
+
): Promise<unknown> {
|
|
113
|
+
const controller = new AbortController();
|
|
114
|
+
const timeout = setTimeout(() => controller.abort(), 30000).unref();
|
|
115
|
+
let res: Response;
|
|
116
|
+
|
|
117
|
+
void this.manager.debug(`[${url} Request] - ${JSON.stringify({ url, options, data })}`);
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
// node-fetch typings are a bit weird, so we have to cast to any to get the correct signature
|
|
121
|
+
// Type 'AbortSignal' is not assignable to type 'import('discord.js-modules/node_modules/@types/node-fetch/externals').AbortSignal'
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
123
|
+
res = await fetch(url, { ...options, signal: controller.signal as any });
|
|
124
|
+
} catch (error: unknown) {
|
|
125
|
+
throw error;
|
|
126
|
+
} finally {
|
|
127
|
+
clearTimeout(timeout);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const parsedRes = await SequentialHandler.parseResponse(res);
|
|
131
|
+
|
|
132
|
+
void this.manager.debug(`[${url} Response] - ${JSON.stringify({ body: parsedRes, res, status: res.status, headers: res.headers })}`);
|
|
133
|
+
|
|
134
|
+
if (res.ok) {
|
|
135
|
+
return parsedRes;
|
|
136
|
+
} else if (res.status === 400 || res.status === 401 || res.status === 404) {
|
|
137
|
+
throw new APIError(parsedRes as string, data.type, data.fullUrl, res.status, data);
|
|
138
|
+
} else if (res.status === 429) {
|
|
139
|
+
const timeout = setTimeout(() => {
|
|
140
|
+
this.manager.removeRateLimit(data.requestTypeId);
|
|
141
|
+
}, 60 * 1000);
|
|
142
|
+
const ratelimitData: RateLimitData = {
|
|
143
|
+
product: data.product,
|
|
144
|
+
type: data.type,
|
|
145
|
+
timeTill: timeout
|
|
146
|
+
};
|
|
147
|
+
this.manager.onRateLimit(data.requestTypeId, ratelimitData);
|
|
148
|
+
} else if (res.status >= 500 && res.status < 600) {
|
|
149
|
+
throw new HTTPError(res.statusText, res.constructor.name, res.status, data.method, url);
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private static parseResponse(res: Response): Promise<unknown> {
|
|
155
|
+
if (res.headers.get('Content-Type')?.startsWith('application/json')) {
|
|
156
|
+
return res.json();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return res.text();
|
|
160
|
+
}
|
|
161
161
|
}
|