Skip to content

Commit

Permalink
refactor(Constants): better type error in cdn endpoints (#6637)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImRodry committed Sep 23, 2021
1 parent 0347826 commit 124e177
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/util/Constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Package = (exports.Package = require('../../package.json'));
const { Error, RangeError } = require('../errors');
const { Error, RangeError, TypeError } = require('../errors');

exports.UserAgent = `DiscordBot (${Package.homepage.split('#')[0]}, ${Package.version}) Node.js/${process.version}`;

Expand All @@ -19,6 +19,7 @@ const AllowedImageFormats = ['webp', 'png', 'jpg', 'jpeg', 'gif'];
const AllowedImageSizes = Array.from({ length: 9 }, (e, i) => 2 ** (i + 4));

function makeImageUrl(root, { format = 'webp', size } = {}) {
if (typeof size !== 'number') throw new TypeError('INVALID_TYPE', 'size', 'number');
if (format && !AllowedImageFormats.includes(format)) throw new Error('IMAGE_FORMAT', format);
if (size && !AllowedImageSizes.includes(size)) throw new RangeError('IMAGE_SIZE', size);
return `${root}.${format}${size ? `?size=${size}` : ''}`;
Expand All @@ -27,8 +28,7 @@ function makeImageUrl(root, { format = 'webp', size } = {}) {
/**
* Options for Image URLs.
* @typedef {StaticImageURLOptions} ImageURLOptions
* @property {boolean} [dynamic] If true, the format will dynamically change to `gif` for
* animated avatars; the default is false
* @property {boolean} [dynamic=false] If true, the format will dynamically change to `gif` for animated avatars.
*/

/**
Expand All @@ -45,32 +45,28 @@ exports.Endpoints = {
Emoji: (emojiId, format = 'webp') => `${root}/emojis/${emojiId}.${format}`,
Asset: name => `${root}/assets/${name}`,
DefaultAvatar: discriminator => `${root}/embed/avatars/${discriminator}.png`,
Avatar: (userId, hash, format = 'webp', size, dynamic = false) => {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
Avatar: (userId, hash, format, size, dynamic = false) => {
if (dynamic && hash.startsWith('a_')) format = 'gif';
return makeImageUrl(`${root}/avatars/${userId}/${hash}`, { format, size });
},
Banner: (id, hash, format = 'webp', size, dynamic = false) => {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
Banner: (id, hash, format, size, dynamic = false) => {
if (dynamic && hash.startsWith('a_')) format = 'gif';
return makeImageUrl(`${root}/banners/${id}/${hash}`, { format, size });
},
Icon: (guildId, hash, format = 'webp', size, dynamic = false) => {
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
Icon: (guildId, hash, format, size, dynamic = false) => {
if (dynamic && hash.startsWith('a_')) format = 'gif';
return makeImageUrl(`${root}/icons/${guildId}/${hash}`, { format, size });
},
AppIcon: (appId, hash, { format = 'webp', size } = {}) =>
makeImageUrl(`${root}/app-icons/${appId}/${hash}`, { size, format }),
AppAsset: (appId, hash, { format = 'webp', size } = {}) =>
makeImageUrl(`${root}/app-assets/${appId}/${hash}`, { size, format }),
StickerPackBanner: (bannerId, format = 'webp', size) =>
AppIcon: (appId, hash, options) => makeImageUrl(`${root}/app-icons/${appId}/${hash}`, options),
AppAsset: (appId, hash, options) => makeImageUrl(`${root}/app-assets/${appId}/${hash}`, options),
StickerPackBanner: (bannerId, format, size) =>
makeImageUrl(`${root}/app-assets/710982414301790216/store/${bannerId}`, { size, format }),
GDMIcon: (channelId, hash, format = 'webp', size) =>
GDMIcon: (channelId, hash, format, size) =>
makeImageUrl(`${root}/channel-icons/${channelId}/${hash}`, { size, format }),
Splash: (guildId, hash, format = 'webp', size) =>
makeImageUrl(`${root}/splashes/${guildId}/${hash}`, { size, format }),
DiscoverySplash: (guildId, hash, format = 'webp', size) =>
Splash: (guildId, hash, format, size) => makeImageUrl(`${root}/splashes/${guildId}/${hash}`, { size, format }),
DiscoverySplash: (guildId, hash, format, size) =>
makeImageUrl(`${root}/discovery-splashes/${guildId}/${hash}`, { size, format }),
TeamIcon: (teamId, hash, { format = 'webp', size } = {}) =>
makeImageUrl(`${root}/team-icons/${teamId}/${hash}`, { size, format }),
TeamIcon: (teamId, hash, options) => makeImageUrl(`${root}/team-icons/${teamId}/${hash}`, options),
Sticker: (stickerId, stickerFormat) =>
`${root}/stickers/${stickerId}.${stickerFormat === 'LOTTIE' ? 'json' : 'png'}`,
};
Expand Down

0 comments on commit 124e177

Please sign in to comment.