green-api / whatsapp-api-client-js

This library helps you easily create a javascript application with Whatsapp support
https://green-api.com
MIT License
126 stars 32 forks source link

types are not detected in a deno project #145

Closed uriva closed 1 month ago

uriva commented 1 month ago
import * as greenApi from "npm:@green-api/whatsapp-api-client@0.4.0-0";
error TS7016: Could not find a declaration file for module '@green-api/whatsapp-api-client'. '/home/runner/work/abstract-bot-api/abstract-bot-api/dist/node_modules/@green-api/whatsapp-api-client/lib/bundle.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/green-api__whatsapp-api-client` if it exists or add a new declaration (.d.ts) file containing `declare module '@green-api/whatsapp-api-client';`
astashenkoj commented 1 month ago

Hello! We have forwarded the question to the developers.

kocherovv commented 1 month ago

Hello! There are several ways to import the library in a project, please use one of them: https://github.com/green-api/whatsapp-api-client-js/blob/master/README.md

image

uriva commented 1 month ago

@kocherovv still not working for me. Here's how to reproduce:

  1. open a directory
  2. cd into it, yarn init
  3. yarn add @green-api/whatsapp-api-client
  4. touch index.ts
  5. add import greenApi from "@green-api/whatsapp-api-client";
  6. hover over it and get a message saying type information is unavailable
uriva commented 1 month ago

the exact message:

Could not find a declaration file for module '@green-api/whatsapp-api-client'. '/home/uri/uriva/bla/node_modules/@green-api/whatsapp-api-client/lib/bundle.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/green-api__whatsapp-api-client` if it exists or add a new declaration (.d.ts) file containing `declare module '@green-api/whatsapp-api-client';`ts(7016)
kocherovv commented 1 month ago

Checking

astashenkoj commented 1 month ago

Hello! Thank you for waiting! Try creating a global.d.ts file (you can choose any name) with the following content: declare module '@green-api/whatsapp-api-client';

You also need to add the following to tsconfig: "compilerOptions": { ... "typeRoots": ["./node_modules/@types", "./global.d.ts"] }

After which, the compiler works, there should be no errors.

uriva commented 1 month ago

The problem is that as I'm using Deno, I don't have a tsconfig nor does it do any good to have a global.d.ts file.

uriva commented 1 month ago

I mean there is this: https://github.com/denoland/deno/pull/2746

But why doesn't it work like other libraries ootb? Shouldn't that be the solution?

prostraction commented 1 month ago

Hello, @uriva! I don't have an experience with Deno, but I successfully imported this library using it. As I can see, tsconfig.json or global.d.ts is not required using Deno, but it is required using tsc. Here is what i did:

Install all necessary tools (npm is already installed):

curl -fsSL https://deno.land/install.sh | sh
export DENO_INSTALL="/root/.deno"
export PATH="$DENO_INSTALL/bin:$PATH"
npm install -g npm
npm install --global yarn

Init project:

yarn init
yarn add @green-api/whatsapp-api-client

Add code:

touch index.ts

index.ts file:

import whatsAppClient from "npm:@green-api/whatsapp-api-client";
Deno.serve((_req: Request) => {
    const restAPI = whatsAppClient.restAPI({
        idInstance: "ID",
        apiTokenInstance: "APIToken",
    });
    restAPI.message.sendMessage("Number@c.us", null, "hello world").then((data) => {
        console.log(data);
    });
    return new Response("Hello, world");
});

And now I have "Hello world" example with imported library working: Shell 1:

# deno run --allow-net index.ts
Listening on http://0.0.0.0:8000/
✅ Granted all env access.
✅ Granted all read access.
{ idMessage: "BAE52B1C00F05BC6" }

Shell 2:

# curl localhost:8000 && echo ""
Hello, world

I used this import:

import whatsAppClient from "npm:@green-api/whatsapp-api-client";

not

import greenApi from "npm:@green-api/whatsapp-api-client"

because of example code in Readme.

uriva commented 1 month ago

@AknurKh I know it's possible to use this in deno, the problem I have is:

  1. vscode doesn't recognize the types, so I don't have code assistance
  2. look at this: https://github.com/uriva/abstract-bot-api/blob/1687ffd8e85a84cbabe8165e350ecaf89d3e0a50/src/greenApi.ts#L54 I have to reverse engineers the types myself because I can't import them
prostraction commented 1 month ago

@uriva Unfortunately, we can't help you with this issue, because it doesn't depend on us. This library correctly imports to Deno project and there are no errors during the execution of it. Please look up to the libraries import process of your VS Code.

uriva commented 1 month ago

@prostraction you are not answering the second question - is it possible to import the API types to use in my code?

e.g. import { GreenApiMessage } from "npm:@green-api/whatsapp-api-client@0.4.0-0"; doesn't seem to work

(this has nothing to do with vscode)

prostraction commented 1 month ago

@uriva it's not possible to import the API types using this library.

Code like this:

import { messageApi } from "npm:@green-api/whatsapp-api-client@0.4.0-0";

causes:

error: Uncaught SyntaxError: The requested module 'npm:@green-api/whatsapp-api-client@0.4.0-0' does not provide an export named 'messageApi'

The default import, which you can find at readme, should work:

import whatsAppClient from "npm:@green-api/whatsapp-api-client";

uriva commented 1 month ago

Okay, so I propose a feature request to expose the API types.

They are essential for writing typescript code that uses this library, like in the example I shared above.