From 1aa6cae0f3ce545b6d4d32f84a95d3baaa50cea1 Mon Sep 17 00:00:00 2001 From: Jerko Steiner Date: Thu, 5 Sep 2019 09:25:38 +0700 Subject: [PATCH] Add ability to specify jsonrpc context asynchronously --- packages/jsonrpc/src/express.test.ts | 8 +++--- packages/jsonrpc/src/express.ts | 37 +++++++++++++++++++--------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/jsonrpc/src/express.test.ts b/packages/jsonrpc/src/express.test.ts index 2759682..e6d0ecd 100644 --- a/packages/jsonrpc/src/express.test.ts +++ b/packages/jsonrpc/src/express.test.ts @@ -243,7 +243,7 @@ describe('jsonrpc', () => { .expect(405) }) - describe('wrapCall', () => { + describe('hook', () => { let requests: IRequest[] = [] let results: any[] = [] @@ -258,10 +258,10 @@ describe('jsonrpc', () => { app.use(bodyParser.json()) app.use('/', jsonrpc( - req => ({userId}), + req => Promise.resolve({userId}), noopLogger, - async (path, req, makeRequest) => { - requests.push(req) + async (details, makeRequest) => { + requests.push(details.request) const result = await makeRequest() results.push(result) return result diff --git a/packages/jsonrpc/src/express.ts b/packages/jsonrpc/src/express.ts index f7c3c99..1bafcb8 100644 --- a/packages/jsonrpc/src/express.ts +++ b/packages/jsonrpc/src/express.ts @@ -11,7 +11,7 @@ import { IRequest, } from './jsonrpc' -export type TGetContext = (req: Request) => Context +export type TGetContext = (req: Request) => Promise | Context export interface IJSONRPCReturnType { addService>( @@ -22,19 +22,26 @@ export interface IJSONRPCReturnType { router(): Router } -async function wrap( - path: string, request: A, fn: () => Promise): Promise { - const result = await fn() +export interface IInvocationDetails { + context: Context + path: string + request: A +} + +async function defaultHook( + details: IInvocationDetails, + invoke: () => Promise, +): Promise { + const result = await invoke() return result } export function jsonrpc( getContext: TGetContext, logger: ILogger, - wrapCall: ( - path: string, - request: A, - fn: (request?: A) => Promise) => Promise = wrap, + hook: ( + details: IInvocationDetails, + invoke: (request?: A) => Promise) => Promise = defaultHook, idempotentMethodRegex = IDEMPOTENT_METHOD_REGEX, ): IJSONRPCReturnType { @@ -105,15 +112,21 @@ export function jsonrpc( method: req.query.method, params: JSON.parse(req.query.params), } - wrapCall(path, request, - (body = request) => rpcService.invoke(body, getContext(req))) + Promise.resolve(getContext(req)) + .then(context => + hook( + {path, request, context}, + (body = request) => rpcService.invoke(body, context))) .then(response => handleResponse(response, res)) .catch(next) }) router.post(path, (req, res, next) => { - wrapCall(path, req.body, - (body = req.body) => rpcService.invoke(body, getContext(req))) + Promise.resolve(getContext(req)) + .then(context => + hook( + {path, request: req.body, context}, + (body = req.body) => rpcService.invoke(body, context))) .then(response => handleResponse(response, res)) .catch(next) })