From 83695c6bb7b9fd0a41e55c3e3bc370efeac5feb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Raiti?= <46955459+Secreto31126@users.noreply.github.com> Date: Wed, 14 Aug 2024 07:23:52 -0300 Subject: [PATCH 1/5] Type fix --- src/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6eb01000..5b95ea09 100644 --- a/src/index.ts +++ b/src/index.ts @@ -322,8 +322,8 @@ export class WhatsAppAPI { message: ClientMessage, batch_size = 50, delay = 1000 - ): Array> { - const responses = [] as ReturnType[]; + ): Array> { + const responses = [] as ReturnType[]; if (batch_size < 1) { throw new RangeError("batch_size must be greater than 0"); From 811b587b82f17a9067d4878e392af65a535f1da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Raiti?= <46955459+Secreto31126@users.noreply.github.com> Date: Wed, 14 Aug 2024 07:25:16 -0300 Subject: [PATCH 2/5] No "slow types" Required to publish to jsr --- src/messages/contacts.ts | 4 ++-- src/messages/template.ts | 20 ++++++++++++-------- src/middleware/adonis.ts | 4 ++-- src/middleware/bun.ts | 4 ++-- src/middleware/deno.ts | 4 ++-- src/middleware/express.ts | 4 ++-- src/middleware/next.ts | 4 ++-- src/middleware/node-http.ts | 4 ++-- src/middleware/sveltekit.ts | 4 ++-- src/middleware/vercel.ts | 4 ++-- src/middleware/web-standard.ts | 4 ++-- 11 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/messages/contacts.ts b/src/messages/contacts.ts index e3f24f87..d8a42e4c 100644 --- a/src/messages/contacts.ts +++ b/src/messages/contacts.ts @@ -126,7 +126,7 @@ export class Contacts extends ClientMessage { * @override * @internal */ - _build() { + _build(): string { return JSON.stringify(this.component); } } @@ -245,7 +245,7 @@ export class Birthday extends ContactUniqueComponent { * @override * @internal */ - _build() { + _build(): string { return this.birthday; } } diff --git a/src/messages/template.ts b/src/messages/template.ts index 6ea749bf..77ca6262 100644 --- a/src/messages/template.ts +++ b/src/messages/template.ts @@ -157,7 +157,11 @@ export class Template extends ClientMessage { * @param code - The one time password to be sent * @returns A Template object for the API */ - static OTP(name: string, language: string | Language, code: string) { + static OTP( + name: string, + language: string | Language, + code: string + ): Template { return new Template( name, language, @@ -324,7 +328,7 @@ export abstract class ButtonComponent implements TemplateComponent { * @override * @internal */ - _build(pointers: BuildingPointers) { + _build(pointers: BuildingPointers): this { this.index = pointers.button_counter++; return this; } @@ -603,7 +607,7 @@ export class SkipButtonComponent extends ButtonComponent { * @override * @internal */ - _build(pointers: BuildingPointers) { + _build(pointers: BuildingPointers): this { pointers.button_counter++; return null as unknown as this; } @@ -638,7 +642,7 @@ export class HeaderComponent implements TemplateComponent { * @override * @internal */ - _build() { + _build(): this { return this; } } @@ -762,7 +766,7 @@ export class BodyComponent implements TemplateComponent { * @internal * @throws If theres_only_body is false and one of the parameters is a string and it's over 1024 characters */ - _build({ theres_only_body }: BuildingPointers) { + _build({ theres_only_body }: BuildingPointers): this { // If it needs to check for the shorter max text length if (!theres_only_body) { for (const param of this.parameters) { @@ -868,7 +872,7 @@ export class CarouselComponent * @override * @internal */ - _build() { + _build(): this { return this; } } @@ -919,7 +923,7 @@ export class CarouselCard implements ClientBuildableMessageComponent { * @override * @internal */ - _build(ptr: { counter: number }) { + _build(ptr: { counter: number }): this { this.card_index = ptr.counter++; return this; } @@ -974,7 +978,7 @@ export class LTOComponent implements TemplateComponent { * @override * @internal */ - _build() { + _build(): this { return this; } } diff --git a/src/middleware/adonis.ts b/src/middleware/adonis.ts index 7125cd5d..5fe46a61 100644 --- a/src/middleware/adonis.ts +++ b/src/middleware/adonis.ts @@ -31,7 +31,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @param req - The request object from AdonisJS * @returns The status code to be sent to the client */ - async handle_post(req: Request) { + async handle_post(req: Request): Promise { try { await this.post( req.body() as PostData, @@ -74,7 +74,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: Request) { + handle_get(req: Request): string { try { return this.get(req.qs() as GetParams); } catch (e) { diff --git a/src/middleware/bun.ts b/src/middleware/bun.ts index cf2dd955..4984c8d1 100644 --- a/src/middleware/bun.ts +++ b/src/middleware/bun.ts @@ -29,7 +29,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @param req - The request object * @returns The status code to be sent to the client */ - async handle_post(req: Request) { + async handle_post(req: Request): Promise { return super.handle_post(req); } @@ -61,7 +61,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: Request) { + handle_get(req: Request): string { return super.handle_get(req); } } diff --git a/src/middleware/deno.ts b/src/middleware/deno.ts index 35d2f407..39168e09 100644 --- a/src/middleware/deno.ts +++ b/src/middleware/deno.ts @@ -29,7 +29,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @param req - The request object * @returns The status code to be sent to the client */ - async handle_post(req: Request) { + async handle_post(req: Request): Promise { return super.handle_post(req); } @@ -61,7 +61,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: Request) { + handle_get(req: Request): string { return super.handle_get(req); } } diff --git a/src/middleware/express.ts b/src/middleware/express.ts index dc3d11c9..18933eba 100644 --- a/src/middleware/express.ts +++ b/src/middleware/express.ts @@ -41,7 +41,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @param req - The request object from Express.js * @returns The status code to be sent to the client */ - async handle_post(req: Request) { + async handle_post(req: Request): Promise { try { await this.post( JSON.parse(req.body ?? "{}"), @@ -85,7 +85,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: Request) { + handle_get(req: Request): string { try { return this.get(req.query as GetParams); } catch (e) { diff --git a/src/middleware/next.ts b/src/middleware/next.ts index 5cbc79f9..38c1016e 100644 --- a/src/middleware/next.ts +++ b/src/middleware/next.ts @@ -27,7 +27,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @param req - The request object * @returns The status code to be sent to the client */ - async handle_post(req: Request) { + async handle_post(req: Request): Promise { return super.handle_post(req); } @@ -57,7 +57,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: Request) { + handle_get(req: Request): string { return super.handle_get(req); } } diff --git a/src/middleware/node-http.ts b/src/middleware/node-http.ts index 495ce49e..f9ee552c 100644 --- a/src/middleware/node-http.ts +++ b/src/middleware/node-http.ts @@ -38,7 +38,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @param req - The request object * @returns The status code to be sent to the client */ - async handle_post(req: IncomingMessage) { + async handle_post(req: IncomingMessage): Promise { /** * Copy pasted from an issue on Deno's repository :) * @@ -101,7 +101,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: IncomingMessage) { + handle_get(req: IncomingMessage): string { try { return this.get( Object.fromEntries( diff --git a/src/middleware/sveltekit.ts b/src/middleware/sveltekit.ts index 11d4fa4b..304f3577 100644 --- a/src/middleware/sveltekit.ts +++ b/src/middleware/sveltekit.ts @@ -31,7 +31,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @param req - The request object * @returns The status code to be sent to the client */ - async handle_post(req: Request) { + async handle_post(req: Request): Promise { return super.handle_post(req); } @@ -63,7 +63,7 @@ export class WhatsAppAPI extends WebStandardMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: Request) { + handle_get(req: Request): string { return super.handle_get(req); } } diff --git a/src/middleware/vercel.ts b/src/middleware/vercel.ts index bcd20fac..d6552b1b 100644 --- a/src/middleware/vercel.ts +++ b/src/middleware/vercel.ts @@ -43,7 +43,7 @@ export class WhatsAppAPI extends NodeHTTPMiddleware { * @param req - The request object * @returns The status code to be sent to the client */ - handle_post(req: VercelRequest) { + handle_post(req: VercelRequest): Promise { return super.handle_post(req); } @@ -78,7 +78,7 @@ export class WhatsAppAPI extends NodeHTTPMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: VercelRequest) { + handle_get(req: VercelRequest): string { try { return this.get(req.query as GetParams); } catch (e) { diff --git a/src/middleware/web-standard.ts b/src/middleware/web-standard.ts index b626edc5..a0377c93 100644 --- a/src/middleware/web-standard.ts +++ b/src/middleware/web-standard.ts @@ -14,7 +14,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @param req - The request object * @returns The status code to be sent to the client */ - async handle_post(req: Request) { + async handle_post(req: Request): Promise { try { const body = await req.text(); @@ -39,7 +39,7 @@ export class WhatsAppAPI extends WhatsAppAPIMiddleware { * @returns The challenge string to be sent to the client * @throws The error code */ - handle_get(req: Request) { + handle_get(req: Request): string { try { return this.get( Object.fromEntries(new URL(req.url).searchParams) as GetParams From 607f92fd2a41dc07bd1fb9105031937ec74a7b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Raiti?= <46955459+Secreto31126@users.noreply.github.com> Date: Wed, 14 Aug 2024 07:34:59 -0300 Subject: [PATCH 3/5] Development QoL Autocompletition now always prompt the property "error", encouraging the users to always check for errors and get full type support for each branch (Response kinda breaks this unfortunately) --- src/types.ts | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/types.ts b/src/types.ts index 174bb492..39e06a1e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -790,10 +790,16 @@ export type ServerErrorResponse = { }; }; -export type ServerSuccessResponse = { - success: true; +export type NoServerError = { + error: never; }; +export type ServerSuccessResponse = + | { + success: true; + } + | NoServerError; + export type ServerSentMessageResponse = { messaging_product: "whatsapp"; contacts: [ @@ -811,7 +817,7 @@ export type ServerSentMessageResponse = { }; export type ServerMessageResponse = - | ServerSentMessageResponse + | (ServerSentMessageResponse | NoServerError) | ServerErrorResponse; export type ServerMarkAsReadResponse = @@ -825,15 +831,22 @@ export type ServerQR = { qr_image_url?: string; }; -export type ServerCreateQRResponse = ServerQR | ServerErrorResponse; +export type ServerCreateQRResponse = + | (ServerQR | NoServerError) + | ServerErrorResponse; export type ServerRetrieveQRResponse = - | { - data: ServerQR[]; - } + | ( + | { + data: ServerQR[]; + } + | NoServerError + ) | ServerErrorResponse; -export type ServerUpdateQRResponse = ServerQR | ServerErrorResponse; +export type ServerUpdateQRResponse = + | (ServerQR | NoServerError) + | ServerErrorResponse; export type ServerDeleteQRResponse = | ServerSuccessResponse @@ -843,7 +856,9 @@ export type ServerMedia = { id: string; }; -export type ServerMediaUploadResponse = ServerMedia | ServerErrorResponse; +export type ServerMediaUploadResponse = + | (ServerMedia | NoServerError) + | ServerErrorResponse; export type ValidMimeTypes = | "audio/aac" @@ -866,13 +881,16 @@ export type ValidMimeTypes = | "image/webp"; export type ServerMediaRetrieveResponse = - | ({ - messaging_product: "whatsapp"; - url: string; - mime_type: ValidMimeTypes; - sha256: string; - file_size: string; - } & ServerMedia) + | ( + | ({ + messaging_product: "whatsapp"; + url: string; + mime_type: ValidMimeTypes; + sha256: string; + file_size: string; + } & ServerMedia) + | NoServerError + ) | ServerErrorResponse; export type ServerMediaDeleteResponse = From bbcb4cdcff286540f259184b9496f8cc2fc0c78b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Raiti?= <46955459+Secreto31126@users.noreply.github.com> Date: Wed, 14 Aug 2024 08:17:42 -0300 Subject: [PATCH 4/5] Ugh --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4a33b0c5..4bc149d9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "whatsapp-api-js", - "version": "4.0.0", + "version": "4.0.1", "author": "Secreto31126", "description": "A TypeScript server agnostic Whatsapp's Official API framework", "license": "MIT", From 0f6697bd530dfef625bd729e0a0e41efbce7843f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Raiti?= <46955459+Secreto31126@users.noreply.github.com> Date: Wed, 14 Aug 2024 09:03:53 -0300 Subject: [PATCH 5/5] Docs update --- src/types.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/types.ts b/src/types.ts index 39e06a1e..16f9db94 100644 --- a/src/types.ts +++ b/src/types.ts @@ -145,10 +145,14 @@ export type WhatsAppAPIConstructorArguments = TheBasicConstructorArguments & export abstract class ClientMessage { /** * The message type + * + * @internal */ abstract get _type(): ClientMessageNames; /** * The message built as a string. In most cases it's just JSON.stringify(this) + * + * @internal */ _build(): string { return JSON.stringify(this); @@ -158,6 +162,8 @@ export abstract class ClientMessage { export interface ClientTypedMessageComponent { /** * The message's component type + * + * @internal */ get _type(): string; } @@ -165,6 +171,8 @@ export interface ClientTypedMessageComponent { export interface ClientBuildableMessageComponent { /** * The message's component builder method + * + * @internal */ _build(..._: unknown[]): unknown; } @@ -245,6 +253,7 @@ export abstract class ContactComponent { /** * @override + * @internal */ _build(): unknown { return this; @@ -252,8 +261,14 @@ export abstract class ContactComponent /** * Whether the component can be repeated multiple times in a contact. + * + * @internal */ abstract get _many(): boolean; + /** + * @override + * @internal + */ abstract get _type(): string; } @@ -263,11 +278,16 @@ export abstract class ContactComponent export abstract class ContactMultipleComponent extends ContactComponent { /** * @override + * @internal */ get _many(): true { return true; } + /** + * @override + * @internal + */ abstract get _type(): string; } @@ -277,11 +297,16 @@ export abstract class ContactMultipleComponent extends ContactComponent { export abstract class ContactUniqueComponent extends ContactComponent { /** * @override + * @internal */ get _many(): false { return false; } + /** + * @override + * @internal + */ abstract get _type(): string; } @@ -291,6 +316,7 @@ export abstract class ContactUniqueComponent extends ContactComponent { export interface InteractiveAction extends ClientTypedMessageComponent { /** * @overload + * @internal */ get _type(): | "list"