Skip to content

Commit a66c422

Browse files
committed
refactor: change the rest
1 parent 1cdbac1 commit a66c422

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

packages/rest/__tests__/CDN.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test('avatar default', () => {
2323
});
2424

2525
test('avatar dynamic-animated', () => {
26-
expect(cdn.avatar(id, animatedHash)).toEqual(`${baseCDN}/avatars/${id}/${animatedHash}.gif`);
26+
expect(cdn.avatar(id, animatedHash)).toEqual(`${baseCDN}/avatars/${id}/${animatedHash}.webp?animated=true`);
2727
});
2828

2929
test('avatar dynamic-not-animated', () => {
@@ -68,7 +68,7 @@ test('guildMemberAvatar default', () => {
6868

6969
test('guildMemberAvatar dynamic-animated', () => {
7070
expect(cdn.guildMemberAvatar(id, id, animatedHash)).toEqual(
71-
`${baseCDN}/guilds/${id}/users/${id}/avatars/${animatedHash}.gif`,
71+
`${baseCDN}/guilds/${id}/users/${id}/avatars/${animatedHash}.webp?animated=true`,
7272
);
7373
});
7474

@@ -82,7 +82,7 @@ test('guildMemberBanner default', () => {
8282

8383
test('guildMemberBanner dynamic-animated', () => {
8484
expect(cdn.guildMemberBanner(id, id, animatedHash)).toEqual(
85-
`${baseCDN}/guilds/${id}/users/${id}/banners/${animatedHash}.gif`,
85+
`${baseCDN}/guilds/${id}/users/${id}/banners/${animatedHash}.webp?animated=true`,
8686
);
8787
});
8888

@@ -99,7 +99,7 @@ test('icon default', () => {
9999
});
100100

101101
test('icon dynamic-animated', () => {
102-
expect(cdn.icon(id, animatedHash)).toEqual(`${baseCDN}/icons/${id}/${animatedHash}.gif`);
102+
expect(cdn.icon(id, animatedHash)).toEqual(`${baseCDN}/icons/${id}/${animatedHash}.webp?animated=true`);
103103
});
104104

105105
test('icon dynamic-not-animated', () => {
@@ -145,5 +145,7 @@ test('makeURL throws on invalid extension', () => {
145145
});
146146

147147
test('makeURL valid size', () => {
148-
expect(cdn.avatar(id, animatedHash, { size: 512 })).toEqual(`${baseCDN}/avatars/${id}/${animatedHash}.gif?size=512`);
148+
expect(cdn.avatar(id, animatedHash, { size: 512 })).toEqual(
149+
`${baseCDN}/avatars/${id}/${animatedHash}.webp?animated=true&size=512`,
150+
);
149151
});

packages/rest/src/lib/CDN.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,62 @@ import {
1111
} from './utils/constants.js';
1212

1313
/**
14-
* The options used for image URLs
14+
* The options used for image URLs.
1515
*/
1616
export interface BaseImageURLOptions {
1717
/**
18-
* The extension to use for the image URL
18+
* The extension to use for the image URL.
1919
*
2020
* @defaultValue `'webp'`
2121
*/
2222
extension?: ImageExtension;
2323
/**
24-
* The size specified in the image URL
24+
* The size specified in the image URL.
2525
*/
2626
size?: ImageSize;
2727
}
2828

29-
interface EmojiURLOptionsWebp extends BaseImageURLOptions {
29+
export interface EmojiURLOptionsWebp extends BaseImageURLOptions {
3030
/**
3131
* Whether to use the `animated` query parameter.
32-
*
33-
* @remarks An animated custom emoji with the WebP format utilises this query paramter to be animated.
3432
*/
3533
animated?: boolean;
3634
extension?: 'webp';
3735
}
3836

39-
interface EmojiURLOptionsNotWebP extends BaseImageURLOptions {
37+
export interface EmojiURLOptionsNotWebp extends BaseImageURLOptions {
4038
extension: Exclude<ImageExtension, 'webp'>;
4139
}
4240

4341
/**
4442
* The options used for emoji URLs.
4543
*/
46-
export type EmojiURLOptions = EmojiURLOptionsNotWebP | EmojiURLOptionsWebp;
44+
export type EmojiURLOptions = EmojiURLOptionsNotWebp | EmojiURLOptionsWebp;
4745

48-
/**
49-
* The options used for image URLs with animated content
50-
*/
51-
export interface ImageURLOptions extends BaseImageURLOptions {
46+
export interface BaseAnimatedImageURLOptions extends BaseImageURLOptions {
5247
/**
53-
* Whether or not to prefer the static version of an image asset.
48+
* Whether to prefer the static asset.
5449
*/
5550
forceStatic?: boolean;
5651
}
5752

53+
export interface ImageURLOptionsWebp extends BaseAnimatedImageURLOptions {
54+
extension?: 'webp';
55+
}
56+
57+
export interface ImageURLOptionsNotWebp extends BaseAnimatedImageURLOptions {
58+
extension: Exclude<ImageExtension, 'webp'>;
59+
}
60+
61+
/**
62+
* The options used for image URLs that may be animated.
63+
*/
64+
export type ImageURLOptions = ImageURLOptionsNotWebp | ImageURLOptionsWebp;
65+
5866
/**
5967
* The options to use when making a CDN URL
6068
*/
61-
export interface MakeURLOptions {
69+
interface MakeURLOptions {
6270
/**
6371
* The allowed extensions that can be used
6472
*/
@@ -332,7 +340,7 @@ export class CDN {
332340
hash: string,
333341
{ forceStatic = false, ...options }: Readonly<ImageURLOptions> = {},
334342
): string {
335-
return this.makeURL(route, !forceStatic && hash.startsWith('a_') ? { ...options, extension: 'gif' } : options);
343+
return this.makeURL(route, !forceStatic && hash.startsWith('a_') ? { ...options, animated: true } : options);
336344
}
337345

338346
/**
@@ -364,14 +372,14 @@ export class CDN {
364372

365373
const url = new URL(`${base}${route}.${extension}`);
366374

367-
if (size) {
368-
url.searchParams.set('size', String(size));
369-
}
370-
371375
if (animated !== undefined) {
372376
url.searchParams.set('animated', String(animated));
373377
}
374378

379+
if (size) {
380+
url.searchParams.set('size', String(size));
381+
}
382+
375383
return url.toString();
376384
}
377385
}

0 commit comments

Comments
 (0)