Make express jsonrpc use a builder pattern
This commit is contained in:
parent
f3f6166aab
commit
86e90d28bb
@ -56,16 +56,17 @@ describe('jsonrpc', () => {
|
||||
function createApp() {
|
||||
const app = express()
|
||||
app.use(bodyParser.json())
|
||||
app.use('/myService',
|
||||
app.use('/',
|
||||
jsonrpc(req => ({userId: 1000}), noopLogger)
|
||||
.addService(new Service(5), [
|
||||
.addService('/myService', new Service(5), [
|
||||
'add',
|
||||
'delay',
|
||||
'syncError',
|
||||
'asyncError',
|
||||
'httpError',
|
||||
'addWithContext',
|
||||
]),
|
||||
])
|
||||
.router(),
|
||||
)
|
||||
return app
|
||||
}
|
||||
|
||||
@ -43,19 +43,20 @@ export function jsonrpc<Context>(
|
||||
res.json(errorResponse)
|
||||
}
|
||||
|
||||
return {
|
||||
const router = Router()
|
||||
|
||||
const self = {
|
||||
/**
|
||||
* Adds middleware for handling JSON RPC requests. Expects JSON middleware to
|
||||
* already be configured.
|
||||
*/
|
||||
addService<T, F extends FunctionPropertyNames<T>>(
|
||||
path: string,
|
||||
service: T,
|
||||
methods: F[],
|
||||
) {
|
||||
const rpcService = createRpcService(service, methods)
|
||||
|
||||
const router = Router()
|
||||
|
||||
function handleResponse(
|
||||
response: ISuccessResponse<unknown> | null,
|
||||
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)) {
|
||||
// TODO fix status code and error type
|
||||
const err = createError(ERROR_METHOD_NOT_FOUND, {
|
||||
@ -89,18 +90,21 @@ export function jsonrpc<Context>(
|
||||
.catch(next)
|
||||
})
|
||||
|
||||
router.post('/', (req, res, next) => {
|
||||
router.post(path, (req, res, next) => {
|
||||
rpcService.invoke(req.body, getContext(req))
|
||||
.then(response => handleResponse(response, res))
|
||||
.catch(next)
|
||||
})
|
||||
|
||||
router.use('/', handleError)
|
||||
|
||||
router.use(path, handleError)
|
||||
return self
|
||||
},
|
||||
router() {
|
||||
return router
|
||||
},
|
||||
}
|
||||
|
||||
return self
|
||||
}
|
||||
|
||||
function getRequestId(req: express.Request) {
|
||||
|
||||
@ -55,11 +55,12 @@ describe('createActions', () => {
|
||||
|
||||
const app = express()
|
||||
app.use(bodyParser.json())
|
||||
app.use('/service', jsonrpc(() => ({userId: 1000}), noopLogger)
|
||||
.addService(
|
||||
new Service(),
|
||||
keys<IService>(),
|
||||
))
|
||||
app.use(
|
||||
'/',
|
||||
jsonrpc(() => ({userId: 1000}), noopLogger)
|
||||
.addService('/service', new Service(), keys<IService>())
|
||||
.router(),
|
||||
)
|
||||
|
||||
let baseUrl: string
|
||||
let server: Server
|
||||
|
||||
@ -35,8 +35,12 @@ describe('remote', () => {
|
||||
function createApp() {
|
||||
const a = express()
|
||||
a.use(bodyParser.json())
|
||||
a.use('/myService', jsonrpc(() => ({}), noopLogger)
|
||||
.addService(service, IServiceKeys))
|
||||
a.use(
|
||||
'/',
|
||||
jsonrpc(() => ({}), noopLogger)
|
||||
.addService('/myService', service, IServiceKeys)
|
||||
.router(),
|
||||
)
|
||||
return a
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user