Skip to content

Commit

Permalink
Added docs for Context Storage Middleware (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloverdijk authored Sep 11, 2024
1 parent d223aca commit f86f242
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const sidebars = (): DefaultTheme.SidebarItem[] => [
{ text: 'Cache', link: '/docs/middleware/builtin/cache' },
{ text: 'Combine', link: '/docs/middleware/builtin/combine' },
{ text: 'Compress', link: '/docs/middleware/builtin/compress' },
{ text: 'Context Storage', link: '/docs/middleware/builtin/context-storage' },
{ text: 'CORS', link: '/docs/middleware/builtin/cors' },
{
text: 'CSRF Protection',
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Out of the box, Hono provides middleware and helpers for:
- [Body Limit](/docs/middleware/builtin/body-limit)
- [Cache](/docs/middleware/builtin/cache)
- [Compress](/docs/middleware/builtin/compress)
- [Context Storage](/docs/middleware/builtin/context-storage)
- [Cookie](/docs/helpers/cookie)
- [CORS](/docs/middleware/builtin/cors)
- [ETag](/docs/middleware/builtin/etag)
Expand Down
30 changes: 30 additions & 0 deletions docs/middleware/builtin/context-storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Context Storage Middleware

The Context Storage Middleware stores the Hono `Context` in the `AsyncLocalStorage`, to make it globally accessible.

## Import

```ts
import { Hono } from 'hono'
import { contextStorage, getContext } from 'hono/context-storage'
```

## Usage

```ts
type Env = {
Variables: {
message: string
}
}

const app = new Hono<Env>()

app.use(contextStorage())

app.get('/', (c) => c.text(getMessage())

const getMessage = () => {
return getContext<Env>().var.message
}
```

0 comments on commit f86f242

Please sign in to comment.