Files
cypress/packages/server/lib/session.ts
Emily Rohrbough 797c8f8d77 chore: update session api types & exposed global helpers (#24980)
Co-authored-by: Matt Schile <mschile@cypress.io>
Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>
2022-12-06 08:19:39 -06:00

48 lines
998 B
TypeScript

import type { StoredSessions } from '@packages/types'
type State = {
globalSessions: StoredSessions
specSessions: StoredSessions
}
const state: State = {
globalSessions: {},
specSessions: {},
}
export function saveSession (data: Cypress.ServerSessionData): void {
if (!data.id) throw new Error('session data had no id')
if (data.cacheAcrossSpecs) {
state.globalSessions[data.id] = data
return
}
state.specSessions[data.id] = data
}
export function getActiveSessions (): StoredSessions {
return state.globalSessions
}
export function getSession (id: string): Cypress.ServerSessionData {
const session = state.globalSessions[id] || state.specSessions[id]
if (!session) throw new Error(`session with id "${id}" not found`)
return session
}
export function getState (): State {
return state
}
export function clearSessions (clearAllSessions: boolean = false): void {
state.specSessions = {}
if (clearAllSessions) {
state.globalSessions = {}
}
}