Add jsonrpc services to server(s)

This commit is contained in:
Jerko Steiner 2019-08-30 10:38:01 +07:00
parent 7434c9fb42
commit 2f35b65f48
5 changed files with 22 additions and 11 deletions

View File

@ -16,7 +16,7 @@ export interface IJSONRPCReturnType {
addService<T, F extends FunctionPropertyNames<T>>( addService<T, F extends FunctionPropertyNames<T>>(
path: string, path: string,
service: T, service: T,
methods: F[], methods?: F[],
): IJSONRPCReturnType, ): IJSONRPCReturnType,
router(): Router router(): Router
} }
@ -62,7 +62,7 @@ export function jsonrpc<Context>(
addService<T, F extends FunctionPropertyNames<T>>( addService<T, F extends FunctionPropertyNames<T>>(
path: string, path: string,
service: T, service: T,
methods: F[], methods?: F[],
) { ) {
const rpcService = createRpcService(service, methods) const rpcService = createRpcService(service, methods)

View File

@ -102,9 +102,9 @@ async function validateServiceContext<
export const createRpcService = <T, M extends FunctionPropertyNames<T>>( export const createRpcService = <T, M extends FunctionPropertyNames<T>>(
service: T, service: T,
methods: M[], methods?: M[],
) => { ) => {
const rpcService = pick(service, methods) const rpcService = methods ? pick(service, methods) : service
return { return {
async invoke<Context>( async invoke<Context>(
req: IRequest<M, ArgumentTypes<T[M]>>, req: IRequest<M, ArgumentTypes<T[M]>>,
@ -127,6 +127,7 @@ export const createRpcService = <T, M extends FunctionPropertyNames<T>>(
const isNotification = req.id === null || req.id === undefined const isNotification = req.id === null || req.id === undefined
if ( if (
method.startsWith('_') ||
!rpcService.hasOwnProperty(method) || !rpcService.hasOwnProperty(method) ||
typeof rpcService[method] !== 'function' typeof rpcService[method] !== 'function'
) { ) {

View File

@ -60,6 +60,7 @@ export class Application implements IApplication {
this.configureMiddleware(router) this.configureMiddleware(router)
this.configureRouter(router) this.configureRouter(router)
this.configureRPC(router)
this.configureApiErrorHandling(router) this.configureApiErrorHandling(router)
this.configureFrontend(router) this.configureFrontend(router)
@ -109,17 +110,23 @@ export class Application implements IApplication {
this.services.userPermissions, this.services.userPermissions,
this.createTransactionalRouter(), this.createTransactionalRouter(),
).handle) ).handle)
}
protected jsonrpc() {
return jsonrpc(
req => ({user: req.user}),
this.getApiLogger(),
)
}
protected configureRPC(router: express.Router) {
router.use( router.use(
'/rpc', '/rpc',
jsonrpc( this.jsonrpc()
req => ({user: req.user}), .addService('/team',
this.getApiLogger(),
)
.addService('/teamService',
new rpc.TeamService(this.database, this.services.userPermissions), new rpc.TeamService(this.database, this.services.userPermissions),
keys<rpc.TeamService>()) keys<rpc.TeamService>())
.addService('/userService', .addService('/user',
new rpc.UserService(this.database), new rpc.UserService(this.database),
keys<rpc.UserService>()) keys<rpc.UserService>())
.router(), .router(),

View File

@ -11,3 +11,6 @@ export * from './session'
export * from './user' export * from './user'
export * from './team' export * from './team'
export * from './validator' export * from './validator'
import * as rpc from './rpc'
export {rpc}

View File

@ -11,7 +11,7 @@
"semicolon": [true, "never"], "semicolon": [true, "never"],
"object-literal-sort-keys": false, "object-literal-sort-keys": false,
"ordered-imports": false, "ordered-imports": false,
"max-line-length": [true, 80], "max-line-length": [true, {"limit":80, "ignore-pattern": "^import .* from "}],
"arrow-parens": false, "arrow-parens": false,
"variable-name": [ "variable-name": [
true, true,