Skip to content

Commit

Permalink
feat(expressjs): make mware be always async
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Mar 25, 2020
1 parent aa1c00b commit 047ebb1
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 81 deletions.
2 changes: 2 additions & 0 deletions packages/expressjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"postupdate": "yarn && yarn build && yarn test"
},
"dependencies": {
"@qiwi/decorator-utils": "^1.3.1",
"@qiwi/mware": "^1.10.0",
"@qiwi/json-rpc-common": "^1.0.2",
"@qiwi/substrate": "^1.18.16",
"@types/express": "^4.17.3",
Expand Down
15 changes: 14 additions & 1 deletion packages/expressjs/src/main/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'reflect-metadata'

import {get} from 'lodash'

import {Request, Response} from 'express'
import {Request, Response, NextFunction} from 'express'

import {
JSON_RPC_METADATA,
Expand All @@ -18,6 +18,8 @@ import {
OK,
} from '@qiwi/json-rpc-common'
import {IMetaTypedValue} from '@qiwi/substrate'
import {constructDecorator, METHOD} from '@qiwi/decorator-utils'

export * from '@qiwi/json-rpc-common'

export const enum JsonRpcDecoratorType {
Expand All @@ -26,6 +28,16 @@ export const enum JsonRpcDecoratorType {
PARAMS = 'params',
}

const asyncMiddleware = (fn: Function) => function(this: any, req: Request, res: Response, next: NextFunction) {
return Promise.resolve(fn.call(this, req, res, next)).catch(next)
}

const AsyncMiddleware = constructDecorator((targetType, target) => {
if (targetType === METHOD) {
return asyncMiddleware(target)
}
})

type IJsonRpcMetaTypedValue = IMetaTypedValue<IParsedObject, 'jsonRpc', {}>

export function JsonRpcMiddleware(): ClassDecorator {
Expand All @@ -34,6 +46,7 @@ export function JsonRpcMiddleware(): ClassDecorator {
const extend: Extender = (base) => {
class Extended extends base {

@AsyncMiddleware()
protected middleware(req: Request, res: Response): any {
const boxedJsonRpc = (this.constructor as any).parseRequest(req)
if (!boxedJsonRpc) {
Expand Down
22 changes: 11 additions & 11 deletions packages/expressjs/src/test/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('expressjs-json-rpc', () => {
const controller = new SomeJsonRpcController()
const mware = controller.middleware.bind(controller)

it('properly handles request', () => {
it('properly handles request', async() => {
const {req, res} = reqresnext({
body: {
jsonrpc: '2.0',
Expand All @@ -59,7 +59,7 @@ describe('expressjs-json-rpc', () => {
},
})

mware(req, res)
await mware(req, res)

expect(res.statusCode).toBe(OK)
expect(res.body).toBe(
Expand All @@ -71,7 +71,7 @@ describe('expressjs-json-rpc', () => {
)
})

it('finds proper method', () => {
it('finds proper method', async() => {
const {req, res} = reqresnext({
body: {
jsonrpc: '2.0',
Expand All @@ -84,7 +84,7 @@ describe('expressjs-json-rpc', () => {
},
})

mware(req, res)
await mware(req, res)

expect(res.statusCode).toBe(OK)
expect(res.body).toBe(
Expand All @@ -96,7 +96,7 @@ describe('expressjs-json-rpc', () => {
)
})

it('returns error if method does not exist', () => {
it('returns error if method does not exist', async() => {
const {req, res} = reqresnext({
body: {
jsonrpc: '2.0',
Expand All @@ -106,7 +106,7 @@ describe('expressjs-json-rpc', () => {
},
})

mware(req, res)
await mware(req, res)

expect(res.statusCode).toBe(OK)
expect(res.body).toBe(
Expand All @@ -122,7 +122,7 @@ describe('expressjs-json-rpc', () => {
)
})

it('returns error if method returns JsonRpcError', () => {
it('returns error if method returns JsonRpcError', async() => {
const {req, res} = reqresnext({
body: {
jsonrpc: '2.0',
Expand All @@ -132,7 +132,7 @@ describe('expressjs-json-rpc', () => {
},
})

mware(req, res)
await mware(req, res)

expect(res.statusCode).toBe(OK)
expect(res.body).toBe(
Expand All @@ -148,7 +148,7 @@ describe('expressjs-json-rpc', () => {
})

describe('static', () => {
describe('parseRequest', () => {
describe('#parseRequest', () => {
it('parseRequest return IJsonRpcMetaTypedValue', () => {
// @ts-ignore
const res = SomeJsonRpcController.parseRequest({
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('expressjs-json-rpc', () => {
})
})

describe('resolveHandler', () => {
describe('#resolveHandler', () => {
it('work correctly', () => {
// @ts-ignore
const boxedRpc = SomeJsonRpcController.parseRequest({
Expand All @@ -205,7 +205,7 @@ describe('expressjs-json-rpc', () => {
})
})

describe('resolveParam', () => {
describe('#resolveParam', () => {
it('work correctly', () => {
// @ts-ignore
const boxedRpc = SomeJsonRpcController.parseRequest({
Expand Down
Loading

0 comments on commit 047ebb1

Please sign in to comment.