Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5302 from LiskHQ/5188-controller-typescript
Browse files Browse the repository at this point in the history
Update controller to be typescript - Closes #5188
  • Loading branch information
shuse2 authored May 11, 2020
2 parents 67e50b3 + bb5bd80 commit 0540d72
Show file tree
Hide file tree
Showing 46 changed files with 1,966 additions and 1,690 deletions.
4 changes: 3 additions & 1 deletion framework/src/application/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const {
const { getNetworkIdentifier } = require('@liskhq/lisk-cryptography');
const { validator: liskValidator } = require('@liskhq/lisk-validator');
const _ = require('lodash');
const Controller = require('../controller/controller');
const { Controller } = require('../controller/controller');
const version = require('../version');
const validator = require('./validator');
const configurator = require('./default_configurator');
Expand Down Expand Up @@ -443,9 +443,11 @@ class Application {
handler: action => this._network.broadcast(action.params),
},
requestFromNetwork: {
// eslint-disable-next-line @typescript-eslint/require-await
handler: async action => this._network.request(action.params),
},
requestFromPeer: {
// eslint-disable-next-line @typescript-eslint/require-await
handler: async action => this._network.requestFromPeer(action.params),
},
getConnectedPeers: {
Expand Down
67 changes: 0 additions & 67 deletions framework/src/controller/action.js

This file was deleted.

100 changes: 100 additions & 0 deletions framework/src/controller/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright © 2019 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/

import { strict as assert } from 'assert';
import { actionWithModuleNameReg, moduleNameReg } from './constants';

export interface ActionInfoObject {
readonly module: string;
readonly name: string;
readonly source?: string;
readonly params?: object;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type ActionHandler = (action: ActionInfoObject) => any;

export interface ActionsDefinition {
[key: string]: ActionHandler | { handler: ActionHandler; isPublic?: boolean };
}

export interface ActionsObject {
[key: string]: Action;
}

export class Action {
public module: string;
public name: string;
public isPublic: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public handler?: (action: ActionInfoObject) => any;
public source?: string;
public params?: object;

public constructor(
name: string,
params?: object,
source?: string,
isPublic?: boolean,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
handler?: (action: ActionInfoObject) => any,
) {
assert(
actionWithModuleNameReg.test(name),
`Action name "${name}" must be a valid name with module name.`,
);
[this.module, this.name] = name.split(':');
this.params = params;

if (source) {
assert(
moduleNameReg.test(source),
`Source name "${source}" must be a valid module name.`,
);
this.source = source;
}

this.handler = handler;
this.isPublic = isPublic ?? false;
}

public static deserialize(data: ActionInfoObject | string): Action {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const parsedAction: ActionInfoObject =
typeof data === 'string' ? JSON.parse(data) : data;

return new Action(
`${parsedAction.module}:${parsedAction.name}`,
parsedAction.params,
parsedAction.source,
);
}

public serialize(): ActionInfoObject {
return {
name: this.name,
module: this.module,
source: this.source,
params: this.params,
};
}

public toString(): string {
return `${this.source ?? 'undefined'} -> ${this.module}:${this.name}`;
}

public key(): string {
return `${this.module}:${this.name}`;
}
}
Loading

0 comments on commit 0540d72

Please sign in to comment.