Skip to content

Commit

Permalink
fix: ctx types
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 1, 2018
1 parent 2a4e266 commit ff44115
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
13 changes: 6 additions & 7 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,20 @@ export default class Alice {
* Если новая сессия, то запускаем стартовую команду
*/
if (req.session.new && this.welcomeCallback) {
const ctx = new Ctx(ctxDefaultParams)
return await this.welcomeCallback(ctx)
const ctxInstance = new Ctx(ctxDefaultParams)
return await this.welcomeCallback(ctxInstance)
}
/*
* Команда нашлась в списке.
* Запускаем её обработчик.
*/
if (requestedCommands.length !== 0) {
const requestedCommand = requestedCommands[0]
const ctx = new Ctx(merge(ctxDefaultParams, {
const ctxInstance = new Ctx(merge(ctxDefaultParams, {
command: requestedCommand,
}))

return await requestedCommand.callback(ctx)
return await requestedCommand.callback(ctxInstance)
}

/*
Expand Down Expand Up @@ -237,11 +237,10 @@ export default class Alice {
}
}

public _handleEnterScene(sceneName) {
private _handleEnterScene(sceneName) {
this.currentScene = sceneName
}
public _handleLeaveScene(sceneName) {
console.log('leaving scene', sceneName)
private _handleLeaveScene(sceneName) {
this.currentScene = null
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/buttonBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ButtonBuilder {
constructor(buttonConstructor) {
export default class ButtonBuilder {
constructor(buttonConstructor?: {}) {
/* No button object passed to the constructor */
if (!buttonConstructor) {
this.button = {}
Expand All @@ -11,7 +11,7 @@ class ButtonBuilder {
throw new Error('Invalid ButtonBuilder constructor type. Should be object')
}
const {
title, text
title, text,
} = buttonConstructor
if (!title && !text) {
throw new Error('Button [title] or [text] is required for ButtonBuilder constructor.')
Expand All @@ -21,35 +21,35 @@ class ButtonBuilder {
return this.button
}

_setTitle(title) {
public _setTitle(title) {
this.button.title = title
return this
}

text(text) {
public text(text) {
return this._setTitle(text)
}

title(title) {
public title(title) {
return this._setTitle(title)
}

url(url) {
public url(url) {
this.button.url = url
return this
}

shouldHide(flag) {
public shouldHide(flag) {
this.button.hide = flag
return this
}

payload(payload) {
public payload(payload) {
this.button.payload = payload
return this
}

get() {
public get() {
return this.button
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {

CommandType,
} from './constants'
import Ctx from './ctx'

const foo: 'bar' = 'bar'

export default class Command {
public name: any[] | string | RegExp
public callback: () => void
public callback: (ctx: Ctx) => void
public type: | CommandType

constructor(name: string, callback: (() => void)) {
Expand Down
33 changes: 26 additions & 7 deletions src/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
const ReplyBuilder = require('./replyBuilder')
const ButtonBuilder = require('./buttonBuilder')
const { reversedInterpolation, selectCommand } = require('./utils')
import Session from './session'

class Ctx {
import ReplyBuilder from './replyBuilder'
import ButtonBuilder from './buttonBuilder'
import Command from './command'

export default class Ctx {
public req: {}
public sessionId: string
public messageId: string
public userId: string
public payload: {}
public message: string
public session: Session

public command?: Command

public replyBuilder: ReplyBuilder
public buttonBuilder: ButtonBuilder

private sendResponse: (response: string) => void
private enterScene: () => void
private leaveScene: () => void
constructor({
req,
sendResponse,
Expand All @@ -11,7 +30,7 @@ class Ctx {
enterScene,
leaveScene,

command
command,
}) {
this.req = req
this.sendResponse = sendResponse
Expand All @@ -20,7 +39,7 @@ class Ctx {
this.messageId = req.session.message_id
this.userId = req.session.user_id
this.payload = req.request.payload
this.messsage = req.request.original_utterance
this.message = req.request.original_utterance

this.session = session

Expand All @@ -42,7 +61,7 @@ class Ctx {
return reversedInterpolation(this.command.name, requestText)
}

async reply(replyMessage) {
public async reply(replyMessage) {
if (!replyMessage) {
throw new Error('Reply message could not be empty!')
}
Expand All @@ -65,7 +84,7 @@ class Ctx {
return this._sendReply(replyMessage)
}

_sendReply(replyMessage) {
public _sendReply(replyMessage) {
/*
* That fires when listening on port.
*/
Expand Down

0 comments on commit ff44115

Please sign in to comment.