@sonoransoftware/sonoran.js 1.0.3 → 1.0.5
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/dist/constants.d.ts +56 -0
- package/dist/libs/rest/src/lib/REST.js +18 -1
- package/dist/libs/rest/src/lib/utils/constants.d.ts +23 -3
- package/dist/libs/rest/src/lib/utils/constants.js +12 -0
- package/dist/managers/CMSManager.d.ts +19 -0
- package/dist/managers/CMSManager.js +61 -6
- package/dist/managers/CMSServerManager.js +6 -0
- package/package.json +2 -2
- package/src/builders/cad/DispatchCall.ts +158 -158
- package/src/builders/index.ts +2 -2
- package/src/constants.ts +58 -0
- package/src/errors/LibraryErrors.ts +42 -42
- package/src/errors/Messages.ts +6 -6
- package/src/errors/index.ts +1 -1
- package/src/index.ts +3 -3
- package/src/instance/Instance.ts +117 -117
- package/src/instance/instance.types.ts +16 -16
- package/src/libs/rest/src/index.ts +5 -5
- package/src/libs/rest/src/lib/REST.ts +18 -1
- package/src/libs/rest/src/lib/RequestManager.ts +255 -255
- package/src/libs/rest/src/lib/errors/APIError.ts +14 -14
- package/src/libs/rest/src/lib/errors/HTTPError.ts +21 -21
- package/src/libs/rest/src/lib/errors/RateLimitError.ts +20 -20
- package/src/libs/rest/src/lib/errors/index.ts +3 -3
- package/src/libs/rest/src/lib/handlers/IHandler.ts +12 -12
- package/src/libs/rest/src/lib/handlers/SequentialHandler.ts +157 -157
- package/src/libs/rest/src/lib/utils/constants.ts +35 -2
- package/src/libs/rest/src/lib/utils/utils.ts +17 -17
- package/src/managers/BaseManager.ts +15 -15
- package/src/managers/CADActiveUnitsManager.ts +49 -49
- package/src/managers/CADManager.ts +58 -58
- package/src/managers/CADServerManager.ts +26 -26
- package/src/managers/CMSManager.ts +60 -11
- package/src/managers/CMSServerManager.ts +6 -0
- package/src/managers/CacheManager.ts +37 -37
- package/src/managers/DataManager.ts +63 -63
- package/src/structures/Base.ts +27 -27
- package/src/structures/CADActiveUnit.ts +84 -84
- package/src/structures/CADServer.ts +36 -36
- package/src/structures/CMSServer.ts +25 -25
- package/src/utils/utils.ts +74 -74
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
import { Instance } from '../instance/Instance';
|
|
2
|
-
import { CADSubscriptionVersionEnum } from '../constants';
|
|
3
|
-
import { APIError, DefaultCADRestOptions, REST } from '../libs/rest/src';
|
|
4
|
-
import { BaseManager } from './BaseManager';
|
|
5
|
-
import * as globalTypes from '../constants';
|
|
6
|
-
import type { Mutable } from '../constants';
|
|
7
|
-
import { CADServerManager } from './CADServerManager';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Manages all Sonoran CAD data and methods to interact with the public API.
|
|
11
|
-
*/
|
|
12
|
-
export class CADManager extends BaseManager {
|
|
13
|
-
public readonly version: CADSubscriptionVersionEnum = 0;
|
|
14
|
-
public rest: REST | undefined;
|
|
15
|
-
public servers: CADServerManager | undefined;
|
|
16
|
-
|
|
17
|
-
constructor(instance: Instance) {
|
|
18
|
-
super(instance);
|
|
19
|
-
|
|
20
|
-
this.rest = new REST(instance, this, globalTypes.productEnums.CAD, DefaultCADRestOptions);
|
|
21
|
-
this.buildManager(instance);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
protected async buildManager(instance: Instance) {
|
|
25
|
-
try {
|
|
26
|
-
const versionResp: any = await this.rest?.request('GET_VERSION');
|
|
27
|
-
const mutableThis = this as Mutable<CADManager>;
|
|
28
|
-
mutableThis.version = Number.parseInt(versionResp.replace(/(^\d+)(.+$)/i,'$1'));
|
|
29
|
-
if (this.version >= globalTypes.CADSubscriptionVersionEnum.STANDARD) {
|
|
30
|
-
this.servers = new CADServerManager(instance, this);
|
|
31
|
-
}
|
|
32
|
-
instance.isCADSuccessful = true;
|
|
33
|
-
} catch (err) {
|
|
34
|
-
console.log(err);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Gets a community account by `accId` or `apiId`.
|
|
40
|
-
* @param {Object} params The object that contains parameters to get a community account.
|
|
41
|
-
* @param {string} [data.accId] The account id to find a community account.
|
|
42
|
-
* @param {string} [data.apiId] The api id to find a community account.
|
|
43
|
-
* @returns {Promise} Promise object represents if the request was successful with reason for failure if needed and the account data object if found.
|
|
44
|
-
*/
|
|
45
|
-
public async getAccount(params: { apiId?: string, username?: string }): Promise<globalTypes.CADGetAccountPromiseResult> {
|
|
46
|
-
return new Promise(async (resolve, reject) => {
|
|
47
|
-
try {
|
|
48
|
-
const getAccountRequest: any = await this.rest?.request('GET_ACCOUNT', params.apiId, params.username);
|
|
49
|
-
resolve({ success: true, data: getAccountRequest });
|
|
50
|
-
} catch (err) {
|
|
51
|
-
if (err instanceof APIError) {
|
|
52
|
-
resolve({ success: false, reason: err.response });
|
|
53
|
-
} else {
|
|
54
|
-
reject(err);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
}
|
|
1
|
+
import { Instance } from '../instance/Instance';
|
|
2
|
+
import { CADSubscriptionVersionEnum } from '../constants';
|
|
3
|
+
import { APIError, DefaultCADRestOptions, REST } from '../libs/rest/src';
|
|
4
|
+
import { BaseManager } from './BaseManager';
|
|
5
|
+
import * as globalTypes from '../constants';
|
|
6
|
+
import type { Mutable } from '../constants';
|
|
7
|
+
import { CADServerManager } from './CADServerManager';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Manages all Sonoran CAD data and methods to interact with the public API.
|
|
11
|
+
*/
|
|
12
|
+
export class CADManager extends BaseManager {
|
|
13
|
+
public readonly version: CADSubscriptionVersionEnum = 0;
|
|
14
|
+
public rest: REST | undefined;
|
|
15
|
+
public servers: CADServerManager | undefined;
|
|
16
|
+
|
|
17
|
+
constructor(instance: Instance) {
|
|
18
|
+
super(instance);
|
|
19
|
+
|
|
20
|
+
this.rest = new REST(instance, this, globalTypes.productEnums.CAD, DefaultCADRestOptions);
|
|
21
|
+
this.buildManager(instance);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
protected async buildManager(instance: Instance) {
|
|
25
|
+
try {
|
|
26
|
+
const versionResp: any = await this.rest?.request('GET_VERSION');
|
|
27
|
+
const mutableThis = this as Mutable<CADManager>;
|
|
28
|
+
mutableThis.version = Number.parseInt(versionResp.replace(/(^\d+)(.+$)/i,'$1'));
|
|
29
|
+
if (this.version >= globalTypes.CADSubscriptionVersionEnum.STANDARD) {
|
|
30
|
+
this.servers = new CADServerManager(instance, this);
|
|
31
|
+
}
|
|
32
|
+
instance.isCADSuccessful = true;
|
|
33
|
+
} catch (err) {
|
|
34
|
+
console.log(err);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Gets a community account by `accId` or `apiId`.
|
|
40
|
+
* @param {Object} params The object that contains parameters to get a community account.
|
|
41
|
+
* @param {string} [data.accId] The account id to find a community account.
|
|
42
|
+
* @param {string} [data.apiId] The api id to find a community account.
|
|
43
|
+
* @returns {Promise} Promise object represents if the request was successful with reason for failure if needed and the account data object if found.
|
|
44
|
+
*/
|
|
45
|
+
public async getAccount(params: { apiId?: string, username?: string }): Promise<globalTypes.CADGetAccountPromiseResult> {
|
|
46
|
+
return new Promise(async (resolve, reject) => {
|
|
47
|
+
try {
|
|
48
|
+
const getAccountRequest: any = await this.rest?.request('GET_ACCOUNT', params.apiId, params.username);
|
|
49
|
+
resolve({ success: true, data: getAccountRequest });
|
|
50
|
+
} catch (err) {
|
|
51
|
+
if (err instanceof APIError) {
|
|
52
|
+
resolve({ success: false, reason: err.response });
|
|
53
|
+
} else {
|
|
54
|
+
reject(err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
59
|
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { Instance } from '../instance/Instance';
|
|
2
|
-
import { CADServerAPIStruct } from '../libs/rest/src';
|
|
3
|
-
import { CADServer } from '../structures/CADServer';
|
|
4
|
-
import { CacheManager } from './CacheManager';
|
|
5
|
-
import { CADManager } from './CADManager';
|
|
6
|
-
|
|
7
|
-
export class CADServerManager extends CacheManager<number, CADServer, CADServerAPIStruct> {
|
|
8
|
-
constructor(instance: Instance, manager: CADManager) {
|
|
9
|
-
super(instance, CADServer, []);
|
|
10
|
-
|
|
11
|
-
(async () => {
|
|
12
|
-
try {
|
|
13
|
-
const serversRes: any = await manager.rest?.request('GET_SERVERS');
|
|
14
|
-
const servers = JSON.parse(serversRes).servers;
|
|
15
|
-
servers.forEach((server: CADServerAPIStruct) => {
|
|
16
|
-
const serverStruct = {
|
|
17
|
-
id: server.id,
|
|
18
|
-
config: server
|
|
19
|
-
};
|
|
20
|
-
this._add(serverStruct, true, server.id);
|
|
21
|
-
});
|
|
22
|
-
} catch (err) {
|
|
23
|
-
throw new Error(String(err));
|
|
24
|
-
}
|
|
25
|
-
})();
|
|
26
|
-
}
|
|
1
|
+
import { Instance } from '../instance/Instance';
|
|
2
|
+
import { CADServerAPIStruct } from '../libs/rest/src';
|
|
3
|
+
import { CADServer } from '../structures/CADServer';
|
|
4
|
+
import { CacheManager } from './CacheManager';
|
|
5
|
+
import { CADManager } from './CADManager';
|
|
6
|
+
|
|
7
|
+
export class CADServerManager extends CacheManager<number, CADServer, CADServerAPIStruct> {
|
|
8
|
+
constructor(instance: Instance, manager: CADManager) {
|
|
9
|
+
super(instance, CADServer, []);
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
try {
|
|
13
|
+
const serversRes: any = await manager.rest?.request('GET_SERVERS');
|
|
14
|
+
const servers = JSON.parse(serversRes).servers;
|
|
15
|
+
servers.forEach((server: CADServerAPIStruct) => {
|
|
16
|
+
const serverStruct = {
|
|
17
|
+
id: server.id,
|
|
18
|
+
config: server
|
|
19
|
+
};
|
|
20
|
+
this._add(serverStruct, true, server.id);
|
|
21
|
+
});
|
|
22
|
+
} catch (err) {
|
|
23
|
+
throw new Error(String(err));
|
|
24
|
+
}
|
|
25
|
+
})();
|
|
26
|
+
}
|
|
27
27
|
}
|
|
@@ -3,7 +3,6 @@ import { CMSSubscriptionVersionEnum } from '../constants';
|
|
|
3
3
|
import { APIError, DefaultCMSRestOptions, REST } from '../libs/rest/src';
|
|
4
4
|
import { BaseManager } from './BaseManager';
|
|
5
5
|
import * as globalTypes from '../constants';
|
|
6
|
-
import type { Mutable } from '../constants';
|
|
7
6
|
import { CMSServerManager } from './CMSServerManager';
|
|
8
7
|
|
|
9
8
|
/**
|
|
@@ -25,12 +24,16 @@ export class CMSManager extends BaseManager {
|
|
|
25
24
|
protected async buildManager(instance: Instance) {
|
|
26
25
|
try {
|
|
27
26
|
const versionResp: any = await this.rest?.request('GET_SUB_VERSION');
|
|
28
|
-
const
|
|
29
|
-
mutableThis
|
|
30
|
-
if (
|
|
27
|
+
const version = Number.parseInt(versionResp.replace(/(^\d+)(.+$)/i,'$1'));
|
|
28
|
+
const mutableThis = this as globalTypes.Mutable<CMSManager>;
|
|
29
|
+
if (version >= globalTypes.CMSSubscriptionVersionEnum.STANDARD) {
|
|
31
30
|
this.servers = new CMSServerManager(instance, this);
|
|
32
31
|
}
|
|
33
32
|
mutableThis.ready = true;
|
|
33
|
+
mutableThis.version = version;
|
|
34
|
+
console.log(mutableThis.version);
|
|
35
|
+
console.log(this.version);
|
|
36
|
+
console.log(version);
|
|
34
37
|
instance.isCMSSuccessful = true;
|
|
35
38
|
instance.emit('CMS_SETUP_SUCCESSFUL');
|
|
36
39
|
} catch (err) {
|
|
@@ -95,10 +98,10 @@ export class CMSManager extends BaseManager {
|
|
|
95
98
|
* @param {string} [data.username] The username to find a community account.
|
|
96
99
|
* @returns {Promise} Promise object represents if the request was successful with reason for failure if needed and the account data object if found.
|
|
97
100
|
*/
|
|
98
|
-
public async getComAccount(params: { accId?: string, apiId?: string, username?: string }): Promise<globalTypes.CMSGetComAccountPromiseResult> {
|
|
101
|
+
public async getComAccount(params: { accId?: string, apiId?: string, username?: string, discord?: string }): Promise<globalTypes.CMSGetComAccountPromiseResult> {
|
|
99
102
|
return new Promise(async (resolve, reject) => {
|
|
100
103
|
try {
|
|
101
|
-
const getAccountRequest: any = await this.rest?.request('GET_COM_ACCOUNT', params.apiId, params.username, params.accId);
|
|
104
|
+
const getAccountRequest: any = await this.rest?.request('GET_COM_ACCOUNT', params.apiId, params.username, params.accId, params.discord);
|
|
102
105
|
resolve({ success: true, data: getAccountRequest });
|
|
103
106
|
} catch (err) {
|
|
104
107
|
if (err instanceof APIError) {
|
|
@@ -118,10 +121,10 @@ export class CMSManager extends BaseManager {
|
|
|
118
121
|
* @param {string} [data.username] (Optional) The username to find a community account.
|
|
119
122
|
* @returns {Promise} Promise object represents if the request was successful with reason for failure if needed and the account data object if found.
|
|
120
123
|
*/
|
|
121
|
-
public async getAccountRanks(params: { accId?: string, apiId?: string, username?: string }): Promise<globalTypes.CMSGetAccountRanksPromiseResult> {
|
|
124
|
+
public async getAccountRanks(params: { accId?: string, apiId?: string, username?: string, discord?: string }): Promise<globalTypes.CMSGetAccountRanksPromiseResult> {
|
|
122
125
|
return new Promise(async (resolve, reject) => {
|
|
123
126
|
try {
|
|
124
|
-
const getAccountRanksRequest: any = await this.rest?.request('GET_ACCOUNT_RANKS', params.apiId, params.username, params.accId);
|
|
127
|
+
const getAccountRanksRequest: any = await this.rest?.request('GET_ACCOUNT_RANKS', params.apiId, params.username, params.accId, params.discord);
|
|
125
128
|
resolve({ success: true, data: getAccountRanksRequest });
|
|
126
129
|
} catch (err) {
|
|
127
130
|
if (err instanceof APIError) {
|
|
@@ -144,9 +147,10 @@ export class CMSManager extends BaseManager {
|
|
|
144
147
|
public async clockInOut(data: { accId?: string, apiId?: string, forceClockIn?: boolean }): Promise<globalTypes.CMSClockInOutPromiseResult> {
|
|
145
148
|
return new Promise(async (resolve, reject) => {
|
|
146
149
|
try {
|
|
147
|
-
const clockInOutRequest
|
|
148
|
-
const
|
|
149
|
-
resolve({ success:
|
|
150
|
+
const clockInOutRequest = await this.rest?.request('CLOCK_IN_OUT', data.apiId, data.accId, !!data.forceClockIn);
|
|
151
|
+
const clockInOutResponse = clockInOutRequest as globalTypes.clockInOutRequest;
|
|
152
|
+
if (!clockInOutResponse) resolve({ success: false, reason: clockInOutRequest as string });
|
|
153
|
+
resolve({ success: true, clockedIn: clockInOutResponse.completed });
|
|
150
154
|
} catch (err) {
|
|
151
155
|
if (err instanceof APIError) {
|
|
152
156
|
resolve({ success: false, reason: err.response });
|
|
@@ -176,4 +180,49 @@ export class CMSManager extends BaseManager {
|
|
|
176
180
|
}
|
|
177
181
|
});
|
|
178
182
|
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Gets all department information within the community CMS.
|
|
186
|
+
* @returns {Promise} Promise object represents if the request was successful with reason for failure if needed.
|
|
187
|
+
*/
|
|
188
|
+
public async getDepartments(): Promise<globalTypes.CMSGetDepartmentsPromiseResult> {
|
|
189
|
+
return new Promise(async (resolve, reject) => {
|
|
190
|
+
try {
|
|
191
|
+
const getDepartmentsRequest: any = await this.rest?.request('GET_DEPARTMENTS');
|
|
192
|
+
resolve({ success: true, data: getDepartmentsRequest });
|
|
193
|
+
} catch (err) {
|
|
194
|
+
if (err instanceof APIError) {
|
|
195
|
+
resolve({ success: false, reason: err.response });
|
|
196
|
+
} else {
|
|
197
|
+
reject(err);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Sets a community account's ranks for the CMS community.
|
|
205
|
+
* @param {string} accountId The object that contains critical data to clock in or out an account.
|
|
206
|
+
* @param {Object} changes The object that contains change data for setting account ranks.
|
|
207
|
+
* @param {Object} [changes.set] (Optional) The object that contains primary and secondary data for setting account ranks.
|
|
208
|
+
* @param {string} [changes.set.primary] (Optional) The primary rank ID wanting to set to the account.
|
|
209
|
+
* @param {string} [changes.set.secondary] (Optional) The secondary rank ID(s) wanting to set to the account.
|
|
210
|
+
* @param {Array} [changes.add] (Optional) The secondary rank IDs wanting to add to the account.
|
|
211
|
+
* @param {Array} [changes.remove] (Optional) The secondary rank IDs wanting to remove to the account.
|
|
212
|
+
* @returns {Promise} Promise object represents if the request was successful with reason for failure if needed.
|
|
213
|
+
*/
|
|
214
|
+
public async setAccountRanks(accountId: string, changes: globalTypes.CMSSetAccountRanksChangesObject): Promise<globalTypes.CMSSetAccountRanksPromiseResult> {
|
|
215
|
+
return new Promise(async (resolve, reject) => {
|
|
216
|
+
try {
|
|
217
|
+
const setAccountRanksRequest: any = await this.rest?.request('SET_ACCOUNT_RANKS', accountId, changes.set, changes.add, changes.remove);
|
|
218
|
+
resolve({ success: true, data: setAccountRanksRequest });
|
|
219
|
+
} catch (err) {
|
|
220
|
+
if (err instanceof APIError) {
|
|
221
|
+
resolve({ success: false, reason: err.response });
|
|
222
|
+
} else {
|
|
223
|
+
reject(err);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
179
228
|
}
|
|
@@ -9,6 +9,11 @@ export class CMSServerManager extends CacheManager<number, CMSServer, CMSServerA
|
|
|
9
9
|
super(instance, CMSServer, []);
|
|
10
10
|
|
|
11
11
|
(async () => {
|
|
12
|
+
while(!manager.ready) {
|
|
13
|
+
await new Promise((resolve) => {
|
|
14
|
+
setTimeout(resolve, 100);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
12
17
|
try {
|
|
13
18
|
const serversRes: any = await manager.rest?.request('GET_GAME_SERVERS');
|
|
14
19
|
const servers = serversRes.servers;
|
|
@@ -19,6 +24,7 @@ export class CMSServerManager extends CacheManager<number, CMSServer, CMSServerA
|
|
|
19
24
|
};
|
|
20
25
|
this._add(serverStruct, true, server.id);
|
|
21
26
|
});
|
|
27
|
+
console.log(`Found ${servers.length} servers`);
|
|
22
28
|
} catch (err) {
|
|
23
29
|
throw new Error(String(err));
|
|
24
30
|
}
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import Collection from '@discordjs/collection';
|
|
2
|
-
import { Instance } from '../instance/Instance';
|
|
3
|
-
import { Constructable } from '../constants';
|
|
4
|
-
import { DataManager } from './DataManager';
|
|
5
|
-
|
|
6
|
-
export class CacheManager<K, Holds, R> extends DataManager<K, Holds, R> {
|
|
7
|
-
public _cache: Collection<any, any> | never;
|
|
8
|
-
protected constructor(instance: Instance, holds: Constructable<Holds>, iterable: Iterable<any>) {
|
|
9
|
-
super(instance, holds);
|
|
10
|
-
this._cache = new Collection();
|
|
11
|
-
if (iterable) {
|
|
12
|
-
for (const item of iterable) {
|
|
13
|
-
this._add(item);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get cache() {
|
|
19
|
-
return this._cache;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
_add(data: any, cache: boolean = true, id?: K) {
|
|
23
|
-
const existing = this.cache.get(id ?? data.id);
|
|
24
|
-
if (existing) {
|
|
25
|
-
if (cache) {
|
|
26
|
-
existing._patch(data);
|
|
27
|
-
return existing;
|
|
28
|
-
}
|
|
29
|
-
const clone = existing._clone();
|
|
30
|
-
clone._patch(data);
|
|
31
|
-
return clone;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const entry = data;
|
|
35
|
-
if (cache) this.cache.set(id ?? entry.id, entry);
|
|
36
|
-
return entry;
|
|
37
|
-
}
|
|
1
|
+
import Collection from '@discordjs/collection';
|
|
2
|
+
import { Instance } from '../instance/Instance';
|
|
3
|
+
import { Constructable } from '../constants';
|
|
4
|
+
import { DataManager } from './DataManager';
|
|
5
|
+
|
|
6
|
+
export class CacheManager<K, Holds, R> extends DataManager<K, Holds, R> {
|
|
7
|
+
public _cache: Collection<any, any> | never;
|
|
8
|
+
protected constructor(instance: Instance, holds: Constructable<Holds>, iterable: Iterable<any>) {
|
|
9
|
+
super(instance, holds);
|
|
10
|
+
this._cache = new Collection();
|
|
11
|
+
if (iterable) {
|
|
12
|
+
for (const item of iterable) {
|
|
13
|
+
this._add(item);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
get cache() {
|
|
19
|
+
return this._cache;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
_add(data: any, cache: boolean = true, id?: K) {
|
|
23
|
+
const existing = this.cache.get(id ?? data.id);
|
|
24
|
+
if (existing) {
|
|
25
|
+
if (cache) {
|
|
26
|
+
existing._patch(data);
|
|
27
|
+
return existing;
|
|
28
|
+
}
|
|
29
|
+
const clone = existing._clone();
|
|
30
|
+
clone._patch(data);
|
|
31
|
+
return clone;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const entry = data;
|
|
35
|
+
if (cache) this.cache.set(id ?? entry.id, entry);
|
|
36
|
+
return entry;
|
|
37
|
+
}
|
|
38
38
|
}
|
|
@@ -1,64 +1,64 @@
|
|
|
1
|
-
import { Instance } from '../instance/Instance';
|
|
2
|
-
import { BaseManager } from './BaseManager';
|
|
3
|
-
import { GenericError } from '../errors';
|
|
4
|
-
import { Constructable } from '../constants';
|
|
5
|
-
|
|
6
|
-
import Collection from '@discordjs/collection';
|
|
7
|
-
|
|
8
|
-
interface DataManagerInstanceObject {
|
|
9
|
-
id: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export class DataManager<_K, Holds, _R> extends BaseManager {
|
|
13
|
-
public holds: Constructable<Holds>;
|
|
14
|
-
|
|
15
|
-
constructor(instance: Instance, holds: any) {
|
|
16
|
-
super(instance);
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* The data structure belonging to this manager.
|
|
20
|
-
* @name DataManager#holds
|
|
21
|
-
* @type {Function}
|
|
22
|
-
* @private
|
|
23
|
-
* @readonly
|
|
24
|
-
*/
|
|
25
|
-
this.holds = holds;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* The cache of items for this manager.
|
|
30
|
-
* @type {Collection}
|
|
31
|
-
* @abstract
|
|
32
|
-
*/
|
|
33
|
-
get cache(): Collection<any, any> {
|
|
34
|
-
throw new GenericError('NOT_IMPLEMENTED', 'get cache', this.constructor.name);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Resolves a data entry to a data Object.
|
|
39
|
-
* @param idOrInstance The id or instance of something in this Manager
|
|
40
|
-
* @returns {?Object} An instance from this Manager
|
|
41
|
-
*/
|
|
42
|
-
public resolve(idOrInstance: string | object): object | null {
|
|
43
|
-
if (this.cache instanceof Collection) {
|
|
44
|
-
if (typeof idOrInstance === 'object') return idOrInstance;
|
|
45
|
-
if (typeof idOrInstance === 'string') return this.cache.get(idOrInstance) ?? null;
|
|
46
|
-
}
|
|
47
|
-
return null;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Resolves a data entry to an instance id.
|
|
52
|
-
* @param {string|Object} idOrInstance The id or instance of something in this Manager
|
|
53
|
-
* @returns {?Snowflake}
|
|
54
|
-
*/
|
|
55
|
-
resolveId(idOrInstance: string | DataManagerInstanceObject): string | null {
|
|
56
|
-
if (typeof idOrInstance === 'object') return idOrInstance.id;
|
|
57
|
-
if (typeof idOrInstance === 'string') return idOrInstance;
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
valueOf() {
|
|
62
|
-
return this.cache;
|
|
63
|
-
}
|
|
1
|
+
import { Instance } from '../instance/Instance';
|
|
2
|
+
import { BaseManager } from './BaseManager';
|
|
3
|
+
import { GenericError } from '../errors';
|
|
4
|
+
import { Constructable } from '../constants';
|
|
5
|
+
|
|
6
|
+
import Collection from '@discordjs/collection';
|
|
7
|
+
|
|
8
|
+
interface DataManagerInstanceObject {
|
|
9
|
+
id: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class DataManager<_K, Holds, _R> extends BaseManager {
|
|
13
|
+
public holds: Constructable<Holds>;
|
|
14
|
+
|
|
15
|
+
constructor(instance: Instance, holds: any) {
|
|
16
|
+
super(instance);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The data structure belonging to this manager.
|
|
20
|
+
* @name DataManager#holds
|
|
21
|
+
* @type {Function}
|
|
22
|
+
* @private
|
|
23
|
+
* @readonly
|
|
24
|
+
*/
|
|
25
|
+
this.holds = holds;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The cache of items for this manager.
|
|
30
|
+
* @type {Collection}
|
|
31
|
+
* @abstract
|
|
32
|
+
*/
|
|
33
|
+
get cache(): Collection<any, any> {
|
|
34
|
+
throw new GenericError('NOT_IMPLEMENTED', 'get cache', this.constructor.name);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Resolves a data entry to a data Object.
|
|
39
|
+
* @param idOrInstance The id or instance of something in this Manager
|
|
40
|
+
* @returns {?Object} An instance from this Manager
|
|
41
|
+
*/
|
|
42
|
+
public resolve(idOrInstance: string | object): object | null {
|
|
43
|
+
if (this.cache instanceof Collection) {
|
|
44
|
+
if (typeof idOrInstance === 'object') return idOrInstance;
|
|
45
|
+
if (typeof idOrInstance === 'string') return this.cache.get(idOrInstance) ?? null;
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Resolves a data entry to an instance id.
|
|
52
|
+
* @param {string|Object} idOrInstance The id or instance of something in this Manager
|
|
53
|
+
* @returns {?Snowflake}
|
|
54
|
+
*/
|
|
55
|
+
resolveId(idOrInstance: string | DataManagerInstanceObject): string | null {
|
|
56
|
+
if (typeof idOrInstance === 'object') return idOrInstance.id;
|
|
57
|
+
if (typeof idOrInstance === 'string') return idOrInstance;
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
valueOf() {
|
|
62
|
+
return this.cache;
|
|
63
|
+
}
|
|
64
64
|
}
|
package/src/structures/Base.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { Instance } from '../instance/Instance';
|
|
2
|
-
import { flatten } from '../utils';
|
|
3
|
-
|
|
4
|
-
export abstract class Base {
|
|
5
|
-
public instance: Instance;
|
|
6
|
-
constructor(instance: Instance) {
|
|
7
|
-
this.instance = instance;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
public _clone() {
|
|
11
|
-
return Object.assign(Object.create(this), this);
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public _patch(data: any) {
|
|
16
|
-
return data;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public _update(data: any) {
|
|
20
|
-
const clone = this._clone();
|
|
21
|
-
this._patch(data);
|
|
22
|
-
return clone;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public toJSON(...props: Record<string, boolean | string>[]): unknown {
|
|
26
|
-
return flatten(this, ...props);
|
|
27
|
-
}
|
|
1
|
+
import { Instance } from '../instance/Instance';
|
|
2
|
+
import { flatten } from '../utils';
|
|
3
|
+
|
|
4
|
+
export abstract class Base {
|
|
5
|
+
public instance: Instance;
|
|
6
|
+
constructor(instance: Instance) {
|
|
7
|
+
this.instance = instance;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
public _clone() {
|
|
11
|
+
return Object.assign(Object.create(this), this);
|
|
12
|
+
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public _patch(data: any) {
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public _update(data: any) {
|
|
20
|
+
const clone = this._clone();
|
|
21
|
+
this._patch(data);
|
|
22
|
+
return clone;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public toJSON(...props: Record<string, boolean | string>[]): unknown {
|
|
26
|
+
return flatten(this, ...props);
|
|
27
|
+
}
|
|
28
28
|
}
|