diff --git a/.changeset/twelve-panthers-mix.md b/.changeset/twelve-panthers-mix.md new file mode 100644 index 0000000..d4bdd58 --- /dev/null +++ b/.changeset/twelve-panthers-mix.md @@ -0,0 +1,5 @@ +--- +"@great-detail/whatsapp": patch +--- + +Add Phone Numbers' APIs diff --git a/src/BusinessProfile/index.ts b/src/BusinessProfile/index.ts index 242be39..fe4cee7 100644 --- a/src/BusinessProfile/index.ts +++ b/src/BusinessProfile/index.ts @@ -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(","), }, })>( this.getEndpoint(phoneNumberID), diff --git a/src/Client.ts b/src/Client.ts index 920f604..0854cbf 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -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"; @@ -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; @@ -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(); diff --git a/src/PhoneNumbers/index.ts b/src/PhoneNumbers/index.ts new file mode 100644 index 0000000..c028df7 --- /dev/null +++ b/src/PhoneNumbers/index.ts @@ -0,0 +1,70 @@ +/** + * WhatsApp NodeJS SDK. + * + * @author Great Detail Ltd + * @author Dom Webber + * @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({ + phoneNumberID, + fields, + request, + }: MethodOptions & GetPhoneNumberOptions) { + return ky.create({ + ...this._request, + method: "GET", + searchParams: { + fields: Object.keys(fields ?? {}).join(","), + }, + })>( + encodeURIComponent(phoneNumberID), + request, + ); + } + + public listPhoneNumbers({ + businessAccountID, + sort, + filtering, + request, + }: MethodOptions & ListPhoneNumbersOptions) { + return ky.create({ + ...this._request, + method: "GET", + searchParams: { + ...(sort + ? { + sort, + } + : {}), + ...(filtering + ? { + filtering, + } + : {}), + }, + })(this.getEndpoint(businessAccountID), request); + } +} diff --git a/src/index.ts b/src/index.ts index b269111..d8831e2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/types/PhoneNumbers/index.ts b/src/types/PhoneNumbers/index.ts new file mode 100644 index 0000000..80bcc64 --- /dev/null +++ b/src/types/PhoneNumbers/index.ts @@ -0,0 +1,73 @@ +/** + * WhatsApp NodeJS SDK. + * + * @author Great Detail Ltd + * @author Dom Webber + * @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); + 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) + : undefined; + code_verification_status: "VERIFIED" | (string & NonNullable); + display_phone_number: string; + quality_rating: string; + platform_type: string; + throughput: { + level: string; + }; + last_onboarded_time?: string; + webhook_configuration: { + application?: string; + }; +};