UNPKG

@kikiutils/nitro-session

Version:
1 lines 4.24 kB
{"version":3,"file":"utils.mjs","sources":["../../src/runtime/utils.ts"],"sourcesContent":["import type { H3Event } from 'h3';\nimport onChange from 'on-change';\n\nimport { cachedHandlers } from '../constants';\nimport type { PartialH3EventContextSession } from '../types/session';\nimport { setupH3EventContextSession } from '../utils';\n\n/**\n * Deletes the session storage data associated with the given token.\n *\n * @param {string} token - The token associated with the session data to be deleted.\n * @returns {Promise<void>} - A promise that resolves when the session data is deleted.\n *\n * @example\n * ```typescript\n * // Example usage to delete session storage data\n * await deleteH3EventContextSessionStorageData('your-session-token');\n * ```\n */\nexport const deleteH3EventContextSessionStorageData = async (token: string) => await cachedHandlers.data?.delete(token);\n\n/**\n * Retrieves the session token from the H3 request event.\n *\n * @param {H3Event} event - The H3 request event object.\n * @returns {string | undefined} - The session token, or undefined if not present.\n *\n * @example\n * ```typescript\n * // Example usage to get the session token in an event handler\n * export default defineEventHandler((event) => {\n * const sessionToken = getH3EventContextSessionToken(event);\n * console.log('Session Token:', sessionToken);\n * // Remaining operations...\n * });\n * ```\n */\nexport const getH3EventContextSessionToken = (event: H3Event) => event.context._nitroSessionUnstorageKey;\n\n/**\n * Clears the session data in the H3 request event context.\n *\n * Executing this function will not immediately update the session data in storage.\n * The update will occur before the response ends. To clear the data immediately,\n * please use `deleteH3EventContextSessionStorageData`.\n *\n * @param {H3Event} event - The H3 request event object.\n *\n * @example\n * ```typescript\n * // Example usage in an event handler to clear the session\n * export default defineEventHandler((event) => {\n * clearH3EventContextSession(event);\n * // Remaining operations...\n * });\n * ```\n */\nexport function clearH3EventContextSession(event: H3Event) {\n onChange.unsubscribe(event.context.session);\n event.context._nitroSessionChanged = event.context._nitroSessionCleared = true;\n setupH3EventContextSession(event, {}, (event) => delete event.context._nitroSessionCleared);\n}\n\n/**\n * Removes and returns a value from the session context in the H3 request event.\n *\n * This function retrieves the value associated with the specified key from the session context,\n * deletes the key-value pair from the session, and returns the value.\n *\n * @param {H3Event} event - The H3 request event object.\n * @param {K} key - The key of the session context property to be removed and returned.\n * @returns {PartialH3EventContextSession[K]}\n * The value associated with the specified key, or undefined if the key does not exist.\n *\n * @example\n * ```typescript\n * // Example usage to pop a value from the session context\n * export default defineEventHandler((event) => {\n * const userId = popH3EventContextSession(event, 'userId');\n * console.log('Popped User ID:', userId);\n * // Remaining operations...\n * });\n * ```\n */\nexport function popH3EventContextSession<K extends keyof PartialH3EventContextSession>(event: H3Event, key: K) {\n const value = event.context.session[key];\n delete event.context.session[key];\n return value;\n}\n"],"names":[],"mappings":";;;;AAOA;;;;;;;;;;;AAWG;AACU,MAAA,sCAAsC,GAAG,OAAO,KAAa,KAAK,MAAM,cAAc,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK;AAEtH;;;;;;;;;;;;;;;AAeG;AACI,MAAM,6BAA6B,GAAG,CAAC,KAAc,KAAK,KAAK,CAAC,OAAO,CAAC;AAE/E;;;;;;;;;;;;;;;;;AAiBG;AACG,SAAU,0BAA0B,CAAC,KAAc,EAAA;IACrD,QAAQ,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;AAC3C,IAAA,KAAK,CAAC,OAAO,CAAC,oBAAoB,GAAG,KAAK,CAAC,OAAO,CAAC,oBAAoB,GAAG,IAAI;AAC9E,IAAA,0BAA0B,CAAC,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,KAAK,OAAO,KAAK,CAAC,OAAO,CAAC,oBAAoB,CAAC;AAC/F;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACa,SAAA,wBAAwB,CAA+C,KAAc,EAAE,GAAM,EAAA;IACzG,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC;IACxC,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC;AACjC,IAAA,OAAO,KAAK;AAChB;;;;"}