From 6aa9b25698ad095b0d516806fe47a8f28591674f Mon Sep 17 00:00:00 2001 From: ayntee Date: Fri, 2 Apr 2021 23:21:13 +0400 Subject: [PATCH] refactor(structures/channel): auto-convert the paylaod fields to camel case (#730) --- src/structures/channel.ts | 40 ++++++++++++++++----------------------- src/util/utils.ts | 2 +- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/structures/channel.ts b/src/structures/channel.ts index e04f49b20..8663fcdf7 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -6,8 +6,9 @@ import { editChannel } from "../helpers/channels/edit_channel.ts"; import { editChannelOverwrite } from "../helpers/channels/edit_channel_overwrite.ts"; import { sendMessage } from "../helpers/messages/send_message.ts"; import { disconnectMember } from "../helpers/mod.ts"; +import { Channel, DiscordChannel } from "../types/channels/channel.ts"; import { Collection } from "../util/collection.ts"; -import { createNewProp } from "../util/utils.ts"; +import { createNewProp, snakeKeysToCamelCase } from "../util/utils.ts"; const baseChannel: Partial = { get guild() { @@ -61,41 +62,32 @@ const baseChannel: Partial = { }; // deno-lint-ignore require-await +/** Create a structure object */ export async function createChannelStruct( - data: ChannelCreatePayload, + data: DiscordChannel, guildId?: string, ) { const { - guild_id: rawGuildId = "", - last_message_id: lastMessageId, - user_limit: userLimit, - rate_limit_per_user: rateLimitPerUser, - parent_id: parentId = undefined, - last_pin_timestamp: lastPinTimestamp, - permission_overwrites: permissionOverwrites = [], - nsfw = false, + guildId: rawGuildId = "", + lastPinTimestamp, ...rest - } = data; + } = snakeKeysToCamelCase(data) as Channel; - const restProps: Record> = {}; - for (const key of Object.keys(rest)) { + const props: Record = {}; + Object.keys(rest).forEach((key) => { // @ts-ignore index signature - restProps[key] = createNewProp(rest[key]); - } + props[key] = createNewProp(rest[key]); + }); - const channel = Object.create(baseChannel, { - ...restProps, + const channel: ChannelStruct = Object.create(baseChannel, { + ...props, guildId: createNewProp(guildId || rawGuildId), - lastMessageId: createNewProp(lastMessageId), - userLimit: createNewProp(userLimit), - rateLimitPerUser: createNewProp(rateLimitPerUser), - parentId: createNewProp(parentId), lastPinTimestamp: createNewProp( lastPinTimestamp ? Date.parse(lastPinTimestamp) : undefined, ), - permissionOverwrites: createNewProp(permissionOverwrites), - nsfw: createNewProp(nsfw), }); - return channel as Channel; + return channel; } + +export type ChannelStruct = Channel & typeof baseChannel; diff --git a/src/util/utils.ts b/src/util/utils.ts index 6dc5acce0..2e3abec21 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -31,7 +31,7 @@ export async function urlToBase64(url: string) { /** Allows easy way to add a prop to a base object when needing to use complicated getters solution. */ // deno-lint-ignore no-explicit-any -export function createNewProp(value: any): Partial { +export function createNewProp(value: any): PropertyDescriptor { return { configurable: true, enumerable: true, writable: true, value }; }