Skip to content

Commit

Permalink
fix: update codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Aug 3, 2018
1 parent 3420bfa commit fb32fa6
Show file tree
Hide file tree
Showing 28 changed files with 243 additions and 242 deletions.
6 changes: 3 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"parser": "typescript",
"printWidth": 100,
"printWidth": 80,
"singleQuote": true,
"tabWidth": 4,
"semi": false,
"tabWidth": 2,
"semi": true,
"trailingComma": "all",
"arrowParens": "avoid"
}
39 changes: 21 additions & 18 deletions src/alice.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { IImagesApiConfig, IImagesApi, ImagesApi } from "./imagesApi";
import { Middleware, IMiddlewareResult } from "./middleware/middleware";
import { IApiRequest } from "./api/request";
import { IContext } from "./context";
import { IApiResponse } from "./api/response";
import { ALICE_PROTOCOL_VERSION } from "./constants";
import { Reply } from "./reply/reply";
import { IImagesApiConfig, IImagesApi, ImagesApi } from './imagesApi';
import { Middleware, IMiddlewareResult } from './middleware/middleware';
import { IApiRequest } from './api/request';
import { IContext } from './context';
import { IApiResponse } from './api/response';
import { ALICE_PROTOCOL_VERSION } from './constants';
import { Reply } from './reply/reply';

export interface IAliceConfig extends IImagesApiConfig {

}
export interface IAliceConfig extends IImagesApiConfig {}

export interface IAlice {
readonly imagesApi: IImagesApi;
handleRequest(data: IApiRequest): Promise<IApiResponse>;
use(middleware: Middleware): void
use(middleware: Middleware): void;
}

export class Alice implements IAlice {
Expand All @@ -30,23 +28,25 @@ export class Alice implements IAlice {
private _buildContext(request: IApiRequest): IContext {
return {
data: request,
}
};
}

private async _runMiddlewares(
context: IContext
context: IContext,
): Promise<IMiddlewareResult | null> {
const middlewares = Array.from(this._middlewares);
if (middlewares.length <= 0) {
return null;
}

let index = middlewares.length - 1;
const next = async (context: IContext): Promise<IMiddlewareResult | null> => {
const next = async (
context: IContext,
): Promise<IMiddlewareResult | null> => {
const middleware = middlewares[index];
index--;
return middleware(context, index <= 0 ? null : next);
}
};
return next(context);
}

Expand All @@ -63,8 +63,11 @@ export class Alice implements IAlice {
const result = await this._runMiddlewares(context);
if (!result) {
throw new Error(
'No response for request ' + context.data.request.command + ' ' +
'Try add command for it or add default command');
'No response for request ' +
context.data.request.command +
' ' +
'Try add command for it or add default command',
);
}

return {
Expand All @@ -75,7 +78,7 @@ export class Alice implements IAlice {
user_id: data.session.user_id,
},
version: ALICE_PROTOCOL_VERSION,
}
};
}

