@sonoransoftware/sonoran.js 1.0.0 → 1.0.3

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 (92) 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 +174 -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 +4 -0
  17. package/dist/index.js +24 -0
  18. package/dist/instance/Instance.d.ts +23 -0
  19. package/dist/instance/Instance.js +135 -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 +102 -0
  26. package/dist/libs/rest/src/lib/RequestManager.d.ts +58 -0
  27. package/dist/libs/rest/src/lib/RequestManager.js +190 -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 +143 -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 +501 -0
  43. package/dist/libs/rest/src/lib/utils/constants.js +423 -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 +38 -0
  48. package/dist/managers/CADManager.d.ts +27 -0
  49. package/dist/managers/CADManager.js +81 -0
  50. package/dist/managers/CADServerManager.d.ts +8 -0
  51. package/dist/managers/CADServerManager.js +28 -0
  52. package/dist/managers/CMSManager.d.ts +81 -0
  53. package/dist/managers/CMSManager.js +212 -0
  54. package/dist/managers/CMSServerManager.d.ts +8 -0
  55. package/dist/managers/CMSServerManager.js +28 -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 +48 -42
  73. package/src/constants.ts +27 -10
  74. package/src/index.ts +1 -9
  75. package/src/instance/Instance.ts +2 -2
  76. package/src/libs/rest/src/lib/REST.ts +7 -2
  77. package/src/libs/rest/src/lib/RequestManager.ts +2 -2
  78. package/src/libs/rest/src/lib/handlers/SequentialHandler.ts +1 -0
  79. package/src/libs/rest/src/lib/utils/constants.ts +21 -1
  80. package/src/managers/BaseManager.ts +1 -1
  81. package/src/managers/CADActiveUnitsManager.ts +1 -1
  82. package/src/managers/CADManager.ts +2 -1
  83. package/src/managers/CADServerManager.ts +1 -1
  84. package/src/managers/CMSManager.ts +49 -3
  85. package/src/managers/CMSServerManager.ts +1 -1
  86. package/src/managers/CacheManager.ts +1 -1
  87. package/src/managers/DataManager.ts +1 -1
  88. package/src/structures/Base.ts +1 -1
  89. package/src/structures/CADActiveUnit.ts +1 -1
  90. package/src/structures/CADServer.ts +1 -1
  91. package/src/structures/CMSServer.ts +1 -1
  92. package/tsconfig.json +4 -4
