Skip to content

Commit

Permalink
fix: some changes on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Aug 4, 2018
1 parent a068884 commit bd5bf3e
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 48 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"homepage": "https://github.com/fletcherist/yandex-dialogs-sdk#readme",
"dependencies": {
"@types/node-fetch": "^2.1.2",
"debug": "^3.1.0",
"node-fetch": "^2.1.2"
},
"devDependencies": {
Expand All @@ -63,7 +64,7 @@
"prettier": "^1.13.7",
"ts-jest": "^23.0.0",
"tslint": "^5.10.0",
"typescript": "^2.9.2",
"typescript": "^3.0.1",
"typescript-eslint-parser": "^16.0.1"
}
}
79 changes: 36 additions & 43 deletions src/alice.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import * as http from 'http';
import _debug from 'debug';
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 { ALICE_PROTOCOL_VERSION, LIBRARY_NAME } from './constants';

export interface IAliceConfig extends IImagesApiConfig {}
const debug = _debug(LIBRARY_NAME);

export interface IAliceConfig extends IImagesApiConfig {
oAuthToken?: string;
skillId?: string;
}

export interface IAlice {
readonly imagesApi: IImagesApi;
handleRequest(data: IApiRequest): Promise<IApiResponse>;
use(middleware: Middleware): void;
listen(port: number, webhookUrl: string): http.Server;
}

export class Alice implements IAlice {
private readonly _config: IAliceConfig;
private readonly _middlewares: Middleware[];
private readonly _imagesApi: IImagesApi;
private _server: object | null;
private _server: http.Server | null;

constructor(config: IAliceConfig) {
constructor(config: IAliceConfig = {}) {
this._config = config;
this._middlewares = [];
this._imagesApi = new ImagesApi(this._config);
Expand All @@ -46,7 +52,8 @@ export class Alice implements IAlice {
const next = async (
context: IContext,
): Promise<IMiddlewareResult | null> => {
const middleware = middlewares[index--];
const middleware = middlewares[index];
index--;
return middleware(context, index <= 0 ? null : next);
};
return next(context);
Expand Down Expand Up @@ -83,43 +90,29 @@ export class Alice implements IAlice {
};
}

public listen(
port: number = 80,
webhookUrl: string = '/',
callback?: () => void,
) {
this._server = http
.createServer(
async (request: http.ServerRequest, response: http.ServerResponse) => {
const body: (string | Buffer)[] = [];
request
.on('data', chunk => {
body.push(chunk);
})
.on('end', async () => {
const requestData = Buffer.from(body).toString();
if (request.method === 'POST' && request.url === webhookUrl) {
try {
const requestBody = JSON.parse(requestData);
const responseBody = await this.handleRequest(requestBody);
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify(responseBody));
} catch (error) {
throw new Error(error);
}
} else {
response.statusCode = 400;
return response.end();
}
});
},
)
.listen(port, () => {
if (typeof callback === 'function') {
return callback();
}
});
public listen(port: number = 80, webhookUrl: string = '/'): http.Server {
debug(`create server on: ${port}, ${webhookUrl}`);
this._server = http.createServer(
async (request: http.ServerRequest, response: http.ServerResponse) => {
const body: Array<string | Buffer> = [];
request
.on('data', chunk => {
body.push(chunk);
})
.on('end', async () => {
const requestData = Buffer.from(body).toString();
if (request.method !== 'POST' || request.url !== webhookUrl) {
response.statusCode = 400;
return response.end();
}
const requestBody = JSON.parse(requestData);
const responseBody = await this.handleRequest(requestBody);
response.statusCode = 200;
response.setHeader('Content-Type', 'application/json');
response.end(JSON.stringify(responseBody));
});
},
);
return this._server;
}

Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const ALICE_PROTOCOL_VERSION = '1.0';
export const ALICE_API_URL = 'https://dialogs.yandex.net/api/v1/skills';
export const LIBRARY_NAME = 'yandex-dialogs-sdk';
6 changes: 3 additions & 3 deletions src/imagesApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
} from './api/image';

export interface IImagesApiConfig {
oAuthToken: string;
skillId: string;
oAuthToken?: string;
skillId?: string;
}

interface IImagesApiRequestParams {
path: string;
method?: 'GET' | 'POST';
body?: Object;
body?: object;
}

export interface IImagesApi {
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ export {
CommandMatcher,
} from './command/command';

import { Alice } from './alice';
/**
* For compatibility with commonjs
* @example const Alice = require('yandex-dialogs-sdk')
*/
module.exports = Alice;
exports = module.exports;

export default Alice;

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

export { InMemorySession } from './session/inMemorySession';
Expand Down
Empty file added src/server/webhook.ts
Empty file.
2 changes: 1 addition & 1 deletion src/stage/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class Scene<TContext extends IStageContext = IStageContext>

private async run(context: TContext): Promise<CommandCallbackResult> {
const command = await this._commands.getMostRelevant(context);
if (!command) {
if (command) {
return command.run(context);
}

Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"arrow-parens": false,
"object-literal-shorthand": false,
"only-arrow-functions": false,
"member-ordering": false
},
"rulesDirectory": []
}

0 comments on commit bd5bf3e

Please sign in to comment.