Skip to content

Commit

Permalink
feat: add some typings
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 19, 2018
1 parent d52598f commit 7c64230
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {

import aliceStateMiddleware from './middlewares/aliceStateMiddleware'

import { IConfig } from './types/alice'
import { IConfig, IAlice } from './types/alice'
import { ICommand } from './types/command'
import { IContext } from './types/context'
import { WebhookResponse, WebhookRequest } from './types/webhook'
Expand All @@ -39,7 +39,7 @@ import {
const DEFAULT_SESSIONS_LIMIT: number = 1000
const DEFAULT_RESPONSE_TIMEOUT = 1200

export default class Alice {
export default class Alice implements IAlice {
public scenes: Scene[]

protected anyCallback: (ctx: IContext) => void
Expand Down Expand Up @@ -86,7 +86,7 @@ export default class Alice {
* @param {Function} middleware - function, that receives {context}
* and makes some modifications with it.
*/
public use(middleware) {
public use(middleware: (IContext: IContext) => IContext): void {
if (!isFunction(middleware)) {
throw new Error('Any middleware could only be a function.')
}
Expand All @@ -98,14 +98,14 @@ export default class Alice {
* @param {string | Array<string> | regex} name — Trigger for the command
* @param {Function} callback — Handler for the command
*/
public command(name, callback) {
public command(name: ICommand, callback: (IContext) => void) {
this.commands.add(name, callback)
}

/*
* Стартовая команда на начало сессии
*/
public welcome(callback) {
public welcome(callback: (IContext) => void): void {
this.welcomeCallback = callback
}

Expand All @@ -114,7 +114,7 @@ export default class Alice {
* которую запросил пользователь,
* вызывается этот колбек
*/
public any(callback) {
public any(callback: (IContext) => void): void {
this.anyCallback = callback
}

Expand Down
8 changes: 8 additions & 0 deletions src/types/alice.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { IContext } from './context'
import { ICommand } from './command'

export interface IConfig {
fuseOptions?: {}
sessionsLimit?: number
Expand All @@ -6,3 +9,8 @@ export interface IConfig {
devServerUrl?: string
responseTimeout?: number
}

export interface IAlice {
command(name: ICommand, callback: (IContext) => void): void
use(middleware: (IContext: IContext) => IContext): void
}
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const selectCommand = (req): string => req.request.command
export const selectSession = (req) => req.session
export const selectSessionId = (req) => selectSession(req).session_id
export const selectUserId = (req) => selectSession(req).user_id
export const isFunction = (fn: () => void) => fn && typeof fn === 'function'
export const isFunction = (fn: (args: any) => any) => fn && typeof fn === 'function'
export const delay = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))
export const rejectsIn = (ms: number) => new Promise((resolve, reject) => setTimeout(reject, ms))

Expand Down

0 comments on commit 7c64230

Please sign in to comment.