Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit d842c5d

Browse files
authored
release: 0.2.0 (#3)
* release: bump to 0.2.0
1 parent b94f13a commit d842c5d

File tree

11 files changed

+30
-32
lines changed

11 files changed

+30
-32
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"homepage": "https://github.com/sidebase/nuxt-session",
77
"name": "@sidebase/nuxt-session",
8-
"version": "0.1.2",
8+
"version": "0.2.0",
99
"license": "MIT",
1010
"type": "module",
1111
"exports": {

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export default defineNuxtModule<ModuleOptions>({
159159
// 5. Register desired session API endpoints
160160
if (moduleOptions.api.isEnabled) {
161161
for (const apiMethod of moduleOptions.api.methods) {
162-
const handler = resolve(runtimeDir, `server/api/session.${apiMethod}.ts`)
162+
const handler = resolve(runtimeDir, `server/api/session.${apiMethod}`)
163163
addServerHandler({ handler, route: moduleOptions.api.basePath })
164164
}
165165
logger.info(`Session API "${moduleOptions.api.methods.join(', ')}" endpoints registered at "${moduleOptions.api.basePath}"`)

src/runtime/composables/useNuxtSession.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { nanoid } from 'nanoid'
33
import { Ref, ref } from 'vue'
44
import type { SupportedSessionApiMethods } from '../../module'
55
import type { Session } from '../server/middleware/session'
6-
import useConfig from '../config'
6+
import { useRuntimeConfig } from '#imports'
77

88
type SessionData = Record<string, any>
99

@@ -16,11 +16,12 @@ export default async (options: ComposableOptions = {
1616
}) => {
1717
/**
1818
* The currently active session associated with the current client
19+
* @type Ref<Session | null>
1920
*/
2021
const session: Ref<Session | null> = ref(null)
2122

2223
const _performSessionRequest = (method: SupportedSessionApiMethods, body?: SessionData) => {
23-
const config = useConfig()
24+
const config = useRuntimeConfig().public.session
2425
if (!config.api.isEnabled || !config.api.methods.includes(method)) {
2526
const message = `Cannot "${method}" session data as endpoint is not enabled. If you want to be able to "${method}" session data, you can configure this via the "session.api.isEnabled: boolean" and "session.api.methods: ('post' | 'get' | ...)[]" module configuration options.`
2627
throw createError({ message, statusCode: 500 })

src/runtime/config.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/runtime/server/api/session.delete.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineEventHandler } from 'h3'
1+
import { eventHandler } from 'h3'
22
import { deleteSession } from '../middleware/session'
33

4-
export default defineEventHandler(async (event) => {
4+
export default eventHandler(async (event) => {
55
await deleteSession(event)
66

77
return null
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { defineEventHandler } from 'h3'
1+
import { eventHandler } from 'h3'
22

3-
export default defineEventHandler(event => event.context.session)
3+
export default eventHandler(event => event.context.session)

src/runtime/server/api/session.patch.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { defineEventHandler, createError, readBody } from 'h3'
2-
import { checkIfObjectAndContainsIllegalKeys } from '../utils'
1+
import { eventHandler, createError, readBody } from 'h3'
32

4-
export default defineEventHandler(async (event) => {
3+
export const checkIfObjectAndContainsIllegalKeys = (shape: unknown): shape is Object => {
4+
if (typeof shape !== 'object' || !shape) {
5+
return false
6+
}
7+
8+
// see https://stackoverflow.com/a/39283005 for this usage
9+
return Object.prototype.hasOwnProperty.call(shape, 'id') || Object.prototype.hasOwnProperty.call(shape, 'createdAt')
10+
}
11+
12+
export default eventHandler(async (event) => {
513
const body = await readBody(event)
614
if (checkIfObjectAndContainsIllegalKeys(body)) {
715
throw createError({ statusCode: 400, message: 'Trying to pass invalid data to session, likely an object with `id` or `createdAt` fields or a non-object' })

src/runtime/server/api/session.post.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineEventHandler, readBody } from 'h3'
2-
import { checkIfObjectAndContainsIllegalKeys } from '../utils'
1+
import { eventHandler, readBody } from 'h3'
2+
import { checkIfObjectAndContainsIllegalKeys } from './session.patch'
33

4-
export default defineEventHandler(async (event) => {
4+
export default eventHandler(async (event) => {
55
const body = await readBody(event)
66
if (checkIfObjectAndContainsIllegalKeys(body)) {
77
throw createError({ statusCode: 400, message: 'Trying to pass invalid data to session, likely an object with `id` or `createdAt` fields or a non-object' })

src/runtime/server/middleware/session/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import { H3Event, defineEventHandler, setCookie, parseCookies, deleteCookie } fr
22
import { nanoid } from 'nanoid'
33
import dayjs from 'dayjs'
44
import type { SameSiteOptions } from '../../../../module'
5-
import useConfig from '../../../config'
65
import { dropStorageSession, getStorageSession, setStorageSession } from './storage'
6+
import { useRuntimeConfig } from '#imports'
77

88
const SESSION_COOKIE_NAME = 'sessionId'
99
const safeSetCookie = (event: H3Event, name: string, value: string) => setCookie(event, name, value, {
1010
// Max age of cookie in seconds
11-
maxAge: useConfig().session.expiryInSeconds,
11+
maxAge: useRuntimeConfig().session.session.expiryInSeconds,
1212
// Only send cookie via HTTPs to mitigate man-in-the-middle attacks
1313
secure: true,
1414
// Only send cookie via HTTP requests, do not allow access of cookie from JS to mitigate XSS attacks
1515
httpOnly: true,
1616
// Do not send cookies on many cross-site requests to mitigates CSRF and cross-site attacks, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite#lax
17-
sameSite: useConfig().session.cookieSameSite as SameSiteOptions
17+
sameSite: useRuntimeConfig().session.session.cookieSameSite as SameSiteOptions
1818
})
1919

2020
export declare interface Session {
@@ -58,7 +58,7 @@ const newSession = async (event: H3Event) => {
5858
await deleteSession(event)
5959

6060
// (Re-)Set cookie
61-
const sessionId = nanoid(useConfig().session.idLength)
61+
const sessionId = nanoid(useRuntimeConfig().session.session.idLength)
6262
safeSetCookie(event, SESSION_COOKIE_NAME, sessionId)
6363

6464
// Store session data in storage
@@ -82,7 +82,7 @@ const getSession = async (event: H3Event): Promise<null | Session> => {
8282
}
8383

8484
// 3. Is the session not expired?
85-
const sessionExpiryInSeconds = useConfig().session.expiryInSeconds
85+
const sessionExpiryInSeconds = useRuntimeConfig().session.session.expiryInSeconds
8686
if (sessionExpiryInSeconds !== null) {
8787
const now = dayjs()
8888
if (now.diff(dayjs(session.createdAt), 'seconds') > sessionExpiryInSeconds) {

src/runtime/server/middleware/session/storage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createStorage, prefixStorage, StorageValue } from 'unstorage'
2-
import useConfig from '../../../config'
2+
import { useRuntimeConfig } from '#imports'
33

4-
const storage = prefixStorage(createStorage(useConfig().session.storageOptions), useConfig().session.storePrefix)
4+
const storage = prefixStorage(createStorage(useRuntimeConfig().session.session.storageOptions), useRuntimeConfig().session.session.storePrefix)
55

66
export const getStorageSession = (sessionId: string) => storage.getItem(sessionId)
77
export const setStorageSession = (sessionId: string, session: StorageValue) => storage.setItem(sessionId, session)

0 commit comments

Comments
 (0)