From 81aa948c91cae5808ed4abb1266d64933de790f3 Mon Sep 17 00:00:00 2001 From: petebeegle Date: Thu, 4 Aug 2016 19:32:11 -0400 Subject: [PATCH] add preliminary Request/Response typings --- alexa-sdk/alexa-sdk-tests.ts | 13 +++-- alexa-sdk/alexa-sdk.d.ts | 99 ++++++++++++++++++++++++++++++++---- 2 files changed, 97 insertions(+), 15 deletions(-) diff --git a/alexa-sdk/alexa-sdk-tests.ts b/alexa-sdk/alexa-sdk-tests.ts index 4a6941b1d0..4ed5910f59 100644 --- a/alexa-sdk/alexa-sdk-tests.ts +++ b/alexa-sdk/alexa-sdk-tests.ts @@ -3,20 +3,23 @@ import Alexa = require("alexa-sdk"); -exports.handler = function(event: Alexa.Request, context: Alexa.Context, callback) { +exports.handler = function(event: Alexa.RequestBody, context: Alexa.Context, callback) { var alexa = Alexa.handler(event, context); alexa.registerHandlers(handlers); alexa.execute(); }; -var handlers = { +var handlers: Alexa.Handlers = { 'LaunchRequest': function () { - this.emit('SayHello'); + var self: Alexa.Handler = this; + self.emit('SayHello'); }, 'HelloWorldIntent': function () { - this.emit('SayHello'); + var self: Alexa.Handler = this; + self.emit('SayHello'); }, 'SayHello': function () { - this.emit(':tell', 'Hello World!'); + var self: Alexa.Handler = this; + self.emit(':tell', 'Hello World!'); } }; diff --git a/alexa-sdk/alexa-sdk.d.ts b/alexa-sdk/alexa-sdk.d.ts index 68c90f1445..a39e325c63 100644 --- a/alexa-sdk/alexa-sdk.d.ts +++ b/alexa-sdk/alexa-sdk.d.ts @@ -4,7 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare module 'alexa-sdk' { - export function handler(event: Request, context: Context, callback?: () => void): AlexaHandler; + export function handler(event: RequestBody, context: Context, callback?: () => void): AlexaHandler; export function CreateStateHandler(state: string, obj: any): any; export var StateString: string; @@ -17,13 +17,17 @@ declare module 'alexa-sdk' { response: any; dynamoDBTableName: any; saveBeforeResponse: boolean; - registerHandlers: (...handlers: any[]) => any; + registerHandlers: (...handlers: Handlers[]) => any; execute: () => void; } + interface Handlers { + [intent: string]: () => void; + } + interface Handler { on: any; - emit: any; + emit(event: string, ...args: any[]): boolean; emitWithState: any; state: any; handler: any; @@ -34,12 +38,6 @@ declare module 'alexa-sdk' { isOverriden: any; } - interface Request { - version: string; - request: any; - session: any; - } - interface Context { callbackWaitsForEmptyEventLoop: boolean; logGroupName: string; @@ -50,4 +48,85 @@ declare module 'alexa-sdk' { invokeid: string; awsRequestId: string; } -} \ No newline at end of file + + interface RequestBody { + version: string; + session: Session; + request: LaunchRequest | IntentRequest | SessionEndedRequest; + } + + interface Session { + new: boolean; + sessionId: string; + attributes: any; + application: SessionApplication; + user: SessionUser; + } + + interface SessionApplication { + applicationId: string; + } + + interface SessionUser { + userId: string; + accessToken: string; + } + + interface LaunchRequest extends IRequest {} + + interface IntentRequest extends IRequest { + intent: Intent; + } + + interface Intent { + name: string; + slots: any; + } + + interface SessionEndedRequest extends IRequest{ + reason: string; + } + + interface IRequest { + type: "LaunchRequest" | "IntentRequest" | "SessionEndedRequest"; + requestId: string; + timeStamp: string; + } + + interface ResponseBody { + version: string; + sessionAttributes?: any; + response: Response; + } + + interface Response { + outputSpeech?: OutputSpeech; + card?: Card; + reprompt?: Reprompt; + shouldEndSession: boolean; + } + + interface OutputSpeech { + type: "PlainText" | "SSML"; + text?: string; + ssml?: string; + } + + interface Card { + type: "Simple" | "Standard" | "LinkAccount"; + title?: string; + content?: string; + text?: string; + image?: Image; + } + + interface Image { + smallImageUrl: string; + largeImageUrl: string; + } + + interface Reprompt { + outputSpeech: OutputSpeech; + } +} +