@sonoransoftware/sonoran.js 1.0.14 → 1.0.16

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.
@@ -1,4 +1,4 @@
1
- import Collection from '@discordjs/collection';
1
+ import { Collection } from '@discordjs/collection';
2
2
  import { CADDispatchOriginEnums, CADDispatchStatusEnums } from './libs/rest/src';
3
3
  import { DataManager } from './managers/DataManager';
4
4
  import { CADActiveUnitsManager } from './managers/CADActiveUnitsManager';
@@ -23,16 +23,16 @@ export interface CADNewDispatchBuilderOptions {
23
23
  metaData?: Record<string, string>;
24
24
  units?: string[];
25
25
  }
26
- export declare type Constructable<T> = abstract new (...args: any[]) => T;
26
+ export type Constructable<T> = abstract new (...args: any[]) => T;
27
27
  export interface Caches {
28
28
  CADActiveUnitsManager: [manager: typeof CADActiveUnitsManager, holds: CADActiveUnit];
29
29
  }
30
- export declare type CacheConstructors = {
30
+ export type CacheConstructors = {
31
31
  [K in keyof Caches]: Caches[K][0] & {
32
32
  name: K;
33
33
  };
34
34
  };
35
- export declare type CacheFactory = (manager: CacheConstructors[keyof Caches], holds: Caches[typeof manager['name']][1]) => typeof manager['prototype'] extends DataManager<infer K, infer V, any> ? Collection<K, V> : never;
35
+ export type CacheFactory = (manager: CacheConstructors[keyof Caches], holds: Caches[typeof manager['name']][1]) => typeof manager['prototype'] extends DataManager<infer K, infer V, any> ? Collection<K, V> : never;
36
36
  export interface CADActiveUnitFetchOptions {
37
37
  id?: number | number[];
38
38
  accId?: string | string[];
@@ -54,7 +54,7 @@ export declare enum CMSSubscriptionVersionEnum {
54
54
  PRO = 4,
55
55
  ONE = 6
56
56
  }
57
- export declare type Mutable<T> = {
57
+ export type Mutable<T> = {
58
58
  -readonly [k in keyof T]: T[k];
59
59
  };
60
60
  export interface CMSVerifyWhitelistPromiseResult {
@@ -17,6 +17,7 @@ export declare class Instance extends EventEmitter {
17
17
  cad: CADManager | undefined;
18
18
  cms: CMSManager | undefined;
19
19
  debug: boolean;
20
+ apiHeaders: HeadersInit;
20
21
  constructor(options: InstanceTypes.InstanceOptions);
21
22
  private initialize;
22
23
  _debugLog(message: string): void;
@@ -42,9 +42,13 @@ class Instance extends events_1.default {
42
42
  this.cmsDefaultServerId = 1;
43
43
  this.isCMSSuccessful = false;
44
44
  this.debug = false;
45
+ this.apiHeaders = {};
45
46
  if (options.debug) {
46
47
  this.debug = options.debug;
47
48
  }
49
+ if (Object.prototype.hasOwnProperty.call(options, 'apiHeaders') && options.apiHeaders !== undefined) {
50
+ this.apiHeaders = options.apiHeaders;
51
+ }
48
52
  if (Object.prototype.hasOwnProperty.call(options, 'apiKey') && Object.prototype.hasOwnProperty.call(options, 'communityId')) {
49
53
  if (Object.prototype.hasOwnProperty.call(options, 'product')) {
50
54
  switch (options.product) {
@@ -1,5 +1,5 @@
1
1
  import * as globalTypes from '../constants';
2
- export declare type InstanceOptions = {
2
+ export type InstanceOptions = {
3
3
  communityId?: string;
4
4
  apiKey?: string;
5
5
  product?: globalTypes.productEnums;
@@ -13,4 +13,5 @@ export declare type InstanceOptions = {
13
13
  cmsApiUrl?: string;
14
14
  cmsDefaultServerId?: number;
15
15
  debug?: boolean;
16
+ apiHeaders?: HeadersInit;
16
17
  };
@@ -1,4 +1,6 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
2
4
  import { EventEmitter } from 'events';
3
5
  import { RequestData, RequestManager } from './RequestManager';
4
6
  import { AllAPITypesType, RESTTypedAPIDataStructs } from './utils/constants';
@@ -84,7 +86,7 @@ export interface REST {
84
86
  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
87
  removeAllListeners: (<K extends keyof RestEvents>(event?: K) => this) & (<S extends string | symbol>(event?: Exclude<S, keyof RestEvents>) => this);
86
88
  }
87
- export declare type RestManagerTypes = CADManager | CMSManager;
89
+ export type RestManagerTypes = CADManager | CMSManager;
88
90
  export declare class REST extends EventEmitter {
89
91
  readonly requestManager: RequestManager;
90
92
  readonly instance: Instance;
@@ -12,15 +12,15 @@ class REST extends events_1.EventEmitter {
12
12
  this.instance = _instance;
13
13
  this.manager = _manager;
14
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 */));
15
+ .on("restDebug" /* RESTEvents.Debug */, this.emit.bind(this, "restDebug" /* RESTEvents.Debug */))
16
+ .on("rateLimited" /* RESTEvents.RateLimited */, this.emit.bind(this, "rateLimited" /* RESTEvents.RateLimited */))
17
+ .on("invalidRequestWarning" /* RESTEvents.InvalidRequestWarning */, this.emit.bind(this, "invalidRequestWarning" /* RESTEvents.InvalidRequestWarning */));
18
18
  this.on('newListener', (name, listener) => {
19
- if (name === "request" /* Request */ || name === "response" /* Response */)
19
+ if (name === "request" /* RESTEvents.Request */ || name === "response" /* RESTEvents.Response */)
20
20
  this.requestManager.on(name, listener);
21
21
  });
22
22
  this.on('removeListener', (name, listener) => {
23
- if (name === "request" /* Request */ || name === "response" /* Response */)
23
+ if (name === "request" /* RESTEvents.Request */ || name === "response" /* RESTEvents.Response */)
24
24
  this.requestManager.off(name, listener);
25
25
  });
26
26
  }
@@ -1,11 +1,11 @@
1
1
  /// <reference types="node" />
2
- import Collection from '@discordjs/collection';
2
+ import { Collection } from '@discordjs/collection';
3
3
  import { EventEmitter } from 'events';
4
4
  import type { Instance } from '../../../../instance/Instance';
5
5
  import { RESTOptions, RateLimitData, RestEvents } from './REST';
6
6
  import { productEnums } from '../../../../constants';
7
7
  import { IHandler } from './handlers/IHandler';
8
- export declare type RouteLike = `/${string}`;
8
+ export type RouteLike = `/${string}`;
9
9
  export declare const enum RequestMethod {
10
10
  Delete = "delete",
11
11
  Get = "get",
@@ -13,7 +13,7 @@ export declare const enum RequestMethod {
13
13
  Post = "post",
14
14
  Put = "put"
15
15
  }
16
- export declare type ReqDataType = Array<unknown> | unknown;
16
+ export type ReqDataType = Array<unknown> | unknown;
17
17
  export interface RequestData {
18
18
  id: string;
19
19
  key: string;
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.RequestManager = void 0;
7
- const collection_1 = __importDefault(require("@discordjs/collection"));
4
+ const collection_1 = require("@discordjs/collection");
8
5
  // import { DiscordSnowflake } from '@sapphire/snowflake';
9
6
  const events_1 = require("events");
10
7
  const constants_1 = require("./utils/constants");
@@ -14,8 +11,8 @@ const utils_1 = require("../../../../utils/utils");
14
11
  class RequestManager extends events_1.EventEmitter {
15
12
  constructor(_instance, _product, options) {
16
13
  super();
17
- this.ratelimitedTypes = new collection_1.default();
18
- this.handlers = new collection_1.default();
14
+ this.ratelimitedTypes = new collection_1.Collection();
15
+ this.handlers = new collection_1.Collection();
19
16
  this.product = _product;
20
17
  this.instance = _instance;
21
18
  switch (_product) {
@@ -182,7 +179,8 @@ class RequestManager extends events_1.EventEmitter {
182
179
  apiData.fetchOptions.body = JSON.stringify(apiData.data);
183
180
  apiData.fetchOptions.headers = {
184
181
  'Accept': 'application/json',
185
- 'Content-Type': 'application/json'
182
+ 'Content-Type': 'application/json',
183
+ ...instance.apiHeaders
186
184
  };
187
185
  return apiData;
188
186
  }
@@ -33,7 +33,7 @@ export declare const GeneralCMSAPITypes: APITypeData[];
33
33
  export declare const ServersCMSAPITypes: APITypeData[];
34
34
  export declare const EventsCMSAPITypes: APITypeData[];
35
35
  export declare const AllAPITypes: AllAPITypeData[];
36
- export declare type AllAPITypesType = 'GET_SERVERS' | 'SET_SERVERS' | 'GET_VERSION' | 'SET_PENAL_CODES' | 'SET_API_ID' | 'GET_TEMPLATES' | 'NEW_RECORD' | 'EDIT_RECORD' | 'REMOVE_RECORD' | 'LOOKUP_INT' | 'LOOKUP' | 'GET_ACCOUNT' | 'CHECK_APIID' | 'APPLY_PERMISSION_KEY' | 'SET_ACCOUNT_PERMISSIONS' | 'BAN_USER' | 'VERIFY_SECRET' | 'AUTH_STREETSIGNS' | 'SET_POSTALS' | 'SEND_PHOTO' | 'GET_CHARACTERS' | 'NEW_CHARACTER' | 'EDIT_CHARACTER' | 'REMOVE_CHARACTER' | 'GET_IDENTIFIERS' | 'MODIFY_IDENTIFIER' | 'SET_IDENTIFIER' | 'UNIT_PANIC' | 'UNIT_STATUS' | 'GET_BLIPS' | 'ADD_BLIP' | 'MODIFY_BLIP' | 'REMOVE_BLIP' | '911_CALL' | 'REMOVE_911' | 'GET_CALLS' | 'GET_ACTIVE_UNITS' | 'KICK_UNIT' | 'NEW_DISPATCH' | 'ATTACH_UNIT' | 'DETACH_UNIT' | 'SET_CALL_POSTAL' | 'SET_CALL_PRIMARY' | 'ADD_CALL_NOTE' | 'CLOSE_CALL' | 'UNIT_LOCATION' | 'SET_STREETSIGN_CONFIG' | 'UPDATE_STREETSIGN' | 'GET_COM_ACCOUNT' | 'GET_DEPARTMENTS' | 'GET_SUB_VERSION' | 'CHECK_COM_APIID' | 'VERIFY_WHITELIST' | 'CLOCK_IN_OUT' | 'FULL_WHITELIST' | 'GET_ACCOUNT_RANKS' | 'SET_ACCOUNT_RANKS' | 'RSVP';
36
+ export type AllAPITypesType = 'GET_SERVERS' | 'SET_SERVERS' | 'GET_VERSION' | 'SET_PENAL_CODES' | 'SET_API_ID' | 'GET_TEMPLATES' | 'NEW_RECORD' | 'EDIT_RECORD' | 'REMOVE_RECORD' | 'LOOKUP_INT' | 'LOOKUP' | 'GET_ACCOUNT' | 'CHECK_APIID' | 'APPLY_PERMISSION_KEY' | 'SET_ACCOUNT_PERMISSIONS' | 'BAN_USER' | 'VERIFY_SECRET' | 'AUTH_STREETSIGNS' | 'SET_POSTALS' | 'SEND_PHOTO' | 'GET_CHARACTERS' | 'NEW_CHARACTER' | 'EDIT_CHARACTER' | 'REMOVE_CHARACTER' | 'GET_IDENTIFIERS' | 'MODIFY_IDENTIFIER' | 'SET_IDENTIFIER' | 'UNIT_PANIC' | 'UNIT_STATUS' | 'GET_BLIPS' | 'ADD_BLIP' | 'MODIFY_BLIP' | 'REMOVE_BLIP' | '911_CALL' | 'REMOVE_911' | 'GET_CALLS' | 'GET_ACTIVE_UNITS' | 'KICK_UNIT' | 'NEW_DISPATCH' | 'ATTACH_UNIT' | 'DETACH_UNIT' | 'SET_CALL_POSTAL' | 'SET_CALL_PRIMARY' | 'ADD_CALL_NOTE' | 'CLOSE_CALL' | 'UNIT_LOCATION' | 'SET_STREETSIGN_CONFIG' | 'UPDATE_STREETSIGN' | 'GET_COM_ACCOUNT' | 'GET_DEPARTMENTS' | 'GET_SUB_VERSION' | 'CHECK_COM_APIID' | 'VERIFY_WHITELIST' | 'CLOCK_IN_OUT' | 'FULL_WHITELIST' | 'GET_ACCOUNT_RANKS' | 'SET_ACCOUNT_RANKS' | 'RSVP';
37
37
  export interface CMSServerAPIStruct {
38
38
  id: number;
39
39
  name: string;
@@ -428,7 +428,7 @@ export interface RESTTypedAPIDataStructs {
428
428
  accId: string | undefined
429
429
  ];
430
430
  }
431
- export declare type PossibleRequestData = undefined | {
431
+ export type PossibleRequestData = undefined | {
432
432
  data: CADPenalCodeStruct[] | CADSetAPIIDStruct | CADNewEditRecordOptionOneStruct | CADNewEditRecordOptionTwoStruct | CADLookupByIntStruct | CADLookupStruct | CADModifyAccountPermsStruct | CADKickBanUserStruct | CADSetPostalStruct[] | CADModifyIdentifierStruct | CADAddBlipStruct[] | CADModifyBlipStruct[] | CADGetCallsStruct | CADGetActiveUnitsStruct | CADNewDispatchStruct;
433
433
  } | {
434
434
  servers: CADServerAPIStruct[];
@@ -1,4 +1,4 @@
1
- import Collection from '@discordjs/collection';
1
+ import { Collection } from '@discordjs/collection';
2
2
  import { Instance } from '../instance/Instance';
3
3
  import { Constructable } from '../constants';
4
4
  import { DataManager } from './DataManager';
@@ -1,15 +1,12 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.CacheManager = void 0;
7
- const collection_1 = __importDefault(require("@discordjs/collection"));
4
+ const collection_1 = require("@discordjs/collection");
8
5
  const DataManager_1 = require("./DataManager");
9
6
  class CacheManager extends DataManager_1.DataManager {
10
7
  constructor(instance, holds, iterable) {
11
8
  super(instance, holds);
12
- this._cache = new collection_1.default();
9
+ this._cache = new collection_1.Collection();
13
10
  if (iterable) {
14
11
  for (const item of iterable) {
15
12
  this._add(item);
@@ -1,7 +1,7 @@
1
1
  import { Instance } from '../instance/Instance';
2
2
  import { BaseManager } from './BaseManager';
3
3
  import { Constructable } from '../constants';
4
- import Collection from '@discordjs/collection';
4
+ import { Collection } from '@discordjs/collection';
5
5
  interface DataManagerInstanceObject {
6
6
  id: string;
7
7
  }
@@ -1,12 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.DataManager = void 0;
7
4
  const BaseManager_1 = require("./BaseManager");
8
5
  const errors_1 = require("../errors");
9
- const collection_1 = __importDefault(require("@discordjs/collection"));
6
+ const collection_1 = require("@discordjs/collection");
10
7
  class DataManager extends BaseManager_1.BaseManager {
11
8
  constructor(instance, holds) {
12
9
  super(instance);
@@ -34,7 +31,7 @@ class DataManager extends BaseManager_1.BaseManager {
34
31
  */
35
32
  resolve(idOrInstance) {
36
33
  var _a;
37
- if (this.cache instanceof collection_1.default) {
34
+ if (this.cache instanceof collection_1.Collection) {
38
35
  if (typeof idOrInstance === 'object')
39
36
  return idOrInstance;
40
37
  if (typeof idOrInstance === 'string')
@@ -31,7 +31,7 @@ export interface CADActiveUnitDataStruct {
31
31
  rank: string;
32
32
  group: string;
33
33
  }
34
- export declare type CADActiveUnitResolvable = CADActiveUnit | number;
34
+ export type CADActiveUnitResolvable = CADActiveUnit | number;
35
35
  export declare class CADActiveUnit extends Base {
36
36
  id: number;
37
37
  accId: string;
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.debugLog = exports.errorLog = exports.infoLog = exports.warnLog = exports.flatten = exports.cloneObject = exports.mergeDefault = void 0;
7
- const collection_1 = __importDefault(require("@discordjs/collection"));
4
+ const collection_1 = require("@discordjs/collection");
8
5
  const isObject = (d) => typeof d === 'object' && d !== null;
9
6
  function mergeDefault(def, given) {
10
7
  if (!given)
@@ -46,10 +43,10 @@ function flatten(obj, ...props) {
46
43
  const elemIsObj = isObject(element);
47
44
  const valueOf = elemIsObj && typeof element.valueOf === 'function' ? element.valueOf() : null;
48
45
  // If it's a Collection, make the array of keys
49
- if (element instanceof collection_1.default)
46
+ if (element instanceof collection_1.Collection)
50
47
  out[newProp] = Array.from(element.keys());
51
48
  // If the valueOf is a Collection, use its array of keys
52
- else if (valueOf instanceof collection_1.default)
49
+ else if (valueOf instanceof collection_1.Collection)
53
50
  out[newProp] = Array.from(valueOf.keys());
54
51
  // If it's an array, flatten each element
55
52
  else if (Array.isArray(element))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sonoransoftware/sonoran.js",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "description": "Sonoran.js is a library that allows you to interact with the Sonoran CAD and Sonoran CMS API. Based off of and utilizes several Discord.js library techniques for ease of use.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -37,8 +37,8 @@
37
37
  "ext": "*.ts, *.json"
38
38
  },
39
39
  "dependencies": {
40
- "@discordjs/collection": "^0.5.0",
41
- "@sapphire/async-queue": "^1.3.0",
40
+ "@discordjs/collection": "^1.5.0",
41
+ "@sapphire/async-queue": "^1.5.0",
42
42
  "@sapphire/snowflake": "^3.2.0",
43
43
  "events": "^3.3.0",
44
44
  "node-abort-controller": "^3.0.1",
package/src/constants.ts CHANGED
@@ -1,4 +1,4 @@
1
- import Collection from '@discordjs/collection';
1
+ import { Collection } from '@discordjs/collection';
2
2
  import { CADDispatchOriginEnums, CADDispatchStatusEnums } from './libs/rest/src';
3
3
  import { DataManager } from './managers/DataManager';
4
4
  import { CADActiveUnitsManager } from './managers/CADActiveUnitsManager';
@@ -1,118 +1,122 @@
1
- import EventEmitter from 'events';
2
-
3
- import * as globalTypes from '../constants';
4
- import * as InstanceTypes from './instance.types';
5
- import { CADManager } from '../managers/CADManager';
6
- import { CMSManager } from '../managers/CMSManager';
7
- import { debugLog } from '../utils';
8
-
9
- export class Instance extends EventEmitter {
10
- public cadCommunityId: string | undefined;
11
- public cadApiKey: string | undefined;
12
- public cadApiUrl: string = 'https://api.sonorancad.com';
13
- public cadDefaultServerId: number = 1;
14
- public isCADSuccessful: boolean = false;
15
- public cmsCommunityId: string | undefined;
16
- public cmsApiKey: string | undefined;
17
- public cmsApiUrl: string = 'https://api.sonorancms.com';
18
- public cmsDefaultServerId: number = 1;
19
- public isCMSSuccessful: boolean = false;
20
-
21
- public cad: CADManager | undefined;
22
- public cms: CMSManager | undefined;
23
-
24
- public debug: boolean = false;
25
-
26
- constructor(options: InstanceTypes.InstanceOptions) {
27
- super({ captureRejections: true });
28
- if (options.debug) {
29
- this.debug = options.debug;
30
- }
31
- if (Object.prototype.hasOwnProperty.call(options, 'apiKey') && Object.prototype.hasOwnProperty.call(options, 'communityId')) {
32
- if (Object.prototype.hasOwnProperty.call(options, 'product')) {
33
- switch (options.product) {
34
- case globalTypes.productEnums.CAD: {
35
- this.cadCommunityId = options.communityId;
36
- this.cadApiKey = options.apiKey;
37
- if (options.serverId !== undefined) {
38
- this._debugLog(`Overriding default server id... ${options.serverId}`);
39
- this.cadDefaultServerId = options.serverId;
40
- }
41
- if (Object.prototype.hasOwnProperty.call(options, 'cadApiUrl') && typeof options.cadApiUrl === 'string') {
42
- this._debugLog(`Overriding CAD API Url... ${options.cadApiUrl}`);
43
- this.cadApiUrl = options.cadApiUrl;
44
- }
45
- this._debugLog('About to initialize instance.');
46
- this.initialize();
47
- break;
48
- }
49
- case globalTypes.productEnums.CMS: {
50
- this.cmsCommunityId = options.communityId;
51
- this.cmsApiKey = options.apiKey;
52
- if (options.serverId !== undefined) {
53
- this._debugLog(`Overriding default server id... ${options.serverId}`);
54
- this.cmsDefaultServerId = options.serverId;
55
- }
56
- if (Object.prototype.hasOwnProperty.call(options, 'cmsApiUrl') && typeof options.cmsApiUrl === 'string') {
57
- this._debugLog(`Overriding CMS API URL... ${options.cmsApiUrl}`);
58
- this.cmsApiUrl = options.cmsApiUrl;
59
- }
60
- this.initialize();
61
- break;
62
- }
63
- default: {
64
- throw new Error('Invalid product enum given for constructor.');
65
- }
66
- }
67
- } else {
68
- throw new Error('No product enum given when instancing.');
69
- }
70
- } else {
71
- this.cadCommunityId = options.cadCommunityId;
72
- this.cadApiKey = options.cadApiKey;
73
- this.cmsCommunityId = options.cmsCommunityId;
74
- this.cmsApiKey = options.cmsApiKey;
75
-
76
- if (options.cadDefaultServerId !== undefined) {
77
- this._debugLog(`Overriding default CAD server id... ${options.serverId}`);
78
- this.cadDefaultServerId = options.cadDefaultServerId;
79
- }
80
- if (options.cmsDefaultServerId !== undefined) {
81
- this._debugLog(`Overriding default CMS server id... ${options.serverId}`);
82
- this.cmsDefaultServerId = options.cmsDefaultServerId;
83
- }
84
- if (Object.prototype.hasOwnProperty.call(options, 'cadApiUrl') && typeof options.cadApiUrl === 'string') {
85
- this._debugLog(`Overriding CAD API Url... ${options.cadApiUrl}`);
86
- this.cadApiUrl = options.cadApiUrl;
87
- }
88
- if (Object.prototype.hasOwnProperty.call(options, 'cmsApiUrl') && typeof options.cmsApiUrl === 'string') {
89
- this._debugLog(`Overriding CMS API URL... ${options.cmsApiUrl}`);
90
- this.cmsApiUrl = options.cmsApiUrl;
91
- }
92
- this.initialize();
93
- }
94
- }
95
-
96
-
97
-
98
- private initialize() {
99
- if (this.cadCommunityId && this.cadApiKey && this.cadApiUrl) {
100
- this._debugLog('About to initialize CAD Manager');
101
- this.cad = new CADManager(this);
102
- } else {
103
- this._debugLog('Not initializing CAD Manager due to a missing community id, api key, or api url.');
104
- }
105
- if (this.cmsCommunityId && this.cmsApiKey && this.cmsApiUrl) {
106
- this._debugLog('About to initialize CMS Manager');
107
- this.cms = new CMSManager(this);
108
- } else {
109
- this._debugLog('Not initializing CMS Manager due to a missing community id, api key, or api url.');
110
- }
111
- }
112
-
113
- public _debugLog(message: string): void {
114
- if (this.debug) {
115
- debugLog(message);
116
- }
117
- }
1
+ import EventEmitter from 'events';
2
+
3
+ import * as globalTypes from '../constants';
4
+ import * as InstanceTypes from './instance.types';
5
+ import { CADManager } from '../managers/CADManager';
6
+ import { CMSManager } from '../managers/CMSManager';
7
+ import { debugLog } from '../utils';
8
+
9
+ export class Instance extends EventEmitter {
10
+ public cadCommunityId: string | undefined;
11
+ public cadApiKey: string | undefined;
12
+ public cadApiUrl: string = 'https://api.sonorancad.com';
13
+ public cadDefaultServerId: number = 1;
14
+ public isCADSuccessful: boolean = false;
15
+ public cmsCommunityId: string | undefined;
16
+ public cmsApiKey: string | undefined;
17
+ public cmsApiUrl: string = 'https://api.sonorancms.com';
18
+ public cmsDefaultServerId: number = 1;
19
+ public isCMSSuccessful: boolean = false;
20
+
21
+ public cad: CADManager | undefined;
22
+ public cms: CMSManager | undefined;
23
+
24
+ public debug: boolean = false;
25
+ public apiHeaders: HeadersInit = {};
26
+
27
+ constructor(options: InstanceTypes.InstanceOptions) {
28
+ super({ captureRejections: true });
29
+ if (options.debug) {
30
+ this.debug = options.debug;
31
+ }
32
+ if (Object.prototype.hasOwnProperty.call(options, 'apiHeaders') && options.apiHeaders !== undefined) {
33
+ this.apiHeaders = options.apiHeaders;
34
+ }
35
+ if (Object.prototype.hasOwnProperty.call(options, 'apiKey') && Object.prototype.hasOwnProperty.call(options, 'communityId')) {
36
+ if (Object.prototype.hasOwnProperty.call(options, 'product')) {
37
+ switch (options.product) {
38
+ case globalTypes.productEnums.CAD: {
39
+ this.cadCommunityId = options.communityId;
40
+ this.cadApiKey = options.apiKey;
41
+ if (options.serverId !== undefined) {
42
+ this._debugLog(`Overriding default server id... ${options.serverId}`);
43
+ this.cadDefaultServerId = options.serverId;
44
+ }
45
+ if (Object.prototype.hasOwnProperty.call(options, 'cadApiUrl') && typeof options.cadApiUrl === 'string') {
46
+ this._debugLog(`Overriding CAD API Url... ${options.cadApiUrl}`);
47
+ this.cadApiUrl = options.cadApiUrl;
48
+ }
49
+ this._debugLog('About to initialize instance.');
50
+ this.initialize();
51
+ break;
52
+ }
53
+ case globalTypes.productEnums.CMS: {
54
+ this.cmsCommunityId = options.communityId;
55
+ this.cmsApiKey = options.apiKey;
56
+ if (options.serverId !== undefined) {
57
+ this._debugLog(`Overriding default server id... ${options.serverId}`);
58
+ this.cmsDefaultServerId = options.serverId;
59
+ }
60
+ if (Object.prototype.hasOwnProperty.call(options, 'cmsApiUrl') && typeof options.cmsApiUrl === 'string') {
61
+ this._debugLog(`Overriding CMS API URL... ${options.cmsApiUrl}`);
62
+ this.cmsApiUrl = options.cmsApiUrl;
63
+ }
64
+ this.initialize();
65
+ break;
66
+ }
67
+ default: {
68
+ throw new Error('Invalid product enum given for constructor.');
69
+ }
70
+ }
71
+ } else {
72
+ throw new Error('No product enum given when instancing.');
73
+ }
74
+ } else {
75
+ this.cadCommunityId = options.cadCommunityId;
76
+ this.cadApiKey = options.cadApiKey;
77
+ this.cmsCommunityId = options.cmsCommunityId;
78
+ this.cmsApiKey = options.cmsApiKey;
79
+
80
+ if (options.cadDefaultServerId !== undefined) {
81
+ this._debugLog(`Overriding default CAD server id... ${options.serverId}`);
82
+ this.cadDefaultServerId = options.cadDefaultServerId;
83
+ }
84
+ if (options.cmsDefaultServerId !== undefined) {
85
+ this._debugLog(`Overriding default CMS server id... ${options.serverId}`);
86
+ this.cmsDefaultServerId = options.cmsDefaultServerId;
87
+ }
88
+ if (Object.prototype.hasOwnProperty.call(options, 'cadApiUrl') && typeof options.cadApiUrl === 'string') {
89
+ this._debugLog(`Overriding CAD API Url... ${options.cadApiUrl}`);
90
+ this.cadApiUrl = options.cadApiUrl;
91
+ }
92
+ if (Object.prototype.hasOwnProperty.call(options, 'cmsApiUrl') && typeof options.cmsApiUrl === 'string') {
93
+ this._debugLog(`Overriding CMS API URL... ${options.cmsApiUrl}`);
94
+ this.cmsApiUrl = options.cmsApiUrl;
95
+ }
96
+ this.initialize();
97
+ }
98
+ }
99
+
100
+
101
+
102
+ private initialize() {
103
+ if (this.cadCommunityId && this.cadApiKey && this.cadApiUrl) {
104
+ this._debugLog('About to initialize CAD Manager');
105
+ this.cad = new CADManager(this);
106
+ } else {
107
+ this._debugLog('Not initializing CAD Manager due to a missing community id, api key, or api url.');
108
+ }
109
+ if (this.cmsCommunityId && this.cmsApiKey && this.cmsApiUrl) {
110
+ this._debugLog('About to initialize CMS Manager');
111
+ this.cms = new CMSManager(this);
112
+ } else {
113
+ this._debugLog('Not initializing CMS Manager due to a missing community id, api key, or api url.');
114
+ }
115
+ }
116
+
117
+ public _debugLog(message: string): void {
118
+ if (this.debug) {
119
+ debugLog(message);
120
+ }
121
+ }
118
122
  }
@@ -1,17 +1,18 @@
1
- import * as globalTypes from '../constants';
2
-
3
- export type InstanceOptions = {
4
- communityId?: string;
5
- apiKey?: string;
6
- product?: globalTypes.productEnums;
7
- serverId?: number;
8
- cadCommunityId?: string;
9
- cadApiKey?: string;
10
- cadApiUrl?: string;
11
- cadDefaultServerId?: number;
12
- cmsCommunityId?: string;
13
- cmsApiKey?: string;
14
- cmsApiUrl?: string;
15
- cmsDefaultServerId?: number;
16
- debug?: boolean;
1
+ import * as globalTypes from '../constants';
2
+
3
+ export type InstanceOptions = {
4
+ communityId?: string;
5
+ apiKey?: string;
6
+ product?: globalTypes.productEnums;
7
+ serverId?: number;
8
+ cadCommunityId?: string;
9
+ cadApiKey?: string;
10
+ cadApiUrl?: string;
11
+ cadDefaultServerId?: number;
12
+ cmsCommunityId?: string;
13
+ cmsApiKey?: string;
14
+ cmsApiUrl?: string;
15
+ cmsDefaultServerId?: number;
16
+ debug?: boolean;
17
+ apiHeaders?: HeadersInit ;
17
18
  };
@@ -1,4 +1,4 @@
1
- import Collection from '@discordjs/collection';
1
+ import { Collection } from '@discordjs/collection';
2
2
  // import { DiscordSnowflake } from '@sapphire/snowflake';
3
3
  import { EventEmitter } from 'events';
4
4
  // import type { RequestInit, BodyInit } from 'node-fetch';
@@ -248,7 +248,8 @@ export class RequestManager extends EventEmitter {
248
248
  apiData.fetchOptions.body = JSON.stringify(apiData.data);
249
249
  apiData.fetchOptions.headers = {
250
250
  'Accept': 'application/json',
251
- 'Content-Type': 'application/json'
251
+ 'Content-Type': 'application/json',
252
+ ...instance.apiHeaders
252
253
  };
253
254
 
254
255
  return apiData;
@@ -1,4 +1,4 @@
1
- import Collection from '@discordjs/collection';
1
+ import { Collection } from '@discordjs/collection';
2
2
  import { Instance } from '../instance/Instance';
3
3
  import { Constructable } from '../constants';
4
4
  import { DataManager } from './DataManager';
@@ -3,7 +3,7 @@ import { BaseManager } from './BaseManager';
3
3
  import { GenericError } from '../errors';
4
4
  import { Constructable } from '../constants';
5
5
 
6
- import Collection from '@discordjs/collection';
6
+ import { Collection } from '@discordjs/collection';
7
7
 
8
8
  interface DataManagerInstanceObject {
9
9
  id: string;
@@ -39,7 +39,7 @@ export class DataManager<_K, Holds, _R> extends BaseManager {
39
39
  * @param idOrInstance The id or instance of something in this Manager
40
40
  * @returns {?Object} An instance from this Manager
41
41
  */
42
- public resolve(idOrInstance: string | object): object | null {
42
+ public resolve(idOrInstance: string | object): object | null {
43
43
  if (this.cache instanceof Collection) {
44
44
  if (typeof idOrInstance === 'object') return idOrInstance;
45
45
  if (typeof idOrInstance === 'string') return this.cache.get(idOrInstance) ?? null;
@@ -1,4 +1,4 @@
1
- import Collection from '@discordjs/collection';
1
+ import { Collection } from '@discordjs/collection';
2
2
 
3
3
  const isObject = (d: any) => typeof d === 'object' && d !== null;
4
4