Make express jsonrpc use a builder pattern

This commit is contained in:
Jerko Steiner 2019-08-04 19:59:47 +07:00
parent f3f6166aab
commit 86e90d28bb
4 changed files with 27 additions and 17 deletions

View File

@ -56,16 +56,17 @@ describe('jsonrpc', () => {
function createApp() { function createApp() {
const app = express() const app = express()
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use('/myService', app.use('/',
jsonrpc(req => ({userId: 1000}), noopLogger) jsonrpc(req => ({userId: 1000}), noopLogger)
.addService(new Service(5), [ .addService('/myService', new Service(5), [
'add', 'add',
'delay', 'delay',
'syncError', 'syncError',
'asyncError', 'asyncError',
'httpError', 'httpError',
'addWithContext', 'addWithContext',
]), ])
.router(),
) )
return app return app
} }

View File

@ -43,19 +43,20 @@ export function jsonrpc<Context>(
res.json(errorResponse) res.json(errorResponse)
} }
return { const router = Router()
const self = {
/** /**
* Adds middleware for handling JSON RPC requests. Expects JSON middleware to * Adds middleware for handling JSON RPC requests. Expects JSON middleware to
* already be configured. * already be configured.
*/ */
addService<T, F extends FunctionPropertyNames<T>>( addService<T, F extends FunctionPropertyNames<T>>(
path: string,
service: T, service: T,
methods: F[], methods: F[],
) { ) {
const rpcService = createRpcService(service, methods) const rpcService = createRpcService(service, methods)
const router = Router()
function handleResponse( function handleResponse(
response: ISuccessResponse<unknown> | null, response: ISuccessResponse<unknown> | null,
res: Response, res: Response,
@ -68,7 +69,7 @@ export function jsonrpc<Context>(
} }
} }
router.get('/', (req, res, next) => { router.get(path, (req, res, next) => {
if (!idempotentMethodRegex.test(req.query.method)) { if (!idempotentMethodRegex.test(req.query.method)) {
// TODO fix status code and error type // TODO fix status code and error type
const err = createError(ERROR_METHOD_NOT_FOUND, { const err = createError(ERROR_METHOD_NOT_FOUND, {
@ -89,18 +90,21 @@ export function jsonrpc<Context>(
.catch(next) .catch(next)
}) })
router.post('/', (req, res, next) => { router.post(path, (req, res, next) => {
rpcService.invoke(req.body, getContext(req)) rpcService.invoke(req.body, getContext(req))
.then(response => handleResponse(response, res)) .then(response => handleResponse(response, res))
.catch(next) .catch(next)
}) })
router.use('/', handleError) router.use(path, handleError)
return self
},
router() {
return router return router
}, },
} }
return self
} }
function getRequestId(req: express.Request) { function getRequestId(req: express.Request) {

View File

@ -55,11 +55,12 @@ describe('createActions', () => {
const app = express() const app = express()
app.use(bodyParser.json()) app.use(bodyParser.json())
app.use('/service', jsonrpc(() => ({userId: 1000}), noopLogger) app.use(
.addService( '/',
new Service(), jsonrpc(() => ({userId: 1000}), noopLogger)
keys<IService>(), .addService('/service', new Service(), keys<IService>())
)) .router(),
)
let baseUrl: string let baseUrl: string
let server: Server let server: Server

View File

@ -35,8 +35,12 @@ describe('remote', () => {
function createApp() { function createApp() {
const a = express() const a = express()
a.use(bodyParser.json()) a.use(bodyParser.json())
a.use('/myService', jsonrpc(() => ({}), noopLogger) a.use(
.addService(service, IServiceKeys)) '/',
jsonrpc(() => ({}), noopLogger)
.addService('/myService', service, IServiceKeys)
.router(),
)
return a return a
} }