Skip to content

Commit

Permalink
feat: implement fromType
Browse files Browse the repository at this point in the history
  • Loading branch information
WingLim committed May 20, 2022
1 parent 9c96d7c commit ae52764
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/convenience/builders/input_media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,33 @@ import {
type InputMediaVideo,
} from "../../platform.deno.ts";

export class InputMediaBuilder {
class GenericInputMediaBuilder<T extends InputMedia = InputMedia> {
/**
* Initialize a new `InputMediaBuilder` with optional `InputMedia` array.
*
* @param media The media to initialize the builder with
*/
constructor(public readonly media: InputMedia[] = []) {
constructor(public readonly media: T[] = []) {
}
/**
* Allows you to add a `InputMedia`.
*
* @param media The media to add
*/
add(...media: InputMedia[]) {
add(...media: T[]) {
this.media.push(...media);
return this;
}
/**
* Return an array of `InputMedia` objects. You can use it
* in `sendMediaGroup`.
*/
build() {
return this.media;
}
}

export class InputMediaBuilder extends GenericInputMediaBuilder {
/**
* Add a `InputMediaPhoto`.
*
Expand Down Expand Up @@ -64,13 +74,6 @@ export class InputMediaBuilder {
document(document: Omit<InputMediaDocument, "type">) {
return this.add({ type: "document", ...document });
}
/**
* Return an array of `InputMedia` objects. You can use it
* in `sendMediaGroup`.
*/
build() {
return this.media;
}
/**
* Staic method to create an `InputMediaBuilder`.
*
Expand All @@ -79,4 +82,13 @@ export class InputMediaBuilder {
static from(media: InputMedia[]) {
return new InputMediaBuilder(media);
}
/**
* Static method to create an `InputMediaBuilder` from a `type`.
*
* @param media The media to add
* @param type The type of media and builder
*/
static fromType<T extends InputMedia = InputMedia>(media: Omit<T, "type">[], type: T["type"]) {
return new GenericInputMediaBuilder<T>(media.map((m) => ({ ...m, type } as T)));
}
}

0 comments on commit ae52764

Please sign in to comment.