mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
refactor(Constants): better type error in cdn endpoints (#6637)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Package = (exports.Package = require('../../package.json'));
|
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}`;
|
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));
|
const AllowedImageSizes = Array.from({ length: 9 }, (e, i) => 2 ** (i + 4));
|
||||||
|
|
||||||
function makeImageUrl(root, { format = 'webp', size } = {}) {
|
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 (format && !AllowedImageFormats.includes(format)) throw new Error('IMAGE_FORMAT', format);
|
||||||
if (size && !AllowedImageSizes.includes(size)) throw new RangeError('IMAGE_SIZE', size);
|
if (size && !AllowedImageSizes.includes(size)) throw new RangeError('IMAGE_SIZE', size);
|
||||||
return `${root}.${format}${size ? `?size=${size}` : ''}`;
|
return `${root}.${format}${size ? `?size=${size}` : ''}`;
|
||||||
@@ -27,8 +28,7 @@ function makeImageUrl(root, { format = 'webp', size } = {}) {
|
|||||||
/**
|
/**
|
||||||
* Options for Image URLs.
|
* Options for Image URLs.
|
||||||
* @typedef {StaticImageURLOptions} ImageURLOptions
|
* @typedef {StaticImageURLOptions} ImageURLOptions
|
||||||
* @property {boolean} [dynamic] If true, the format will dynamically change to `gif` for
|
* @property {boolean} [dynamic=false] If true, the format will dynamically change to `gif` for animated avatars.
|
||||||
* animated avatars; the default is false
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,32 +45,28 @@ exports.Endpoints = {
|
|||||||
Emoji: (emojiId, format = 'webp') => `${root}/emojis/${emojiId}.${format}`,
|
Emoji: (emojiId, format = 'webp') => `${root}/emojis/${emojiId}.${format}`,
|
||||||
Asset: name => `${root}/assets/${name}`,
|
Asset: name => `${root}/assets/${name}`,
|
||||||
DefaultAvatar: discriminator => `${root}/embed/avatars/${discriminator}.png`,
|
DefaultAvatar: discriminator => `${root}/embed/avatars/${discriminator}.png`,
|
||||||
Avatar: (userId, hash, format = 'webp', size, dynamic = false) => {
|
Avatar: (userId, hash, format, size, dynamic = false) => {
|
||||||
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
|
if (dynamic && hash.startsWith('a_')) format = 'gif';
|
||||||
return makeImageUrl(`${root}/avatars/${userId}/${hash}`, { format, size });
|
return makeImageUrl(`${root}/avatars/${userId}/${hash}`, { format, size });
|
||||||
},
|
},
|
||||||
Banner: (id, hash, format = 'webp', size, dynamic = false) => {
|
Banner: (id, hash, format, size, dynamic = false) => {
|
||||||
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
|
if (dynamic && hash.startsWith('a_')) format = 'gif';
|
||||||
return makeImageUrl(`${root}/banners/${id}/${hash}`, { format, size });
|
return makeImageUrl(`${root}/banners/${id}/${hash}`, { format, size });
|
||||||
},
|
},
|
||||||
Icon: (guildId, hash, format = 'webp', size, dynamic = false) => {
|
Icon: (guildId, hash, format, size, dynamic = false) => {
|
||||||
if (dynamic) format = hash.startsWith('a_') ? 'gif' : format;
|
if (dynamic && hash.startsWith('a_')) format = 'gif';
|
||||||
return makeImageUrl(`${root}/icons/${guildId}/${hash}`, { format, size });
|
return makeImageUrl(`${root}/icons/${guildId}/${hash}`, { format, size });
|
||||||
},
|
},
|
||||||
AppIcon: (appId, hash, { format = 'webp', size } = {}) =>
|
AppIcon: (appId, hash, options) => makeImageUrl(`${root}/app-icons/${appId}/${hash}`, options),
|
||||||
makeImageUrl(`${root}/app-icons/${appId}/${hash}`, { size, format }),
|
AppAsset: (appId, hash, options) => makeImageUrl(`${root}/app-assets/${appId}/${hash}`, options),
|
||||||
AppAsset: (appId, hash, { format = 'webp', size } = {}) =>
|
StickerPackBanner: (bannerId, format, size) =>
|
||||||
makeImageUrl(`${root}/app-assets/${appId}/${hash}`, { size, format }),
|
|
||||||
StickerPackBanner: (bannerId, format = 'webp', size) =>
|
|
||||||
makeImageUrl(`${root}/app-assets/710982414301790216/store/${bannerId}`, { size, format }),
|
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 }),
|
makeImageUrl(`${root}/channel-icons/${channelId}/${hash}`, { size, format }),
|
||||||
Splash: (guildId, hash, format = 'webp', size) =>
|
Splash: (guildId, hash, format, size) => makeImageUrl(`${root}/splashes/${guildId}/${hash}`, { size, format }),
|
||||||
makeImageUrl(`${root}/splashes/${guildId}/${hash}`, { size, format }),
|
DiscoverySplash: (guildId, hash, format, size) =>
|
||||||
DiscoverySplash: (guildId, hash, format = 'webp', size) =>
|
|
||||||
makeImageUrl(`${root}/discovery-splashes/${guildId}/${hash}`, { size, format }),
|
makeImageUrl(`${root}/discovery-splashes/${guildId}/${hash}`, { size, format }),
|
||||||
TeamIcon: (teamId, hash, { format = 'webp', size } = {}) =>
|
TeamIcon: (teamId, hash, options) => makeImageUrl(`${root}/team-icons/${teamId}/${hash}`, options),
|
||||||
makeImageUrl(`${root}/team-icons/${teamId}/${hash}`, { size, format }),
|
|
||||||
Sticker: (stickerId, stickerFormat) =>
|
Sticker: (stickerId, stickerFormat) =>
|
||||||
`${root}/stickers/${stickerId}.${stickerFormat === 'LOTTIE' ? 'json' : 'png'}`,
|
`${root}/stickers/${stickerId}.${stickerFormat === 'LOTTIE' ? 'json' : 'png'}`,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user