From e0c53a4a1b36a18a98d693a3641d121bb6f797a7 Mon Sep 17 00:00:00 2001 From: Dom Webber Date: Wed, 18 Oct 2023 11:20:13 +0100 Subject: [PATCH 1/5] refactor: Move createMessage options to object BREAKING CHANGE: toNumber, replyMessageId, requestProps parameters for MessageAPI.createMessage have moved to the options object --- src/MessageAPI/index.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/MessageAPI/index.ts b/src/MessageAPI/index.ts index 49ae8bb..c198fbf 100644 --- a/src/MessageAPI/index.ts +++ b/src/MessageAPI/index.ts @@ -26,6 +26,12 @@ import StatusMessageType, { import { TemplateObjectMessageType } from "../Message/TemplateMessageType"; import { TextObjectMessageType } from "../Message/TextObjectMessageType"; +type CreateMessageOptionsType = { + toNumber: string, + replyMessageId?: string, + requestProps?: GraphRequestProps, +}; + /** * WhatsApp Message API. * @@ -68,19 +74,17 @@ export default class MessageAPI extends AbstractAPI { >( type: T, payload: - | AudioObjectMediaMessageType - | [ContactsObjectMessageType] - | DocumentObjectMediaMessageType - | ImageObjectMediaMessageType - | InteractiveObjectMessageType - | LocationObjectMessageType - | TemplateObjectMessageType - | StickerObjectMediaMessageType - | TextObjectMessageType - | VideoObjectMediaMessageType, - toNumber: string, - replyMessageId?: string, - requestProps: GraphRequestProps = {}, + | AudioObjectMediaMessageType + | [ContactsObjectMessageType] + | DocumentObjectMediaMessageType + | ImageObjectMediaMessageType + | InteractiveObjectMessageType + | LocationObjectMessageType + | TemplateObjectMessageType + | StickerObjectMediaMessageType + | TextObjectMessageType + | VideoObjectMediaMessageType, + { toNumber, replyMessageId, requestProps = {} }: CreateMessageOptionsType ) { const body: MessageType = { messaging_product: "whatsapp", From b9b88c51cbe9b9f07bc7eff36fa305e01ff22bd6 Mon Sep 17 00:00:00 2001 From: Dom Webber Date: Wed, 18 Oct 2023 11:38:13 +0100 Subject: [PATCH 2/5] feat: Introduce shorthand access for message creation --- src/MessageAPI/index.ts | 71 ++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 15 deletions(-) diff --git a/src/MessageAPI/index.ts b/src/MessageAPI/index.ts index c198fbf..fabe344 100644 --- a/src/MessageAPI/index.ts +++ b/src/MessageAPI/index.ts @@ -26,10 +26,22 @@ 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, + toNumber: string; + replyMessageId?: string; + requestProps?: GraphRequestProps; }; /** @@ -47,6 +59,12 @@ export default class MessageAPI extends AbstractAPI { ); } + /** + * Create Status Read Message. + * + * @since 2.0.0 + * @author Dom Webber + */ public createStatusRead( payload: StatusObjectMessageType, requestProps: GraphRequestProps = {}, @@ -68,23 +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, replyMessageId, requestProps = {} }: CreateMessageOptionsType + payload: CreateMessagePayloadType, + { toNumber, replyMessageId, requestProps = {} }: CreateMessageOptionsType, ) { const body: MessageType = { messaging_product: "whatsapp", @@ -107,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); } From ba9611d4e4840a7c34c1454910f31b38aaede622 Mon Sep 17 00:00:00 2001 From: Dom Webber Date: Wed, 18 Oct 2023 11:39:06 +0100 Subject: [PATCH 3/5] refactor: Rename createReadStatus to createStatus BREAKING CHANGE: Changes createReadStatus to createStatus, calls to such method should be renamed --- src/MessageAPI/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MessageAPI/index.ts b/src/MessageAPI/index.ts index fabe344..b363a79 100644 --- a/src/MessageAPI/index.ts +++ b/src/MessageAPI/index.ts @@ -60,12 +60,12 @@ export default class MessageAPI extends AbstractAPI { } /** - * Create Status Read Message. + * Create Status Message. * * @since 2.0.0 * @author Dom Webber */ - public createStatusRead( + public createStatus( payload: StatusObjectMessageType, requestProps: GraphRequestProps = {}, ) { From 647f8f4824f08556b0de1d367e805636c1a4c05d Mon Sep 17 00:00:00 2001 From: Dom Webber Date: Wed, 18 Oct 2023 11:40:17 +0100 Subject: [PATCH 4/5] feat: Add response typing to createStatus GraphRequest instances --- src/MessageAPI/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MessageAPI/index.ts b/src/MessageAPI/index.ts index b363a79..4f0cf72 100644 --- a/src/MessageAPI/index.ts +++ b/src/MessageAPI/index.ts @@ -74,7 +74,7 @@ export default class MessageAPI extends AbstractAPI { ...payload, }; - return new GraphRequest({ + return new GraphRequest({ ...requestProps, endpoint: this.getEndpoint(), method: "POST", From f74550aca2c598fd41995a21fcaaeee9a0fb028e Mon Sep 17 00:00:00 2001 From: Dom Webber Date: Wed, 18 Oct 2023 11:45:42 +0100 Subject: [PATCH 5/5] chore: Introduce codeowners --- .github/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/CODEOWNERS 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