From ca4cd35a218d9479791841e660b9f66162bb2b40 Mon Sep 17 00:00:00 2001 From: fletcherist Date: Tue, 3 Jul 2018 02:24:49 +0300 Subject: [PATCH] feat: implement aliceStateMiddleware --- package.json | 1 + src/alice.ts | 6 ++-- src/button.ts | 10 ++++++- src/commands.ts | 3 +- src/middlewares.ts | 18 ----------- src/middlewares/aliceStateMiddleware.ts | 17 +++++++++++ src/reply.ts | 40 ++++++++++++++++++------- src/scene.ts | 14 +++++++-- tsconfig.json | 2 +- 9 files changed, 73 insertions(+), 38 deletions(-) create mode 100644 src/middlewares/aliceStateMiddleware.ts diff --git a/package.json b/package.json index 4c2350f..1e21545 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "ramda": "^0.25.0" }, "devDependencies": { + "@types/express": "^4.16.0", "@types/jest": "^23.1.3", "@types/node": "^10.5.1", "conventional-changelog-cli": "^2.0.0", diff --git a/src/alice.ts b/src/alice.ts index cc4e437..15ee3e3 100644 --- a/src/alice.ts +++ b/src/alice.ts @@ -15,10 +15,10 @@ import { import { applyMiddlewares, MiddlewareType, - - createAliceStateMiddleware, } from './middlewares' +import aliceStateMiddleware from './middlewares/aliceStateMiddleware' + const DEFAULT_SESSIONS_LIMIT: number = 1000 export default class Alice { @@ -36,7 +36,7 @@ export default class Alice { this.anyCallback = null this.welcomeCallback = null this.commands = new Commands(config.fuseOptions || null) - this.middlewares = [createAliceStateMiddleware()] + this.middlewares = [aliceStateMiddleware()] this.scenes = [] this.currentScene = null this.sessions = new Sessions() diff --git a/src/button.ts b/src/button.ts index 43ea543..6365c5c 100644 --- a/src/button.ts +++ b/src/button.ts @@ -1,4 +1,12 @@ -const button = (params) => { +export interface ButtonParams { + title: string, + text: string, + tts?: string, + url?: string, + hide?: boolean, + payload?: {} +} +const button = (params: ButtonParams) => { // Button has been created from string if (typeof params === 'string') { return { diff --git a/src/commands.ts b/src/commands.ts index fa23e2e..85a32fb 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -8,7 +8,7 @@ const TYPE_FIGURE = 'figure' const TYPE_REGEXP = 'regexp' const TYPE_ARRAY = 'array' -class Commands { +export default class Commands { public commands: Command[] public fuseOptions: {} constructor(config = null) { @@ -93,5 +93,4 @@ class Commands { module.exports = Commands module.exports.Command = Command -export default Commands export { Command } diff --git a/src/middlewares.ts b/src/middlewares.ts index 9348024..f02739f 100644 --- a/src/middlewares.ts +++ b/src/middlewares.ts @@ -21,21 +21,3 @@ export async function applyMiddlewares(middlewares, ctx: Ctx): Promise { } return newContext } - -export function createAliceStateMiddleware() { - const store = new Map() - - return async (ctx) => { - const key = ctx.session.sessionId - let { state } = store.get(key) || { state: {} } - Object.defineProperty(ctx, 'state', { - get() { return state }, - set(value) { state = Object.assign({}, value) }, - }) - store.set(key, { - state, - }) - - return ctx - } -} diff --git a/src/middlewares/aliceStateMiddleware.ts b/src/middlewares/aliceStateMiddleware.ts new file mode 100644 index 0000000..05a99a0 --- /dev/null +++ b/src/middlewares/aliceStateMiddleware.ts @@ -0,0 +1,17 @@ +export default function aliceStateMiddleware() { + const store = new Map() + + return async (ctx) => { + const key = ctx.session.sessionId + let { state } = store.get(key) || { state: {} } + Object.defineProperty(ctx, 'state', { + get() { return state }, + set(value) { state = Object.assign({}, value) }, + }) + store.set(key, { + state, + }) + + return ctx + } +} diff --git a/src/reply.ts b/src/reply.ts index f5ff1e7..278b840 100644 --- a/src/reply.ts +++ b/src/reply.ts @@ -2,17 +2,37 @@ const { DEFAULT_END_SESSION, - ALICE_PROTOCOL_VERSION + ALICE_PROTOCOL_VERSION, } = require('./constants') +import { ButtonParams } from './button' -const reply = (params) => { - const data = { +interface ReplyType { + response: { + text: string, + buttons?: ButtonParams[], + end_session: boolean, + }, + session?: {}, + version: string, +} + +interface ParamsType { + text: string, + tts?: string, + shouldEndSession?: boolean, + endSession?: boolean, + end_session?: boolean, + session: {}, + buttons: any[] +} +const reply = (params: ParamsType) => { + const data: ReplyType = { response: { buttons: [], - end_session: DEFAULT_END_SESSION + end_session: DEFAULT_END_SESSION, }, session: null, - version: ALICE_PROTOCOL_VERSION + version: ALICE_PROTOCOL_VERSION, } if (typeof params === 'string') { @@ -26,16 +46,16 @@ const reply = (params) => { endSession, end_session, session, - buttons + buttons, } = params - if (text) data.response.text = text - if (tts) data.response.tts = tts - if (buttons) data.response.buttons = buttons + if (text) { data.response.text = text } + if (tts) { data.response.tts = tts } + if (buttons) { data.response.buttons = buttons } if (shouldEndSession || end_session || endSession) { data.response.end_session = shouldEndSession || end_session || endSession } - if (session) data.session = session + if (session) { data.session = session } return data } else { diff --git a/src/scene.ts b/src/scene.ts index 91673e7..9d59f5f 100644 --- a/src/scene.ts +++ b/src/scene.ts @@ -1,11 +1,18 @@ import Alice from './alice' -const Commands = require('./commands') -const Command = require('./commands').Command -const Ctx = require('./ctx') +import Commands from './commands' +import Command from './Command' +import Ctx from './ctx' const selectCommand = (req) => req.request.command export default class Scene extends Alice { + public name: string + public enterCommand: Command + public leaveCommand: Command + public anyCallback: (ctx: Ctx) => void + public commands: Commands + public config: {} + constructor(name, config = {}) { super() this.name = name @@ -16,6 +23,7 @@ export default class Scene extends Alice { this.enterCommand = null this.leaveCommand = null } + get title() { return this.name } diff --git a/tsconfig.json b/tsconfig.json index 3c1b58e..04a7bbd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,7 @@ "src/types/*" ] }, - "types": ["node", "jest"], + "types": ["node", "jest", "express"], // typeRoots option has been previously configured "typeRoots": [ // add path to @types