Closed uriva closed 1 month ago
Hello! We have forwarded the question to the developers.
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
@kocherovv still not working for me. Here's how to reproduce:
import greenApi from "@green-api/whatsapp-api-client";
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)
Checking
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.
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.
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?
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.
@AknurKh I know it's possible to use this in deno, the problem I have is:
@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.
@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)
@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";
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.