@@ -0,0 +1,135 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.Instance = void 0;
30
+ const events_1 = __importDefault(require("events"));
31
+ const globalTypes = __importStar(require("../constants"));
32
+ const CADManager_1 = require("../managers/CADManager");
33
+ const CMSManager_1 = require("../managers/CMSManager");
34
+ const utils_1 = require("../utils");
35
+ class Instance extends events_1.default {
36
+ constructor(options) {
37
+ super({ captureRejections: true });
38
+ this.cadApiUrl = 'https://api.sonorancad.com';
39
+ this.cadDefaultServerId = 1;
40
+ this.isCADSuccessful = false;
41
+ this.cmsApiUrl = 'https://api.sonorancms.com';
42
+ this.cmsDefaultServerId = 1;
43
+ this.isCMSSuccessful = false;
44
+ this.debug = false;
45
+ if (options.debug) {
46
+ this.debug = options.debug;
47
+ }
48
+ if (Object.prototype.hasOwnProperty.call(options, 'apiKey') && Object.prototype.hasOwnProperty.call(options, 'communityId')) {
49
+ if (Object.prototype.hasOwnProperty.call(options, 'product')) {
50
+ switch (options.product) {
51
+ case globalTypes.productEnums.CAD: {
52
+ this.cadCommunityId = options.communityId;
53
+ this.cadApiKey = options.apiKey;
54
+ if (options.serverId !== undefined) {
55
+ this._debugLog(`Overriding default server id... ${options.serverId}`);
56
+ this.cadDefaultServerId = options.serverId;
57
+ }
58
+ if (Object.prototype.hasOwnProperty.call(options, 'cadApiUrl') && typeof options.cadApiUrl === 'string') {
59
+ this._debugLog(`Overriding CAD API Url... ${options.cadApiUrl}`);
60
+ this.cadApiUrl = options.cadApiUrl;
61
+ }
62
+ this._debugLog('About to initialize instance.');
63
+ this.initialize();
64
+ break;
65
+ }
66
+ case globalTypes.productEnums.CMS: {
67
+ this.cmsCommunityId = options.communityId;
68
+ this.cmsApiKey = options.apiKey;
69
+ if (options.serverId !== undefined) {
70
+ this._debugLog(`Overriding default server id... ${options.serverId}`);
71
+ this.cmsDefaultServerId = options.serverId;
72
+ }
73
+ if (Object.prototype.hasOwnProperty.call(options, 'cmsApiUrl') && typeof options.cmsApiUrl === 'string') {
74
+ this._debugLog(`Overriding CMS API URL... ${options.cmsApiUrl}`);
75
+ this.cmsApiUrl = options.cmsApiUrl;
76
+ }
77
+ this.initialize();
78
+ break;
79
+ }
80
+ default: {
81
+ throw new Error('Invalid product enum given for constructor.');
82
+ }
83
+ }
84
+ }
85
+ else {
86
+ throw new Error('No product enum given when instancing.');
87
+ }
88
+ }
89
+ else {
90
+ this.cadCommunityId = options.cadCommunityId;
91
+ this.cadApiKey = options.cadApiKey;
92
+ this.cmsCommunityId = options.cmsCommunityId;
93
+ this.cmsApiKey = options.cmsApiKey;
94
+ if (options.cadDefaultServerId !== undefined) {
95
+ this._debugLog(`Overriding default CAD server id... ${options.serverId}`);
96
+ this.cadDefaultServerId = options.cadDefaultServerId;
97
+ }
98
+ if (options.cmsDefaultServerId !== undefined) {
99
+ this._debugLog(`Overriding default CMS server id... ${options.serverId}`);
100
+ this.cmsDefaultServerId = options.cmsDefaultServerId;
101
+ }
102
+ if (Object.prototype.hasOwnProperty.call(options, 'cadApiUrl') && typeof options.cadApiUrl === 'string') {
103
+ this._debugLog(`Overriding CAD API Url... ${options.cadApiUrl}`);
104
+ this.cadApiUrl = options.cadApiUrl;
105
+ }
106
+ if (Object.prototype.hasOwnProperty.call(options, 'cmsApiUrl') && typeof options.cmsApiUrl === 'string') {
107
+ this._debugLog(`Overriding CMS API URL... ${options.cmsApiUrl}`);
108
+ this.cmsApiUrl = options.cmsApiUrl;
109
+ }
110
+ this.initialize();
111
+ }
112
+ }
113
+ initialize() {
114
+ if (this.cadCommunityId && this.cadApiKey && this.cadApiUrl) {
115
+ this._debugLog('About to initialize CAD Manager');
116
+ this.cad = new CADManager_1.CADManager(this);
117
+ }
118
+ else {
119
+ this._debugLog('Not initializing CAD Manager due to a missing community id, api key, or api url.');
120
+ }
121
+ if (this.cmsCommunityId && this.cmsApiKey && this.cmsApiUrl) {
122
+ this._debugLog('About to initialize CMS Manager');
123
+ this.cms = new CMSManager_1.CMSManager(this);
124
+ }
125
+ else {
126
+ this._debugLog('Not initializing CMS Manager due to a missing community id, api key, or api url.');
127
+ }
128
+ }
129
+ _debugLog(message) {
130
+ if (this.debug) {
131
+ (0, utils_1.debugLog)(message);
132
+ }
133
+ }
134
+ }
135
+ exports.Instance = Instance;
@@ -0,0 +1,16 @@
1
+ import * as globalTypes from '../constants';
2
+ export declare type InstanceOptions = {
3
+ communityId?: string;
4
+ apiKey?: string;
5
+ product?: globalTypes.productEnums;
6
+ serverId?: number;
7
+ cadCommunityId?: string;
8
+ cadApiKey?: string;
9
+ cadApiUrl?: string;
10
+ cadDefaultServerId?: number;
11
+ cmsCommunityId?: string;
12
+ cmsApiKey?: string;
13
+ cmsApiUrl?: string;
14
+ cmsDefaultServerId?: number;
15
+ debug?: boolean;
16
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ export * from './lib/errors/APIError';
2
+ export * from './lib/errors/HTTPError';
3
+ export * from './lib/errors/RateLimitError';
4
+ export * from './lib/RequestManager';
5
+ export * from './lib/REST';
6
+ export * from './lib/utils/constants';
@@ -0,0 +1,22 @@
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("./lib/errors/APIError"), exports);
18
+ __exportStar(require("./lib/errors/HTTPError"), exports);
19
+ __exportStar(require("./lib/errors/RateLimitError"), exports);
20
+ __exportStar(require("./lib/RequestManager"), exports);
21
+ __exportStar(require("./lib/REST"), exports);
22
+ __exportStar(require("./lib/utils/constants"), exports);
@@ -0,0 +1,99 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'events';
3
+ import { RequestData, RequestManager } from './RequestManager';
4
+ import { AllAPITypesType, RESTTypedAPIDataStructs } from './utils/constants';
5
+ import { productEnums } from '../../../../constants';
6
+ import type { AgentOptions } from 'node:https';
7
+ import type { RequestInit, Response } from 'node-fetch';
8
+ import { Instance } from '../../../../instance/Instance';
9
+ import { CADManager } from '../../../../managers/CADManager';
10
+ import { CMSManager } from '../../../../managers/CMSManager';
11
+ /**
12
+ * Options to be passed when creating the REST instance
13
+ */
14
+ export interface RESTOptions {
15
+ /**
16
+ * HTTPS Agent options
17
+ * @default {}
18
+ */
19
+ agent: Omit<AgentOptions, 'keepAlive'>;
20
+ /**
21
+ * The base api path, without version
22
+ */
23
+ api: string;
24
+ /**
25
+ * Additional headers to send for all API requests
26
+ * @default {}
27
+ */
28
+ headers: Record<string, string>;
29
+ /**
30
+ * Wether the request should be queued if there's a current ratelimit or to reject.
31
+ * @default true
32
+ */
33
+ rejectOnRateLimit: boolean;
34
+ }
35
+ /**
36
+ * Data emitted on `RESTEvents.RateLimited`
37
+ */
38
+ export interface RateLimitData {
39
+ product: productEnums;
40
+ type: string;
41
+ timeTill: NodeJS.Timer;
42
+ }
43
+ export interface APIRequest {
44
+ /**
45
+ * The HTTP method used in this request
46
+ */
47
+ type: AllAPITypesType;
48
+ /**
49
+ * Additional HTTP options for this request
50
+ */
51
+ options: RequestInit;
52
+ /**
53
+ * The data that was used to form the body of this request
54
+ */
55
+ data: RequestData;
56
+ }
57
+ export interface InvalidRequestWarningData {
58
+ /**
59
+ * Number of invalid requests that have been made in the window
60
+ */
61
+ count: number;
62
+ /**
63
+ * API request type which the request is for
64
+ */
65
+ type: string;
66
+ /**
67
+ * Product which the invalid request is for
68
+ */
69
+ product: productEnums;
70
+ }
71
+ export interface RestEvents {
72
+ invalidRequestWarning: [invalidRequestInfo: InvalidRequestWarningData];
73
+ restDebug: [info: string];
74
+ rateLimited: [rateLimitInfo: RateLimitData];
75
+ request: [request: APIRequest];
76
+ response: [request: APIRequest, response: Response];
77
+ newListener: [name: string, listener: (...args: any) => void];
78
+ removeListener: [name: string, listener: (...args: any) => void];
79
+ }
80
+ export interface REST {
81
+ on: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
82
+ once: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
83
+ emit: (<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]) => boolean) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]) => boolean);
84
+ off: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
85
+ removeAllListeners: (<K extends keyof RestEvents>(event?: K) => this) & (<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>) => this);
86
+ }
87
+ export declare type RestManagerTypes = CADManager | CMSManager;
88
+ export declare class REST extends EventEmitter {
89
+ readonly requestManager: RequestManager;
90
+ readonly instance: Instance;
91
+ readonly manager: RestManagerTypes;
92
+ constructor(_instance: Instance, _manager: RestManagerTypes, _product: productEnums, options: RESTOptions);
93
+ /**
94
+ * Runs a request from the api
95
+ * @param type API Type Enum
96
+ */
97
+ request<K extends keyof RESTTypedAPIDataStructs>(type: K, ...args: RESTTypedAPIDataStructs[K]): Promise<unknown>;
98
+ private formatDataArguments;
99
+ }
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.REST = void 0;
4
+ const events_1 = require("events");
5
+ const RequestManager_1 = require("./RequestManager");
6
+ const constants_1 = require("./utils/constants");
7
+ const constants_2 = require("../../../../constants");
8
+ const Utils_1 = require("./utils/Utils");
9
+ class REST extends events_1.EventEmitter {
10
+ constructor(_instance, _manager, _product, options) {
11
+ super();
12
+ this.instance = _instance;
13
+ this.manager = _manager;
14
+ this.requestManager = new RequestManager_1.RequestManager(_instance, _product, options)
15
+ .on("restDebug" /* Debug */, this.emit.bind(this, "restDebug" /* Debug */))
16
+ .on("rateLimited" /* RateLimited */, this.emit.bind(this, "rateLimited" /* RateLimited */))
17
+ .on("invalidRequestWarning" /* InvalidRequestWarning */, this.emit.bind(this, "invalidRequestWarning" /* InvalidRequestWarning */));
18
+ this.on('newListener', (name, listener) => {
19
+ if (name === "request" /* Request */ || name === "response" /* Response */)
20
+ this.requestManager.on(name, listener);
21
+ });
22
+ this.on('removeListener', (name, listener) => {
23
+ if (name === "request" /* Request */ || name === "response" /* Response */)
24
+ this.requestManager.off(name, listener);
25
+ });
26
+ }
27
+ /**
28
+ * Runs a request from the api
29
+ * @param type API Type Enum
30
+ */
31
+ request(type, ...args) {
32
+ const apiType = constants_1.AllAPITypes.find((aT) => aT.type === type);
33
+ if (!apiType)
34
+ throw new Error('Invalid API Type given for request.');
35
+ let communityId;
36
+ let apiKey;
37
+ switch (apiType.product) {
38
+ case constants_2.productEnums.CAD: {
39
+ communityId = this.instance.cadCommunityId;
40
+ apiKey = this.instance.cadApiKey;
41
+ break;
42
+ }
43
+ case constants_2.productEnums.CMS: {
44
+ communityId = this.instance.cmsCommunityId;
45
+ apiKey = this.instance.cmsApiKey;
46
+ break;
47
+ }
48
+ }
49
+ if (!communityId || !apiKey)
50
+ throw new Error(`Community ID or API Key could not be found for request. P${apiType.product}`);
51
+ if (apiType.minVersion > this.manager.version)
52
+ throw new Error(`[${type}] Subscription version too low for this API type request. Current Version: ${(0, Utils_1.convertSubNumToName)(this.manager.version)} Needed Version: ${(0, Utils_1.convertSubNumToName)(apiType.minVersion)}`);
53
+ const formattedData = this.formatDataArguments(apiType.type, args);
54
+ const options = {
55
+ id: communityId,
56
+ key: apiKey,
57
+ type,
58
+ data: formattedData,
59
+ product: apiType.product
60
+ };
61
+ return this.requestManager.queueRequest(options);
62
+ }
63
+ formatDataArguments(type, args) {
64
+ switch (type) {
65
+ case 'VERIFY_WHITELIST': {
66
+ return {
67
+ apiId: args[0],
68
+ accId: constants_2.uuidRegex.test(args[1]) ? args[1] : undefined,
69
+ serverId: args[2]
70
+ };
71
+ }
72
+ case 'FULL_WHITELIST': {
73
+ return {
74
+ serverId: args[0]
75
+ };
76
+ }
77
+ case 'GET_COM_ACCOUNT': {
78
+ return {
79
+ apiId: args[0],
80
+ username: args[1],
81
+ accId: args[2]
82
+ };
83
+ }
84
+ case 'CLOCK_IN_OUT': {
85
+ return {
86
+ apiId: args[0],
87
+ accId: args[1],
88
+ forceClockIn: args[2]
89
+ };
90
+ }
91
+ case 'CHECK_COM_APIID': {
92
+ return {
93
+ apiId: args[0]
94
+ };
95
+ }
96
+ default: {
97
+ return args;
98
+ }
99
+ }
100
+ }
101
+ }
102
+ exports.REST = REST;
@@ -0,0 +1,58 @@
1
+ /// <reference types="node" />
2
+ import Collection from '@discordjs/collection';
3
+ import { EventEmitter } from 'events';
4
+ import type { Instance } from '../../../../instance/Instance';
5
+ import { RESTOptions, RateLimitData, RestEvents } from './REST';
6
+ import { productEnums } from '../../../../constants';
7
+ import { IHandler } from './handlers/IHandler';
8
+ export declare type RouteLike = `/${string}`;
9
+ export declare const enum RequestMethod {
10
+ Delete = "delete",
11
+ Get = "get",
12
+ Patch = "patch",
13
+ Post = "post",
14
+ Put = "put"
15
+ }
16
+ export declare type ReqDataType = Array<unknown> | unknown;
17
+ export interface RequestData {
18
+ id: string;
19
+ key: string;
20
+ type: string;
21
+ data: any;
22
+ }
23
+ export interface InternalRequestData extends RequestData {
24
+ product: productEnums;
25
+ }
26
+ export interface RequestHeaders {
27
+ 'User-Agent': string;
28
+ }
29
+ export interface APIData {
30
+ requestTypeId: string;
31
+ typePath: string;
32
+ fullUrl: string;
33
+ method: string;
34
+ fetchOptions: RequestInit;
35
+ data: RequestData;
36
+ product: productEnums;
37
+ type: string;
38
+ }
39
+ export interface RequestManager {
40
+ on: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
41
+ once: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
42
+ emit: (<K extends keyof RestEvents>(event: K, ...args: RestEvents[K]) => boolean) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, ...args: any[]) => boolean);
43
+ off: (<K extends keyof RestEvents>(event: K, listener: (...args: RestEvents[K]) => void) => this) & (<S extends string | symbol>(event: Exclude<S, keyof RestEvents>, listener: (...args: any[]) => void) => this);
44
+ removeAllListeners: (<K extends keyof RestEvents>(event?: K) => this) & (<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>) => this);
45
+ }
46
+ export declare class RequestManager extends EventEmitter {
47
+ readonly ratelimitedTypes: Collection<string, RateLimitData>;
48
+ readonly handlers: Collection<string, IHandler>;
49
+ readonly product: productEnums;
50
+ readonly options: RESTOptions;
51
+ private instance;
52
+ constructor(_instance: Instance, _product: productEnums, options: RESTOptions);
53
+ queueRequest(request: InternalRequestData): Promise<unknown>;
54
+ onRateLimit(id: string, rateLimitData: RateLimitData): void;
55
+ removeRateLimit(id: string): void;
56
+ private createHandler;
57
+ private static resolveRequestData;
58
+ }
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RequestManager = void 0;
7
+ const collection_1 = __importDefault(require("@discordjs/collection"));
8
+ // import { DiscordSnowflake } from '@sapphire/snowflake';
9
+ const events_1 = require("events");
10
+ const constants_1 = require("./utils/constants");
11
+ const constants_2 = require("../../../../constants");
12
+ const SequentialHandler_1 = require("./handlers/SequentialHandler");
13
+ const utils_1 = require("../../../../utils/utils");
14
+ class RequestManager extends events_1.EventEmitter {
15
+ constructor(_instance, _product, options) {
16
+ super();
17
+ this.ratelimitedTypes = new collection_1.default();
18
+ this.handlers = new collection_1.default();
19
+ this.product = _product;
20
+ this.instance = _instance;
21
+ switch (_product) {
22
+ case constants_2.productEnums.CAD: {
23
+ this.options = { ...constants_1.DefaultCADRestOptions, ...options };
24
+ break;
25
+ }
26
+ case constants_2.productEnums.CMS: {
27
+ this.options = { ...constants_1.DefaultCMSRestOptions, ...options };
28
+ break;
29
+ }
30
+ default: {
31
+ throw new Error('No Product provided for RequestManager initialization');
32
+ }
33
+ }
34
+ }
35
+ async queueRequest(request) {
36
+ var _a;
37
+ let requestData = request;
38
+ const resolvedData = RequestManager.resolveRequestData(this.instance, request.type, request.product, requestData);
39
+ const handler = (_a = this.handlers.get(`${resolvedData.typePath}:${String(request.product)}`)) !== null && _a !== void 0 ? _a : this.createHandler(resolvedData);
40
+ return handler.queueRequest(resolvedData.fullUrl, resolvedData.fetchOptions, resolvedData);
41
+ }
42
+ onRateLimit(id, rateLimitData) {
43
+ this.ratelimitedTypes.set(id, rateLimitData);
44
+ }
45
+ removeRateLimit(id) {
46
+ this.ratelimitedTypes.delete(id);
47
+ }
48
+ createHandler(data) {
49
+ const queue = new SequentialHandler_1.SequentialHandler(this, data);
50
+ this.handlers.set(queue.id, queue);
51
+ return queue;
52
+ }
53
+ static resolveRequestData(instance, type, product, data) {
54
+ let apiURL = false;
55
+ let apiData = {
56
+ requestTypeId: `${type}:${String(product)}`,
57
+ typePath: '',
58
+ fullUrl: '',
59
+ method: '',
60
+ fetchOptions: {},
61
+ data,
62
+ product,
63
+ type
64
+ };
65
+ switch (product) {
66
+ case constants_2.productEnums.CAD:
67
+ apiURL = instance.cadApiUrl;
68
+ break;
69
+ case constants_2.productEnums.CMS:
70
+ apiURL = instance.cmsApiUrl;
71
+ break;
72
+ }
73
+ const findType = constants_1.AllAPITypes.find((_type) => _type.type === type);
74
+ if (findType) {
75
+ apiData.fullUrl = `${apiURL}/${findType.path}`;
76
+ apiData.method = findType.method;
77
+ apiData.fetchOptions.method = findType.method;
78
+ apiData.typePath = findType.path;
79
+ const clonedData = (0, utils_1.cloneObject)(data.data);
80
+ switch (findType.type) {
81
+ case 'SET_SERVERS': {
82
+ apiData.data.data = clonedData;
83
+ break;
84
+ }
85
+ case 'SET_PENAL_CODES': {
86
+ apiData.data.data = clonedData.data;
87
+ break;
88
+ }
89
+ case 'SET_API_ID': {
90
+ apiData.data.data = clonedData.data;
91
+ break;
92
+ }
93
+ case 'NEW_RECORD': {
94
+ apiData.data.data = clonedData.data;
95
+ break;
96
+ }
97
+ case 'EDIT_RECORD': {
98
+ apiData.data.data = clonedData.data;
99
+ break;
100
+ }
101
+ case 'LOOKUP_INT': {
102
+ apiData.data.data = clonedData.data;
103
+ break;
104
+ }
105
+ case 'LOOKUP': {
106
+ apiData.data.data = clonedData.data;
107
+ break;
108
+ }
109
+ case 'SET_ACCOUNT_PERMISSIONS': {
110
+ apiData.data.data = clonedData.data;
111
+ break;
112
+ }
113
+ case 'BAN_USER': {
114
+ apiData.data.data = clonedData.data;
115
+ break;
116
+ }
117
+ case 'AUTH_STREETSIGNS': {
118
+ apiData.data.data = clonedData;
119
+ break;
120
+ }
121
+ case 'SET_POSTALS': {
122
+ apiData.data.data = clonedData.data;
123
+ break;
124
+ }
125
+ case 'NEW_CHARACTER': {
126
+ apiData.data.data = clonedData.data;
127
+ break;
128
+ }
129
+ case 'EDIT_CHARACTER': {
130
+ apiData.data.data = clonedData.data;
131
+ break;
132
+ }
133
+ case 'MODIFY_IDENTIFIER': {
134
+ apiData.data.data = clonedData.data;
135
+ break;
136
+ }
137
+ case 'ADD_BLIP': {
138
+ apiData.data.data = clonedData.data;
139
+ break;
140
+ }
141
+ case 'MODIFY_BLIP': {
142
+ apiData.data.data = clonedData.data;
143
+ break;
144
+ }
145
+ case 'GET_CALLS': {
146
+ apiData.data.data = clonedData.data;
147
+ break;
148
+ }
149
+ case 'GET_ACTIVE_UNITS': {
150
+ apiData.data.data = clonedData.data;
151
+ break;
152
+ }
153
+ case 'NEW_DISPATCH': {
154
+ apiData.data.data = clonedData.data;
155
+ break;
156
+ }
157
+ case 'UNIT_LOCATION': {
158
+ apiData.data.data = clonedData.data;
159
+ break;
160
+ }
161
+ default: {
162
+ if (data.data) {
163
+ if (Array.isArray(data.data)) {
164
+ if (data.data.length > 0) {
165
+ apiData.data.data = [clonedData];
166
+ }
167
+ else {
168
+ apiData.data.data = [];
169
+ }
170
+ }
171
+ else {
172
+ apiData.data.data = [clonedData];
173
+ }
174
+ }
175
+ else {
176
+ apiData.data.data = [];
177
+ }
178
+ break;
179
+ }
180
+ }
181
+ }
182
+ apiData.fetchOptions.body = JSON.stringify(apiData.data);
183
+ apiData.fetchOptions.headers = {
184
+ 'Accept': 'application/json',
185
+ 'Content-Type': 'application/json'
186
+ };
187
+ return apiData;
188
+ }
189
+ }
190
+ exports.RequestManager = RequestManager;
@@ -0,0 +1,9 @@
1
+ export declare class APIError extends Error {
2
+ response: string;
3
+ requestType: string;
4
+ requestUrl: string;
5
+ responseCode: number;
6
+ requestData: any;
7
+ constructor(response: string, requestType: string, requestUrl: string, responseCode: number, requestData: any);
8
+ get name(): string;
9
+ }
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.APIError = void 0;
4
+ class APIError extends Error {
5
+ constructor(response, requestType, requestUrl, responseCode, requestData) {
6
+ super(response);
7
+ this.response = response;
8
+ this.requestType = requestType;
9
+ this.requestUrl = requestUrl;
10
+ this.responseCode = responseCode;
11
+ this.requestData = requestData;
12
+ }
13
+ get name() {
14
+ return `Sonoran.js API Error - ${this.requestType} [${this.responseCode}]`;
15
+ }
16
+ }
17
+ exports.APIError = APIError;