Skip to content

Commit

Permalink
fix: req.query not typed
Browse files Browse the repository at this point in the history
  • Loading branch information
gempain committed Mar 5, 2021
1 parent 6239683 commit 581e3ca
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/commons/express-joi/body.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('body', () => {

let app: Express;
let server: Server;
let b: any;

beforeEach(async () => {
app = express();
Expand All @@ -18,7 +19,10 @@ describe('body', () => {
body(object({
test: number().required(),
})),
(req, res) => res.status(200).send(),
(req, res) => {
b = req.body;
res.status(200).send();
},
);
server = app.listen(3000);
});
Expand All @@ -36,6 +40,7 @@ describe('body', () => {
});

expect(response.status).toEqual(200);
expect(b.test).toEqual(1);
});

it('should throw error when token not found', async () => {
Expand Down
6 changes: 5 additions & 1 deletion src/commons/express-joi/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export function params($schema: AnySchema) {
return (req: Request, res: Response, next: NextFunction) => {
$schema
.validateAsync(req.params, JOI_OPTIONS)
.then(() => next(undefined))
.then(value => {
// req.params = value;
// console.log(req.params, value);
next();
})
.catch(err => {
next(new BadRequestError('Invalid params', err.details));
});
Expand Down
9 changes: 7 additions & 2 deletions src/commons/express-joi/query.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import request from 'supertest';
import express, { Express } from 'express';
import { Server } from 'http';
import { query } from './query';
import { number, object } from 'joi';
import { query } from './query';

describe('query', () => {

let app: Express;
let server: Server;
let q: any;

beforeEach(async () => {
app = express();
Expand All @@ -16,7 +17,10 @@ describe('query', () => {
query(object({
test: number().required(),
})),
(req, res) => res.status(200).send(),
(req, res) => {
q = req.query;
return res.status(200).send();
},
);
server = app.listen(3000);
});
Expand All @@ -35,6 +39,7 @@ describe('query', () => {
.send();

expect(response.status).toEqual(200);
expect(q.test).toEqual(1);
});

it('should throw error when token not found', async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/commons/express-joi/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export function query($schema: AnySchema) {
return (req: Request, res: Response, next: NextFunction) => {
$schema
.validateAsync(req.query, JOI_OPTIONS)
.then(() => next(undefined))
.then(value => {
req.query = value;
next(undefined);
})
.catch(err => {
next(new BadRequestError('Invalid query', err.details));
});
Expand Down

0 comments on commit 581e3ca

Please sign in to comment.