@corux/ask-extensions
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Alexa SDK Extensions

Travis (.com) npm npm type definitions Codacy grade

This package provides various extensions to improve development with Alexa Skills Kit SDK v2.

Install

npm install @corux/ask-extensions

Quick Start

The Alexa Extensions consist of two parts: generic code blocks for re-use in different skills and extensions to the ask-sdk framework.

ask-sdk extensions

Decorator Framework

The intention of the decorator framework is to provide an easily recognizable syntax to define request handlers. Decorators replace the canHandle() function of traditional request handlers. To use the decorators, the handler must inherit from BaseRequestHandler.

import {
  BaseRequestHandler, IExtendedHandlerInput,
  Fallback, Intents, Request,
} from "@corux/ask-extensions";

@Intents("AMAZON.StopIntent", "AMAZON.CancelIntent")
class StopIntentHandler extends BaseRequestHandler {
  public handle(handlerInput: IExtendedHandlerInput): Response {
    ...
  }
}

@Request("LaunchRequest")
class LaunchHandler extends BaseRequestHandler {
  public handle(handlerInput: IExtendedHandlerInput): Response {
    ...
  }
}

@Fallback()
class FallbackHandler extends BaseRequestHandler {
  public handle(handlerInput: IExtendedHandlerInput): Response {
    ...
  }
}

Response Builder

Use the extended response builder to conditionally build up a skill response.

class Handler extends BaseRequestHandler {
  public handle(handlerInput: IExtendedHandlerInput): Response {
    return handlerInput.getResponseBuilder()
      .speakIfSupported(...)
      .addRenderTemplateDirectiveIfSupported(...)
      .addHintDirectiveIfSupported(...)
      .addAplDirectiveIfSupported(...)
      .if(checkCondition, (builder) => { ... })
      .getResponse();
  }
}

Reusable code blocks

Handlers

See a list of available handlers here.

Interceptors

See a list of available interceptors here.

LocalizationInterceptor

The localization interceptor configures i18next for usage in the application. Localization files will be loaded from the filesystem.

SkillBuilders.custom()
  .addRequestInterceptors(
    new LocalizationInterceptor("./i18n/{{lng}}.json"),
  )

The i18next t function is added to both the IExtendedHandlerInput and the request attributes.

class Handler extends BaseRequestHandler {
  public handle(handlerInput: IExtendedHandlerInput): Response {
    handlerInput.attributesManager.getRequestAttributes().t("key");
    handlerInput.t("key");
  }
}

LogInterceptor

SkillBuilders.custom()
  .addRequestInterceptors(
    new LogInterceptor(),
  )
  .addResponseInterceptors(
    new LogInterceptor(),
  )

TimezoneInterceptor

Retrieves the device's timezone setting. The interceptor uses Luxon by default.

To access the settings service, the skill must be configured with an ApiClient.

SkillBuilders.custom()
  .addRequestInterceptors(
    new TimezoneInterceptor(),
  )
  .withApiClient(new DefaultApiClient())

Readme

Keywords

Package Sidebar

Install

npm i @corux/ask-extensions

Weekly Downloads

18

Version

2.0.0

License

Apache-2.0

Unpacked Size

47.2 kB

Total Files

54

Last publish

Collaborators

  • corux