Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(CDN): Support emoji size #9787

Merged
merged 5 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -154,14 +157,39 @@ 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<BaseImageURLOptions>): 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<BaseImageURLOptions>): string {
let resolvedOptions;

if (typeof options === 'string') {
if (!deprecationEmittedForEmoji) {
deprecationWarning('Passing a string for CDN#emoji() is deprecated. Use an object instead.');
Jiralite marked this conversation as resolved.
Show resolved Hide resolved
deprecationEmittedForEmoji = true;
}

resolvedOptions = { extension: options };
} else {
resolvedOptions = options;
}

return this.makeURL(`/emojis/${emojiId}`, resolvedOptions);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,10 @@ export const OverwrittenMimeTypes = {
} as const satisfies Readonly<Record<string, string>>;

export const BurstHandlerMajorIdKey = 'burst';

/**
* Prefix for deprecation warnings.
*
* @internal
*/
export const DEPRECATION_WARNING_PREFIX = 'DeprecationWarning' as const;
16 changes: 16 additions & 0 deletions packages/rest/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -140,3 +141,18 @@ export async function sleep(ms: number): Promise<void> {
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);
}
}