From a1e71bc2903389f4b6fa95db3cb98612ad60741c Mon Sep 17 00:00:00 2001 From: H01001000 Date: Tue, 6 Dec 2022 14:23:27 +0800 Subject: [PATCH] refactor: remove transformers in helpers/channel --- .../followAnnouncementChannel.ts | 6 +- .../src/helpers/channels/createChannel.ts | 12 ++-- .../rest/src/helpers/channels/editChannel.ts | 30 +++++----- .../channels/forums/createForumThread.ts | 12 ++-- .../rest/src/helpers/channels/getChannel.ts | 19 +++---- .../src/helpers/channels/getChannelInvites.ts | 56 ++++--------------- .../rest/src/helpers/channels/getChannels.ts | 19 +++---- .../channels/stages/createStageInstance.ts | 9 +-- .../channels/stages/editStageInstance.ts | 9 +-- .../channels/stages/getStageInstance.ts | 14 +++-- .../channels/threads/getActiveThreads.ts | 25 +++++---- .../threads/getPrivateArchivedThreads.ts | 11 ++-- .../getPrivateJoinedArchivedThreads.ts | 11 ++-- .../threads/getPublicArchivedThreads.ts | 11 ++-- .../channels/threads/getThreadMember.ts | 14 +++-- .../channels/threads/getThreadMembers.ts | 14 +++-- .../threads/startThreadWithMessage.ts | 16 +++--- .../threads/startThreadWithoutMessage.ts | 14 ++--- packages/rest/src/transformer.ts | 28 +++++++++- packages/types/src/shared.ts | 15 +++++ 20 files changed, 184 insertions(+), 161 deletions(-) diff --git a/packages/rest/src/helpers/channels/announcements/followAnnouncementChannel.ts b/packages/rest/src/helpers/channels/announcements/followAnnouncementChannel.ts index 110e991fe..8620ff117 100644 --- a/packages/rest/src/helpers/channels/announcements/followAnnouncementChannel.ts +++ b/packages/rest/src/helpers/channels/announcements/followAnnouncementChannel.ts @@ -7,7 +7,7 @@ import type { RestManager } from '../../../restManager.js' * @param bot - The bot instance to use to make the request. * @param sourceChannelId - The ID of the announcement channel to follow. * @param targetChannelId - The ID of the target channel - the channel to cross-post to. - * @returns An instance of {@link FollowedChannel}. + * @returns An instance of {@link DiscordFollowedChannel}. * * @remarks * Requires the `MANAGE_WEBHOOKS` permission in the __target channel__. @@ -20,7 +20,7 @@ export async function followAnnouncementChannel ( rest: RestManager, sourceChannelId: BigString, targetChannelId: BigString -): Promise { +): Promise { const result = await rest.runMethod( rest, 'POST', @@ -30,5 +30,5 @@ export async function followAnnouncementChannel ( } ) - return rest.transformers.snowflake(result.webhook_id) + return result } diff --git a/packages/rest/src/helpers/channels/createChannel.ts b/packages/rest/src/helpers/channels/createChannel.ts index e6c6e0df9..efd5285c9 100644 --- a/packages/rest/src/helpers/channels/createChannel.ts +++ b/packages/rest/src/helpers/channels/createChannel.ts @@ -2,13 +2,14 @@ import type { BigString, DiscordChannel, OverwriteReadable, + SnakeToCamelCaseNested, SortOrderTypes, WithReason } from '@discordeno/types' import { ChannelTypes } from '@discordeno/types' import { calculateBits } from '@discordeno/utils' import type { RestManager } from '../../restManager.js' -import type { Channel } from '../../transformers/channel.js' +import { snakeToCamelCaseNested } from '../../transformer.js' /** * Creates a channel within a guild. @@ -16,7 +17,7 @@ import type { Channel } from '../../transformers/channel.js' * @param bot - The bot instance to use to make the request. * @param guildId - The ID of the guild to create the channel within. * @param options - The parameters for the creation of the channel. - * @returns An instance of the created {@link Channel}. + * @returns An instance of the created {@link DiscordChannel}. * * @remarks * Requires the `MANAGE_CHANNELS` permission. @@ -33,7 +34,7 @@ export async function createChannel ( rest: RestManager, guildId: BigString, options: CreateGuildChannel -): Promise { +): Promise> { // BITRATE IS IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000 if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000 @@ -89,10 +90,7 @@ export async function createChannel ( : {} ) - return rest.transformers.channel(rest, { - channel: result, - guildId: rest.transformers.snowflake(guildId) - }) + return snakeToCamelCaseNested(result) } export interface CreateGuildChannel extends WithReason { diff --git a/packages/rest/src/helpers/channels/editChannel.ts b/packages/rest/src/helpers/channels/editChannel.ts index 49b7e6453..aa8e10662 100644 --- a/packages/rest/src/helpers/channels/editChannel.ts +++ b/packages/rest/src/helpers/channels/editChannel.ts @@ -3,13 +3,14 @@ import type { ChannelTypes, DiscordChannel, OverwriteReadable, + SnakeToCamelCaseNested, SortOrderTypes, VideoQualityModes, WithReason } from '@discordeno/types' import { calculateBits } from '@discordeno/utils' import type { RestManager } from '../../restManager.js' -import type { Channel } from '../../transformers/channel.js' +import { snakeToCamelCaseNested } from '../../transformer.js' /** * Edits a channel's settings. @@ -17,7 +18,7 @@ import type { Channel } from '../../transformers/channel.js' * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the channel to edit. * @param options - The parameters for the edit of the channel. - * @returns An instance of the edited {@link Channel}. + * @returns An instance of the edited {@link DiscordChannel}. * * @remarks * If editing a channel of type {@link ChannelTypes.GroupDm}: @@ -45,7 +46,7 @@ export async function editChannel ( rest: RestManager, channelId: BigString, options: ModifyChannel -): Promise { +): Promise> { if (options.name ?? options.topic) { const request = editChannelNameTopicQueue.get(channelId) if (request == null) { @@ -62,13 +63,15 @@ export async function editChannel ( request.amount = 2 request.timestamp = Date.now() + 600000 } else { - return await new Promise((resolve, reject) => { - // 2 have already been used add to queue - request.items.push({ channelId, options, resolve, reject }) - if (editChannelProcessing) return - editChannelProcessing = true - processEditChannelQueue(rest) - }) + return await new Promise>( + (resolve, reject) => { + // 2 have already been used add to queue + request.items.push({ channelId, options, resolve, reject }) + if (editChannelProcessing) return + editChannelProcessing = true + processEditChannelQueue(rest) + } + ) } } @@ -122,10 +125,7 @@ export async function editChannel ( } ) - return rest.transformers.channel(rest, { - channel: result, - guildId: rest.transformers.snowflake(result.guild_id!) - }) + return snakeToCamelCaseNested(result) } interface EditChannelRequest { @@ -135,7 +135,7 @@ interface EditChannelRequest { items: Array<{ channelId: BigString options: ModifyChannel - resolve: (channel: Channel) => void + resolve: (channel: SnakeToCamelCaseNested) => void // deno-lint-ignore no-explicit-any reject: (error: any) => void }> diff --git a/packages/rest/src/helpers/channels/forums/createForumThread.ts b/packages/rest/src/helpers/channels/forums/createForumThread.ts index 8cb5eacba..43f753f80 100644 --- a/packages/rest/src/helpers/channels/forums/createForumThread.ts +++ b/packages/rest/src/helpers/channels/forums/createForumThread.ts @@ -4,10 +4,11 @@ import type { DiscordChannel, FileContent, MessageComponents, + SnakeToCamelCaseNested, WithReason } from '@discordeno/types' import type { RestManager } from '../../../restManager.js' -import type { Channel } from '../../../transformers/channel.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' import type { Embed } from '../../../transformers/embed.js' /** @@ -16,7 +17,7 @@ import type { Embed } from '../../../transformers/embed.js' * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the forum channel to create the thread within. * @param options - The parameters for the creation of the thread. - * @returns An instance of {@link Channel} with a nested {@link Message} object. + * @returns An instance of {@link DiscordChannel} with a nested {@link Message} object. * * @remarks * Requires the `CREATE_MESSAGES` permission. @@ -32,7 +33,7 @@ export async function createForumThread ( rest: RestManager, channelId: BigString, options: CreateForumPostWithMessage -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'POST', @@ -62,10 +63,7 @@ export async function createForumThread ( } ) - return rest.transformers.channel(rest, { - channel: result, - guildId: rest.transformers.snowflake(result.guild_id!) - }) + return snakeToCamelCaseNested(result) } export interface CreateForumPostWithMessage extends WithReason { diff --git a/packages/rest/src/helpers/channels/getChannel.ts b/packages/rest/src/helpers/channels/getChannel.ts index dc1de77b7..64b38a188 100644 --- a/packages/rest/src/helpers/channels/getChannel.ts +++ b/packages/rest/src/helpers/channels/getChannel.ts @@ -1,13 +1,17 @@ -import type { BigString, DiscordChannel } from '@discordeno/types' +import type { + BigString, + DiscordChannel, + SnakeToCamelCaseNested +} from '@discordeno/types' import type { RestManager } from '../../restManager.js' -import type { Channel } from '../../transformers/channel.js' +import { snakeToCamelCaseNested } from '../../transformer.js' /** * Gets a channel by its ID. * * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the channel to get. - * @returns An instance of {@link Channel}. + * @returns An instance of {@link DiscordChannel}. * * @remarks * If the channel is a thread, a {@link ThreadMember} object is included in the result. @@ -17,7 +21,7 @@ import type { Channel } from '../../transformers/channel.js' export async function getChannel ( rest: RestManager, channelId: BigString -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'GET', @@ -25,10 +29,5 @@ export async function getChannel ( ) // IF A CHANNEL DOESN'T EXIST, DISCORD RETURNS `{}` - return rest.transformers.channel(rest, { - channel: result, - guildId: result.guild_id - ? rest.transformers.snowflake(result.guild_id) - : undefined - }) + return snakeToCamelCaseNested(result) } diff --git a/packages/rest/src/helpers/channels/getChannelInvites.ts b/packages/rest/src/helpers/channels/getChannelInvites.ts index eda952e57..2aaca582c 100644 --- a/packages/rest/src/helpers/channels/getChannelInvites.ts +++ b/packages/rest/src/helpers/channels/getChannelInvites.ts @@ -1,15 +1,18 @@ -import type { BigString, DiscordInviteMetadata } from '@discordeno/types' -import { TargetTypes } from '@discordeno/types' +import type { + BigString, + DiscordInviteMetadata, + SnakeToCamelCaseNested +} from '@discordeno/types' import { Collection } from '@discordeno/utils' import type { RestManager } from '../../restManager.js' -import type { InviteMetadata } from '../guilds/invites/index.js' +import { snakeToCamelCaseNested } from '../../transformer.js' /** * Gets the list of invites for a channel. * * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the channel to get the invites of. - * @returns A collection of {@link InviteMetadata} objects assorted by invite code. + * @returns A collection of {@link DiscordInviteMetadata} objects assorted by invite code. * * @remarks * Requires the `MANAGE_CHANNELS` permission. @@ -21,7 +24,7 @@ import type { InviteMetadata } from '../guilds/invites/index.js' export async function getChannelInvites ( rest: RestManager, channelId: BigString -): Promise> { +): Promise>> { const results = await rest.runMethod( rest, 'GET', @@ -29,45 +32,10 @@ export async function getChannelInvites ( ) return new Collection( - results.map<[string, InviteMetadata]>((result) => { - const invite = { - code: result.code, - guildId: result.guild?.id - ? rest.transformers.snowflake(result.guild.id) - : undefined, - channelId: result.channel?.id - ? rest.transformers.snowflake(result.channel.id) - : undefined, - inviter: result.inviter - ? rest.transformers.user(rest, result.inviter) - : undefined, - targetType: result.target_type - ? result.target_type === 1 - ? TargetTypes.Stream - : TargetTypes.EmbeddedApplication - : undefined, - targetUser: result.target_user - ? rest.transformers.user(rest, result.target_user) - : undefined, - targetApplicationId: result.target_application?.id - ? rest.transformers.snowflake(result.target_application.id) - : undefined, - approximatePresenceCount: result.approximate_presence_count, - approximateMemberCount: result.approximate_member_count, - expiresAt: result.expires_at - ? Date.parse(result.expires_at) - : undefined, - guildScheduledEvent: result.guild_scheduled_event - ? rest.transformers.scheduledEvent(rest, result.guild_scheduled_event) - : undefined, - // Metadata structure - uses: result.uses, - maxUses: result.max_uses, - maxAge: result.max_age, - temporary: result.temporary, - createdAt: Date.parse(result.created_at) + results.map<[string, SnakeToCamelCaseNested]>( + (result) => { + return [result.code, snakeToCamelCaseNested(result)] } - return [invite.code, invite] - }) + ) ) } diff --git a/packages/rest/src/helpers/channels/getChannels.ts b/packages/rest/src/helpers/channels/getChannels.ts index 146b5b12e..0a005e5f9 100644 --- a/packages/rest/src/helpers/channels/getChannels.ts +++ b/packages/rest/src/helpers/channels/getChannels.ts @@ -1,14 +1,18 @@ -import type { BigString, DiscordChannel } from '@discordeno/types' +import type { + BigString, + DiscordChannel, + SnakeToCamelCaseNested +} from '@discordeno/types' import { Collection } from '@discordeno/utils' import type { RestManager } from '../../restManager.js' -import type { Channel } from '../../transformers/channel.js' +import { snakeToCamelCaseNested } from '../../transformer.js' /** * Gets the list of channels for a guild. * * @param bot - The bot instance to use to make the request. * @param guildId - The ID of the guild to get the channels of. - * @returns A collection of {@link Channel} objects assorted by channel ID. + * @returns A collection of {@link DiscordChannel} objects assorted by channel ID. * * @remarks * Excludes threads. @@ -18,21 +22,16 @@ import type { Channel } from '../../transformers/channel.js' export async function getChannels ( rest: RestManager, guildId: BigString -): Promise> { +): Promise>> { const results = await rest.runMethod( rest, 'GET', rest.constants.routes.GUILD_CHANNELS(guildId) ) - const id = rest.transformers.snowflake(guildId) - return new Collection( results.map((result) => { - const channel = rest.transformers.channel(rest, { - channel: result, - guildId: id - }) + const channel = snakeToCamelCaseNested(result) return [channel.id, channel] }) ) diff --git a/packages/rest/src/helpers/channels/stages/createStageInstance.ts b/packages/rest/src/helpers/channels/stages/createStageInstance.ts index ebd3853d5..3089d934c 100644 --- a/packages/rest/src/helpers/channels/stages/createStageInstance.ts +++ b/packages/rest/src/helpers/channels/stages/createStageInstance.ts @@ -1,17 +1,18 @@ import type { BigString, DiscordStageInstance, + SnakeToCamelCaseNested, WithReason } from '@discordeno/types' import type { RestManager } from '../../../restManager.js' -import type { StageInstance } from '../../../transformers/stageInstance.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Creates a stage instance associated with a stage channel. * * @param bot - The bot instance to use to make the request. * @param options - The parameters for the creation of the stage instance. - * @returns An instance of the created {@link StageInstance}. + * @returns An instance of the created {@link DiscordStageInstance}. * * @remarks * Requires the user to be a moderator of the stage channel. @@ -23,7 +24,7 @@ import type { StageInstance } from '../../../transformers/stageInstance.js' export async function createStageInstance ( rest: RestManager, options: CreateStageInstance -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'POST', @@ -36,7 +37,7 @@ export async function createStageInstance ( } ) - return rest.transformers.stageInstance(rest, result) + return snakeToCamelCaseNested(result) } export interface CreateStageInstance extends WithReason { diff --git a/packages/rest/src/helpers/channels/stages/editStageInstance.ts b/packages/rest/src/helpers/channels/stages/editStageInstance.ts index cff93a15a..f53c9366a 100644 --- a/packages/rest/src/helpers/channels/stages/editStageInstance.ts +++ b/packages/rest/src/helpers/channels/stages/editStageInstance.ts @@ -1,17 +1,18 @@ import type { BigString, DiscordStageInstance, + SnakeToCamelCaseNested, WithReason } from '@discordeno/types' import type { RestManager } from '../../../restManager.js' -import type { StageInstance } from '../../../transformers/stageInstance.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Edits a stage instance. * * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the stage channel the stage instance is associated with. - * @returns An instance of the updated {@link StageInstance}. + * @returns An instance of the updated {@link DiscordStageInstance}. * * @remarks * Requires the user to be a moderator of the stage channel. @@ -24,7 +25,7 @@ export async function editStageInstance ( rest: RestManager, channelId: BigString, data: EditStageInstanceOptions -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'PATCH', @@ -34,7 +35,7 @@ export async function editStageInstance ( } ) - return rest.transformers.stageInstance(rest, result) + return snakeToCamelCaseNested(result) } export interface EditStageInstanceOptions extends WithReason { diff --git a/packages/rest/src/helpers/channels/stages/getStageInstance.ts b/packages/rest/src/helpers/channels/stages/getStageInstance.ts index f56b48377..d14c8753a 100644 --- a/packages/rest/src/helpers/channels/stages/getStageInstance.ts +++ b/packages/rest/src/helpers/channels/stages/getStageInstance.ts @@ -1,25 +1,29 @@ -import type { BigString, DiscordStageInstance } from '@discordeno/types' +import type { + BigString, + DiscordStageInstance, + SnakeToCamelCaseNested +} from '@discordeno/types' import type { RestManager } from '../../../restManager.js' -import type { StageInstance } from '../../../transformers/stageInstance.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Gets the stage instance associated with a stage channel, if one exists. * * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the stage channel the stage instance is associated with. - * @returns An instance of {@link StageInstance}. + * @returns An instance of {@link DiscordStageInstance}. * * @see {@link https://discord.com/developers/docs/resources/stage-instance#get-stage-instance} */ export async function getStageInstance ( rest: RestManager, channelId: BigString -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'GET', rest.constants.routes.STAGE_INSTANCE(channelId) ) - return rest.transformers.stageInstance(rest, result) + return snakeToCamelCaseNested(result) } diff --git a/packages/rest/src/helpers/channels/threads/getActiveThreads.ts b/packages/rest/src/helpers/channels/threads/getActiveThreads.ts index d513cf859..fa4cc8a50 100644 --- a/packages/rest/src/helpers/channels/threads/getActiveThreads.ts +++ b/packages/rest/src/helpers/channels/threads/getActiveThreads.ts @@ -1,15 +1,20 @@ -import type { BigString, DiscordListActiveThreads } from '@discordeno/types' +import type { + BigString, + DiscordChannel, + DiscordListActiveThreads, + DiscordThreadMember, + SnakeToCamelCaseNested +} from '@discordeno/types' import { Collection } from '@discordeno/utils' import type { RestManager } from '../../../restManager.js' -import type { Channel } from '../../../transformers/channel.js' -import type { ThreadMember } from '../../../transformers/threadMember.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Gets the list of all active threads for a guild. * * @param bot - The bot instance to use to make the request. * @param guildId - The ID of the guild to get the threads of. - * @returns An instance of {@link ActiveThreads}. + * @returns An instance of {@link DiscordActiveThreads}. * * @remarks * Returns both public and private threads. @@ -21,7 +26,7 @@ import type { ThreadMember } from '../../../transformers/threadMember.js' export async function getActiveThreads ( rest: RestManager, guildId: BigString -): Promise { +): Promise { const results = await rest.runMethod( rest, 'GET', @@ -31,20 +36,20 @@ export async function getActiveThreads ( return { threads: new Collection( results.threads.map((result) => { - const thread = rest.transformers.channel(rest, { channel: result }) + const thread = snakeToCamelCaseNested(result) return [thread.id, thread] }) ), members: new Collection( results.members.map((result) => { - const member = rest.transformers.threadMember(rest, result) + const member = snakeToCamelCaseNested(result) return [member.id!, member] }) ) } } -export interface ActiveThreads { - threads: Collection - members: Collection +export interface DiscordActiveThreads { + threads: Collection> + members: Collection> } diff --git a/packages/rest/src/helpers/channels/threads/getPrivateArchivedThreads.ts b/packages/rest/src/helpers/channels/threads/getPrivateArchivedThreads.ts index 47212684a..2fb2f2277 100644 --- a/packages/rest/src/helpers/channels/threads/getPrivateArchivedThreads.ts +++ b/packages/rest/src/helpers/channels/threads/getPrivateArchivedThreads.ts @@ -1,8 +1,9 @@ import type { BigString, DiscordListArchivedThreads } from '@discordeno/types' import { Collection } from '@discordeno/utils' import type { RestManager } from '../../../restManager.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' import type { - ArchivedThreads, + DiscordArchivedThreads, ListArchivedThreads } from './getPublicArchivedThreads.js' @@ -12,7 +13,7 @@ import type { * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the channel to get the archived threads for. * @param options - The parameters for the fetching of threads. - * @returns An instance of {@link ArchivedThreads}. + * @returns An instance of {@link DiscordArchivedThreads}. * * @remarks * Requires the `READ_MESSAGE_HISTORY` permission. @@ -28,7 +29,7 @@ export async function getPrivateArchivedThreads ( rest: RestManager, channelId: BigString, options?: ListArchivedThreads -): Promise { +): Promise { const results = await rest.runMethod( rest, 'GET', @@ -38,13 +39,13 @@ export async function getPrivateArchivedThreads ( return { threads: new Collection( results.threads.map((result) => { - const thread = rest.transformers.channel(rest, { channel: result }) + const thread = snakeToCamelCaseNested(result) return [thread.id, thread] }) ), members: new Collection( results.members.map((result) => { - const member = rest.transformers.threadMember(rest, result) + const member = snakeToCamelCaseNested(result) return [member.id!, member] }) ), diff --git a/packages/rest/src/helpers/channels/threads/getPrivateJoinedArchivedThreads.ts b/packages/rest/src/helpers/channels/threads/getPrivateJoinedArchivedThreads.ts index 753922cb3..4fd172076 100644 --- a/packages/rest/src/helpers/channels/threads/getPrivateJoinedArchivedThreads.ts +++ b/packages/rest/src/helpers/channels/threads/getPrivateJoinedArchivedThreads.ts @@ -1,8 +1,9 @@ import type { BigString, DiscordListArchivedThreads } from '@discordeno/types' import { Collection } from '@discordeno/utils' import type { RestManager } from '../../../restManager.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' import type { - ArchivedThreads, + DiscordArchivedThreads, ListArchivedThreads } from './getPublicArchivedThreads.js' @@ -12,7 +13,7 @@ import type { * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the channel to get the archived threads for. * @param options - The parameters for the fetching of threads. - * @returns An instance of {@link ArchivedThreads}. + * @returns An instance of {@link DiscordArchivedThreads}. * * @remarks * Requires the `READ_MESSAGE_HISTORY` permission. @@ -27,7 +28,7 @@ export async function getPrivateJoinedArchivedThreads ( rest: RestManager, channelId: BigString, options?: ListArchivedThreads -): Promise { +): Promise { const results = await rest.runMethod( rest, 'GET', @@ -37,13 +38,13 @@ export async function getPrivateJoinedArchivedThreads ( return { threads: new Collection( results.threads.map((result) => { - const thread = rest.transformers.channel(rest, { channel: result }) + const thread = snakeToCamelCaseNested(result) return [thread.id, thread] }) ), members: new Collection( results.members.map((result) => { - const member = rest.transformers.threadMember(rest, result) + const member = snakeToCamelCaseNested(result) return [member.id!, member] }) ), diff --git a/packages/rest/src/helpers/channels/threads/getPublicArchivedThreads.ts b/packages/rest/src/helpers/channels/threads/getPublicArchivedThreads.ts index 812367385..d067fef8c 100644 --- a/packages/rest/src/helpers/channels/threads/getPublicArchivedThreads.ts +++ b/packages/rest/src/helpers/channels/threads/getPublicArchivedThreads.ts @@ -1,7 +1,8 @@ import type { BigString, DiscordListArchivedThreads } from '@discordeno/types' import { Collection } from '@discordeno/utils' import type { RestManager } from '../../../restManager.js' -import type { ActiveThreads } from './getActiveThreads.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' +import type { DiscordActiveThreads } from './getActiveThreads.js' /** * Gets the list of public archived threads for a channel. @@ -25,7 +26,7 @@ export async function getPublicArchivedThreads ( rest: RestManager, channelId: BigString, options?: ListArchivedThreads -): Promise { +): Promise { const results = await rest.runMethod( rest, 'GET', @@ -35,13 +36,13 @@ export async function getPublicArchivedThreads ( return { threads: new Collection( results.threads.map((result) => { - const thread = rest.transformers.channel(rest, { channel: result }) + const thread = snakeToCamelCaseNested(result) return [thread.id, thread] }) ), members: new Collection( results.members.map((result) => { - const member = rest.transformers.threadMember(rest, result) + const member = snakeToCamelCaseNested(result) return [member.id!, member] }) ), @@ -57,6 +58,6 @@ export interface ListArchivedThreads { limit?: number } -export type ArchivedThreads = ActiveThreads & { +export type DiscordArchivedThreads = DiscordActiveThreads & { hasMore: boolean } diff --git a/packages/rest/src/helpers/channels/threads/getThreadMember.ts b/packages/rest/src/helpers/channels/threads/getThreadMember.ts index 57707b2ea..062cf6880 100644 --- a/packages/rest/src/helpers/channels/threads/getThreadMember.ts +++ b/packages/rest/src/helpers/channels/threads/getThreadMember.ts @@ -1,6 +1,10 @@ -import type { BigString, DiscordThreadMember } from '@discordeno/types' +import type { + BigString, + DiscordThreadMember, + SnakeToCamelCaseNested +} from '@discordeno/types' import type { RestManager } from '../../../restManager.js' -import type { ThreadMember } from '../../../transformers/threadMember.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Gets a thread member by their user ID. @@ -8,7 +12,7 @@ import type { ThreadMember } from '../../../transformers/threadMember.js' * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the thread to get the thread member of. * @param userId - The user ID of the thread member to get. - * @returns An instance of {@link ThreadMember}. + * @returns An instance of {@link DiscordThreadMember}. * * @see {@link https://discord.com/developers/docs/resources/channel#get-thread-member} */ @@ -16,12 +20,12 @@ export async function getThreadMember ( rest: RestManager, channelId: BigString, userId: BigString -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'GET', rest.constants.routes.THREAD_USER(channelId, userId) ) - return rest.transformers.threadMember(rest, result) + return snakeToCamelCaseNested(result) } diff --git a/packages/rest/src/helpers/channels/threads/getThreadMembers.ts b/packages/rest/src/helpers/channels/threads/getThreadMembers.ts index f2ad2d582..e9d0324f7 100644 --- a/packages/rest/src/helpers/channels/threads/getThreadMembers.ts +++ b/packages/rest/src/helpers/channels/threads/getThreadMembers.ts @@ -1,14 +1,18 @@ -import type { BigString, DiscordThreadMember } from '@discordeno/types' +import type { + BigString, + DiscordThreadMember, + SnakeToCamelCaseNested +} from '@discordeno/types' import { Collection } from '@discordeno/utils' import type { RestManager } from '../../../restManager.js' -import type { ThreadMember } from '../../../transformers/threadMember.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Gets the list of thread members for a thread. * * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the thread to get the thread members of. - * @returns A collection of {@link ThreadMember} assorted by user ID. + * @returns A collection of {@link DiscordThreadMember} assorted by user ID. * * @remarks * Requires the application to have the `GUILD_MEMBERS` privileged intent enabled. @@ -18,7 +22,7 @@ import type { ThreadMember } from '../../../transformers/threadMember.js' export async function getThreadMembers ( rest: RestManager, channelId: BigString -): Promise> { +): Promise>> { const results = await rest.runMethod( rest, 'GET', @@ -27,7 +31,7 @@ export async function getThreadMembers ( return new Collection( results.map((result) => { - const member = rest.transformers.threadMember(rest, result) + const member = snakeToCamelCaseNested(result) return [member.id!, member] }) ) diff --git a/packages/rest/src/helpers/channels/threads/startThreadWithMessage.ts b/packages/rest/src/helpers/channels/threads/startThreadWithMessage.ts index 69690296b..18935f25d 100644 --- a/packages/rest/src/helpers/channels/threads/startThreadWithMessage.ts +++ b/packages/rest/src/helpers/channels/threads/startThreadWithMessage.ts @@ -1,6 +1,11 @@ -import type { BigString, DiscordChannel, WithReason } from '@discordeno/types' +import type { + BigString, + DiscordChannel, + SnakeToCamelCaseNested, + WithReason +} from '@discordeno/types' import type { RestManager } from '../../../restManager.js' -import type { Channel } from '../../../transformers/channel.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Creates a thread, using an existing message as its point of origin. @@ -27,7 +32,7 @@ export async function startThreadWithMessage ( channelId: BigString, messageId: BigString, options: StartThreadWithMessage -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'POST', @@ -40,10 +45,7 @@ export async function startThreadWithMessage ( } ) - return rest.transformers.channel(rest, { - channel: result, - guildId: rest.transformers.snowflake(result.guild_id!) - }) + return snakeToCamelCaseNested(result) } export interface StartThreadWithMessage extends WithReason { diff --git a/packages/rest/src/helpers/channels/threads/startThreadWithoutMessage.ts b/packages/rest/src/helpers/channels/threads/startThreadWithoutMessage.ts index e70587aa7..0ba2bc735 100644 --- a/packages/rest/src/helpers/channels/threads/startThreadWithoutMessage.ts +++ b/packages/rest/src/helpers/channels/threads/startThreadWithoutMessage.ts @@ -2,10 +2,11 @@ import type { BigString, ChannelTypes, DiscordChannel, + SnakeToCamelCaseNested, WithReason } from '@discordeno/types' import type { RestManager } from '../../../restManager.js' -import type { Channel } from '../../../transformers/channel.js' +import { snakeToCamelCaseNested } from '../../../transformer.js' /** * Creates a thread without using a message as the thread's point of origin. @@ -13,7 +14,7 @@ import type { Channel } from '../../../transformers/channel.js' * @param bot - The bot instance to use to make the request. * @param channelId - The ID of the channel in which to create the thread. * @param options - The parameters to use for the creation of the thread. - * @returns An instance of the created {@link Channel | Thread}. + * @returns An instance of the created {@link DiscordChannel | Thread}. * * @remarks * Creating a private thread requires the server to be boosted. @@ -26,7 +27,7 @@ export async function startThreadWithoutMessage ( rest: RestManager, channelId: BigString, options: StartThreadWithoutMessage -): Promise { +): Promise> { const result = await rest.runMethod( rest, 'POST', @@ -41,12 +42,7 @@ export async function startThreadWithoutMessage ( } ) - return rest.transformers.channel(rest, { - channel: result, - guildId: result.guild_id - ? rest.transformers.snowflake(result.guild_id) - : undefined - }) + return snakeToCamelCaseNested(result) } export interface StartThreadWithoutMessage extends WithReason { diff --git a/packages/rest/src/transformer.ts b/packages/rest/src/transformer.ts index 0771fba52..69a648e5c 100644 --- a/packages/rest/src/transformer.ts +++ b/packages/rest/src/transformer.ts @@ -42,7 +42,8 @@ import type { DiscordVoiceState, DiscordWebhook, DiscordWelcomeScreen, - GetGatewayBot + GetGatewayBot, + SnakeToCamelCaseNested } from '@discordeno/types' import { bigintToSnowflake, snowflakeToBigint } from '@discordeno/utils' import type { RestManager } from './restManager.js' @@ -141,6 +142,31 @@ import { } from './transformers/index.js' import type { CreateApplicationCommand, InteractionResponse } from './types.js' +export const snakeToCamelCaseNested = ( + object: T +): SnakeToCamelCaseNested => { + if (Array.isArray(object)) { + return object.map((element) => + snakeToCamelCaseNested(element) + ) as SnakeToCamelCaseNested + } + if (typeof object === 'object' && object !== null) { + const obj = {} as SnakeToCamelCaseNested; + (Object.keys(object) as Array).forEach((key) => { + (obj[ + (typeof key === 'string' + ? key.replace(/([-_][a-z])/gi, ($1) => { + return $1.toUpperCase().replace('-', '').replace('_', '') + }) + : key) as keyof SnakeToCamelCaseNested + ] as SnakeToCamelCaseNested<(T & object)[keyof T]>) = + snakeToCamelCaseNested(object[key]) + }) + return obj + } + return object as SnakeToCamelCaseNested +} + export interface Transformers { reverse: { allowedMentions: ( diff --git a/packages/types/src/shared.ts b/packages/types/src/shared.ts index 91f2d3ae3..29f29c443 100644 --- a/packages/types/src/shared.ts +++ b/packages/types/src/shared.ts @@ -1537,3 +1537,18 @@ export type PickPartial = { export type OmitFirstFnArg = F extends (x: any, ...args: infer P) => infer R ? (...args: P) => R : never + +export type SnakeToCamelCase = + S extends `${infer T}_${infer U}` + ? `${T}${Capitalize>}` + : S + +export type SnakeToCamelCaseNested = T extends any[] + ? Array> + : T extends object + ? { + [K in keyof T as SnakeToCamelCase]: SnakeToCamelCaseNested< + T[K] + >; + } + : T