refactor(CDN)!: Use an object for the constructor (#10978)

BREAKING CHANGE: The CDN class requires an object to construct.
This commit is contained in:
Jiralite
2025-07-11 15:44:25 +01:00
committed by GitHub
parent 35cc57ab92
commit 1f0fe39156
3 changed files with 28 additions and 6 deletions

View File

@@ -8,7 +8,7 @@ const hash = 'abcdef';
const animatedHash = 'a_bcdef';
const defaultAvatar = 1_234 % 5;
const cdn = new CDN(baseCDN, baseMedia);
const cdn = new CDN({ cdn: baseCDN, mediaProxy: baseMedia });
test('appAsset default', () => {
expect(cdn.appAsset(id, hash)).toEqual(`${baseCDN}/app-assets/${id}/${hash}.webp`);

View File

@@ -83,14 +83,36 @@ interface MakeURLOptions {
size?: ImageSize;
}
/**
* Options for initializing the {@link CDN} class.
*/
export interface CDNOptions {
/**
* The base URL for the CDN.
*
* @defaultValue `DefaultRestOptions.cdn`
*/
cdn?: string | undefined;
/**
* The base URL for the media proxy.
*
* @defaultValue `DefaultRestOptions.mediaProxy`
*/
mediaProxy?: string | undefined;
}
/**
* The CDN link builder
*/
export class CDN {
public constructor(
private readonly cdn: string = DefaultRestOptions.cdn,
private readonly mediaProxy: string = DefaultRestOptions.mediaProxy,
) {}
private readonly cdn: string;
private readonly mediaProxy: string;
public constructor({ cdn, mediaProxy }: CDNOptions = {}) {
this.cdn = cdn ?? DefaultRestOptions.cdn;
this.mediaProxy = mediaProxy ?? DefaultRestOptions.mediaProxy;
}
/**
* Generates an app asset URL for a client's asset.

View File

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