From 9d5d62a08b062b02c24e2db0ca2e9e0c76e3b7b5 Mon Sep 17 00:00:00 2001 From: Dom Webber Date: Wed, 20 Nov 2024 19:56:09 +0000 Subject: [PATCH] Add Business Profile APIs --- .changeset/brave-singers-drop.md | 5 +++ src/BusinessProfile/index.ts | 58 ++++++++++++++++++++++++++++++ src/Client.ts | 3 ++ src/index.ts | 2 ++ src/types/BusinessProfile/index.ts | 54 ++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+) create mode 100644 .changeset/brave-singers-drop.md create mode 100644 src/BusinessProfile/index.ts create mode 100644 src/types/BusinessProfile/index.ts diff --git a/.changeset/brave-singers-drop.md b/.changeset/brave-singers-drop.md new file mode 100644 index 0000000..2a24c07 --- /dev/null +++ b/.changeset/brave-singers-drop.md @@ -0,0 +1,5 @@ +--- +"@great-detail/whatsapp": patch +--- + +Add Business Profile APIs diff --git a/src/BusinessProfile/index.ts b/src/BusinessProfile/index.ts new file mode 100644 index 0000000..764de3a --- /dev/null +++ b/src/BusinessProfile/index.ts @@ -0,0 +1,58 @@ +/** + * WhatsApp NodeJS SDK. + * + * @author Great Detail Ltd + * @author Dom Webber + * @see https://greatdetail.com + */ + +import ky, { Options as KyOptions } from "ky"; +import { PhoneNumberID } from "../types/PhoneNumber.js"; +import { GetBusinessProfileFields, GetBusinessProfileOptions, GetBusinessProfilePayload, UpdateBusinessProfileOptions, UpdateBusinessProfilePayload } from "../types/BusinessProfile/index.js"; + +interface MethodOptions { + request?: KyOptions; +} + +export default class BusinessProfile { + constructor(protected _request: KyOptions) {} + + protected getEndpoint(phoneNumberID: PhoneNumberID) { + return encodeURIComponent(phoneNumberID) + "/whatsapp_business_profile"; + } + + public getBusinessProfile({ + phoneNumberID, + fields, + request, + }: MethodOptions & GetBusinessProfileOptions) { + return ky.create({ + ...this._request, + method: "GET", + searchParams: { + fields: Object.entries(fields ?? {}).map(([key]) => key).join(","), + }, + })>( + this.getEndpoint(phoneNumberID), + request, + ); + } + + public updateBusinessProfile({ + phoneNumberID, + request, + ...json + }: MethodOptions & UpdateBusinessProfileOptions) { + return ky.create({ + ...this._request, + method: "POST", + json: { + messaging_product: "whatsapp", + ...json, + }, + })( + this.getEndpoint(phoneNumberID), + request, + ); + } +} diff --git a/src/Client.ts b/src/Client.ts index 7f5cc72..920f604 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -6,6 +6,7 @@ * @see https://greatdetail.com */ +import BusinessProfile from "./BusinessProfile/index.js"; import Media from "./Media/index.js"; import Message from "./Message/index.js"; import SubscribedApps from "./SubscribedApps/index.js"; @@ -20,6 +21,7 @@ export interface Options { export default class Client { protected _request: KyOptions; + public businessProfile: BusinessProfile; public message: Message; public subscribedApps: SubscribedApps; public media: Media; @@ -33,6 +35,7 @@ export default class Client { prefixUrl: prefixUrl.replace(/\/$/, "") + graphVersion, }; + this.businessProfile = new BusinessProfile(this._request); this.message = new Message(this._request); this.subscribedApps = new SubscribedApps(this._request); this.media = new Media(this._request); diff --git a/src/index.ts b/src/index.ts index 5592fba..b269111 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,8 @@ export { default, default as CloudAPI, default as Client } from "./Client.js"; +export type * from "./BusinessProfile/index.js"; + export type * from "./types/Message/index.js"; export type * from "./types/Message/MessageContact.js"; export type * from "./types/Message/MessageIdentity.js"; diff --git a/src/types/BusinessProfile/index.ts b/src/types/BusinessProfile/index.ts new file mode 100644 index 0000000..02a4e83 --- /dev/null +++ b/src/types/BusinessProfile/index.ts @@ -0,0 +1,54 @@ +/** + * WhatsApp NodeJS SDK. + * + * @author Great Detail Ltd + * @author Dom Webber + * @see https://greatdetail.com + */ + +import { PhoneNumberID } from "../PhoneNumber.js" + +export type GetBusinessProfileFields = { + about?: boolean; + address?: boolean; + description?: boolean; + email?: boolean; + profile_picture_url?: boolean; + websites?: boolean; + vertical?: boolean; +}; + +export type GetBusinessProfileOptions = { + phoneNumberID: PhoneNumberID; + fields?: Fields; +} + +export type GetBusinessProfilePayload = { + data: [{ + about: Fields extends { about: true } ? string : undefined; + address: Fields extends { address: true } ? string : undefined; + description: Fields extends { description: true } ? string : undefined; + email: Fields extends { email: true } ? string : undefined; + messaging_product: "whatsapp"; + profile_picture_url: Fields extends { profile_picture_url: true } ? string : undefined; + vertical: Fields extends { vertical: true } + ? ("" | (string & NonNullable)) + : undefined; + websites: Fields extends { websites: true } ? ([string] | [string, string] | (string[] & NonNullable)) : undefined; + }]; +}; + +export type UpdateBusinessProfileOptions = { + phoneNumberID: PhoneNumberID; + about?: string; + address?: string; + description?: string; + email?: string; + profile_picture_handle?: string; + vertical?: ("" | (string & NonNullable)); + websites?: [string] | [string, string] | (string[] & NonNullable); +} + +export type UpdateBusinessProfilePayload = { + success: boolean; +}