Skip to content

Commit

Permalink
Merge pull request #5 from great-detail/4-add-shortcut-methods-to-mes…
Browse files Browse the repository at this point in the history
…sageapi

4 add shortcut methods to messageapi
  • Loading branch information
domwebber authored Oct 18, 2023
2 parents 54887d3 + f74550a commit 32455a4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @domwebber
77 changes: 61 additions & 16 deletions src/MessageAPI/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ import StatusMessageType, {
import { TemplateObjectMessageType } from "../Message/TemplateMessageType";
import { TextObjectMessageType } from "../Message/TextObjectMessageType";

type CreateMessagePayloadType<C extends ComponentTypesEnum> =
| AudioObjectMediaMessageType
| [ContactsObjectMessageType]
| DocumentObjectMediaMessageType
| ImageObjectMediaMessageType
| InteractiveObjectMessageType
| LocationObjectMessageType
| TemplateObjectMessageType<C>
| StickerObjectMediaMessageType
| TextObjectMessageType
| VideoObjectMediaMessageType;

type CreateMessageOptionsType = {
toNumber: string;
replyMessageId?: string;
requestProps?: GraphRequestProps;
};

/**
* WhatsApp Message API.
*
Expand All @@ -41,7 +59,13 @@ export default class MessageAPI extends AbstractAPI {
);
}

public createStatusRead(
/**
* Create Status Message.
*
* @since 2.0.0
* @author Dom Webber <[email protected]>
*/
public createStatus(
payload: StatusObjectMessageType,
requestProps: GraphRequestProps = {},
) {
Expand All @@ -50,7 +74,7 @@ export default class MessageAPI extends AbstractAPI {
...payload,
};

return new GraphRequest({
return new GraphRequest<MessageResponseType>({
...requestProps,
endpoint: this.getEndpoint(),
method: "POST",
Expand All @@ -62,25 +86,19 @@ export default class MessageAPI extends AbstractAPI {
});
}

/**
* Create Message.
*
* @since 2.0.0
* @author Dom Webber <[email protected]>
*/
public createMessage<
T extends MessageTypesEnum,
C extends ComponentTypesEnum,
>(
type: T,
payload:
| AudioObjectMediaMessageType
| [ContactsObjectMessageType]
| DocumentObjectMediaMessageType
| ImageObjectMediaMessageType
| InteractiveObjectMessageType
| LocationObjectMessageType
| TemplateObjectMessageType<C>
| StickerObjectMediaMessageType
| TextObjectMessageType
| VideoObjectMediaMessageType,
toNumber: string,
replyMessageId?: string,
requestProps: GraphRequestProps = {},
payload: CreateMessagePayloadType<C>,
{ toNumber, replyMessageId, requestProps = {} }: CreateMessageOptionsType,
) {
const body: MessageType<T> = {
messaging_product: "whatsapp",
Expand All @@ -103,4 +121,31 @@ export default class MessageAPI extends AbstractAPI {
},
});
}

protected _shorthandAlias<
T extends MessageTypesEnum,
C extends ComponentTypesEnum,
P extends CreateMessagePayloadType<C>,
>(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);
}

0 comments on commit 32455a4

Please sign in to comment.