mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
refactor(Constants): better type error in cdn endpoints (#6637)
This commit is contained in:
@@ -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'}`,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user