mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01:00
feat!: Support animated WebP (#10911)
* feat: support animated WebP * refactor: change the rest * fix: remove redundant code
This commit is contained in:
@@ -11,27 +11,44 @@ import {
|
||||
} from './utils/constants.js';
|
||||
|
||||
/**
|
||||
* The options used for image URLs
|
||||
* The options used for image URLs.
|
||||
*/
|
||||
export interface BaseImageURLOptions {
|
||||
/**
|
||||
* The extension to use for the image URL
|
||||
* The extension to use for the image URL.
|
||||
*
|
||||
* @defaultValue `'webp'`
|
||||
*/
|
||||
extension?: ImageExtension;
|
||||
/**
|
||||
* The size specified in the image URL
|
||||
* The size specified in the image URL.
|
||||
*/
|
||||
size?: ImageSize;
|
||||
}
|
||||
|
||||
export interface EmojiURLOptionsWebp extends BaseImageURLOptions {
|
||||
/**
|
||||
* Whether to use the `animated` query parameter.
|
||||
*/
|
||||
animated?: boolean;
|
||||
extension?: 'webp';
|
||||
}
|
||||
|
||||
export interface EmojiURLOptionsNotWebp extends BaseImageURLOptions {
|
||||
extension: Exclude<ImageExtension, 'webp'>;
|
||||
}
|
||||
|
||||
/**
|
||||
* The options used for image URLs with animated content
|
||||
* The options used for emoji URLs.
|
||||
*/
|
||||
export type EmojiURLOptions = EmojiURLOptionsNotWebp | EmojiURLOptionsWebp;
|
||||
|
||||
/**
|
||||
* The options used for image URLs that may be animated.
|
||||
*/
|
||||
export interface ImageURLOptions extends BaseImageURLOptions {
|
||||
/**
|
||||
* Whether or not to prefer the static version of an image asset.
|
||||
* Whether to prefer the static asset.
|
||||
*/
|
||||
forceStatic?: boolean;
|
||||
}
|
||||
@@ -39,11 +56,15 @@ export interface ImageURLOptions extends BaseImageURLOptions {
|
||||
/**
|
||||
* The options to use when making a CDN URL
|
||||
*/
|
||||
export interface MakeURLOptions {
|
||||
interface MakeURLOptions {
|
||||
/**
|
||||
* The allowed extensions that can be used
|
||||
*/
|
||||
allowedExtensions?: readonly string[];
|
||||
/**
|
||||
* Whether to use the `animated` query parameter
|
||||
*/
|
||||
animated?: boolean;
|
||||
/**
|
||||
* The base URL.
|
||||
*
|
||||
@@ -162,11 +183,10 @@ export class CDN {
|
||||
* Generates an emoji's URL.
|
||||
*
|
||||
* @param emojiId - The emoji id
|
||||
* @param animated - Whether the emoji is animated
|
||||
* @param options - Optional options for the emoji
|
||||
*/
|
||||
public emoji(emojiId: string, animated: boolean, options?: Readonly<ImageURLOptions>): string {
|
||||
return this.dynamicMakeURL(`/emojis/${emojiId}`, animated ? 'a_' : '', options);
|
||||
public emoji(emojiId: string, options?: Readonly<EmojiURLOptions>): string {
|
||||
return this.makeURL(`/emojis/${emojiId}`, options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -310,7 +330,7 @@ export class CDN {
|
||||
hash: string,
|
||||
{ forceStatic = false, ...options }: Readonly<ImageURLOptions> = {},
|
||||
): string {
|
||||
return this.makeURL(route, !forceStatic && hash.startsWith('a_') ? { ...options, extension: 'gif' } : options);
|
||||
return this.makeURL(route, !forceStatic && hash.startsWith('a_') ? { ...options, animated: true } : options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -326,6 +346,7 @@ export class CDN {
|
||||
base = this.cdn,
|
||||
extension = 'webp',
|
||||
size,
|
||||
animated,
|
||||
}: Readonly<MakeURLOptions> = {},
|
||||
): string {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
@@ -341,6 +362,10 @@ export class CDN {
|
||||
|
||||
const url = new URL(`${base}${route}.${extension}`);
|
||||
|
||||
if (animated !== undefined) {
|
||||
url.searchParams.set('animated', String(animated));
|
||||
}
|
||||
|
||||
if (size) {
|
||||
url.searchParams.set('size', String(size));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user