fix: Correct base path for GIF stickers (#10330)

* fix: correct base path for GIF stickers

* test: add sticker GIF

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Jiralite
2024-06-07 16:19:37 +01:00
committed by GitHub
parent 7f60a8fc5d
commit 599ad3eab5
5 changed files with 65 additions and 34 deletions

View File

@@ -46,6 +46,12 @@ export interface MakeURLOptions {
* The allowed extensions that can be used
*/
allowedExtensions?: readonly string[];
/**
* The base URL.
*
* @defaultValue `DefaultRestOptions.cdn`
*/
base?: string;
/**
* The extension to use for the image URL
*
@@ -62,7 +68,10 @@ export interface MakeURLOptions {
* The CDN link builder
*/
export class CDN {
public constructor(private readonly base: string = DefaultRestOptions.cdn) {}
public constructor(
private readonly cdn: string = DefaultRestOptions.cdn,
private readonly mediaProxy: string = DefaultRestOptions.mediaProxy,
) {}
/**
* Generates an app asset URL for a client's asset.
@@ -287,10 +296,15 @@ export class CDN {
* @param stickerId - The sticker id
* @param extension - The extension of the sticker
* @privateRemarks
* Stickers cannot have a `.webp` extension, so we default to a `.png`
* Stickers cannot have a `.webp` extension, so we default to a `.png`.
* Sticker GIFs do not use the CDN base URL.
*/
public sticker(stickerId: string, extension: StickerExtension = 'png'): string {
return this.makeURL(`/stickers/${stickerId}`, { allowedExtensions: ALLOWED_STICKER_EXTENSIONS, extension });
return this.makeURL(`/stickers/${stickerId}`, {
allowedExtensions: ALLOWED_STICKER_EXTENSIONS,
base: extension === 'gif' ? this.mediaProxy : this.cdn,
extension,
});
}
/**
@@ -352,7 +366,12 @@ export class CDN {
*/
private makeURL(
route: string,
{ allowedExtensions = ALLOWED_EXTENSIONS, extension = 'webp', size }: Readonly<MakeURLOptions> = {},
{
allowedExtensions = ALLOWED_EXTENSIONS,
base = this.cdn,
extension = 'webp',
size,
}: Readonly<MakeURLOptions> = {},
): string {
// eslint-disable-next-line no-param-reassign
extension = String(extension).toLowerCase();
@@ -365,7 +384,7 @@ export class CDN {
throw new RangeError(`Invalid size provided: ${size}\nMust be one of: ${ALLOWED_SIZES.join(', ')}`);
}
const url = new URL(`${this.base}${route}.${extension}`);
const url = new URL(`${base}${route}.${extension}`);
if (size) {
url.searchParams.set('size', String(size));

View File

@@ -75,7 +75,7 @@ export class REST extends AsyncEventEmitter<RestEvents> {
public constructor(options: Partial<RESTOptions> = {}) {
super();
this.cdn = new CDN(options.cdn ?? DefaultRestOptions.cdn);
this.cdn = new CDN(options.cdn ?? DefaultRestOptions.cdn, options.mediaProxy ?? DefaultRestOptions.mediaProxy);
this.options = { ...DefaultRestOptions, ...options };
this.globalRemaining = Math.max(1, this.options.globalRequestsPerSecond);
this.agent = options.agent ?? null;

View File

@@ -31,6 +31,7 @@ export const DefaultRestOptions = {
async makeRequest(...args): Promise<ResponseLike> {
return getDefaultStrategy()(...args);
},
mediaProxy: 'https://media.discordapp.net',
} as const satisfies Required<RESTOptions>;
/**

View File

@@ -85,6 +85,12 @@ export interface RESTOptions {
* For example, to use global fetch, simply provide `makeRequest: fetch`
*/
makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
/**
* The media proxy path
*
* @defaultValue `'https://media.discordapp.net'`
*/
mediaProxy: string;
/**
* The extra offset to add to rate limits in milliseconds
*