zeyneloz / onesignal-node

A Node.js Library for OneSignal push notification service
MIT License
207 stars 48 forks source link

Typescript error with example code #46

Closed lludol closed 4 years ago

lludol commented 4 years ago

Hello,

I installed the latest version, I copy / paste the example code and there is a Typescript error.

The example code :

// ...
// using async/await
try {
  const response = await client.createNotification(notification);
  console.log(response.body.id); // <-- HERE
} catch (e) {
  if (e instanceof OneSignal.HTTPError) {
    // When status code of HTTP response is not 2xx, HTTPError is thrown.
    console.log(e.statusCode);
    console.log(e.body);
  }

The Typescript error:

Property 'id' does not exist on type '{}'

Why there is this error? Because of this in index.d.ts:

export interface ClientResponse {
    statusCode: number;
    body: {};
    headers: {
        [key: string]: any;
    };
}
zeyneloz commented 4 years ago

Hey @lludol ,

This is just a sample code and not meant to execute correctly for every situation. The exception basically says that there is no id property on body. You might try to log body itself and see the content: console.log(response.body);

lludol commented 4 years ago

Yes I know that I can check what is inside. But what's the point of having Typescript types defininitions if there are incomplete?

zeyneloz commented 4 years ago

My bad, I overlooked the error details you provided. I will open a bug-fix request for this ticket. Thanks for opening this issue.

zeyneloz commented 4 years ago

Fixed on https://github.com/zeyneloz/onesignal-node/pull/47 Released as v3.0.2