diff --git a/src/content/docs/en/guides/sessions.mdx b/src/content/docs/en/guides/sessions.mdx index d8c665dac9e36..9ce701377bd2f 100644 --- a/src/content/docs/en/guides/sessions.mdx +++ b/src/content/docs/en/guides/sessions.mdx @@ -57,21 +57,21 @@ When you need a different configuration (e.g. to connect to an external service) The following example takes advantage of [Unstorage compatibility](/en/reference/session-driver-reference/#unstorage-compatibility) to configure the Redis driver in its own entrypoint: -1. Install the [`unstorage` package](https://unstorage.unjs.io/guide): +1. Install the [`unstorage`](https://www.npmjs.com/package/unstorage) and [`ioredis`](https://www.npmjs.com/package/ioredis) packages: ```sh - npm install unstorage + npm install unstorage ioredis ``` ```sh - pnpm add unstorage + pnpm add unstorage ioredis ``` ```sh - yarn add unstorage + yarn add unstorage ioredis ``` @@ -83,29 +83,51 @@ The following example takes advantage of [Unstorage compatibility](/en/reference import { REDIS_HOST, REDIS_PORT } from "astro:env"; export default function (): SessionDriver { - return redisDriver({ + const driver = redisDriver({ host: REDIS_HOST, port: REDIS_PORT, }); + + return { + async getItem(key) { + return await driver.getItem(key); + }, + async setItem(key, value) { + await driver.setItem?.(key, value, {}); + }, + async removeItem(key) { + await driver.removeItem?.(key, {}); + }, + }; } ``` 3. Use this file as the driver's entrypoint in your Astro configuration: - ```js title="astro.config.mjs" {11-13} + ```js title="astro.config.mjs" {21-23} import { defineConfig, envField, sessionDrivers } from "astro/config"; import vercel from "@astrojs/vercel"; export default defineConfig({ adapter: vercel(), env: { - REDIS_HOST: envField.string({ context: "server", access: "public", default: "localhost" }), - REDIS_PORT: envField.number({ context: "server", access: "public", default: 6379 }), + schema: { + REDIS_HOST: envField.string({ + context: "server", + access: "public", + default: "localhost", + }), + REDIS_PORT: envField.number({ + context: "server", + access: "public", + default: 6379, + }), + }, }, session: { driver: { - entrypoint: new URL('./src/session-driver.ts', import.meta.url), - } - } + entrypoint: new URL("./src/session-driver.ts", import.meta.url), + }, + }, }); ``` diff --git a/src/content/docs/en/reference/session-driver-reference.mdx b/src/content/docs/en/reference/session-driver-reference.mdx index 591176d4e6c55..c5ac5cd79ccd6 100644 --- a/src/content/docs/en/reference/session-driver-reference.mdx +++ b/src/content/docs/en/reference/session-driver-reference.mdx @@ -159,33 +159,47 @@ Defines a function that removes session data by key. ## Unstorage compatibility -Unstorage driver types are compatible with Astro's `SessionDriver` type. +The [built-in drivers](#built-in-drivers) provide the same drivers as Unstorage. When you need greater customization, you can [build your own driver](#building-a-session-driver) based on any [Unstorage driver](https://unstorage.unjs.io/drivers). -That means you can use an unstorage package export as an [entrypoint](#entrypoint). For example: +Install the `unstorage` package and pass the driver specifier as the [entrypoint](#entrypoint): ```ts title="driver/config.ts" {5} -import type { SessionDriverConfig } from 'astro' +import type { SessionDriverConfig } from "astro"; export function configuredRedisDriver(): SessionDriverConfig { - return { - entrypoint: 'unstorage/drivers/redis', - config: { - tls: true - } - } + return { + entrypoint: "unstorage/drivers/redis", + config: { + tls: true, + }, + }; } ``` -Alternatively, you can import and use an unstorage driver directly in the implementation. For example: +You can also import an Unstorage driver and wrap it in your own implementation. This can be useful if you want to add extra logic to the driver or [override the configuration at runtime](/en/guides/sessions/#overriding-the-configuration-at-runtime). -```ts title="driver/runtime.ts" {2} -import type { SessionDriver } from 'astro' -import redisDriver from "unstorage/drivers/redis"; +The following example implements a Redis driver with a default `ttl` of 7 days: -export default function(config): SessionDriver { - return redisDriver({ - ...config, - tls: true - }) +```ts title="driver/runtime.ts" +import type { SessionDriver } from "astro"; +import redisDriver, { type RedisOptions } from "unstorage/drivers/redis"; + +export default function (config: RedisOptions): SessionDriver { + const driver = redisDriver({ + ...config, + ttl: config.ttl ?? 60 * 60 * 24 * 7, // default to 7 days + }); + + return { + async getItem(key) { + return await driver.getItem(key); + }, + async setItem(key, value) { + await driver.setItem?.(key, value, {}); + }, + async removeItem(key) { + await driver.removeItem?.(key, {}); + }, + }; } -``` \ No newline at end of file +```