Skip to content

Commit

Permalink
Unify toJSON() logic (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
apacheli authored and abalabahaha committed Nov 25, 2019
1 parent 46f2e23 commit d67880d
Show file tree
Hide file tree
Showing 33 changed files with 401 additions and 302 deletions.
16 changes: 8 additions & 8 deletions index.d.ts
Expand Up @@ -13,7 +13,7 @@ declare namespace Eris {
}

interface SimpleJSON {
toJSON(simple?: boolean): JSONCache;
toJSON(props?: string[]): JSONCache;
}

interface NestedJSON {
Expand Down Expand Up @@ -718,7 +718,7 @@ declare namespace Eris {
constructor(client: Client);
connect(shard: Shard): void;
spawn(id: number): void;
toJSON(): string;
toJSON(props?: string[]): string;
}

interface CreateChannelOptions {
Expand Down Expand Up @@ -1096,7 +1096,7 @@ declare namespace Eris {
on(event: "shardReady" | "shardResume", listener: (id: number) => void): this;
// tslint:disable-next-line
on(event: string, listener: Function): this;
toJSON(simple?: boolean): JSONCache;
toJSON(props?: string[]): JSONCache;
}

export class VoiceConnection extends EventEmitter implements SimpleJSON {
Expand Down Expand Up @@ -1129,7 +1129,7 @@ declare namespace Eris {
on(event: "speakingStart", listener: (userID: string) => void): this;
on(event: "speakingStop", listener: (userID: string) => void): this;
on(event: "end", listener: () => void): this;
toJSON(simple?: boolean): JSONCache;
toJSON(props?: string[]): JSONCache;
}

export class SharedStream extends EventEmitter {
Expand Down Expand Up @@ -1164,15 +1164,15 @@ declare namespace Eris {
join(guildID: string, channelID: string, options: VoiceResourceOptions): Promise<VoiceConnection>;
leave(guildID: string): void;
switch(guildID: string, channelID: string): void;
toJSON(simple?: boolean): JSONCache;
toJSON(props?: string[]): JSONCache;
}

class Base implements SimpleJSON {
id: string;
createdAt: number;
constructor(id: string);
inspect(): this;
toJSON(simple?: boolean): JSONCache;
toJSON(props?: string[]): JSONCache;
}

export class Bucket {
Expand Down Expand Up @@ -1472,7 +1472,7 @@ declare namespace Eris {
memberCount?: number;
constructor(data: BaseData, client: Client);
delete(reason?: string): Promise<void>;
toJSON(simple?: boolean): JSONCache;
toJSON(props?: string[]): JSONCache;
}

interface Activity {
Expand Down Expand Up @@ -1784,7 +1784,7 @@ declare namespace Eris {
// FIXME
// tslint:disable-next-line
on(event: "resume", listener: () => void): this;
toJSON(simple?: boolean): JSONCache;
toJSON(props?: string[]): JSONCache;
// tslint:disable-next-line
sendWS(op: number, _data: object): void;
}
Expand Down
48 changes: 30 additions & 18 deletions lib/Client.js
@@ -1,5 +1,6 @@
"use strict";

const Base = require("./structures/Base");
const Channel = require("./structures/Channel");
const Collection = require("./util/Collection");
const Constants = require("./Constants");
Expand Down Expand Up @@ -2056,24 +2057,35 @@ class Client extends EventEmitter {
}));
}

toJSON() {
const base = {};
for(const key in this) {
if(this.hasOwnProperty(key) && !key.startsWith("_")) {
if(!this[key]) {
base[key] = this[key];
} else if(this[key] instanceof Set) {
base[key] = Array.from(this[key]);
} else if(this[key] instanceof Map) {
base[key] = Array.from(this[key].values());
} else if(typeof this[key].toJSON === "function") {
base[key] = this[key].toJSON();
} else {
base[key] = this[key];
}
}
}
return base;
toJSON(props = []) {
return Base.prototype.toJSON.call(this, [
"options",
"token",
"requestHandler",
"ready",
"bot",
"startTime",
"lastConnect",
"channelGuildMap",
"shards",
"gatewayURL",
"groupChannels",
"guilds",
"privateChannelMap",
"privateChannels",
"guildShardMap",
"unavailableGuilds",
"relationships",
"users",
"presence",
"userGuildSettings",
"userSettings",
"notes",
"voiceConnections",
"lastReconnectDelay",
"reconnectAttempts",
...props
]);
}
}

Expand Down
36 changes: 36 additions & 0 deletions lib/command/Command.js
@@ -1,5 +1,7 @@
"use strict";

const Base = require("../structures/Base");

/**
* Represents an command framework command
* @prop {Object} subcommands Object mapping subcommand labels to Command objects
Expand Down Expand Up @@ -489,6 +491,40 @@ class Command {
delete this.subcommands[label];
}
}

toJSON(props = []) {
return Base.prototype.toJSON.call(this, [
"parentCommand",
"label",
"description",
"fullDescription",
"usage",
"aliases",
"caseInsensitive",
"hooks",
"requirements",
"deleteCommand",
"argsRequired",
"guildOnly",
"dmOnly",
"cooldown",
"cooldownExclusions",
"restartCooldown",
"cooldownReturns",
"cooldownMessage",
"invalidUsageMessage",
"permissionMessage",
"errorMessage",
"reactionButtons",
"reactionButtonTimeout",
"execute",
"defaultSubcommandOptions",
"subcommands",
"subcommandAliases",
"hidden",
...props
]);
}
}

module.exports = Command;
11 changes: 11 additions & 0 deletions lib/command/CommandClient.js
Expand Up @@ -427,6 +427,17 @@ class CommandClient extends Client {
this.removeMessageReactions(channelID, id).catch(() => {});
}
}

toJSON(props = []) {
return super.toJSON([
"commandOptions",
"guildPrefixes",
"commands",
"commandAliases",
"activeMessages",
...props
]);
}
}

module.exports = CommandClient;
42 changes: 24 additions & 18 deletions lib/gateway/Shard.js
@@ -1,5 +1,6 @@
"use strict";

const Base = require("../structures/Base");
const Bucket = require("../util/Bucket");
const Call = require("../structures/Call");
const Channel = require("../structures/Channel");
Expand Down Expand Up @@ -1822,24 +1823,29 @@ class Shard extends EventEmitter {
});
}

toJSON() {
const base = {};
for(const key in this) {
if(this.hasOwnProperty(key) && !key.startsWith("_") && key !== "ws" && key !== "client" && !key.endsWith("Interval") && !key.endsWith("Timeout")) {
if(!this[key]) {
base[key] = this[key];
} else if(this[key] instanceof Set) {
base[key] = Array.from(this[key]);
} else if(this[key] instanceof Map) {
base[key] = Array.from(this[key].values());
} else if(typeof this[key].toJSON === "function") {
base[key] = this[key].toJSON();
} else {
base[key] = this[key];
}
}
}
return base;
toJSON(props = []) {
return Base.prototype.toJSON.call(this, [
"connecting",
"ready",
"discordServerTrace",
"status",
"lastHeartbeatReceived",
"lastHeartbeatSent",
"latency",
"preReady",
"getAllUsersCount",
"getAllUsersQueue",
"getAllUsersLength",
"guildSyncQueue",
"guildSyncQueueLength",
"unsyncedGuilds",
"lastHeartbeatAck",
"seq",
"sessionID",
"reconnectInterval",
"connectAttempts",
...props
]);
}

emit(event, ...args) {
Expand Down
19 changes: 7 additions & 12 deletions lib/gateway/ShardManager.js
Expand Up @@ -114,18 +114,13 @@ class ShardManager extends Collection {
}
}

toJSON() {
const base = {};
for(const key in this) {
if(this.hasOwnProperty(key) && !key.startsWith("_")) {
if(this[key] && typeof this[key].toJSON === "function") {
base[key] = this[key].toJSON();
} else {
base[key] = this[key];
}
}
}
return base;
toJSON(props = []) {
return super.toJSON([
"connectQueue",
"lastConnect",
"connectionTimeout",
...props
]);
}
}

Expand Down
25 changes: 13 additions & 12 deletions lib/rest/RequestHandler.js
@@ -1,5 +1,6 @@
"use strict";

const Base = require("../structures/Base");
const DiscordHTTPError = require("../errors/DiscordHTTPError");
const DiscordRESTError = require("../errors/DiscordRESTError");
const Endpoints = require("./Endpoints");
Expand Down Expand Up @@ -334,18 +335,18 @@ class RequestHandler {
});
}

toJSON() {
const base = {};
for(const key in this) {
if(this.hasOwnProperty(key) && !key.startsWith("_")) {
if(this[key] && typeof this[key].toJSON === "function") {
base[key] = this[key].toJSON();
} else {
base[key] = this[key];
}
}
}
return base;
toJSON(props = []) {
return Base.prototype.toJSON.call(this, [
"baseURL",
"userAgent",
"ratelimits",
"requestTimeout",
"agent",
"latencyRef",
"globalBlock",
"readyQueue",
...props
]);
}
}

Expand Down
37 changes: 17 additions & 20 deletions lib/structures/Base.js
Expand Up @@ -17,29 +17,26 @@ class Base {
return `[${this.constructor.name} ${this.id}]`;
}

toJSON(simple) {
if(simple) {
return {
id: this.id
};
toJSON(props = []) {
const json = {};
if(this.id) {
json.id = this.id;
}
const base = {};
for(const key in this) {
if(!base.hasOwnProperty(key) && this.hasOwnProperty(key) && !key.startsWith("_")) {
if(!this[key]) {
base[key] = this[key];
} else if(this[key] instanceof Set) {
base[key] = Array.from(this[key]);
} else if(this[key] instanceof Map) {
base[key] = Array.from(this[key].values());
} else if(typeof this[key].toJSON === "function") {
base[key] = this[key].toJSON();
} else {
base[key] = this[key];
}
for(const prop of props) {
const value = this[prop];
if(value === undefined) {
continue;
} else if(typeof value !== "object" || value === null) {
json[prop] = value;
} else if(value.toJSON !== undefined) {
json[prop] = value.toJSON();
} else if(value.values !== undefined) {
json[prop] = Array.from(value.values());
} else if(typeof value !== "function") {
json[prop] = value;
}
}
return base;
return json;
}

[util.inspect.custom]() {
Expand Down
18 changes: 10 additions & 8 deletions lib/structures/Call.js
Expand Up @@ -48,14 +48,16 @@ class Call extends Base {
}
}

toJSON() {
const base = super.toJSON(true);
for(const prop of ["endedTimestamp", "participants", "region", "ringing", "unavailable", "voiceStates"]) {
if(this[prop] !== undefined) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
}
return base;
toJSON(props = []) {
return super.toJSON([
"endedTimestamp",
"participants",
"region",
"ringing",
"unavailable",
"voiceStates",
...props
]);
}
}

Expand Down

0 comments on commit d67880d

Please sign in to comment.