refactor(builder): remove unsafe*Builders (#8074)

This commit is contained in:
Parbez
2022-07-07 00:12:51 +05:30
committed by GitHub
parent 34531c45e3
commit a4d1862982
24 changed files with 705 additions and 843 deletions

View File

@@ -1,4 +1,4 @@
import type { APIEmbedField } from 'discord-api-types/v10';
import type { APIEmbed, APIEmbedAuthor, APIEmbedField, APIEmbedFooter, APIEmbedImage } from 'discord-api-types/v10';
import {
colorPredicate,
descriptionPredicate,
@@ -11,84 +11,228 @@ import {
urlPredicate,
validateFieldLength,
} from './Assertions';
import { EmbedAuthorOptions, EmbedFooterOptions, RGBTuple, UnsafeEmbedBuilder } from './UnsafeEmbed';
import { normalizeArray, type RestOrArray } from '../../util/normalizeArray';
export type RGBTuple = [red: number, green: number, blue: number];
export interface IconData {
/**
* The URL of the icon
*/
iconURL?: string;
/**
* The proxy URL of the icon
*/
proxyIconURL?: string;
}
export type EmbedAuthorData = Omit<APIEmbedAuthor, 'icon_url' | 'proxy_icon_url'> & IconData;
export type EmbedAuthorOptions = Omit<EmbedAuthorData, 'proxyIconURL'>;
export type EmbedFooterData = Omit<APIEmbedFooter, 'icon_url' | 'proxy_icon_url'> & IconData;
export type EmbedFooterOptions = Omit<EmbedFooterData, 'proxyIconURL'>;
export interface EmbedImageData extends Omit<APIEmbedImage, 'proxy_url'> {
/**
* The proxy URL for the image
*/
proxyURL?: string;
}
/**
* Represents a validated embed in a message (image/video preview, rich embed, etc.)
* Represents a embed in a message (image/video preview, rich embed, etc.)
*/
export class EmbedBuilder extends UnsafeEmbedBuilder {
public override addFields(...fields: RestOrArray<APIEmbedField>): this {
export class EmbedBuilder {
public readonly data: APIEmbed;
public constructor(data: APIEmbed = {}) {
this.data = { ...data };
if (data.timestamp) this.data.timestamp = new Date(data.timestamp).toISOString();
}
/**
* Adds fields to the embed (max 25)
*
* @param fields The fields to add
*/
public addFields(...fields: RestOrArray<APIEmbedField>): this {
fields = normalizeArray(fields);
// Ensure adding these fields won't exceed the 25 field limit
validateFieldLength(fields.length, this.data.fields);
// Data assertions
return super.addFields(...embedFieldsArrayPredicate.parse(fields));
embedFieldsArrayPredicate.parse(fields);
if (this.data.fields) this.data.fields.push(...fields);
else this.data.fields = fields;
return this;
}
public override spliceFields(index: number, deleteCount: number, ...fields: APIEmbedField[]): this {
/**
* Removes, replaces, or inserts fields in the embed (max 25)
*
* @param index The index to start at
* @param deleteCount The number of fields to remove
* @param fields The replacing field objects
*/
public spliceFields(index: number, deleteCount: number, ...fields: APIEmbedField[]): this {
// Ensure adding these fields won't exceed the 25 field limit
validateFieldLength(fields.length - deleteCount, this.data.fields);
// Data assertions
return super.spliceFields(index, deleteCount, ...embedFieldsArrayPredicate.parse(fields));
embedFieldsArrayPredicate.parse(fields);
if (this.data.fields) this.data.fields.splice(index, deleteCount, ...fields);
else this.data.fields = fields;
return this;
}
public override setAuthor(options: EmbedAuthorOptions | null): this {
/**
* Sets the embed's fields (max 25).
* @param fields The fields to set
*/
public setFields(...fields: RestOrArray<APIEmbedField>) {
this.spliceFields(0, this.data.fields?.length ?? 0, ...normalizeArray(fields));
return this;
}
/**
* Sets the author of this embed
*
* @param options The options for the author
*/
public setAuthor(options: EmbedAuthorOptions | null): this {
if (options === null) {
return super.setAuthor(null);
this.data.author = undefined;
return this;
}
// Data assertions
embedAuthorPredicate.parse(options);
return super.setAuthor(options);
this.data.author = { name: options.name, url: options.url, icon_url: options.iconURL };
return this;
}
public override setColor(color: number | RGBTuple | null): this {
/**
* Sets the color of this embed
*
* @param color The color of the embed
*/
public setColor(color: number | RGBTuple | null): this {
// Data assertions
return super.setColor(colorPredicate.parse(color));
colorPredicate.parse(color);
if (Array.isArray(color)) {
const [red, green, blue] = color;
this.data.color = (red << 16) + (green << 8) + blue;
return this;
}
this.data.color = color ?? undefined;
return this;
}
public override setDescription(description: string | null): this {
/**
* Sets the description of this embed
*
* @param description The description
*/
public setDescription(description: string | null): this {
// Data assertions
return super.setDescription(descriptionPredicate.parse(description));
descriptionPredicate.parse(description);
this.data.description = description ?? undefined;
return this;
}
public override setFooter(options: EmbedFooterOptions | null): this {
/**
* Sets the footer of this embed
*
* @param options The options for the footer
*/
public setFooter(options: EmbedFooterOptions | null): this {
if (options === null) {
return super.setFooter(null);
this.data.footer = undefined;
return this;
}
// Data assertions
embedFooterPredicate.parse(options);
return super.setFooter(options);
this.data.footer = { text: options.text, icon_url: options.iconURL };
return this;
}
public override setImage(url: string | null): this {
/**
* Sets the image of this embed
*
* @param url The URL of the image
*/
public setImage(url: string | null): this {
// Data assertions
return super.setImage(imageURLPredicate.parse(url));
imageURLPredicate.parse(url);
this.data.image = url ? { url } : undefined;
return this;
}
public override setThumbnail(url: string | null): this {
/**
* Sets the thumbnail of this embed
*
* @param url The URL of the thumbnail
*/
public setThumbnail(url: string | null): this {
// Data assertions
return super.setThumbnail(imageURLPredicate.parse(url));
imageURLPredicate.parse(url);
this.data.thumbnail = url ? { url } : undefined;
return this;
}
public override setTimestamp(timestamp: number | Date | null = Date.now()): this {
/**
* Sets the timestamp of this embed
*
* @param timestamp The timestamp or date
*/
public setTimestamp(timestamp: number | Date | null = Date.now()): this {
// Data assertions
return super.setTimestamp(timestampPredicate.parse(timestamp));
timestampPredicate.parse(timestamp);
this.data.timestamp = timestamp ? new Date(timestamp).toISOString() : undefined;
return this;
}
public override setTitle(title: string | null): this {
/**
* Sets the title of this embed
*
* @param title The title
*/
public setTitle(title: string | null): this {
// Data assertions
return super.setTitle(titlePredicate.parse(title));
titlePredicate.parse(title);
this.data.title = title ?? undefined;
return this;
}
public override setURL(url: string | null): this {
/**
* Sets the URL of this embed
*
* @param url The URL
*/
public setURL(url: string | null): this {
// Data assertions
return super.setURL(urlPredicate.parse(url));
urlPredicate.parse(url);
this.data.url = url ?? undefined;
return this;
}
/**
* Transforms the embed to a plain object
*/
public toJSON(): APIEmbed {
return { ...this.data };
}
}