diff --git a/.gitignore b/.gitignore index 6bf6340..1f65743 100644 --- a/.gitignore +++ b/.gitignore @@ -110,6 +110,7 @@ sw.* client_secret.json yarn.lock *.lock +*lock* package-lock.json *.zip @@ -118,4 +119,7 @@ package-lock.json .test.js .index.js .errors -experiments/ \ No newline at end of file +experiments/ + +#exclude playground +playground/ \ No newline at end of file diff --git a/package.json b/package.json index cdeb351..c8f5c6f 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ }, "dependencies": { "signale": "^1.4.0", - "unirest": "^0.6.0" + "unirest": "^0.6.0", + "zod": "^3.22.4" }, "keywords": [ "whatsapp", @@ -43,5 +44,8 @@ "homepage": "https://github.com/daggieblanqx/whatsappcloudapi_wrapper#readme", "engines": { "node": ">=10" + }, + "devDependencies": { + "typescript": "^5.2.2" } } diff --git a/types/whatsappcloudapi.d.ts b/types/whatsappcloudapi.d.ts new file mode 100644 index 0000000..9d86963 --- /dev/null +++ b/types/whatsappcloudapi.d.ts @@ -0,0 +1,29 @@ +import { + WhatsappCloudAPIConfig, + WhatsappCloudAPIText, + WhatsappCloudAPILocation, + WhatsappCloudAPIDocument, + WhatsappCloudAPISimpleButton, + WhatsappCloudAPIRadioButton, + WhatsappCloudAPIContact, + WhatsappCloudAPIQrCodeMessage, + WhatsappCloudAPIQrCodeResponse, + WhatsappCloudAPIMarkAsRead +} from "./whatsappcloudapi.types"; + +declare module 'whatsappcloudapi_wrapper' { + export default class WhatsappCloudAPI { + constructor(config: WhatsappCloudAPIConfig); + sendText(payload: WhatsappCloudAPIText): void; + sendLocation(payload: WhatsappCloudAPILocation ): void; + sendDocument(payload: WhatsappCloudAPIDocument): void; + sendImage(payload: WhatsappCloudAPIDocument): void; + sendVideo(payload: WhatsappCloudAPIDocument): void; + sendAudio(payload: WhatsappCloudAPIDocument): void; + sendSimpleButtons(payload: WhatsappCloudAPISimpleButton): void; + sendRadioButtons(payload: WhatsappCloudAPIRadioButton): void; + sendContact(payload: WhatsappCloudAPIContact): void; + createQRCodeMessage(payload: WhatsappCloudAPIQrCodeMessage): WhatsappCloudAPIQrCodeResponse; + markMessageAsRead(payload: WhatsappCloudAPIMarkAsRead): void; + } +} diff --git a/types/whatsappcloudapi.types.ts b/types/whatsappcloudapi.types.ts new file mode 100644 index 0000000..ee9ead4 --- /dev/null +++ b/types/whatsappcloudapi.types.ts @@ -0,0 +1,158 @@ +import {z} from "zod"; + +export const TextSchema = z.object({ + message: z.string({required_error: "Message is required"}), + recipientPhone: z.string().min(13, "Invalid Phone number") +}); +export type WhatsappCloudAPIText = z.infer; + +export const LocationSchema = z.object({ + recipientPhone: z.string().min(13, "Invalid Phone number"), + name: z.string({required_error: "Location name is required"}), + latitude: z.number().gte(-90).lte(90), + longitude: z.number().gte(-180).lte(180), + address: z.string({required_error: "Address is required"}) +}); +export type WhatsappCloudAPILocation = z.infer; + +export const DocumentSchema = z.object({ + recipientPhone: z.string().min(13, "Invalid Phone number"), + caption: z.string().optional(), + url: z.string().optional(), + file_path: z.string().optional(), +}); +export type WhatsappCloudAPIDocument = z.infer; + +export const ListOfButtonSchema = z.object({ + title: z.string(), + id: z.string(), +}); +export type WhatsappCloudAPIListOfButton = z.infer; + +export const SimpleButtonSchema = z.object({ + recipientPhone: z.string().min(13, "Invalid Phone number"), + message: z.string(), + listOfButtons: z.array(ListOfButtonSchema), +}); +export type WhatsappCloudAPISimpleButton = z.infer; + + +export const RowSchema = z.object({ + title: z.string(), + description: z.string(), + id: z.string(), +}); +export type WhatsappCloudAPIRow = z.infer; + +export const ListOfSectionSchema = z.object({ + title: z.string(), + rows: z.array(RowSchema), +}); +export type WhatsappCloudAPIListOfSection = z.infer; + +export const RadioButtonSchema = z.object({ + recipientPhone: z.string().min(13, "Invalid Phone number"), + headerText: z.string(), + bodyText: z.string(), + footerText: z.string(), + listOfSections: z.array(ListOfSectionSchema), +}); +export type WhatsappCloudAPIRadioButton = z.infer; + + + + +export const UrlSchema = z.object({ + url: z.string(), + type: z.string(), +}); +export type WhatsappCloudAPIUrl = z.infer; + +export const PhoneSchema = z.object({ + phone: z.string(), + type: z.string(), + wa_id: z.string(), +}); +export type WhatsappCloudAPIPhone = z.infer; + +export const OrgSchema = z.object({ + company: z.string(), + department: z.string(), + title: z.string(), +}); +export type WhatsappCloudAPIOrg = z.infer; + +export const NameSchema = z.object({ + formatted_name: z.string(), + first_name: z.string(), + last_name: z.string(), + middle_name: z.string(), + suffix: z.string(), + prefix: z.string(), +}); +export type WhatsappCloudAPIName = z.infer; + +export const EmailSchema = z.object({ + email: z.string(), + type: z.string(), +}); +export type WhatsappCloudAPIEmail = z.infer; + +export const AddressSchema = z.object({ + street: z.string(), + city: z.string(), + state: z.string(), + zip: z.string(), + country: z.string(), + country_code: z.string(), + type: z.string(), +}); +export type WhatsappCloudAPIAddress = z.infer; + +export const ContactProfileSchema = z.object({ + addresses: z.array(AddressSchema), + birthday: z.string(), + emails: z.array(EmailSchema), + name: NameSchema, + org: OrgSchema, + phones: z.array(PhoneSchema), + urls: z.array(UrlSchema), +}); +export type WhatsappCloudAPIContactProfile = z.infer; + +export const ContactSchema = z.object({ + recipientPhone: z.string().min(13, "Invalid Phone number"), + contact_profile: ContactProfileSchema, +}); +export type WhatsappCloudAPIContact = z.infer; + + +export const QrCodeMessageSchema = z.object({ + message: z.string(), + imageType: z.string(), +}); +export type WhatsappCloudAPIQrCodeMessage = z.infer; + +export const DataSchema = z.object({ + qr_image_url: z.string(), +}); +export type WhatsappCloudAPIData = z.infer; + +export const QrCodeResponseSchema = z.object({ + data: DataSchema, +}); +export type WhatsappCloudAPIQrCodeResponse = z.infer; + + +export const MarkAsReadSchema = z.object({ + message_id: z.string(), +}); +export type WhatsappCloudAPIMarkAsRead = z.infer; + + + +export type WhatsappCloudAPIConfig = { + accessToken: string, + senderPhoneNumberId: string, + WABA_ID: string +} \ No newline at end of file