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

feat(ask-sdk-core/attributes) Add TypeScript Generics on the getSessionAttributes #554

Closed hideokamoto closed 5 years ago

hideokamoto commented 5 years ago

We can make own Alexa Skill more easily by using TypeScript features. I think to set generics on attributesManager is better to develop the skill. How do you think the idea?

Description

This is the first step to define own attributes type. I want to discuss and get feedback to set a type to the attributesManager.

Usage

const t: RequestHandler = {
  canHandle() {
    return true
  },
  handle(handlerInput) {
    const attributes = handlerInput.attributesManager.getSessionAttributes<{
      name: string,
      phone: number
    }>()
    const name = attributes.name
    if (attributes.phone) {
      return handlerInput.responseBuilder
        .speak(`Hello ${name}! Can I call you?`)
        .reprompt('Can I?')
        .getResponse()
    }
    return handlerInput.responseBuilder
      .speak(`Hello ${name}. Could you tell me your phone number?`)
      .reprompt('Could you tell me your phone number?')
      .getResponse()
  }
}

Motivation and Context

Now the attributesManager return values is always {[key: string: any}. But we want to define the type.

Testing

$  lerna --scope ask-sdk-core run gulp -- test

  110 passing (117ms)

=============================== Coverage summary ===============================
Statements   : 100% ( 351/351 )
Branches     : 100% ( 88/88 )
Functions    : 96.26% ( 103/107 )
Lines        : 100% ( 330/330 )
================================================================================

[10:49:43] Finished 'test' after 2.32 s
lerna success run Ran npm script 'gulp' in 1 package in 3.6s:
lerna success - ask-sdk-core

Screenshots (if appropriate)

We want

スクリーンショット 2019-05-26 10 55 41

Actual

スクリーンショット 2019-05-26 10 56 58

Types of changes

Checklist

License

hideokamoto commented 5 years ago

Hi ! Do you have any issue about the PR?

ShenChen93 commented 5 years ago

@hideokamoto Thanks for the PR. Approved and merged.

hideokamoto commented 5 years ago

Thanks !