11import { addImportsDir , addServerHandler , createResolver , defineNuxtModule , useLogger } from '@nuxt/kit'
22import { CreateStorageOptions } from 'unstorage'
33import { defu } from 'defu'
4-
5- export type SameSiteOptions = 'lax' | 'strict' | 'none'
6- export type SupportedSessionApiMethods = 'patch' | 'delete' | 'get' | 'post'
7-
8- declare interface SessionOptions {
9- /**
10- * Set the session duration in seconds. Once the session expires, a new one with a new id will be created. Set to `null` for infinite sessions
11- * @default 600
12- * @example 30
13- * @type number | null
14- */
15- expiryInSeconds : number | null
16- /**
17- * How many characters the random session id should be long
18- * @default 64
19- * @example 128
20- * @type number
21- */
22- idLength : number
23- /**
24- * What prefix to use to store session information via `unstorage`
25- * @default 64
26- * @example 128
27- * @type number
28- * @docs https://github.com/unjs/unstorage
29- */
30- storePrefix : string
31- /**
32- * When to attach session cookie to requests
33- * @default 'lax'
34- * @example 'strict'
35- * @type SameSiteOptions
36- * @docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
37- */
38- cookieSameSite : SameSiteOptions
39- /**
40- * Driver configuration for session-storage. Per default in-memory storage is used
41- * @default {}
42- * @example { driver: redisDriver({ base: 'storage:' }) }
43- * @type CreateStorageOptions
44- * @docs https://github.com/unjs/unstorage
45- */
46- storageOptions : CreateStorageOptions ,
47- /**
48- * Set the domain the session cookie will be receivable by. Setting `domain: null` results in setting the domain the cookie is initially set on. Specifying a domain will allow the domain and all its sub-domains.
49- * @default null
50- * @example '.example.com'
51- * @type string | null
52- * @docs https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#define_where_cookies_are_sent
53- */
54- domain : string | null
55- }
56-
57- declare interface ApiOptions {
58- /**
59- * Whether to enable the session API endpoints that allow read, update and delete operations from the client side. Use `/api/session` to access the endpoints.
60- * @default true
61- * @example false
62- * @type boolean
63- */
64- isEnabled : boolean
65- /**
66- * Configure which session API methods are enabled. All api methods are enabled by default. Restricting the enabled methods can be useful if you want to allow the client to read session-data but not modify it. Passing
67- * an empty array will result in all API methods being registered. Disable the api via the `api.isEnabled` option.
68- * @default []
69- * @example ['get']
70- * @type SupportedSessionApiMethods[]
71- */
72- methods : SupportedSessionApiMethods [ ]
73- /**
74- * Base path of the session api.
75- * @default /api/session
76- * @example /_session
77- * @type string
78- */
79- basePath : string
80- }
81-
82- export interface ModuleOptions {
83- /**
84- * Whether to enable the module
85- * @default true
86- * @example true
87- * @type boolean
88- */
89- isEnabled : boolean ,
90- /**
91- * Configure session-behvaior
92- * @type SessionOptions
93- */
94- session : Partial < SessionOptions >
95- /**
96- * Configure session-api and composable-behavior
97- * @type ApiOptions
98- */
99- api : Partial < ApiOptions >
100- }
4+ import type {
5+ FilledModuleOptions ,
6+ ModuleOptions ,
7+ ModulePublicRuntimeConfig ,
8+ SessionIpPinningOptions ,
9+ SupportedSessionApiMethods
10+ } from './types'
10111
10212const PACKAGE_NAME = 'nuxt-session'
10313
104- const defaults : ModuleOptions = {
14+ const defaults : FilledModuleOptions = {
10515 isEnabled : true ,
10616 session : {
10717 expiryInSeconds : 60 * 10 ,
10818 idLength : 64 ,
10919 storePrefix : 'sessions' ,
11020 cookieSameSite : 'lax' ,
21+ storageOptions : { } as CreateStorageOptions ,
11122 domain : null ,
112- storageOptions : { }
23+ ipPinning : false as boolean | SessionIpPinningOptions
11324 } ,
11425 api : {
11526 isEnabled : true ,
116- methods : [ ] ,
27+ methods : [ ] as SupportedSessionApiMethods [ ] ,
11728 basePath : '/api/session'
11829 }
119- }
30+ } as const
12031
12132export default defineNuxtModule < ModuleOptions > ( {
12233 meta : {
@@ -140,10 +51,13 @@ export default defineNuxtModule<ModuleOptions>({
14051 logger . info ( 'Setting up sessions...' )
14152
14253 // 2. Set public and private runtime configuration
143- const options = defu ( moduleOptions , defaults )
54+ const options : FilledModuleOptions = defu ( moduleOptions , defaults )
14455 options . api . methods = moduleOptions . api . methods . length > 0 ? moduleOptions . api . methods : [ 'patch' , 'delete' , 'get' , 'post' ]
145- nuxt . options . runtimeConfig . session = defu ( nuxt . options . runtimeConfig . session , options )
146- nuxt . options . runtimeConfig . public = defu ( nuxt . options . runtimeConfig . public , { session : { api : options . api } } )
56+ // @ts -ignore TODO: Fix this `nuxi prepare` bug (see https://github.com/nuxt/framework/issues/8728)
57+ nuxt . options . runtimeConfig . session = defu ( nuxt . options . runtimeConfig . session , options ) as FilledModuleOptions
58+
59+ const publicConfig : ModulePublicRuntimeConfig = { session : { api : options . api } }
60+ nuxt . options . runtimeConfig . public = defu ( nuxt . options . runtimeConfig . public , publicConfig )
14761
14862 // 3. Locate runtime directory and transpile module
14963 const { resolve } = createResolver ( import . meta. url )
@@ -174,3 +88,5 @@ export default defineNuxtModule<ModuleOptions>({
17488 logger . success ( 'Session setup complete' )
17589 }
17690} )
91+
92+ export * from './types'
0 commit comments