From 2f35b65f485c80c9cb18d05eafff91ef26df4de5 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Fri, 30 Aug 2019 10:38:01 +0700 Subject: [PATCH] Add jsonrpc services to server(s) --- packages/jsonrpc/src/express.ts | 4 ++-- packages/jsonrpc/src/jsonrpc.ts | 5 +++-- .../server/src/application/Application.ts | 19 +++++++++++++------ packages/server/src/index.ts | 3 +++ packages/tslint.json | 2 +- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/jsonrpc/src/express.ts b/packages/jsonrpc/src/express.ts index 85d5c14..a4cca57 100644 --- a/packages/jsonrpc/src/express.ts +++ b/packages/jsonrpc/src/express.ts @@ -16,7 +16,7 @@ export interface IJSONRPCReturnType { addService>( path: string, service: T, - methods: F[], + methods?: F[], ): IJSONRPCReturnType, router(): Router } @@ -62,7 +62,7 @@ export function jsonrpc( addService>( path: string, service: T, - methods: F[], + methods?: F[], ) { const rpcService = createRpcService(service, methods) diff --git a/packages/jsonrpc/src/jsonrpc.ts b/packages/jsonrpc/src/jsonrpc.ts index cbb0d63..098a455 100644 --- a/packages/jsonrpc/src/jsonrpc.ts +++ b/packages/jsonrpc/src/jsonrpc.ts @@ -102,9 +102,9 @@ async function validateServiceContext< export const createRpcService = >( service: T, - methods: M[], + methods?: M[], ) => { - const rpcService = pick(service, methods) + const rpcService = methods ? pick(service, methods) : service return { async invoke( req: IRequest>, @@ -127,6 +127,7 @@ export const createRpcService = >( const isNotification = req.id === null || req.id === undefined if ( + method.startsWith('_') || !rpcService.hasOwnProperty(method) || typeof rpcService[method] !== 'function' ) { diff --git a/packages/server/src/application/Application.ts b/packages/server/src/application/Application.ts index a57baf5..73629dc 100644 --- a/packages/server/src/application/Application.ts +++ b/packages/server/src/application/Application.ts @@ -60,6 +60,7 @@ export class Application implements IApplication { this.configureMiddleware(router) this.configureRouter(router) + this.configureRPC(router) this.configureApiErrorHandling(router) this.configureFrontend(router) @@ -109,17 +110,23 @@ export class Application implements IApplication { this.services.userPermissions, this.createTransactionalRouter(), ).handle) + } + protected jsonrpc() { + return jsonrpc( + req => ({user: req.user}), + this.getApiLogger(), + ) + } + + protected configureRPC(router: express.Router) { router.use( '/rpc', - jsonrpc( - req => ({user: req.user}), - this.getApiLogger(), - ) - .addService('/teamService', + this.jsonrpc() + .addService('/team', new rpc.TeamService(this.database, this.services.userPermissions), keys()) - .addService('/userService', + .addService('/user', new rpc.UserService(this.database), keys()) .router(), diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index c539ef1..6770366 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -11,3 +11,6 @@ export * from './session' export * from './user' export * from './team' export * from './validator' + +import * as rpc from './rpc' +export {rpc} diff --git a/packages/tslint.json b/packages/tslint.json index 7b94864..867e1c4 100644 --- a/packages/tslint.json +++ b/packages/tslint.json @@ -11,7 +11,7 @@ "semicolon": [true, "never"], "object-literal-sort-keys": false, "ordered-imports": false, - "max-line-length": [true, 80], + "max-line-length": [true, {"limit":80, "ignore-pattern": "^import .* from "}], "arrow-parens": false, "variable-name": [ true,