Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
feat(CDN): add sticker endpoints (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaporoxx authored Oct 18, 2021
1 parent 3e2edc8 commit 3b714ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
8 changes: 8 additions & 0 deletions packages/rest/__tests__/CDN.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ test('splash default', () => {
expect(cdn.splash(id, hash)).toBe(`${base}/splashes/${id}/${hash}.png`);
});

test('sticker default', () => {
expect(cdn.sticker(id)).toBe(`${base}/stickers/${id}.png`);
});

test('stickerPackBanner default', () => {
expect(cdn.stickerPackBanner(id)).toBe(`${base}/app-assets/710982414301790216/store/${id}.png`);
});

test('teamIcon default', () => {
expect(cdn.teamIcon(id, hash)).toBe(`${base}/team-icons/${id}/${hash}.png`);
});
Expand Down
47 changes: 40 additions & 7 deletions packages/rest/src/lib/CDN.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { ALLOWED_EXTENSIONS, ALLOWED_SIZES, DefaultRestOptions, ImageExtension, ImageSize } from './utils/constants';
import {
ALLOWED_EXTENSIONS,
ALLOWED_SIZES,
ALLOWED_STICKER_EXTENSIONS,
DefaultRestOptions,
ImageExtension,
ImageSize,
StickerExtension,
} from './utils/constants';

export interface ImageURLOptions {
extension?: ImageExtension;
size?: ImageSize;
dynamic?: boolean;
}

export interface MakeURLOptions {
extension?: string;
size?: ImageSize;
allowedExtensions?: readonly string[];
}

/**
* The CDN link builder
*/
Expand Down Expand Up @@ -147,6 +161,24 @@ export class CDN {
return this.makeURL(`/splashes/${guildId}/${splashHash}`, options);
}

/**
* Generates a sticker URL.
* @param stickerId The sticker id
* @param extension The extension of the sticker
*/
public sticker(stickerId: string, extension?: StickerExtension): string {
return this.makeURL(`/stickers/${stickerId}`, { allowedExtensions: ALLOWED_STICKER_EXTENSIONS, extension });
}

/**
* Generates a sticker pack banner URL.
* @param bannerId The banner id
* @param options Optional options for the banner
*/
public stickerPackBanner(bannerId: string, options?: ImageURLOptions): string {
return this.makeURL(`/app-assets/710982414301790216/store/${bannerId}`, options);
}

/**
* Generates a team icon URL for a team's icon.
* @param teamId The team id that has the icon
Expand All @@ -162,13 +194,14 @@ export class CDN {
* @param base The base cdn route
* @param options The extension/size options for the link
*/
private makeURL(base: string, { extension = 'png', size }: ImageURLOptions = {}): string {
extension = String(extension).toLowerCase() as ImageExtension;
private makeURL(
base: string,
{ allowedExtensions = ALLOWED_EXTENSIONS, extension = 'png', size }: MakeURLOptions = {},
): string {
extension = String(extension).toLowerCase();

if (!ALLOWED_EXTENSIONS.includes(extension)) {
throw new RangeError(
`Invalid extension provided: ${extension}\nMust be one of: ${ALLOWED_EXTENSIONS.join(', ')}`,
);
if (!allowedExtensions.includes(extension)) {
throw new RangeError(`Invalid extension provided: ${extension}\nMust be one of: ${allowedExtensions.join(', ')}`);
}

if (size && !ALLOWED_SIZES.includes(size)) {
Expand Down
2 changes: 2 additions & 0 deletions packages/rest/src/lib/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export const enum RESTEvents {
}

export const ALLOWED_EXTENSIONS = ['webp', 'png', 'jpg', 'jpeg', 'gif'] as const;
export const ALLOWED_STICKER_EXTENSIONS = ['png', 'json'] as const;
export const ALLOWED_SIZES = [16, 32, 64, 128, 256, 512, 1024, 2048, 4096] as const;

export type ImageExtension = typeof ALLOWED_EXTENSIONS[number];
export type StickerExtension = typeof ALLOWED_STICKER_EXTENSIONS[number];
export type ImageSize = typeof ALLOWED_SIZES[number];

0 comments on commit 3b714ba

Please sign in to comment.