0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

feat(contextStorage): added Context Storage Middleware (#3373)

This commit is contained in:
Marcel Overdijk 2024-09-08 08:53:01 +02:00 committed by GitHub
parent 39600c4449
commit fdf77862ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 94 additions and 0 deletions

View File

@ -26,6 +26,7 @@
"./cookie": "./src/helper/cookie/index.ts",
"./accepts": "./src/helper/accepts/index.ts",
"./compress": "./src/middleware/compress/index.ts",
"./context-storage": "./src/middleware/context-storage/index.ts",
"./cors": "./src/middleware/cors/index.ts",
"./csrf": "./src/middleware/csrf/index.ts",
"./etag": "./src/middleware/etag/index.ts",

View File

@ -104,6 +104,11 @@
"import": "./dist/middleware/compress/index.js",
"require": "./dist/cjs/middleware/compress/index.js"
},
"./context-storage": {
"types": "./dist/types/middleware/context-storage/index.d.ts",
"import": "./dist/middleware/context-storage/index.js",
"require": "./dist/cjs/middleware/context-storage/index.js"
},
"./cors": {
"types": "./dist/types/middleware/cors/index.d.ts",
"import": "./dist/middleware/cors/index.js",
@ -421,6 +426,9 @@
"compress": [
"./dist/types/middleware/compress"
],
"context-storage": [
"./dist/types/middleware/context-storage"
],
"cors": [
"./dist/types/middleware/cors"
],

View File

@ -0,0 +1,30 @@
import { Hono } from '../../hono'
import { contextStorage, getContext } from '.'
describe('Context Storage Middleware', () => {
type Env = {
Variables: {
message: string
}
}
const app = new Hono<Env>()
app.use(contextStorage())
app.use(async (c, next) => {
c.set('message', 'Hono is cool!!')
await next()
})
app.get('/', (c) => {
return c.text(getMessage())
})
const getMessage = () => {
return getContext<Env>().var.message
}
it('Should get context', async () => {
const res = await app.request('/')
expect(await res.text()).toBe('Hono is cool!!')
})
})

View File

@ -0,0 +1,55 @@
/**
* @module
* Context Storage Middleware for Hono.
*/
import type { Context } from '../../context'
import type { Env, MiddlewareHandler } from '../../types'
import { AsyncLocalStorage } from 'node:async_hooks'
const asyncLocalStorage = new AsyncLocalStorage<Context>()
/**
* Context Storage Middleware for Hono.
*
* @see {@link https://hono.dev/docs/middleware/builtin/context-storage}
*
* @returns {MiddlewareHandler} The middleware handler function.
*
* @example
* ```ts
* type Env = {
* Variables: {
* message: string
* }
* }
*
* const app = new Hono<Env>()
*
* app.use(contextStorage())
*
* app.use(async (c, next) => {
* c.set('message', 'Hono is cool!!)
* await next()
* })
*
* app.get('/', async (c) => { c.text(getMessage()) })
*
* const getMessage = () => {
* return getContext<Env>().var.message
* }
* ```
*/
export const contextStorage = (): MiddlewareHandler => {
return async function contextStorage(c, next) {
await asyncLocalStorage.run(c, next)
}
}
export const getContext = <E extends Env = Env>(): Context<E> => {
const context = asyncLocalStorage.getStore()
if (!context) {
throw new Error('Context is not available')
}
return context
}