@sonoransoftware/sonoran.js 1.0.0 → 1.0.1

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 (73) hide show
  1. package/.github/workflows/npm-publish.yml +34 -0
  2. package/dist/builders/cad/DispatchCall.d.ts +92 -0
  3. package/dist/builders/cad/DispatchCall.js +144 -0
  4. package/dist/builders/cad/index.d.ts +1 -0
  5. package/dist/builders/cad/index.js +17 -0
  6. package/dist/builders/index.d.ts +1 -0
  7. package/dist/builders/index.js +19 -0
  8. package/dist/constants.d.ts +156 -0
  9. package/dist/constants.js +27 -0
  10. package/dist/errors/LibraryErrors.d.ts +19 -0
  11. package/dist/errors/LibraryErrors.js +47 -0
  12. package/dist/errors/Messages.d.ts +1 -0
  13. package/dist/errors/Messages.js +8 -0
  14. package/dist/errors/index.d.ts +2 -0
  15. package/dist/errors/index.js +18 -0
  16. package/dist/index.d.ts +5 -0
  17. package/dist/index.js +24 -0
  18. package/dist/instance/Instance.d.ts +23 -0
  19. package/dist/instance/Instance.js +134 -0
  20. package/dist/instance/instance.types.d.ts +16 -0
  21. package/dist/instance/instance.types.js +2 -0
  22. package/dist/libs/rest/src/index.d.ts +6 -0
  23. package/dist/libs/rest/src/index.js +22 -0
  24. package/dist/libs/rest/src/lib/REST.d.ts +99 -0
  25. package/dist/libs/rest/src/lib/REST.js +97 -0
  26. package/dist/libs/rest/src/lib/RequestManager.d.ts +58 -0
  27. package/dist/libs/rest/src/lib/RequestManager.js +201 -0
  28. package/dist/libs/rest/src/lib/errors/APIError.d.ts +9 -0
  29. package/dist/libs/rest/src/lib/errors/APIError.js +17 -0
  30. package/dist/libs/rest/src/lib/errors/HTTPError.d.ts +17 -0
  31. package/dist/libs/rest/src/lib/errors/HTTPError.js +23 -0
  32. package/dist/libs/rest/src/lib/errors/RateLimitError.d.ts +13 -0
  33. package/dist/libs/rest/src/lib/errors/RateLimitError.js +19 -0
  34. package/dist/libs/rest/src/lib/errors/index.d.ts +4 -0
  35. package/dist/libs/rest/src/lib/errors/index.js +20 -0
  36. package/dist/libs/rest/src/lib/handlers/IHandler.d.ts +7 -0
  37. package/dist/libs/rest/src/lib/handlers/IHandler.js +2 -0
  38. package/dist/libs/rest/src/lib/handlers/SequentialHandler.d.ts +45 -0
  39. package/dist/libs/rest/src/lib/handlers/SequentialHandler.js +155 -0
  40. package/dist/libs/rest/src/lib/utils/Utils.d.ts +1 -0
  41. package/dist/libs/rest/src/lib/utils/Utils.js +22 -0
  42. package/dist/libs/rest/src/lib/utils/constants.d.ts +493 -0
  43. package/dist/libs/rest/src/lib/utils/constants.js +408 -0
  44. package/dist/managers/BaseManager.d.ts +14 -0
  45. package/dist/managers/BaseManager.js +18 -0
  46. package/dist/managers/CADActiveUnitsManager.d.ts +15 -0
  47. package/dist/managers/CADActiveUnitsManager.js +49 -0
  48. package/dist/managers/CADManager.d.ts +27 -0
  49. package/dist/managers/CADManager.js +93 -0
  50. package/dist/managers/CADServerManager.d.ts +8 -0
  51. package/dist/managers/CADServerManager.js +37 -0
  52. package/dist/managers/CMSManager.d.ts +62 -0
  53. package/dist/managers/CMSManager.js +181 -0
  54. package/dist/managers/CMSServerManager.d.ts +8 -0
  55. package/dist/managers/CMSServerManager.js +37 -0
  56. package/dist/managers/CacheManager.d.ts +10 -0
  57. package/dist/managers/CacheManager.js +39 -0
  58. package/dist/managers/DataManager.d.ts +31 -0
  59. package/dist/managers/DataManager.js +61 -0
  60. package/dist/structures/Base.d.ts +9 -0
  61. package/dist/structures/Base.js +24 -0
  62. package/dist/structures/CADActiveUnit.d.ts +47 -0
  63. package/dist/structures/CADActiveUnit.js +66 -0
  64. package/dist/structures/CADServer.d.ts +26 -0
  65. package/dist/structures/CADServer.js +15 -0
  66. package/dist/structures/CMSServer.d.ts +18 -0
  67. package/dist/structures/CMSServer.js +12 -0
  68. package/dist/utils/index.d.ts +1 -0
  69. package/dist/utils/index.js +17 -0
  70. package/dist/utils/utils.d.ts +13 -0
  71. package/dist/utils/utils.js +82 -0
  72. package/package.json +8 -4
  73. package/tsconfig.json +3 -3
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Represents a HTTP error
3
+ */
4
+ export declare class HTTPError extends Error {
5
+ name: string;
6
+ status: number;
7
+ method: string;
8
+ url: string;
9
+ /**
10
+ * @param message The error message
11
+ * @param name The name of the error
12
+ * @param status The status code of the response
13
+ * @param method The method of the request that erred
14
+ * @param url The url of the request that erred
15
+ */
16
+ constructor(message: string, name: string, status: number, method: string, url: string);
17
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HTTPError = void 0;
4
+ /**
5
+ * Represents a HTTP error
6
+ */
7
+ class HTTPError extends Error {
8
+ /**
9
+ * @param message The error message
10
+ * @param name The name of the error
11
+ * @param status The status code of the response
12
+ * @param method The method of the request that erred
13
+ * @param url The url of the request that erred
14
+ */
15
+ constructor(message, name, status, method, url) {
16
+ super(message);
17
+ this.name = name;
18
+ this.status = status;
19
+ this.method = method;
20
+ this.url = url;
21
+ }
22
+ }
23
+ exports.HTTPError = HTTPError;
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import type { RateLimitData } from '../REST';
3
+ import { productEnums } from '../../../../../constants';
4
+ export declare class RateLimitError extends Error implements RateLimitData {
5
+ product: productEnums;
6
+ type: string;
7
+ timeTill: NodeJS.Timer;
8
+ constructor({ product, type, timeTill }: RateLimitData);
9
+ /**
10
+ * The name of the error
11
+ */
12
+ get name(): string;
13
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RateLimitError = void 0;
4
+ const constants_1 = require("../../../../../constants");
5
+ class RateLimitError extends Error {
6
+ constructor({ product, type, timeTill }) {
7
+ super();
8
+ this.product = product;
9
+ this.type = type;
10
+ this.timeTill = timeTill;
11
+ }
12
+ /**
13
+ * The name of the error
14
+ */
15
+ get name() {
16
+ return `Ratelimit Hit - [${this.product === constants_1.productEnums.CAD ? 'Sonoran CAD' : this.product === constants_1.productEnums.CMS ? 'Sonoran CMS' : 'Invalid Product'} '${this.type}']`;
17
+ }
18
+ }
19
+ exports.RateLimitError = RateLimitError;
@@ -0,0 +1,4 @@
1
+ export * from './APIError';
2
+ export * from './APIError';
3
+ export * from './HTTPError';
4
+ export * from './RateLimitError';
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./APIError"), exports);
18
+ __exportStar(require("./APIError"), exports);
19
+ __exportStar(require("./HTTPError"), exports);
20
+ __exportStar(require("./RateLimitError"), exports);
@@ -0,0 +1,7 @@
1
+ import type { RequestInit } from 'node-fetch';
2
+ import type { APIData } from '../RequestManager';
3
+ export interface IHandler {
4
+ queueRequest: (url: string, options: RequestInit, data: APIData) => Promise<unknown>;
5
+ get inactive(): boolean;
6
+ readonly id: string;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,45 @@
1
+ import { RequestInit } from 'node-fetch';
2
+ import type { RequestManager, APIData } from '../RequestManager';
3
+ import type { IHandler } from './IHandler';
4
+ export declare class SequentialHandler implements IHandler {
5
+ #private;
6
+ private readonly manager;
7
+ private readonly data;
8
+ /**
9
+ * The unique id of the handler
10
+ */
11
+ readonly id: string;
12
+ /**
13
+ * @param manager The request manager
14
+ * @param hash The hash that this RequestHandler handles
15
+ * @param majorParameter The major parameter for this handler
16
+ */
17
+ constructor(manager: RequestManager, data: APIData);
18
+ /**
19
+ * If the bucket is currently inactive (no pending requests)
20
+ */
21
+ get inactive(): boolean;
22
+ getMang(): RequestManager;
23
+ /**
24
+ * Emits a debug message
25
+ * @param message The message to debug
26
+ */
27
+ /**
28
+ * Queues a request to be sent
29
+ * @param routeId The generalized api route with literal ids for major parameters
30
+ * @param url The url to do the request on
31
+ * @param options All the information needed to make a request
32
+ * @param requestData Extra data from the user's request needed for errors and additional processing
33
+ */
34
+ queueRequest(url: string, options: RequestInit, data: APIData): Promise<unknown>;
35
+ /**
36
+ * The method that actually makes the request to the api, and updates info about the bucket accordingly
37
+ * @param routeId The generalized api route with literal ids for major parameters
38
+ * @param url The fully resolved url to make the request to
39
+ * @param options The node-fetch options needed to make the request
40
+ * @param requestData Extra data from the user's request needed for errors and additional processing
41
+ * @param retries The number of retries this request has already attempted (recursion)
42
+ */
43
+ private runRequest;
44
+ private static parseResponse;
45
+ }
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
12
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
13
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
14
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ var _SequentialHandler_asyncQueue;
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.SequentialHandler = void 0;
22
+ // import { setTimeout as sleep } from 'node:timers/promises';
23
+ const async_queue_1 = require("@sapphire/async-queue");
24
+ const node_fetch_1 = __importDefault(require("node-fetch"));
25
+ // import { DiscordAPIError, DiscordErrorData, OAuthErrorData } from '../errors/DiscordAPIError';
26
+ const errors_1 = require("../errors");
27
+ const HTTPError_1 = require("../errors/HTTPError");
28
+ class SequentialHandler {
29
+ /**
30
+ * @param manager The request manager
31
+ * @param hash The hash that this RequestHandler handles
32
+ * @param majorParameter The major parameter for this handler
33
+ */
34
+ constructor(manager, data) {
35
+ this.manager = manager;
36
+ this.data = data;
37
+ /**
38
+ * The total number of requests that can be made before we are rate limited
39
+ */
40
+ // private limit = Infinity;
41
+ /**
42
+ * The interface used to sequence async requests sequentially
43
+ */
44
+ // eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
45
+ _SequentialHandler_asyncQueue.set(this, new async_queue_1.AsyncQueue());
46
+ this.id = `${this.data.typePath}:${String(this.data.product)}`;
47
+ }
48
+ /**
49
+ * If the bucket is currently inactive (no pending requests)
50
+ */
51
+ get inactive() {
52
+ return (__classPrivateFieldGet(this, _SequentialHandler_asyncQueue, "f").remaining === 0);
53
+ }
54
+ getMang() {
55
+ return this.manager;
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
+ * Determines whether the request should be queued or whether a RateLimitError should be thrown
66
+ */
67
+ // private async onRateLimit(rateLimitData: RateLimitData) {
68
+ // const { options } = this.manager;
69
+ // if (options.rejectOnRateLimit) {
70
+ // throw new RateLimitError(rateLimitData);
71
+ // }
72
+ // }
73
+ /**
74
+ * Queues a request to be sent
75
+ * @param routeId The generalized api route with literal ids for major parameters
76
+ * @param url The url to do the request on
77
+ * @param options All the information needed to make a request
78
+ * @param requestData Extra data from the user's request needed for errors and additional processing
79
+ */
80
+ queueRequest(url, options, data) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ let queue = __classPrivateFieldGet(this, _SequentialHandler_asyncQueue, "f");
83
+ // Wait for any previous requests to be completed before this one is run
84
+ yield queue.wait();
85
+ try {
86
+ // Make the request, and return the results
87
+ return yield this.runRequest(url, options, data);
88
+ }
89
+ finally {
90
+ // Allow the next request to fire
91
+ queue.shift();
92
+ }
93
+ });
94
+ }
95
+ /**
96
+ * The method that actually makes the request to the api, and updates info about the bucket accordingly
97
+ * @param routeId The generalized api route with literal ids for major parameters
98
+ * @param url The fully resolved url to make the request to
99
+ * @param options The node-fetch options needed to make the request
100
+ * @param requestData Extra data from the user's request needed for errors and additional processing
101
+ * @param retries The number of retries this request has already attempted (recursion)
102
+ */
103
+ runRequest(url, options, data) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const controller = new AbortController();
106
+ const timeout = setTimeout(() => controller.abort(), 30000).unref();
107
+ let res;
108
+ try {
109
+ // node-fetch typings are a bit weird, so we have to cast to any to get the correct signature
110
+ // Type 'AbortSignal' is not assignable to type 'import('discord.js-modules/node_modules/@types/node-fetch/externals').AbortSignal'
111
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
112
+ res = yield (0, node_fetch_1.default)(url, Object.assign(Object.assign({}, options), { signal: controller.signal }));
113
+ }
114
+ catch (error) {
115
+ throw error;
116
+ }
117
+ finally {
118
+ clearTimeout(timeout);
119
+ }
120
+ if (res.ok) {
121
+ const parsedRes = yield SequentialHandler.parseResponse(res);
122
+ return parsedRes;
123
+ }
124
+ else if (res.status === 400 || res.status === 401 || res.status === 404) {
125
+ const parsedRes = yield SequentialHandler.parseResponse(res);
126
+ // throw new HTTPError(String(parsedRes), res.constructor.name, res.status, data.method, url);
127
+ throw new errors_1.APIError(parsedRes, data.type, data.fullUrl, res.status, data);
128
+ }
129
+ else if (res.status === 429) {
130
+ const timeout = setTimeout(() => {
131
+ this.manager.removeRateLimit(data.requestTypeId);
132
+ }, 60 * 1000);
133
+ const ratelimitData = {
134
+ product: data.product,
135
+ type: data.type,
136
+ timeTill: timeout
137
+ };
138
+ this.manager.onRateLimit(data.requestTypeId, ratelimitData);
139
+ }
140
+ else if (res.status >= 500 && res.status < 600) {
141
+ throw new HTTPError_1.HTTPError(res.statusText, res.constructor.name, res.status, data.method, url);
142
+ }
143
+ return null;
144
+ });
145
+ }
146
+ static parseResponse(res) {
147
+ var _a;
148
+ if ((_a = res.headers.get('Content-Type')) === null || _a === void 0 ? void 0 : _a.startsWith('application/json')) {
149
+ return res.json();
150
+ }
151
+ return res.text();
152
+ }
153
+ }
154
+ exports.SequentialHandler = SequentialHandler;
155
+ _SequentialHandler_asyncQueue = new WeakMap();
@@ -0,0 +1 @@
1
+ export declare function convertSubNumToName(subLevel: number): "FREE" | "STARTER" | "STANDARD" | "PLUS" | "PRO" | "ONE";
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertSubNumToName = void 0;
4
+ function convertSubNumToName(subLevel) {
5
+ switch (subLevel) {
6
+ case 0:
7
+ return 'FREE';
8
+ case 1:
9
+ return 'STARTER';
10
+ case 2:
11
+ return 'STANDARD';
12
+ case 3:
13
+ return 'PLUS';
14
+ case 4:
15
+ return 'PRO';
16
+ case 6:
17
+ return 'ONE';
18
+ default:
19
+ return 'FREE';
20
+ }
21
+ }
22
+ exports.convertSubNumToName = convertSubNumToName;