public use(middleware: Middleware): void {
Expand Down
54 changes: 25 additions & 29 deletions src/command/command.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import {
IContext
} from '../context';
import { IContext } from '../context';
import { IApiResponseBody } from '../api/response';

// Let it be void for now, but this type can be changed in future.
export type CommandCallbackResult =
IApiResponseBody;
export type CommandCallbackResult = IApiResponseBody;
export type CommandCallback<TContext extends IContext = IContext> =
((context: TContext) => CommandCallbackResult) |
((context: TContext) => Promise<CommandCallbackResult>);
| ((context: TContext) => CommandCallbackResult)
| ((context: TContext) => Promise<CommandCallbackResult>);
export type CommandMatcher<TContext extends IContext = IContext> =
((ctx: TContext) => number) |
((ctx: TContext) => Promise<number>);
| ((ctx: TContext) => number)
| ((ctx: TContext) => Promise<number>);
export type CommandDeclaration<TContext extends IContext = IContext> =
CommandMatcher<TContext> |
string[] |
string |
RegExp;
| CommandMatcher<TContext>
| string[]
| string
| RegExp;

export interface ICommand<TContext extends IContext = IContext> {
run(context: TContext): Promise<CommandCallbackResult>;
getRelevance(context: TContext): Promise<number>;
}

export class Command<TContext extends IContext = IContext> implements ICommand<TContext> {
private readonly _callback: CommandCallback<TContext>
private readonly _matcher: CommandMatcher
export class Command<TContext extends IContext = IContext>
implements ICommand<TContext> {
private readonly _callback: CommandCallback<TContext>;
private readonly _matcher: CommandMatcher;

constructor(matcher: CommandMatcher, callback: CommandCallback<TContext>) {
this._matcher = matcher;
Expand All @@ -42,33 +40,31 @@ export class Command<TContext extends IContext = IContext> implements ICommand<T

public static createCommand<TContext extends IContext = IContext>(
declaration: CommandDeclaration,
callback: CommandCallback<TContext>
callback: CommandCallback<TContext>,
): ICommand<TContext> {
if (typeof declaration === 'function') {
return new Command(declaration, callback);
}

if (typeof declaration === 'string') {
return new Command(
this.createMatcherFromStrings([declaration]),
callback);
this.createMatcherFromStrings([declaration]),
callback,
);
}

if (Array.isArray(declaration)) {
return new Command(
this.createMatcherFromStrings(declaration),
callback);
return new Command(this.createMatcherFromStrings(declaration), callback);
}

if (declaration instanceof RegExp) {
return new Command(
this.createMatcherFromRegExp(declaration),
callback);
return new Command(this.createMatcherFromRegExp(declaration), callback);
}

throw new Error(
'Command declaration is not of proper type. ' +
'Could be only string, array of strings, RegExp or function.');
'Command declaration is not of proper type. ' +
'Could be only string, array of strings, RegExp or function.',
);
}

public static createMatcherFromStrings(strings: string[]): CommandMatcher {
Expand All @@ -81,15 +77,15 @@ export class Command<TContext extends IContext = IContext> implements ICommand<T
const commandText = context.data.request.command;
const lowerMessage = commandText ? commandText.toLowerCase() : '';
return lowerStrings.some(s => s === lowerMessage) ? 1 : 0;
}
};
}

public static createMatcherFromRegExp(regexp: RegExp): CommandMatcher {
return (context: IContext) => {
const commandText = context.data.request.command;
const lowerMessage = commandText ? commandText.toLowerCase() : '';
return regexp.test(lowerMessage) ? 1 : 0;
}
};
}

public static createMatcherAlways(): CommandMatcher {
Expand Down
32 changes: 19 additions & 13 deletions src/command/commandsGroup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
ICommand
} from './command'
import { ICommand } from './command';

import { IContext } from '../context'
import { IContext } from '../context';

