11import { AsyncLocalStorage } from 'node:async_hooks' ;
22import { randomUUID } from 'node:crypto' ;
3- import { type Session , type GlobalOptions } from './options' ;
4- import { DEFAULT_OPTIONS , LOG_BASENAME , type LoggingSession , type DefaultOptions } from './options.defaults' ;
3+ import { type AppSession , type GlobalOptions , type DefaultOptionsOverrides } from './options' ;
4+ import { DEFAULT_OPTIONS , LOG_BASENAME , type LoggingSession } from './options.defaults' ;
55import { mergeObjects , freezeObject , isPlainObject } from './server.helpers' ;
66
77/**
@@ -10,14 +10,14 @@ import { mergeObjects, freezeObject, isPlainObject } from './server.helpers';
1010 * The `sessionContext` allows sharing a common context without explicitly
1111 * passing it as a parameter.
1212 */
13- const sessionContext = new AsyncLocalStorage < Session > ( ) ;
13+ const sessionContext = new AsyncLocalStorage < AppSession > ( ) ;
1414
1515/**
1616 * Initialize and return session data.
1717 *
18- * @returns {Session } Immutable session with a session ID and channel name.
18+ * @returns {AppSession } Immutable session with a session ID and channel name.
1919 */
20- const initializeSession = ( ) : Session => {
20+ const initializeSession = ( ) : AppSession => {
2121 const sessionId = ( process . env . NODE_ENV === 'local' && '1234d567-1ce9-123d-1413-a1234e56c789' ) || randomUUID ( ) ;
2222 const channelName = `${ LOG_BASENAME } :${ sessionId } ` ;
2323
@@ -27,10 +27,10 @@ const initializeSession = (): Session => {
2727/**
2828 * Set and return the current session options.
2929 *
30- * @param {Session } [session]
31- * @returns {Session }
30+ * @param {AppSession } [session]
31+ * @returns {AppSession }
3232 */
33- const setSessionOptions = ( session : Session = initializeSession ( ) ) => {
33+ const setSessionOptions = ( session : AppSession = initializeSession ( ) ) => {
3434 sessionContext . enterWith ( session ) ;
3535
3636 return session ;
@@ -39,10 +39,10 @@ const setSessionOptions = (session: Session = initializeSession()) => {
3939/**
4040 * Get the current session options or set a new session with defaults.
4141 */
42- const getSessionOptions = ( ) : Session => sessionContext . getStore ( ) || setSessionOptions ( ) ;
42+ const getSessionOptions = ( ) : AppSession => sessionContext . getStore ( ) || setSessionOptions ( ) ;
4343
4444const runWithSession = async < TReturn > (
45- session : Session ,
45+ session : AppSession ,
4646 callback : ( ) => TReturn | Promise < TReturn >
4747) => {
4848 const frozen = freezeObject ( structuredClone ( session ) ) ;
@@ -61,10 +61,10 @@ const optionsContext = new AsyncLocalStorage<GlobalOptions>();
6161/**
6262 * Set and freeze cloned options in the current async context.
6363 *
64- * @param {Partial<DefaultOptions> } [options] - Optional options to set in context. Merged with DEFAULT_OPTIONS.
64+ * @param {DefaultOptionsOverrides } [options] - Optional overrides merged with DEFAULT_OPTIONS.
6565 * @returns {GlobalOptions } Cloned frozen default options object with session.
6666 */
67- const setOptions = ( options ?: Partial < DefaultOptions > ) : GlobalOptions => {
67+ const setOptions = ( options ?: DefaultOptionsOverrides ) : GlobalOptions => {
6868 const base = mergeObjects ( DEFAULT_OPTIONS , options , { allowNullValues : false , allowUndefinedValues : false } ) ;
6969 const baseLogging = isPlainObject ( base . logging ) ? base . logging : DEFAULT_OPTIONS . logging ;
7070 const merged : GlobalOptions = {
@@ -102,7 +102,7 @@ const getOptions = (): GlobalOptions => optionsContext.getStore() || setOptions(
102102/**
103103 * Get logging options from the current context.
104104 *
105- * @param {Session } [session] - Session options to use in context.
105+ * @param {AppSession } [session] - Session options to use in context.
106106 * @returns {LoggingSession } Logging options from context.
107107 */
108108const getLoggerOptions = ( session = getSessionOptions ( ) ) : LoggingSession => {
0 commit comments