From f86f242fcd18794e661e53835873f4c2bf8c71d8 Mon Sep 17 00:00:00 2001 From: Marcel Overdijk Date: Wed, 11 Sep 2024 07:32:51 +0200 Subject: [PATCH] Added docs for Context Storage Middleware (#463) --- .vitepress/config.ts | 1 + docs/index.md | 1 + docs/middleware/builtin/context-storage.md | 30 ++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 docs/middleware/builtin/context-storage.md diff --git a/.vitepress/config.ts b/.vitepress/config.ts index d6720bc9..d8154878 100644 --- a/.vitepress/config.ts +++ b/.vitepress/config.ts @@ -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', diff --git a/docs/index.md b/docs/index.md index ee8366bc..0bdafe32 100644 --- a/docs/index.md +++ b/docs/index.md @@ -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) diff --git a/docs/middleware/builtin/context-storage.md b/docs/middleware/builtin/context-storage.md new file mode 100644 index 00000000..4645bc8c --- /dev/null +++ b/docs/middleware/builtin/context-storage.md @@ -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() + +app.use(contextStorage()) + +app.get('/', (c) => c.text(getMessage()) + +const getMessage = () => { + return getContext().var.message +} +```