alexa / alexa-skills-kit-sdk-for-nodejs

The Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
Apache License 2.0
3.12k stars 736 forks source link

Typescript : Request type definition for Skill Enabled | Disable events is incorrect #469

Closed sebsto closed 6 years ago

sebsto commented 6 years ago

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report  
[ ] Performance issue
[ ] Feature request
[X] Documentation issue or request
[ ] Other... Please describe:

TL;DR

SkillEnabledEvents and SkillDisabledEvent type definitions are not aligned with real life skill enabled | disabled events sent to a live skill.

This is a real event received by the skill.

{
  "version": "1.0",
  "context": {
    "System": {
      "application": {
        "applicationId": "amzn1.ask.skill.123"
      },
      "user": {
        "userId": "amzn1.ask.account.xxx"
      },
      "apiEndpoint": "https://api.eu.amazonalexa.com",
      "apiAccessToken": "xxx"
    }
  },
  "request": {
    "type": "AlexaSkillEvent.SkillEnabled",
    "requestId": "abc",
    "timestamp": "2018-09-20T14:15:20Z",
    "eventCreationTime": "2018-09-20T14:15:20Z",
    "eventPublishingTime": "2018-09-20T14:15:20Z"
  }
}

Issues :

Notice that the tech doc at https://developer.amazon.com/docs/smapi/skill-events-in-alexa-skills.html#skill-enabled-event also misses the latter two attributes.

The below diff fixes the type definition.

$ diff node_modules/ask-sdk-model/index.d.ts node_modules/ask-sdk-model/index.d.ts.orig
784c784
<         'device'?: Device;
---
>         'device': Device;
1478,1480c1478,1479
<         'eventCreationTime': string;
<         'eventPublishingTime': string;
<         }
---
>         'locale': string;
>     }
1491,1492c1490
<         'eventCreationTime': string;
<         'eventPublishingTime': string;
---
>         'locale': string;

Expected Behavior

Code like the below should compile and execute.

import * as r from './request/SkillEnabledEvent.json';
const request : RequestEnvelope = <RequestEnvelope>r;

with tsconfig.json

(...)
    "compilerOptions": {
        "alwaysStrict" : true,
        "target": "es2017",
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist/",
        "allowSyntheticDefaultImports": true
    }
(...)

Current Behavior

Code like the above crashes at runtime when executed from mocha, using ./node_modules/mocha/bin/mocha -r ts-node/register/type-check test/SkillEnabledTest.ts

TSError: ⨯ Unable to compile TypeScript
test/SkillEnabledTest.ts (12,35): Type '{ "version": string; "context": { "System": { "application": { "applicationId": string; }; "user": { "userId": string; "permissions": { "consentToken": string; }; }; "apiEndpoint": string; "apiAccessToken": string; }; }; "request": { ...; }; }' cannot be converted to type 'RequestEnvelope'.
  Types of property ''context'' are incompatible.
    Type '{ "System": { "application": { "applicationId": string; }; "user": { "userId": string; "permissions": { "consentToken": string; }; }; "apiEndpoint": string; "apiAccessToken": string; }; }' is not comparable to type 'Context'.
      Types of property ''System'' are incompatible.
        Type '{ "application": { "applicationId": string; }; "user": { "userId": string; "permissions": { "consentToken": string; }; }; "apiEndpoint": string; "apiAccessToken": string; }' is not comparable to type 'SystemState'.
          Property ''device'' is missing in type '{ "application": { "applicationId": string; }; "user": { "userId": string; "permissions": {"consentToken": string; }; }; "apiEndpoint": string; "apiAccessToken": string; }'. (2352)

Possible Solution

Modify index.d.ts from ask-sdk-model as this :

784c784
<         'device'?: Device;
---
>         'device': Device;
1478,1480c1478,1479
<         'eventCreationTime': string;
<         'eventPublishingTime': string;
<         }
---
>         'locale': string;
>     }
1491,1492c1490
<         'eventCreationTime': string;
<         'eventPublishingTime': string;
---
>         'locale': string;

Steps to Reproduce (for bugs)

Write a Mocha unit test with

import * as r from './request/SkillEnabledEvent.json';
const request : RequestEnvelope = <RequestEnvelope>r;

and execute with

./node_modules/mocha/bin/mocha -r ts-node/register/type-check test/SkillEnabledTest.ts

(somehow tsc does not catch the error, while type-check does)

Context

Unable to run my unit test.

Your Environment

Node.js and NPM Info

$ node --version && npm --version
v10.10.0
6.4.1
Chris-Liao commented 6 years ago

Hi @sebsto, thank you so much for pointing it out. We had a fix for this issue released this morning. Can you check in your side? Feel free to ask any question!

sebsto commented 6 years ago

Just tested using 2.0.10 and I confirm problem is fixed. Thank you for the quick turnaround.