Vonage / vonage-node-sdk

Vonage API client for Node.js. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
Apache License 2.0
374 stars 178 forks source link

Argument of type '{ apiKey: string; apiSecret: string; }' is not assignable to parameter of type 'AuthInterface' #740

Closed fmasclef closed 1 year ago

fmasclef commented 1 year ago

Migrating from v2, I can't instantiate a Vonage object. From README.md :

const { Vonage } = require('@vonage/server-sdk');

const vonage = new Vonage({
    apiKey: API_KEY,
    apiSecret: API_SECRET,
    applicationId: APP_ID,
    privateKey: PRIVATE_KEY_PATH,
    signatureSecret: SIGNATURE_SECRET,
    signatureMethod: SIGNATURE_METHOD
  }, options);

To suit our coding guidelines, I use:

import { Vonage } from '@vonage/server-sdk';

const vonage = new Vonage({
    apiKey: ##REDACTED##,
    apiSecret: ##REDACTED##,
}, { debug: false });

I can't transpile with the following Typescript error:

error TS2345: Argument of type '{ apiKey: string; apiSecret: string; }' is not assignable to parameter of type 'AuthInterface'.
  Type '{ apiKey: string; apiSecret: string; }' is missing the following properties from type 'AuthInterface': getQueryParams, createSignatureHash, createBasicHeader, createBearerHeader

Expected Behavior

By the doc, Typescript shouldn't complain and transpile.

Current Behavior

It's not working mate :(

Possible Solution

???

Steps to Reproduce (for bugs)

Copy paste code I provide, then tsc -p .

Context

Your Environment

my tsconfig.json as follows:

{
  "compilerOptions": {
    "target": "es2019",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "../dist",
    "rootDir": ".",
    "removeComments": true,
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "noEmitOnError": true,
    "skipLibCheck": true,
  },
  "references": [
    { "path": "../" }
  ]
}
Kirween commented 1 year ago

Based on the Typescript declaration, this should work

import { Vonage } from '@vonage/server-sdk';
import { Auth } from '@vonage/auth';

const vonage = new Vonage( new Auth({
    apiKey: ##REDACTED##,
    apiSecret: ##REDACTED##,
}));
fmasclef commented 1 year ago

Thanks @Kirween. This obviously works. Someone to fix the README.md?

Kirween commented 1 year ago

Someone to fix the README.md?

It would be better to fix the type of the first parameter of the constructor

https://github.com/Vonage/vonage-node-sdk/blob/40ea2c74cc83ac78f46901c7fccff4213d1ffccc/packages/server-sdk/lib/vonage.ts#L28-L37

Should be

 constructor( 
     credentials: AuthInterface | AuthOpts, 
     options?: { 
...
dragonmantank commented 1 year ago

The issue is not the constructor, but the differences between JS and TS enforcing code.

Our code snippets are for JavaScript, which lets us have users pass in a similar credentials structure from previous versions. We then convert that into an Auth object internally. This is a convenience that JS affords us.

For TypeScript, we require you to pass in an Auth object. At the moment while we have much better TS support than before, our published code snippets are taken from a JavaScript perspective.

manchuck commented 1 year ago

@fmasclef Updating and Fixing the readmes is on my to-do for the week. I was at a conference the week after the V3 release and last week was short due to the holiday. Apologies for the issues with the readme

manchuck commented 1 year ago

@fmasclef This is addressed in #801

You can now use the ConfigParams, AuthInterface or AuthParams to overwrite the client constructor.