forked from sidebase/nuxt-session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.ts
102 lines (88 loc) · 3.21 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { addImportsDir, addServerHandler, createResolver, defineNuxtModule, useLogger } from '@nuxt/kit'
import { defu } from 'defu'
import { builtinDrivers } from 'unstorage'
import type {
FilledModuleOptions,
ModuleOptions,
ModulePublicRuntimeConfig,
SessionIpPinningOptions,
SupportedSessionApiMethods
} from './types'
const PACKAGE_NAME = 'nuxt-session'
const defaults: FilledModuleOptions = {
isEnabled: true,
session: {
expiryInSeconds: 60 * 10,
idLength: 64,
storePrefix: 'sessions',
cookieSameSite: 'lax',
storageOptions: {
driver: 'memory',
options: {}
},
domain: null,
ipPinning: false as boolean|SessionIpPinningOptions
},
api: {
isEnabled: true,
methods: [] as SupportedSessionApiMethods[],
basePath: '/api/session'
}
} as const
export default defineNuxtModule<ModuleOptions>({
meta: {
name: `@sidebase/${PACKAGE_NAME}`,
configKey: 'session',
compatibility: {
bridge: false
}
},
defaults,
hooks: {},
setup (moduleOptions, nuxt) {
const logger = useLogger(PACKAGE_NAME)
// 1. Check if module should be enabled at all
if (!moduleOptions.isEnabled) {
logger.info(`Skipping ${PACKAGE_NAME} setup, as module is disabled`)
return
}
logger.info('Setting up sessions...')
// 2. Set public and private runtime configuration
const options: FilledModuleOptions = defu(moduleOptions, defaults)
options.api.methods = moduleOptions.api.methods.length > 0 ? moduleOptions.api.methods : ['patch', 'delete', 'get', 'post']
// @ts-ignore TODO: Fix this `nuxi prepare` bug (see https://github.com/nuxt/framework/issues/8728)
nuxt.options.runtimeConfig.session = defu(nuxt.options.runtimeConfig.session, options) as FilledModuleOptions
const publicConfig: ModulePublicRuntimeConfig = { session: { api: options.api } }
nuxt.options.runtimeConfig.public = defu(nuxt.options.runtimeConfig.public, publicConfig)
// setup unstorage
nuxt.options.nitro.virtual = defu(nuxt.options.nitro.virtual, {
'#session-driver': `export { default } from '${
builtinDrivers[options.session.storageOptions.driver]
}'`
})
// 3. Locate runtime directory and transpile module
const { resolve } = createResolver(import.meta.url)
// 4. Setup middleware, use `.unshift` to ensure (reasonably well) that the session middleware is first
const handler = resolve('./runtime/server/middleware/session')
const serverHandler = {
middleware: true,
handler
}
nuxt.options.serverHandlers.unshift(serverHandler)
// 5. Register desired session API endpoints
if (options.api.isEnabled) {
for (const apiMethod of options.api.methods) {
const handler = resolve(`./runtime/server/api/session.${apiMethod}`)
addServerHandler({ handler, route: options.api.basePath })
}
logger.info(`Session API "${options.api.methods.join(', ')}" endpoints registered at "${options.api.basePath}"`)
} else {
logger.info('Session API disabled')
}
// 6. Add nuxt-session composables
const composables = resolve('./runtime/composables')
addImportsDir(composables)
logger.success('Session setup complete')
}
})
export * from './types'