diff --git a/src/alice.ts b/src/alice.ts index b15ff83..d539cb1 100644 --- a/src/alice.ts +++ b/src/alice.ts @@ -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' @@ -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 @@ -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.') } @@ -98,14 +98,14 @@ export default class Alice { * @param {string | Array | 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 } @@ -114,7 +114,7 @@ export default class Alice { * которую запросил пользователь, * вызывается этот колбек */ - public any(callback) { + public any(callback: (IContext) => void): void { this.anyCallback = callback } diff --git a/src/types/alice.d.ts b/src/types/alice.d.ts index 4592a1a..e790b39 100644 --- a/src/types/alice.d.ts +++ b/src/types/alice.d.ts @@ -1,3 +1,6 @@ +import { IContext } from './context' +import { ICommand } from './command' + export interface IConfig { fuseOptions?: {} sessionsLimit?: number @@ -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 +} diff --git a/src/utils/index.ts b/src/utils/index.ts index ef028c4..09ad0f1 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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 => new Promise((resolve) => setTimeout(resolve, ms)) export const rejectsIn = (ms: number) => new Promise((resolve, reject) => setTimeout(reject, ms))