From 1a8da4084bc32ce32dc837b0bc3bb694a8319dfb Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Sat, 2 Sep 2023 10:56:09 +0100 Subject: [PATCH] feat(CDN): Support emoji size (#9787) * feat(CDN): support emoji size * refactor: try to warn in all environments * feat: add prefix * refactor: better feedback message --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/rest/src/lib/CDN.ts | 35 ++++++++++++++++++++++-- packages/rest/src/lib/utils/constants.ts | 7 +++++ packages/rest/src/lib/utils/utils.ts | 16 +++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/packages/rest/src/lib/CDN.ts b/packages/rest/src/lib/CDN.ts index aa4bc90dcbf44..6ef6f67e2b27a 100644 --- a/packages/rest/src/lib/CDN.ts +++ b/packages/rest/src/lib/CDN.ts @@ -8,6 +8,9 @@ import { type ImageSize, type StickerExtension, } from './utils/constants.js'; +import { deprecationWarning } from './utils/utils.js'; + +let deprecationEmittedForEmoji = false; /** * The options used for image URLs @@ -154,14 +157,42 @@ export class CDN { return this.makeURL(`/discovery-splashes/${guildId}/${splashHash}`, options); } + /** + * Generates an emoji's URL for an emoji. + * + * @param emojiId - The emoji id + * @param options - Optional options for the emoji + */ + public emoji(emojiId: string, options?: Readonly): string; + /** * Generates an emoji's URL for an emoji. * * @param emojiId - The emoji id * @param extension - The extension of the emoji + * @deprecated This overload is deprecated. Pass an object containing the extension instead. */ - public emoji(emojiId: string, extension?: ImageExtension): string { - return this.makeURL(`/emojis/${emojiId}`, { extension }); + // eslint-disable-next-line @typescript-eslint/unified-signatures + public emoji(emojiId: string, extension?: ImageExtension): string; + + public emoji(emojiId: string, options?: ImageExtension | Readonly): string { + let resolvedOptions; + + if (typeof options === 'string') { + if (!deprecationEmittedForEmoji) { + deprecationWarning( + 'Passing a string for the second parameter of CDN#emoji() is deprecated. Use an object instead.', + ); + + deprecationEmittedForEmoji = true; + } + + resolvedOptions = { extension: options }; + } else { + resolvedOptions = options; + } + + return this.makeURL(`/emojis/${emojiId}`, resolvedOptions); } /** diff --git a/packages/rest/src/lib/utils/constants.ts b/packages/rest/src/lib/utils/constants.ts index 5451ab8b6d6fb..ae56bd437e928 100644 --- a/packages/rest/src/lib/utils/constants.ts +++ b/packages/rest/src/lib/utils/constants.ts @@ -59,3 +59,10 @@ export const OverwrittenMimeTypes = { } as const satisfies Readonly>; export const BurstHandlerMajorIdKey = 'burst'; + +/** + * Prefix for deprecation warnings. + * + * @internal + */ +export const DEPRECATION_WARNING_PREFIX = 'DeprecationWarning' as const; diff --git a/packages/rest/src/lib/utils/utils.ts b/packages/rest/src/lib/utils/utils.ts index 59f461827a938..1acff0cb16885 100644 --- a/packages/rest/src/lib/utils/utils.ts +++ b/packages/rest/src/lib/utils/utils.ts @@ -1,6 +1,7 @@ import type { RESTPatchAPIChannelJSONBody, Snowflake } from 'discord-api-types/v10'; import type { REST } from '../REST.js'; import { RateLimitError } from '../errors/RateLimitError.js'; +import { DEPRECATION_WARNING_PREFIX } from './constants.js'; import { RequestMethod, type RateLimitData, type ResponseLike } from './types.js'; function serializeSearchParam(value: unknown): string | null { @@ -140,3 +141,18 @@ export async function sleep(ms: number): Promise { export function isBufferLike(value: unknown): value is ArrayBuffer | Buffer | Uint8Array | Uint8ClampedArray { return value instanceof ArrayBuffer || value instanceof Uint8Array || value instanceof Uint8ClampedArray; } + +/** + * Irrespective environment warning. + * + * @remarks Only the message is needed. The deprecation prefix is handled already. + * @param message - A string the warning will emit with + * @internal + */ +export function deprecationWarning(message: string) { + if (typeof globalThis.process === 'undefined') { + console.warn(`${DEPRECATION_WARNING_PREFIX}: ${message}`); + } else { + process.emitWarning(message, DEPRECATION_WARNING_PREFIX); + } +}