Secreto31126 / whatsapp-api-js

A TypeScript server agnostic Whatsapp's Official API framework
MIT License
139 stars 31 forks source link

error TS2307: Cannot find module 'whatsapp-api-js/messages' #113

Closed 50c10 closed 1 year ago

50c10 commented 1 year ago

when i try to import whatsapp-api-js/messages i get the error message. this only happens wen import some message

import WhatsAppAPI from "whatsapp-api-js"
import { Text } from "whatsapp-api-js/messages";
Secreto31126 commented 1 year ago

Hi! Which is your whatsapp-api-js version? Keep in mind the modular import syntax is for the 1.0.0 version

50c10 commented 1 year ago

i try with 1.0.0-beta.5

Secreto31126 commented 1 year ago

Great, let me know if it works so I can mark the issue as complete

arivanbastos commented 1 year ago

Same problem here. 1.0.0-beta.5 / commonjs Some workaround? Cant run the library in my ts project. @50c10 , have you overcome this?

Secreto31126 commented 1 year ago

I managed to replicate the issue, I'm 100% sure it's a configuration issue with the exports at package.json.

I'm reading the documentation to understand how's the "types" property meant to be used. Tomorrow I have a 2 and a half hour flight, so I might be able to find a solution on the go.

No workaround other than @ts-ignore the issue unfortunately, as it's only a types problem :/

Secreto31126 commented 1 year ago

I finally came across the GitHub issue at TS saying why this specific error isn't "supported":

https://github.com/microsoft/TypeScript/issues/33079

Unfortunately, this unsupported feature was how I supposed exports were meant to be used with TypeScript.

Under the hood possible fixes:

  1. NPM workspaces is the recommended solution at the GitHub issue (I think), but I can't implement it right now.

  2. Build all the types into a single file, although I'm not even sure that's possible

  3. Creating duplicated types definitions for module and commonjs folders, removing the need to point to a different folder (not ideal, but the easiest to implement)

Unfortunately, until I settle down which solution to use, the only fix is ts-ignore

Thanks for opening and commenting on the issue!

Further reading: https://github.com/andrewbranch/example-subpath-exports-ts-compat

50c10 commented 1 year ago

@arivanbastos no, i still have the problem

Secreto31126 commented 1 year ago

Hi! I finally fixed the issue with minimal changes to the package.json, so there are no breaking changes.

Check out the PR, I won't be able to publish a new release from my phone in at least a few hours, but it's easy to update locally:

  1. Copy the new package.json from the pull request

  2. Replace your_code/node_modules/whatsapp-api-js/package.json with the new file version

Let me know if this fixes the issue for both of you, and I will merge the PR

Thanks for the issue!

arivanbastos commented 1 year ago

@Secreto31126 thnk you for reply. The error remains here, even after replacing node_modules/whatsapp-api-js/package.json by the one present in PR. But It can be a local problem cause I've changed some files to make it work and I'm not 100% sure I undo all these changes. Want to hear @50c10 feedback.

@50c10 If the PR package.json solution doesnt work I was able to bypass typescript type errors by:

1) Add the following to the end of node_modules/whatapp-api-js/lib/types/index.d.ts export { GetParams, ClientMessage } from "./types.js";

2) Create a class that implements ClientMessage (this is a bit dumb solution cause this class is equals to the Text lib class...).

import { ClientMessage } from "whatsapp-api-js";
export class TextMessage implements ClientMessage {
    body: string;
    preview_url: boolean;

    get _type(): string {
        return "text";
    }

    constructor(body: string, preview_url: boolean) {
        if (body.length > 4096) {
            throw new Error("Text body must be less than 4096 characters");
        }

        this.body = body;
        this.preview_url = preview_url;
    }

    _build() {
        return JSON.stringify(this);
    }
}

3) Use the class created at step 3 to send messages;

whatsappAPI.sendMessage(phoneID, phoneNumber, new TextMessage(messageText, true))
    .then((res)=>{}) 
    .catch((err)=>{});

I had to import GetParams type also, cause I got typescript type errors when using the whatsappAPI.get() method (for webhook validation proccess).

      let params: GetParams = {
          'hub.mode': 'subscribe',
          'hub.verify_token': (urlRequestParams['hub.verify_token'] as string) || '',
          'hub.challenge': (urlRequestParams['hub.challenge'] as string) || '',
      };
      whatsappAPI.get(params);
50c10 commented 1 year ago

@arivanbastos. I tried the solution and it worked fine. Thanks @Secreto31126

Secreto31126 commented 1 year ago

@arivanbastos. I tried the solution and it worked fine. Thanks @Secreto31126

Thanks for the confirmation! I will be merging the branch in a few hours and pushing a release to npm.