Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/content/docs/en/guides/sessions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,22 @@ 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, {});
},
};
}
```

Expand Down
33 changes: 22 additions & 11 deletions src/content/docs/en/reference/session-driver-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -159,33 +159,44 @@ Defines a function that removes session data by key.

## Unstorage compatibility

Unstorage driver types are compatible with Astro's `SessionDriver` type.
You can use an [unstorage](https://unstorage.unjs.io/) package export as a session driver [entrypoint](#entrypoint). For example:

That means you can use an unstorage package export as an [entrypoint](#entrypoint). For example:

```ts title="driver/config.ts" {5}
```ts title="driver/config.ts" {5-7}
import type { SessionDriverConfig } from 'astro'

export function configuredRedisDriver(): SessionDriverConfig {
return {
entrypoint: 'unstorage/drivers/redis',
config: {
tls: true
// Options are forwarded to the unstorage Redis driver (see its RedisOptions type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Because we can't have a link here, I think the parentheses is not useful:

Suggested change
// Options are forwarded to the unstorage Redis driver (see its RedisOptions type)
// Options are forwarded to the unstorage Redis driver

ttl: 60 * 60 * 24 * 7, // 7 days
}
}
}
```

Alternatively, you can import and use an unstorage driver directly in the implementation. For example:
Alternatively, you can import an unstorage driver in the implementation and adapt it to Astro's `SessionDriver` interface. For example:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The page doesn't explicitly says what is the SessionDriver type. So, I think we should reword this a bit and add a link to the section where we describe the expected methods. Something like:

Suggested change
Alternatively, you can import an unstorage driver in the implementation and adapt it to Astro's `SessionDriver` interface. For example:
Alternatively, you can import an unstorage driver and [implement a session driver](#the-session-driver-implementation) by wrapping the available methods. For example:


```ts title="driver/runtime.ts" {2}
```ts title="driver/runtime.ts"
import type { SessionDriver } from 'astro'
import redisDriver from "unstorage/drivers/redis";
import redisDriver, { type RedisOptions } from 'unstorage/drivers/redis'

export default function(config): SessionDriver {
return redisDriver({
export default function (config: RedisOptions): SessionDriver {
const driver = redisDriver({
...config,
tls: true
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, {})
},
}
}
```
Loading