diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..e3aad03 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @domwebber \ No newline at end of file diff --git a/src/MessageAPI/index.ts b/src/MessageAPI/index.ts index 49ae8bb..4f0cf72 100644 --- a/src/MessageAPI/index.ts +++ b/src/MessageAPI/index.ts @@ -26,6 +26,24 @@ import StatusMessageType, { import { TemplateObjectMessageType } from "../Message/TemplateMessageType"; import { TextObjectMessageType } from "../Message/TextObjectMessageType"; +type CreateMessagePayloadType = + | AudioObjectMediaMessageType + | [ContactsObjectMessageType] + | DocumentObjectMediaMessageType + | ImageObjectMediaMessageType + | InteractiveObjectMessageType + | LocationObjectMessageType + | TemplateObjectMessageType + | StickerObjectMediaMessageType + | TextObjectMessageType + | VideoObjectMediaMessageType; + +type CreateMessageOptionsType = { + toNumber: string; + replyMessageId?: string; + requestProps?: GraphRequestProps; +}; + /** * WhatsApp Message API. * @@ -41,7 +59,13 @@ export default class MessageAPI extends AbstractAPI { ); } - public createStatusRead( + /** + * Create Status Message. + * + * @since 2.0.0 + * @author Dom Webber + */ + public createStatus( payload: StatusObjectMessageType, requestProps: GraphRequestProps = {}, ) { @@ -50,7 +74,7 @@ export default class MessageAPI extends AbstractAPI { ...payload, }; - return new GraphRequest({ + return new GraphRequest({ ...requestProps, endpoint: this.getEndpoint(), method: "POST", @@ -62,25 +86,19 @@ export default class MessageAPI extends AbstractAPI { }); } + /** + * Create Message. + * + * @since 2.0.0 + * @author Dom Webber + */ public createMessage< T extends MessageTypesEnum, C extends ComponentTypesEnum, >( type: T, - payload: - | AudioObjectMediaMessageType - | [ContactsObjectMessageType] - | DocumentObjectMediaMessageType - | ImageObjectMediaMessageType - | InteractiveObjectMessageType - | LocationObjectMessageType - | TemplateObjectMessageType - | StickerObjectMediaMessageType - | TextObjectMessageType - | VideoObjectMediaMessageType, - toNumber: string, - replyMessageId?: string, - requestProps: GraphRequestProps = {}, + payload: CreateMessagePayloadType, + { toNumber, replyMessageId, requestProps = {} }: CreateMessageOptionsType, ) { const body: MessageType = { messaging_product: "whatsapp", @@ -103,4 +121,31 @@ export default class MessageAPI extends AbstractAPI { }, }); } + + protected _shorthandAlias< + T extends MessageTypesEnum, + C extends ComponentTypesEnum, + P extends CreateMessagePayloadType, + >(type: T) { + const shorthandAliasFunction = function ( + this: MessageAPI, + payload: P, + options: CreateMessageOptionsType, + ) { + return this.createMessage(type, payload, options); + }; + + return shorthandAliasFunction.bind(this); + } + + public audio = this._shorthandAlias(MessageTypesEnum.Audio); + public contacts = this._shorthandAlias(MessageTypesEnum.Contacts); + public document = this._shorthandAlias(MessageTypesEnum.Document); + public image = this._shorthandAlias(MessageTypesEnum.Image); + public interactive = this._shorthandAlias(MessageTypesEnum.Interactive); + public location = this._shorthandAlias(MessageTypesEnum.Location); + public sticker = this._shorthandAlias(MessageTypesEnum.Sticker); + public template = this._shorthandAlias(MessageTypesEnum.Template); + public text = this._shorthandAlias(MessageTypesEnum.Text); + public video = this._shorthandAlias(MessageTypesEnum.Video); }