export interface ICommandRelevance<TContext extends IContext = IContext> {
readonly command: ICommand<TContext>;
Expand All @@ -11,21 +9,28 @@ export interface ICommandRelevance<TContext extends IContext = IContext> {

export interface ICommandsGroup<TContext extends IContext = IContext> {
add(command: ICommand): void;
getRelevance(context: TContext): Promise<ICommandRelevance<TContext>[] | null>;
getRelevance(
context: TContext,
): Promise<ICommandRelevance<TContext>[] | null>;
getMostRelevant(context: TContext): Promise<ICommand<TContext> | null>;
}

export class CommandsGroup<TContext extends IContext = IContext> implements ICommandsGroup<TContext> {
export class CommandsGroup<TContext extends IContext = IContext>
implements ICommandsGroup<TContext> {
private readonly _commands: ICommand<TContext>[];

constructor() {
this._commands = []
this._commands = [];
}

public async getRelevance(context: TContext): Promise<ICommandRelevance[] | null> {
return Promise.all(this._commands.map(async command => {
return {command, relevance: await command.getRelevance(context)}
}));
public async getRelevance(
context: TContext,
): Promise<ICommandRelevance[] | null> {
return Promise.all(
this._commands.map(async command => {
return { command, relevance: await command.getRelevance(context) };
}),
);
}

async getMostRelevant(context: TContext): Promise<ICommand<TContext> | null> {
Expand All @@ -35,8 +40,9 @@ export class CommandsGroup<TContext extends IContext = IContext> implements ICom
}

const mostRelevant = relevances.reduce<ICommandRelevance<TContext>>(
(last, current) => current.relevance > last.relevance ? current : last,
relevances[0]);
(last, current) => (current.relevance > last.relevance ? current : last),
relevances[0],
);
return mostRelevant.relevance > 0 ? mostRelevant.command : null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const ALICE_PROTOCOL_VERSION = '1.0'
export const ALICE_API_URL = 'https://dialogs.yandex.net/api/v1/skills'
export const ALICE_PROTOCOL_VERSION = '1.0';
export const ALICE_API_URL = 'https://dialogs.yandex.net/api/v1/skills';
4 changes: 2 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IApiRequest } from "./api/request";
import { IApiRequest } from './api/request';

export interface IContext {
readonly data: IApiRequest,
readonly data: IApiRequest;
}
18 changes: 11 additions & 7 deletions src/imagesApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import fetch from 'node-fetch'
import { ALICE_API_URL } from './constants'
import { IApiImageUploadResponse, IApiImageItem, IApiImageListResponse } from './api/image';
import fetch from 'node-fetch';
import { ALICE_API_URL } from './constants';
import {
IApiImageUploadResponse,
IApiImageItem,
IApiImageListResponse,
} from './api/image';

export interface IImagesApiConfig {
oAuthToken: string;
Expand Down Expand Up @@ -29,27 +33,27 @@ export class ImagesApi implements IImagesApi {
}

private async _makeRequest<TResult>(
params: IImagesApiRequestParams
params: IImagesApiRequestParams,
): Promise<TResult> {
const url = `${ALICE_API_URL}/${this._skillId}/${params.path}`;
const method = params.method || 'GET';
const body = params.body ? JSON.stringify(params.body) : undefined;
const response = await fetch(url, {
method: method,
headers: {
'Authorization': `OAuth ${this._oAuthToken}`,
Authorization: `OAuth ${this._oAuthToken}`,
'Content-type': 'application/json',
},
body: body,
});
return <TResult>(await response.json());
return <TResult>await response.json();
}

public async uploadImageByUrl(url: string): Promise<IApiImageItem> {
const response = await this._makeRequest<IApiImageUploadResponse>({
path: 'images',
method: 'POST',
body: {url},
body: { url },
});
return response.image;
}
Expand Down
14 changes: 6 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ export {
CommandMatcher,
} from './command/command';

export { Reply } from './reply/reply'
export { Reply } from './reply/reply';

export { InMemorySession } from './session/inMemorySession';
export { InMemorySessionsStorage } from './session/inMemorySessionsStorage';
export { ISession, ISessionStorage } from './session/session';
export { ISessionContext } from './session/sessionContext';
export { sessionMiddleware } from './session/sesstionMiddleware';

export { IStageСompere } from './stage/compere'
export { IScene, Scene } from './stage/scene'
export { IStage, Stage } from './stage/stage'
export { IStageContext } from './stage/stageContext'
export { IStageСompere } from './stage/compere';
export { IScene, Scene } from './stage/scene';
export { IStage, Stage } from './stage/stage';
export { IStageContext } from './stage/stageContext';

export {
IMiddlewareResult,
Middleware,
MiddlewareNext,
} from './middleware/middleware'
} from './middleware/middleware';

export { Alice, IAlice } from './alice';
export { IContext } from './context';
export { IImagesApi } from './imagesApi';


14 changes: 6 additions & 8 deletions src/reply/bodyButtonBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IApiResponseBodyButton } from "../api/response";
import { IApiResponseBodyButton } from '../api/response';

export interface IBodyButtonReply {
title: string;
Expand All @@ -7,13 +7,11 @@ export interface IBodyButtonReply {
hide?: boolean;
}

export type BodyButtonDeclaration =
IBodyButtonReply |
string;
export type BodyButtonDeclaration = IBodyButtonReply | string;

export class BodyButtonBuilder {
public static createBodyButton(
declaration: BodyButtonDeclaration
declaration: BodyButtonDeclaration,
): IApiResponseBodyButton {
if (typeof declaration === 'object') {
return declaration;
Expand All @@ -22,13 +20,13 @@ export class BodyButtonBuilder {
if (typeof declaration === 'string') {
return {
title: declaration,
payload: {title: declaration},
payload: { title: declaration },
};
}

throw new Error(
'Card button declaration is not of proper type. ' +
'Could be only string or object.');
'Could be only string or object.',
);
}
}

Loading

0 comments on commit fb32fa6

Please sign in to comment.