Skip to content

Commit

Permalink
Add Business Profile APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
domwebber committed Nov 20, 2024
1 parent 500e6e8 commit 9d5d62a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-singers-drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@great-detail/whatsapp": patch
---

Add Business Profile APIs
58 changes: 58 additions & 0 deletions src/BusinessProfile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* WhatsApp NodeJS SDK.
*
* @author Great Detail Ltd <[email protected]>
* @author Dom Webber <[email protected]>
* @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<Fields extends GetBusinessProfileFields = {}>({
phoneNumberID,
fields,
request,
}: MethodOptions & GetBusinessProfileOptions<Fields>) {
return ky.create({
...this._request,
method: "GET",
searchParams: {
fields: Object.entries(fields ?? {}).map(([key]) => key).join(","),
},
})<GetBusinessProfilePayload<Fields>>(
this.getEndpoint(phoneNumberID),
request,
);
}

public updateBusinessProfile({
phoneNumberID,
request,
...json
}: MethodOptions & UpdateBusinessProfileOptions) {
return ky.create({
...this._request,
method: "POST",
json: {
messaging_product: "whatsapp",
...json,
},
})<UpdateBusinessProfilePayload>(
this.getEndpoint(phoneNumberID),
request,
);
}
}
3 changes: 3 additions & 0 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
54 changes: 54 additions & 0 deletions src/types/BusinessProfile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* WhatsApp NodeJS SDK.
*
* @author Great Detail Ltd <[email protected]>
* @author Dom Webber <[email protected]>
* @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<Fields extends GetBusinessProfileFields = {}> = {
phoneNumberID: PhoneNumberID;
fields?: Fields;
}

export type GetBusinessProfilePayload<Fields extends GetBusinessProfileFields = {}> = {
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<unknown>))
: undefined;
websites: Fields extends { websites: true } ? ([string] | [string, string] | (string[] & NonNullable<unknown>)) : undefined;
}];
};

export type UpdateBusinessProfileOptions = {
phoneNumberID: PhoneNumberID;
about?: string;
address?: string;
description?: string;
email?: string;
profile_picture_handle?: string;
vertical?: ("" | (string & NonNullable<unknown>));
websites?: [string] | [string, string] | (string[] & NonNullable<unknown>);
}

export type UpdateBusinessProfilePayload = {
success: boolean;
}

0 comments on commit 9d5d62a

Please sign in to comment.