From 124e177e91d1cee3566c784ab8efad2a0c6a9519 Mon Sep 17 00:00:00 2001 From: Rodry <38259440+ImRodry@users.noreply.github.com> Date: Thu, 23 Sep 2021 12:46:40 +0100 Subject: [PATCH] refactor(Constants): better type error in cdn endpoints (#6637) --- src/util/Constants.js | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/util/Constants.js b/src/util/Constants.js index c59f4b5b2..a1ef04230 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -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}`; @@ -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}` : ''}`; @@ -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. */ /** @@ -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'}`, };