Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed nested message stringify #393

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,7 @@ export class WhatsAppAPI<EmittersReturnType = void> {

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore - TS dumb, the _type will always match the type
request[type] =
// Prettier will probably kill me, but this comment has a purpose.
// It prevents ts-ignore from ignoring more than intended.
message._build();
request[type] = message;

if (context) request.context = { message_id: context };
if (biz_opaque_callback_data)
Expand Down
3 changes: 1 addition & 2 deletions src/messages/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ export class Contacts extends ClientMessage {
}

/**
* @override
* @internal
*/
_build(): string {
toJSON() {
return JSON.stringify(this.component);
}
}
Expand Down
43 changes: 24 additions & 19 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@
* feel free to read the docs :)
*/

import type {
Text,
Audio,
Document,
Image,
Sticker,
Video,
Location,
Interactive,
Template,
Reaction,
Contacts
} from "./messages";
import type { AtLeastOne } from "./utils";

export const DEFAULT_API_VERSION = "v20.0";
Expand Down Expand Up @@ -149,14 +162,6 @@ export abstract class ClientMessage {
* @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);
}
}

export interface ClientTypedMessageComponent {
Expand Down Expand Up @@ -381,47 +386,47 @@ export type ClientMessageRequest = {
} & (
| {
type: "text";
text?: string;
text?: Text;
}
| {
type: "audio";
audio?: string;
audio?: Audio;
}
| {
type: "document";
document?: string;
document?: Document;
}
| {
type: "image";
image?: string;
image?: Image;
}
| {
type: "sticker";
sticker?: string;
sticker?: Sticker;
}
| {
type: "video";
video?: string;
video?: Video;
}
| {
type: "location";
location?: string;
location?: Location;
}
| {
type: "contacts";
contacts?: string;
contacts?: Contacts;
}
| {
type: "interactive";
interactive?: string;
interactive?: Interactive;
}
| {
type: "template";
template?: string;
template?: Template;
}
| {
type: "reaction";
reaction?: string;
reaction?: Reaction;
}
);

Expand Down
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe("WhatsAppAPI", () => {
messaging_product: "whatsapp",
type,
to: user,
text: JSON.stringify(message)
text: message
};

const id = "4";
Expand Down Expand Up @@ -405,7 +405,7 @@ describe("WhatsAppAPI", () => {
messaging_product: "whatsapp",
type,
to: user,
text: JSON.stringify(message)
text: message
};

const requestWithContext = {
Expand Down
Loading