Skip to content

Commit

Permalink
Merge pull request #265 from domwebber/main
Browse files Browse the repository at this point in the history
Add Phone Numbers' APIs
  • Loading branch information
domwebber authored Nov 20, 2024
2 parents 14309e9 + 2b88c0e commit cf547f4
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-panthers-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@great-detail/whatsapp": patch
---

Add Phone Numbers' APIs
4 changes: 1 addition & 3 deletions src/BusinessProfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export default class BusinessProfile {
...this._request,
method: "GET",
searchParams: {
fields: Object.entries(fields ?? {})
.map(([key]) => key)
.join(","),
fields: Object.keys(fields ?? {}).join(","),
},
})<GetBusinessProfilePayload<Fields>>(
this.getEndpoint(phoneNumberID),
Expand Down
3 changes: 3 additions & 0 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import BusinessProfile from "./BusinessProfile/index.js";
import Media from "./Media/index.js";
import Message from "./Message/index.js";
import PhoneNumbers from "./PhoneNumbers/index.js";
import SubscribedApps from "./SubscribedApps/index.js";
import Webhook from "./Webhook/index.js";
import type { Options as KyOptions } from "ky";
Expand All @@ -23,6 +24,7 @@ export default class Client {

public businessProfile: BusinessProfile;
public message: Message;
public phoneNumbers: PhoneNumbers;
public subscribedApps: SubscribedApps;
public media: Media;
public webhook: Webhook;
Expand All @@ -37,6 +39,7 @@ export default class Client {

this.businessProfile = new BusinessProfile(this._request);
this.message = new Message(this._request);
this.phoneNumbers = new PhoneNumbers(this._request);
this.subscribedApps = new SubscribedApps(this._request);
this.media = new Media(this._request);
this.webhook = new Webhook();
Expand Down
70 changes: 70 additions & 0 deletions src/PhoneNumbers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* 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 { BusinessAccountID } from "../types/BusinessAccount.js";
import {
GetPhoneNumberFields,
GetPhoneNumberOptions,
GetPhoneNumberPayload,
ListPhoneNumbersOptions,
ListPhoneNumbersPaylod,
} from "../types/PhoneNumbers/index.js";

interface MethodOptions {
request?: KyOptions;
}

export default class PhoneNumbers {
constructor(protected _request: KyOptions) {}

public getEndpoint(businessAccountID: BusinessAccountID) {
return encodeURIComponent(businessAccountID) + "/phone_numbers";
}

public getPhoneNumber<Fields extends GetPhoneNumberFields = object>({
phoneNumberID,
fields,
request,
}: MethodOptions & GetPhoneNumberOptions) {
return ky.create({
...this._request,
method: "GET",
searchParams: {
fields: Object.keys(fields ?? {}).join(","),
},
})<GetPhoneNumberPayload<Fields>>(
encodeURIComponent(phoneNumberID),
request,
);
}

public listPhoneNumbers({
businessAccountID,
sort,
filtering,
request,
}: MethodOptions & ListPhoneNumbersOptions) {
return ky.create({
...this._request,
method: "GET",
searchParams: {
...(sort
? {
sort,
}
: {}),
...(filtering
? {
filtering,
}
: {}),
},
})<ListPhoneNumbersPaylod>(this.getEndpoint(businessAccountID), request);
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type * from "./types/Message/MessageReferral.js";
export type * from "./types/Message/MessageSystem.js";
export type * from "./types/Message/MessageText.js";

export type * from "./PhoneNumbers/index.js";

export type * from "./types/SubscribedApps/index.js";

export type * from "./types/Webhook/WebhookEventNotification.js";
Expand Down
73 changes: 73 additions & 0 deletions src/types/PhoneNumbers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* WhatsApp NodeJS SDK.
*
* @author Great Detail Ltd <[email protected]>
* @author Dom Webber <[email protected]>
* @see https://greatdetail.com
*/

import { BusinessAccountID } from "../BusinessAccount.js";
import { PhoneNumberID } from "../PhoneNumber.js";

export type ListPhoneNumbersOptions = {
businessAccountID: BusinessAccountID;
sort?: string;
filtering?: string;
};

export type ListPhoneNumbersPaylod = {
data: {
id: PhoneNumberID;
verified_name: string;
code_verification_status: "VERIFIED" | (string & NonNullable<unknown>);
display_phone_number: string;
quality_rating: string;
platform_type: string;
throughput: {
level: string;
};
last_onboarded_time?: string;
webhook_configuration: {
application?: string;
};
}[];
};

export type GetPhoneNumberFields = {
name_status?: boolean;
};

export type GetPhoneNumberOptions<
Fields extends GetPhoneNumberFields = object,
> = {
phoneNumberID: PhoneNumberID;
fields?: Fields;
};

export type GetPhoneNumberPayload<
Fields extends GetPhoneNumberFields = object,
> = {
id: PhoneNumberID;
verified_name: string;
name_status: Fields extends { name_status: true }
?
| "APPROVED"
| "AVAILABLE_WITHOUT_REVIEW"
| "DECLINED"
| "EXPIRED"
| "PENDING_REVIEW"
| "NONE"
| (string & NonNullable<unknown>)
: undefined;
code_verification_status: "VERIFIED" | (string & NonNullable<unknown>);
display_phone_number: string;
quality_rating: string;
platform_type: string;
throughput: {
level: string;
};
last_onboarded_time?: string;
webhook_configuration: {
application?: string;
};
};

0 comments on commit cf547f4

Please sign in to comment.