Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable specifying interface for context data #48

Merged
merged 5 commits into from May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -87,6 +87,23 @@ app.listen(3000, (err, address) => {
return app.ready()
```

In TypeScript you can augment the module to type your context:

```ts
import {requestContext} from 'fastify-request-context'

declare module 'fastify-request-context' {
interface RequestContextData {
foo: string
}
}

// Type is string
const foo = requestContext.get('foo')
// Type for unspecified keys is any
const bar = requestContext.get('bar')
```

[npm-image]: https://img.shields.io/npm/v/fastify-request-context.svg
[npm-url]: https://npmjs.org/package/fastify-request-context
[downloads-image]: https://img.shields.io/npm/dm/fastify-request-context.svg
Expand Down
16 changes: 10 additions & 6 deletions index.d.ts
@@ -1,8 +1,12 @@
import { FastifyPlugin, FastifyRequest } from 'fastify'
import { FastifyPlugin } from 'fastify'

export type RequestContext = {
get: <T>(key: string) => T | undefined
set: <T>(key: string, value: T) => void
export interface RequestContextData {
[key: string]: any
}

export interface RequestContext {
get<K extends keyof RequestContextData>(key: K): RequestContextData[K] | undefined
set<K extends keyof RequestContextData>(key: K, value: RequestContextData[K]): void
}

export type Hook =
Expand All @@ -20,8 +24,8 @@ export type Hook =
| 'onReady'
| 'onClose'

export type RequestContextOptions = {
defaultStoreValues?: Record<string, any>
export interface RequestContextOptions {
defaultStoreValues?: RequestContextData
hook?: Hook
}

Expand Down
16 changes: 15 additions & 1 deletion index.test-d.ts
@@ -1,4 +1,9 @@
import { requestContext, fastifyRequestContextPlugin, RequestContextOptions, RequestContext } from './index'
import {
requestContext,
fastifyRequestContextPlugin,
RequestContextOptions,
RequestContext,
} from './index'
import { expectAssignable, expectType } from 'tsd'
import { FastifyInstance, RouteHandlerMethod } from 'fastify'

Expand All @@ -24,3 +29,12 @@ expectType<RequestContext>(requestContext)
const getHandler: RouteHandlerMethod = function (request, _reply) {
expectType<RequestContext>(request.requestContext)
}

declare module './index' {
interface RequestContextData {
foo?: string
}
}

expectType<string | undefined>(requestContext.get('foo'))
expectType<any>(requestContext.get('bar'))
6 changes: 3 additions & 3 deletions test/requestContextPlugin.spec.js
Expand Up @@ -21,7 +21,7 @@ describe('requestContextPlugin', () => {
const promiseRequest1 = new Promise((resolveRequest1Promise) => {
const route = (req, reply) => {
function prepareReply() {
testService.processRequest(requestId).then(() => {
return testService.processRequest(requestId).then(() => {
const storedValue = req.requestContext.get('testKey')
reply.status(204).send({
storedValue,
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('requestContextPlugin', () => {
const promiseRequest1 = new Promise((resolveRequest1Promise) => {
const route = (req, reply) => {
function prepareReply() {
testService.processRequest(requestId).then(() => {
return testService.processRequest(requestId).then(() => {
const storedValue = req.requestContext.get('testKey')
reply.status(204).send({
storedValue,
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('requestContextPlugin', () => {
const requestId = req.requestContext.get('testKey')

function prepareReply() {
testService.processRequest(requestId.replace('testValue', '')).then(() => {
return testService.processRequest(requestId.replace('testValue', '')).then(() => {
const storedValue = req.requestContext.get('testKey')
reply.status(204).send({
storedValue,
Expand Down