Skip to content

Commit

Permalink
[Dev] Introduce eslint and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispanag committed Nov 20, 2020
1 parent 4265c89 commit 7a577a3
Show file tree
Hide file tree
Showing 22 changed files with 754 additions and 100 deletions.
3 changes: 2 additions & 1 deletion examples/echo-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"fb:config": "yarn node ./dist/scripts/install.js",
"prettier:base": "npx prettier --config ../../.prettierrc",
"prettier:check": "yarn run prettier:base -- --list-different \"src/**/*.{ts,tsx}\"",
"prettier:write": "yarn run prettier:base -- --write \"src/**/*.{ts,tsx}\""
"prettier:write": "yarn run prettier:base -- --write \"src/**/*.{ts,tsx}\"",
"lint": "eslint '*/**/*.ts' --quiet --fix -c ../../.eslintrc.js --ignore-path ../../.eslintignore"
},
"dependencies": {
"@ebenos/elements": "^3.4.4",
Expand Down
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
],
"scripts": {},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"lerna": "^3.22.1",
"prettier": "^2.1.2",
"tslint": "^6.1.3",
"prettier": "^2.2.0",
"typescript": "^4.1.0"
},
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions packages/elements/lib/sendAPI/attachments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
/** Generic attachment class */
export class Attachment {
public type: string;
public payload: {};
public payload: Record<string, unknown>;

/**
* Create a generic attachment
* @param {string} type - The type of the attachment
* @param {object} payload - The attachment
*/
constructor(type: string, payload: {}) {
constructor(type: string, payload: Record<string, unknown>) {
this.type = type;
this.payload = payload;
}
Expand Down Expand Up @@ -50,7 +50,7 @@ export class TemplateAttachment extends Attachment {
* A template attachment (List, Button or Generic)
* @param {object} template - The template included within this attachment
*/
constructor(template: {}) {
constructor(template: Record<string, unknown>) {
super('template', template);
}
}
8 changes: 4 additions & 4 deletions packages/elements/lib/sendAPI/buttons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import { GenericTemplate } from './templates';
/** A General button class */
export class Button {
public type: string;
public params: {};
public params: Record<string, unknown>;

/**
* Creates a generic button
* @param {string} type - The type of the button
* @param {object} params - The various parameters the button includes
*/
constructor(type: string, params: {} = {}) {
constructor(type: string, params: Record<string, unknown> = {}) {
this.type = type;
this.params = params;
}
Expand Down Expand Up @@ -60,7 +60,7 @@ export class UrlButton extends Button {
title: string,
url: string,
webview_height_ratio = 'full',
messenger_extensions: boolean = false
messenger_extensions = false
) {
super('web_url', {
title,
Expand Down Expand Up @@ -99,7 +99,7 @@ export class PostbackButton extends Button {
* @param {string} title - The title of the button
* @param {object|string} payload - The payload returned when the button is pushed
*/
constructor(title: string, payload: any | string = '') {
constructor(title: string, payload: Record<string, unknown> | string = '') {
let serializedPayload = payload;
if (typeof payload === 'object') {
serializedPayload = JSON.stringify(payload);
Expand Down
8 changes: 5 additions & 3 deletions packages/elements/lib/sendAPI/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import { Button } from './buttons';
import { QuickReply } from './quickreplies';
import { Attachment } from './attachments';

export function isObjectPayload(payload: string | {}): payload is {} {
export function isObjectPayload(
payload: string | Record<string, unknown>
): payload is Record<string, unknown> {
return typeof payload === 'object';
}

export interface MessageBody {
recipient: {
id: string;
};
message: {};
message: Record<string, unknown>;
notification_type: string;
messaging_type: string;
}
Expand Down Expand Up @@ -38,7 +40,7 @@ export interface MessageOptions {
export type SerializedMessage = SerializedAttachmentMessage | SerializedTextMessage;

export interface SerializedAttachmentMessage {
attachment: {};
attachment: Record<string, unknown>;
}

export interface SerializedTextMessage {
Expand Down
6 changes: 5 additions & 1 deletion packages/elements/lib/sendAPI/quickreplies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export class QuickReply {
* @param {string} title - The title of the QuickReply
* @param {?object|?string} payload - The payload sent when the QuickReply is pushed
*/
constructor(content_type: string, title: string, payload: string | {} = '') {
constructor(
content_type: string,
title: string,
payload: string | Record<string, unknown> = ''
) {
let serializedPayload = JSON.stringify(payload);
if (!isObjectPayload(payload)) {
serializedPayload = payload;
Expand Down
6 changes: 5 additions & 1 deletion packages/elements/lib/sendAPI/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ export class MediaTemplate extends TemplateAttachment {
/**
* Create a MediaTemplate
*/
constructor(attachment_id: string, buttons: Button[] | null = null, media_type: 'image' | 'video' = 'image') {
constructor(
attachment_id: string,
buttons: Button[] | null = null,
media_type: 'image' | 'video' = 'image'
) {
let serializedButtons: Array<{ [key: string]: any }> = [];
if (buttons) {
serializedButtons = buttons.map((b) => b.serialize());
Expand Down
3 changes: 2 additions & 1 deletion packages/elements/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"prepare": "yarn run build",
"prettier:base": "npx prettier --config ../../.prettierrc",
"prettier:check": "yarn run prettier:base -- --list-different \"lib/**/*.{ts,tsx}\"",
"prettier:write": "yarn run prettier:base -- --write \"lib/**/*.{ts,tsx}\""
"prettier:write": "yarn run prettier:base -- --write \"lib/**/*.{ts,tsx}\"",
"lint": "eslint '*/**/*.ts' --quiet --fix -c ../../.eslintrc.js --ignore-path ../../.eslintignore"
},
"types": "./build/index.d.ts",
"author": "Christos Panagiotakopoulos <chrispanag@gmail.com>",
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/lib/handlers/nlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ function nlpHandlerFactory<U extends User>(
intentRouter: IntentRouter,
options?: INLPHandlerOptions
) {
const MAX_LENGTH = options?.maxMessageLength ? options.maxMessageLength : 51
const MIN_CONFIDENCE = options?.confidenceThreshold ? options.confidenceThreshold : 0.9
const MAX_LENGTH = options?.maxMessageLength ? options.maxMessageLength : 51;
const MIN_CONFIDENCE = options?.confidenceThreshold ? options.confidenceThreshold : 0.9;
function nlpHandler(this: Bot<U>, user: U, message: { text: string }, nlp: WitNLP) {
// The NLP object doesn't exist if the user hasn't activated the built in NLP
if (nlp) {
Expand Down
6 changes: 5 additions & 1 deletion packages/framework/lib/handlers/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import Bot from '../bot';
* @returns {function} - Returns a textHandler function
*/

type nlpHandlerF<U extends User> = (user: U, message: { text: string }, nlp: WitNLP) => Promise<any>;
type nlpHandlerF<U extends User> = (
user: U,
message: { text: string },
nlp: WitNLP
) => Promise<any>;

function defaultNlp<U extends User>(user: U, message: { text: string }, nlp: WitNLP | undefined) {
if (!nlp) {
Expand Down
9 changes: 2 additions & 7 deletions packages/framework/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ export { ISerializable } from './interfaces/elements';
export { ActionMiddleware } from './utilities/actions';
export { UserModel, IRouters, EbonyHandlers, IBaseMessage, IBaseMessageOptions } from './adapter';
export { WitNLP } from './interfaces/nlp';
export { GenericAttachment } from './interfaces/attachment'
export { GenericAttachment } from './interfaces/attachment';
export * from './interfaces/interactions';


export {
Bot,
User,
GenericAdapter,
};
export { Bot, User, GenericAdapter };
2 changes: 1 addition & 1 deletion packages/framework/lib/interfaces/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface IBaseInteraction {
options: IBaseMessageOptions;
}

export interface IMessageInteraction<T extends {}> extends IBaseInteraction {
export interface IMessageInteraction<T> extends IBaseInteraction {
type: 'message';
id: string;
message: ISerializable;
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/lib/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class User extends UserModel {
this.handovered = false;
}
if (!data.context) {
// Initialize context
// Initialize context
this._context = {};
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/lib/routers/ReferralsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export default class ReferralsRouter extends BasicRouter {
return defaultFunc(user, ref);
}

throw new Error("Unkown referral type " + ref);
throw new Error('Unkown referral type ' + ref);
}
}
14 changes: 7 additions & 7 deletions packages/framework/lib/utilities/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function notify<A extends GenericAdapter<U>, U extends User>(
...params: [string, ...any[]]
) {
if (this._consolidated) {
throw new Error("Scenario has already ended.")
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'notify',
Expand All @@ -43,7 +43,7 @@ function handover<A extends GenericAdapter<U>, U extends User>(
...params: [string, ...any[]]
) {
if (this._consolidated) {
throw new Error("Scenario has already ended.")
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'handover',
Expand All @@ -70,7 +70,7 @@ async function end<A extends GenericAdapter<U>, U extends User>(

function wait<A extends GenericAdapter<U>, U extends User>(this: Scenario<A, U>, millis: number) {
if (this._consolidated) {
throw new Error("Scenario has already ended.")
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'wait',
Expand All @@ -85,7 +85,7 @@ function send<A extends GenericAdapter<U>, U extends User>(
options: any = {}
) {
if (this._consolidated) {
throw new Error("Scenario has already ended.")
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'message',
Expand All @@ -96,7 +96,7 @@ function send<A extends GenericAdapter<U>, U extends User>(

function types<A extends GenericAdapter<U>, U extends User>(this: Scenario<A, U>) {
if (this._consolidated) {
throw new Error("Scenario has already ended.")
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'typing_on',
Expand All @@ -107,7 +107,7 @@ function types<A extends GenericAdapter<U>, U extends User>(this: Scenario<A, U>

function seen<A extends GenericAdapter<U>, U extends User>(this: Scenario<A, U>) {
if (this._consolidated) {
throw new Error("Scenario has already ended.")
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'mark_seen',
Expand All @@ -118,7 +118,7 @@ function seen<A extends GenericAdapter<U>, U extends User>(this: Scenario<A, U>)

function stopTyping<A extends GenericAdapter<U>, U extends User>(this: Scenario<A, U>) {
if (this._consolidated) {
throw new Error("Scenario has already ended.")
throw new Error('Scenario has already ended.');
}
this._actions.push({
call: 'typing_off',
Expand Down
3 changes: 2 additions & 1 deletion packages/framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test": "yarn run build && yarn node ./build/index.js",
"prettier:base": "npx prettier --config ../../.prettierrc",
"prettier:check": "yarn run prettier:base -- --list-different \"lib/**/*.{ts,tsx}\"",
"prettier:write": "yarn run prettier:base -- --write \"lib/**/*.{ts,tsx}\""
"prettier:write": "yarn run prettier:base -- --write \"lib/**/*.{ts,tsx}\"",
"lint": "eslint '*/**/*.ts' --quiet --fix -c ../../.eslintrc.js --ignore-path ../../.eslintignore"
},
"keywords": [
"bot",
Expand Down
10 changes: 9 additions & 1 deletion packages/messenger-adapter/lib/adapter/interfaces/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ export interface Referral {
}

// TODO: Add it
export interface Standby {}
export interface Standby {
sender: {
id: string;
};

recipient: {
id: string;
};
}

export interface Delivery {
mids?: string[];
Expand Down
2 changes: 1 addition & 1 deletion packages/messenger-adapter/lib/adapter/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function senderFactory(pageToken: string, call: SenderFunction = sendAPI,
return getUserDataCall(id, fields, qs);
}

function handover(id: string, targetAppId: string = '263902037430900', metadata?: string) {
function handover(id: string, targetAppId = '263902037430900', metadata?: string) {
return passThreadControl(id, qs, targetAppId, metadata);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/messenger-adapter/lib/messengerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function getUserDataCall(id: string, fields: UserDataFields[], qs:
export async function passThreadControl(
id: string,
qs: string,
targetAppId: string = '263902037430900',
targetAppId = '263902037430900',
metadata?: string
) {
const bodyWithoutMetadata = {
Expand Down
10 changes: 2 additions & 8 deletions packages/messenger-adapter/lib/webhooks/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ export default function messagingWebhook<T extends MessengerUser>(
// TODO: Postbacks and text handler
const qr = e.message.quick_reply;
if (qr.payload) {
routerExists(routers.PostbackRouter).stringPayloadHandler(
qr.payload,
user
);
routerExists(routers.PostbackRouter).stringPayloadHandler(qr.payload, user);
return;
}
throw new Error('Not implemented');
Expand Down Expand Up @@ -59,10 +56,7 @@ export default function messagingWebhook<T extends MessengerUser>(
throw new Error('Not implemented');
}
if (e.postback.payload) {
routerExists(routers.PostbackRouter).stringPayloadHandler(
e.postback.payload,
user
);
routerExists(routers.PostbackRouter).stringPayloadHandler(e.postback.payload, user);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/messenger-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"prepare": "yarn run build",
"prettier:base": "npx prettier --config ../../.prettierrc",
"prettier:check": "yarn run prettier:base -- --list-different \"lib/**/*.{ts,tsx}\"",
"prettier:write": "yarn run prettier:base -- --write \"lib/**/*.{ts,tsx}\""
"prettier:write": "yarn run prettier:base -- --write \"lib/**/*.{ts,tsx}\"",
"lint": "eslint '*/**/*.ts' --quiet --fix -c ../../.eslintrc.js --ignore-path ../../.eslintignore"
},
"types": "./build/index.d.ts",
"author": "Christos Panagiotakopoulos <chrispanag@gmail.com>",
Expand Down
Loading

0 comments on commit 7a577a3

Please sign in to comment.