-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from great-detail/4-add-shortcut-methods-to-mes…
…sageapi 4 add shortcut methods to messageapi
- Loading branch information
Showing
2 changed files
with
62 additions
and
16 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @domwebber |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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 = {}, | ||
) { | ||
|
@@ -50,7 +74,7 @@ export default class MessageAPI extends AbstractAPI { | |
...payload, | ||
}; | ||
|
||
return new GraphRequest({ | ||
return new GraphRequest<MessageResponseType>({ | ||
...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 <[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", | ||
|
@@ -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); | ||
} |