Skip to content

Commit faaa3e1

Browse files
authored
fix: make intercept method receive body as parameter (#21)
* fix: make intercept method receive body as parameter * style: update package to version 1.0.5
1 parent 2ac5514 commit faaa3e1

File tree

9 files changed

+64
-19
lines changed

9 files changed

+64
-19
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,14 @@ export class Middleware implements MiddlewareContract {
168168
ctx.body.intercepted = true
169169
ctx.response.status(304)
170170

171-
ctx.next()
171+
// Example
172+
if ('intercept method logic changes the body') {
173+
// In intercept method, next function needs to receive the new body payload as parameter.
174+
ctx.next({ hello: 'intercepted', ...ctx.body })
175+
} else {
176+
// If your logic does not change the body you can just do like this
177+
ctx.next(ctx.body)
178+
}
172179
}
173180

174181
// Terminate method will be executed after the response goes to client

package-lock.json

Lines changed: 23 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@secjs/http",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "",
55
"license": "MIT",
66
"author": "João Lenon",
@@ -26,7 +26,7 @@
2626
"@secjs/env": "1.2.8",
2727
"@secjs/exceptions": "1.0.4",
2828
"@secjs/ioc": "1.0.8",
29-
"@secjs/utils": "1.6.5",
29+
"@secjs/utils": "1.6.7",
3030
"@types/jest": "27.0.1",
3131
"@types/node": "14.14.37",
3232
"@types/supertest": "^2.0.11",

src/Contracts/Context/Middlewares/Intercept/InterceptContextContract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import { NextContract } from '../../NextContract'
1110
import { RequestContract } from '../../RequestContract'
1211
import { ResponseContract } from '../../ResponseContract'
12+
import { NextInterceptContract } from './NextInterceptContract'
1313

1414
export interface InterceptContextContract {
1515
request: RequestContract
@@ -18,5 +18,5 @@ export interface InterceptContextContract {
1818
queries: Record<string, string>
1919
body: Record<string, any>
2020
data?: Record<string, any>
21-
next: NextContract
21+
next: NextInterceptContract
2222
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* @secjs/http
3+
*
4+
* (c) João Lenon <lenon@secjs.com.br>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
export interface NextInterceptContract {
11+
(body: Record<string, any>, error?: any): Promise<void> | void
12+
}

src/Utils/FastifyHandler.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { HandlerContract } from '../Contracts/Context/HandlerContract'
1616
import { ErrorHandlerContract } from '../Contracts/Context/Error/ErrorHandlerContract'
1717
import { HandleHandlerContract } from '../Contracts/Context/Middlewares/Handle/HandleHandlerContract'
1818
import { InterceptHandlerContract } from '../Contracts/Context/Middlewares/Intercept/InterceptHandlerContract'
19+
import { Is } from '@secjs/utils'
1920

2021
declare module 'fastify' {
2122
interface FastifyRequest {
@@ -33,7 +34,11 @@ export class FastifyHandler {
3334
if (!req.query) req.query = {}
3435
if (!req.params) req.params = {}
3536

36-
const body = JSON.parse(payload)
37+
let body = payload
38+
39+
if (Is.Json(payload)) {
40+
body = JSON.parse(body)
41+
}
3742

3843
return handler({
3944
request,
@@ -42,7 +47,14 @@ export class FastifyHandler {
4247
params: req.params as Record<string, string>,
4348
queries: req.query as Record<string, string>,
4449
data: req.data,
45-
next: () => done(null, JSON.stringify(body)),
50+
next: (
51+
body: string | Buffer | Record<string, any> | null,
52+
error = null,
53+
) => {
54+
if (Is.Object(body)) body = JSON.stringify(body)
55+
56+
done(error, body)
57+
},
4658
})
4759
}
4860
}

tests/http.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe('\n Http Class', () => {
101101
await middlewareHttp.use(ctx => {
102102
ctx.body.hello = ctx.body.hello.replace('world', 'world-intercepted')
103103

104-
ctx.next()
104+
ctx.next(ctx.body)
105105
}, 'intercept')
106106

107107
middlewareHttp.get('/test', handler)

tests/stubs/InterceptMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export class InterceptMiddleware implements MiddlewareContract {
1616

1717
ctx.body.middlewares.push('intercept')
1818

19-
ctx.next()
19+
ctx.next(ctx.body)
2020
}
2121
}

tests/stubs/TestMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class TestMiddleware implements MiddlewareContract {
2424
ctx.data.param = 'param'
2525
ctx.request.queries.test = 'middleware'
2626

27-
ctx.next()
27+
ctx.next(ctx.body)
2828
}
2929

3030
async terminate(ctx: TerminateContextContract) {

0 commit comments

Comments
 (0)