-
-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contextStorage): added Context Storage Middleware (#3373)
- Loading branch information
1 parent
39600c4
commit fdf7786
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |