Skip to content

Commit 0244b63

Browse files
authored
feat: expose getStore() (#215)
1 parent 150564a commit 0244b63

4 files changed

Lines changed: 55 additions & 22 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ app.addHook('onRequest', (req, reply, done) => {
107107
app.get('/', (req, reply) => {
108108
// requestContext singleton exposed by the library retains same request-scoped values that were set using `req.requestContext`
109109
const user = requestContext.get('user');
110-
reply.code(200).send( { user });
110+
111+
// read the whole store
112+
const store = req.requestContext.getStore();
113+
reply.code(200).send( { store });
111114
});
112115

113116
app.get('/decorator', function (req, reply) {

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const requestContext = {
1919
store[key] = value
2020
}
2121
},
22+
getStore: () => {
23+
return asyncLocalStorage.getStore()
24+
},
2225
}
2326

2427
function fastifyRequestContext(fastify, opts, next) {

test-tap/requestContextPlugin.e2e.test.js

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test('correctly preserves values set in prevalidation phase within single POST r
5858
const response1Promise = request('POST', url)
5959
.send({ requestId: 1 })
6060
.then((response) => {
61-
t.assert.deepStrictEqual(response.body.storedValue, 'testValue1')
61+
t.assert.strictEqual(response.body.storedValue, 'testValue1')
6262
responseCounter++
6363
if (responseCounter === 2) {
6464
resolveResponsePromise()
@@ -68,7 +68,7 @@ test('correctly preserves values set in prevalidation phase within single POST r
6868
const response2Promise = request('POST', url)
6969
.send({ requestId: 2 })
7070
.then((response) => {
71-
t.assert.deepStrictEqual(response.body.storedValue, 'testValue2')
71+
t.assert.strictEqual(response.body.storedValue, 'testValue2')
7272
responseCounter++
7373
if (responseCounter === 2) {
7474
resolveResponsePromise()
@@ -100,8 +100,8 @@ test('correctly preserves values set in multiple phases within single POST reque
100100
const preValidationValue = req.requestContext.get('preValidation')
101101
const preHandlerValue = req.requestContext.get('preHandler')
102102

103-
t.assert.deepStrictEqual(onRequestValue, undefined)
104-
t.assert.deepStrictEqual(preParsingValue, undefined)
103+
t.assert.strictEqual(onRequestValue, undefined)
104+
t.assert.strictEqual(preParsingValue, undefined)
105105
t.assert.ok(typeof preValidationValue === 'number')
106106
t.assert.ok(typeof preHandlerValue === 'number')
107107

@@ -137,7 +137,7 @@ test('correctly preserves values set in multiple phases within single POST reque
137137
const response1Promise = request('POST', url)
138138
.send({ requestId: 1 })
139139
.then((response) => {
140-
t.assert.deepStrictEqual(response.body.storedValue, 'testValue1')
140+
t.assert.strictEqual(response.body.storedValue, 'testValue1')
141141
responseCounter++
142142
if (responseCounter === 2) {
143143
resolveResponsePromise()
@@ -147,7 +147,7 @@ test('correctly preserves values set in multiple phases within single POST reque
147147
const response2Promise = request('POST', url)
148148
.send({ requestId: 2 })
149149
.then((response) => {
150-
t.assert.deepStrictEqual(response.body.storedValue, 'testValue2')
150+
t.assert.strictEqual(response.body.storedValue, 'testValue2')
151151
responseCounter++
152152
if (responseCounter === 2) {
153153
resolveResponsePromise()
@@ -174,8 +174,8 @@ test('correctly preserves values set in multiple phases within single POST reque
174174
const preValidationValue = req.requestContext.get('preValidation')
175175
const preHandlerValue = req.requestContext.get('preHandler')
176176

177-
t.assert.deepStrictEqual(onRequestValue, 'dummy')
178-
t.assert.deepStrictEqual(preParsingValue, 'dummy')
177+
t.assert.strictEqual(onRequestValue, 'dummy')
178+
t.assert.strictEqual(preParsingValue, 'dummy')
179179
t.assert.ok(typeof preValidationValue === 'number')
180180
t.assert.ok(typeof preHandlerValue === 'number')
181181

@@ -191,9 +191,9 @@ test('correctly preserves values set in multiple phases within single POST reque
191191
return request('POST', url)
192192
.send({ requestId: 1 })
193193
.then((response) => {
194-
t.assert.deepStrictEqual(response.body.storedValue, 'testValue1')
195-
t.assert.deepStrictEqual(response.body.preSerialization1, 'dummy')
196-
t.assert.deepStrictEqual(response.body.preSerialization2, 1)
194+
t.assert.strictEqual(response.body.storedValue, 'testValue1')
195+
t.assert.strictEqual(response.body.preSerialization1, 'dummy')
196+
t.assert.strictEqual(response.body.preSerialization2, 1)
197197
})
198198
})
199199
})
@@ -219,7 +219,7 @@ test('does not affect new request context when mutating context data using no de
219219
return request('GET', url)
220220
.query({ action: 'setvalue' })
221221
.then((response1) => {
222-
t.assert.deepStrictEqual(response1.body.userId, 'abc')
222+
t.assert.strictEqual(response1.body.userId, 'abc')
223223

224224
return request('GET', url).then((response2) => {
225225
t.assert.ok(!response2.body.userId)
@@ -251,10 +251,10 @@ test('does not affect new request context when mutating context data using defau
251251
return request('GET', url)
252252
.query({ action: 'setvalue' })
253253
.then((response1) => {
254-
t.assert.deepStrictEqual(response1.body.userId, 'abc')
254+
t.assert.strictEqual(response1.body.userId, 'abc')
255255

256256
return request('GET', url).then((response2) => {
257-
t.assert.deepStrictEqual(response2.body.userId, 'bar')
257+
t.assert.strictEqual(response2.body.userId, 'bar')
258258
})
259259
})
260260
})
@@ -283,10 +283,10 @@ test('does not affect new request context when mutating context data using defau
283283
return request('GET', url)
284284
.query({ action: 'setvalue' })
285285
.then((response1) => {
286-
t.assert.deepStrictEqual(response1.body.userId, 'bob')
286+
t.assert.strictEqual(response1.body.userId, 'bob')
287287

288288
return request('GET', url).then((response2) => {
289-
t.assert.deepStrictEqual(response2.body.userId, 'system')
289+
t.assert.strictEqual(response2.body.userId, 'system')
290290
})
291291
})
292292
})
@@ -308,7 +308,7 @@ test('ensure request instance is properly exposed to default values factory', (t
308308
const url = `${address}:${port}`
309309

310310
return request('GET', url).then((response1) => {
311-
t.assert.deepStrictEqual(response1.body.userId, 'http')
311+
t.assert.strictEqual(response1.body.userId, 'http')
312312
})
313313
})
314314
})
@@ -328,10 +328,10 @@ test('does not throw when accessing context object outside of context', (t) => {
328328
const { address, port } = app.server.address()
329329
const url = `${address}:${port}`
330330

331-
t.assert.deepStrictEqual(app.requestContext.get('user'), undefined)
331+
t.assert.strictEqual(app.requestContext.get('user'), undefined)
332332

333333
return request('GET', url).then((response1) => {
334-
t.assert.deepStrictEqual(response1.body.userId, 'system')
334+
t.assert.strictEqual(response1.body.userId, 'system')
335335
})
336336
})
337337
})
@@ -351,7 +351,7 @@ test('passing a custom resource factory function when create as AsyncResource',
351351

352352
const route = (req) => {
353353
const store = container.getStore(executionAsyncId())
354-
t.assert.deepStrictEqual(store.traceId, '1111-2222-3333')
354+
t.assert.strictEqual(store.traceId, '1111-2222-3333')
355355
return Promise.resolve({ userId: req.requestContext.get('user').id })
356356
}
357357

@@ -362,7 +362,33 @@ test('passing a custom resource factory function when create as AsyncResource',
362362
const url = `${address}:${port}`
363363

364364
return request('GET', url).then((response1) => {
365-
t.assert.deepStrictEqual(response1.body.userId, 'system')
365+
t.assert.strictEqual(response1.body.userId, 'system')
366+
})
367+
})
368+
})
369+
370+
test('returns the store', (t) => {
371+
t.plan(2)
372+
373+
app = fastify({ logger: true })
374+
app.register(fastifyRequestContext, {
375+
defaultStoreValues: { foo: 42 },
376+
})
377+
378+
const route = (req) => {
379+
const store = req.requestContext.getStore()
380+
t.assert.strictEqual(store.foo, 42)
381+
return store.foo
382+
}
383+
384+
app.get('/', route)
385+
386+
return app.listen({ port: 0, host: '127.0.0.1' }).then(() => {
387+
const { address, port } = app.server.address()
388+
const url = `${address}:${port}`
389+
390+
return request('GET', url).then((response1) => {
391+
t.assert.strictEqual(response1.body, 42)
366392
})
367393
})
368394
})

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ declare namespace fastifyRequestContext {
2222
export interface RequestContext {
2323
get<K extends keyof RequestContextData>(key: K): RequestContextData[K] | undefined
2424
set<K extends keyof RequestContextData>(key: K, value: RequestContextData[K]): void
25+
getStore(): RequestContextData | undefined
2526
}
2627

2728
export type CreateAsyncResourceFactory<T extends AsyncResource = AsyncResource> = (

0 commit comments

Comments
 (0)