Skip to content

Commit 08b8f63

Browse files
committed
feat: add ctx property on request and response
1 parent 01e5172 commit 08b8f63

File tree

8 files changed

+57
-17
lines changed

8 files changed

+57
-17
lines changed

adonis-typings/request.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ declare module '@ioc:Adonis/Core/Request' {
1515
import { UrlWithStringQuery } from 'url'
1616
import { MacroableConstructorContract } from 'macroable'
1717
import { EncryptionContract } from '@ioc:Adonis/Core/Encryption'
18+
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
1819
import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http'
1920

2021
/**
@@ -24,7 +25,8 @@ declare module '@ioc:Adonis/Core/Request' {
2425
parsedUrl: UrlWithStringQuery
2526
request: IncomingMessage
2627
response: ServerResponse
27-
id (): string | undefined,
28+
ctx?: HttpContextContract
29+
id (): string | undefined
2830
setInitialBody (body: any): void
2931
updateBody (body: any): void
3032
updateRawBody (body: string): void

adonis-typings/response.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ declare module '@ioc:Adonis/Core/Response' {
1515
import { CookieOptions } from '@poppinss/cookie'
1616
import { ServerResponse, IncomingMessage } from 'http'
1717
import { MacroableConstructorContract } from 'macroable'
18+
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
1819

1920
/**
2021
* Types from which response header can be casted to a
@@ -57,6 +58,7 @@ declare module '@ioc:Adonis/Core/Response' {
5758
isPending: boolean
5859
request: IncomingMessage
5960
response: ServerResponse
61+
ctx?: HttpContextContract
6062

6163
getHeader (key: string): string | string[] | number | undefined
6264
header (key: string, value: CastableHeader): this

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"anyBranch": false
9393
},
9494
"dependencies": {
95-
"@poppinss/cookie": "^1.0.7",
95+
"@poppinss/cookie": "^1.0.8",
9696
"@poppinss/utils": "^2.1.2",
9797
"accepts": "^1.3.7",
9898
"clone-deep": "^4.0.1",

src/HttpContext/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ export class HttpContext extends Macroable implements HttpContextContract {
5353
public profiler: ProfilerRowContract,
5454
) {
5555
super()
56+
/**
57+
* Creating the circular reference. We do this, since request and response
58+
* are meant to be extended and at times people would want to access
59+
* other ctx properties like `logger`, `profiler` inside those
60+
* extended methods.
61+
*/
62+
this.request.ctx = this
63+
this.response.ctx = this
5664
}
5765

5866
/**

src/Request/index.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { parse as parseCookie } from '@poppinss/cookie'
3333
import { ServerResponse, IncomingMessage, IncomingHttpHeaders } from 'http'
3434

3535
import { EncryptionContract } from '@ioc:Adonis/Core/Encryption'
36+
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
3637
import { RequestContract, RequestConfigContract } from '@ioc:Adonis/Core/Request'
3738

3839
import { trustProxy } from '../helpers'
@@ -47,13 +48,6 @@ import { trustProxy } from '../helpers'
4748
* using `request.request` property.
4849
*/
4950
export class Request extends Macroable implements RequestContract {
50-
/**
51-
* Parses copy of the URL with query string as a string and not
52-
* object. This is done to build URL's with query string without
53-
* stringifying the object
54-
*/
55-
public parsedUrl: UrlWithStringQuery = parse(this.request.url!, false)
56-
5751
/**
5852
* Request body set using `setBody` method
5953
*/
@@ -100,6 +94,19 @@ export class Request extends Macroable implements RequestContract {
10094
protected static macros = {}
10195
protected static getters = {}
10296

97+
/**
98+
* Parses copy of the URL with query string as a string and not
99+
* object. This is done to build URL's with query string without
100+
* stringifying the object
101+
*/
102+
public parsedUrl: UrlWithStringQuery = parse(this.request.url!, false)
103+
104+
/**
105+
* The ctx will be set by the context itself. It creates a circular
106+
* reference
107+
*/
108+
public ctx?: HttpContextContract
109+
103110
constructor (
104111
public request: IncomingMessage,
105112
public response: ServerResponse,

src/Response/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ class HttpException extends Exception {
110110
* 3. The call to `finish` method is a noop.
111111
*/
112112
export class Response extends Macroable implements ResponseContract {
113+
protected static macros = {}
114+
protected static getters = {}
115+
116+
private headers: any = {}
117+
private explicitStatus = false
118+
113119
/**
114120
* Lazy body is used to set the response body. However, do not
115121
* write it on the socket immediately unless `response.finish`
@@ -119,11 +125,11 @@ export class Response extends Macroable implements ResponseContract {
119125
*/
120126
public lazyBody: LazyBody | null = null
121127

122-
protected static macros = {}
123-
protected static getters = {}
124-
125-
private headers: any = {}
126-
private explicitStatus = false
128+
/**
129+
* The ctx will be set by the context itself. It creates a circular
130+
* reference
131+
*/
132+
public ctx?: HttpContextContract
127133

128134
constructor (
129135
public request: IncomingMessage,

test/http-context.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ test.group('Http Context', () => {
5656
assert.equal(ctx.request.url(), '/1')
5757
assert.deepEqual(ctx.params, { id: '1' })
5858
})
59+
60+
test('pass ctx to request and response', async (assert) => {
61+
const logger = new Logger({ enabled: true, name: 'adonis', level: 'trace' })
62+
const profiler = new Profiler(__dirname, logger, {})
63+
64+
const ctx = HttpContext.create('/', {}, logger, profiler.create('ctx'), encryption)
65+
assert.deepEqual(ctx.request.ctx, ctx)
66+
assert.deepEqual(ctx.response.ctx, ctx)
67+
})
5968
})

0 commit comments

Comments
 (0)