Skip to content

Commit 778df45

Browse files
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>
1 parent 23a6424 commit 778df45

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

packages/rest/src/lib/CDN.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import {
88
type ImageSize,
99
type StickerExtension,
1010
} from './utils/constants.js';
11+
import { deprecationWarning } from './utils/utils.js';
12+
13+
let deprecationEmittedForEmoji = false;
1114

1215
/**
1316
* The options used for image URLs
@@ -154,14 +157,42 @@ export class CDN {
154157
return this.makeURL(`/discovery-splashes/${guildId}/${splashHash}`, options);
155158
}
156159

160+
/**
161+
* Generates an emoji's URL for an emoji.
162+
*
163+
* @param emojiId - The emoji id
164+
* @param options - Optional options for the emoji
165+
*/
166+
public emoji(emojiId: string, options?: Readonly<BaseImageURLOptions>): string;
167+
157168
/**
158169
* Generates an emoji's URL for an emoji.
159170
*
160171
* @param emojiId - The emoji id
161172
* @param extension - The extension of the emoji
173+
* @deprecated This overload is deprecated. Pass an object containing the extension instead.
162174
*/
163-
public emoji(emojiId: string, extension?: ImageExtension): string {
164-
return this.makeURL(`/emojis/${emojiId}`, { extension });
175+
// eslint-disable-next-line @typescript-eslint/unified-signatures
176+
public emoji(emojiId: string, extension?: ImageExtension): string;
177+
178+
public emoji(emojiId: string, options?: ImageExtension | Readonly<BaseImageURLOptions>): string {
179+
let resolvedOptions;
180+
181+
if (typeof options === 'string') {
182+
if (!deprecationEmittedForEmoji) {
183+
deprecationWarning(
184+
'Passing a string for the second parameter of CDN#emoji() is deprecated. Use an object instead.',
185+
);
186+
187+
deprecationEmittedForEmoji = true;
188+
}
189+
190+
resolvedOptions = { extension: options };
191+
} else {
192+
resolvedOptions = options;
193+
}
194+
195+
return this.makeURL(`/emojis/${emojiId}`, resolvedOptions);
165196
}
166197

167198
/**

packages/rest/src/lib/utils/constants.ts

+7
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,10 @@ export const OverwrittenMimeTypes = {
5959
} as const satisfies Readonly<Record<string, string>>;
6060

6161
export const BurstHandlerMajorIdKey = 'burst';
62+
63+
/**
64+
* Prefix for deprecation warnings.
65+
*
66+
* @internal
67+
*/
68+
export const DEPRECATION_WARNING_PREFIX = 'DeprecationWarning' as const;

packages/rest/src/lib/utils/utils.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { RESTPatchAPIChannelJSONBody, Snowflake } from 'discord-api-types/v10';
22
import type { REST } from '../REST.js';
33
import { RateLimitError } from '../errors/RateLimitError.js';
4+
import { DEPRECATION_WARNING_PREFIX } from './constants.js';
45
import { RequestMethod, type RateLimitData, type ResponseLike } from './types.js';
56

67
function serializeSearchParam(value: unknown): string | null {
@@ -140,3 +141,18 @@ export async function sleep(ms: number): Promise<void> {
140141
export function isBufferLike(value: unknown): value is ArrayBuffer | Buffer | Uint8Array | Uint8ClampedArray {
141142
return value instanceof ArrayBuffer || value instanceof Uint8Array || value instanceof Uint8ClampedArray;
142143
}
144+
145+
/**
146+
* Irrespective environment warning.
147+
*
148+
* @remarks Only the message is needed. The deprecation prefix is handled already.
149+
* @param message - A string the warning will emit with
150+
* @internal
151+
*/
152+
export function deprecationWarning(message: string) {
153+
if (typeof globalThis.process === 'undefined') {
154+
console.warn(`${DEPRECATION_WARNING_PREFIX}: ${message}`);
155+
} else {
156+
process.emitWarning(message, DEPRECATION_WARNING_PREFIX);
157+
}
158+
}

0 commit comments

Comments
 (0)