@adonisjs/session 7.7.0 → 8.0.0-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cookie-aLBno-zS.js +31 -0
- package/build/debug-Ba-0Cgn9.js +3 -0
- package/build/dynamodb-CU8BrQfU.js +72 -0
- package/build/factories/main.d.ts +1 -1
- package/build/factories/main.js +25 -49
- package/build/factories/session_middleware_factory.d.ts +2 -2
- package/build/file-CNxCs957.js +71 -0
- package/build/index.d.ts +6 -7
- package/build/index.js +5 -33
- package/build/providers/session_provider.d.ts +2 -3
- package/build/providers/session_provider.js +27 -64
- package/build/redis-Bcjum7z7.js +36 -0
- package/build/session-CBqhcnvJ.js +209 -0
- package/build/session-Cc1LPXRc.js +107 -0
- package/build/session_middleware-CS0R7hmq.js +27 -0
- package/build/src/client.d.ts +46 -10
- package/build/src/client.js +38 -9
- package/build/src/debug.d.ts +1 -1
- package/build/src/define_config.d.ts +49 -10
- package/build/src/errors.d.ts +20 -9
- package/build/src/plugins/edge.d.ts +16 -2
- package/build/src/plugins/edge.js +74 -126
- package/build/src/plugins/japa/api_client.d.ts +22 -4
- package/build/src/plugins/japa/api_client.js +76 -97
- package/build/src/plugins/japa/browser_client.d.ts +18 -4
- package/build/src/plugins/japa/browser_client.js +58 -82
- package/build/src/session.d.ts +170 -65
- package/build/src/session_middleware.d.ts +28 -5
- package/build/src/session_middleware.js +5 -11
- package/build/src/stores/cookie.d.ts +41 -7
- package/build/src/stores/dynamodb.d.ts +44 -8
- package/build/src/stores/file.d.ts +34 -8
- package/build/src/stores/memory.d.ts +32 -15
- package/build/src/stores/redis.d.ts +39 -16
- package/build/src/types.d.ts +151 -82
- package/build/src/types.js +1 -1
- package/build/src/values_store.d.ts +113 -21
- package/build/values_store-smX0sQBJ.js +78 -0
- package/package.json +57 -56
- package/build/chunk-5ECC6OWF.js +0 -8
- package/build/chunk-5ECC6OWF.js.map +0 -1
- package/build/chunk-6BSSM3HO.js +0 -44
- package/build/chunk-6BSSM3HO.js.map +0 -1
- package/build/chunk-G7KFHA57.js +0 -59
- package/build/chunk-G7KFHA57.js.map +0 -1
- package/build/chunk-GCO66PQD.js +0 -85
- package/build/chunk-GCO66PQD.js.map +0 -1
- package/build/chunk-PZ5AY32C.js +0 -10
- package/build/chunk-PZ5AY32C.js.map +0 -1
- package/build/chunk-Q24L5XPC.js +0 -186
- package/build/chunk-Q24L5XPC.js.map +0 -1
- package/build/chunk-TE5JP3SX.js +0 -151
- package/build/chunk-TE5JP3SX.js.map +0 -1
- package/build/chunk-V3OAEXMJ.js +0 -8
- package/build/chunk-V3OAEXMJ.js.map +0 -1
- package/build/chunk-XP3CBOXR.js +0 -414
- package/build/chunk-XP3CBOXR.js.map +0 -1
- package/build/commands/commands.json +0 -1
- package/build/commands/main.d.ts +0 -4
- package/build/commands/main.js +0 -36
- package/build/commands/make_session_table.d.ts +0 -9
- package/build/commands/make_session_table.js +0 -21
- package/build/commands/make_session_table.js.map +0 -1
- package/build/cookie-3C33DMWA.js +0 -57
- package/build/cookie-3C33DMWA.js.map +0 -1
- package/build/database-443FB7MM.js +0 -118
- package/build/database-443FB7MM.js.map +0 -1
- package/build/dynamodb-53YXEFIJ.js +0 -108
- package/build/dynamodb-53YXEFIJ.js.map +0 -1
- package/build/factories/main.js.map +0 -1
- package/build/file-K3GBSVSU.js +0 -117
- package/build/file-K3GBSVSU.js.map +0 -1
- package/build/index.js.map +0 -1
- package/build/make/migration/sessions.stub +0 -26
- package/build/providers/session_provider.js.map +0 -1
- package/build/redis-YGX2CNE2.js +0 -119
- package/build/redis-YGX2CNE2.js.map +0 -1
- package/build/src/client.js.map +0 -1
- package/build/src/plugins/edge.js.map +0 -1
- package/build/src/plugins/japa/api_client.js.map +0 -1
- package/build/src/plugins/japa/browser_client.js.map +0 -1
- package/build/src/session_collection.d.ts +0 -55
- package/build/src/session_middleware.js.map +0 -1
- package/build/src/stores/database.d.ts +0 -55
- package/build/src/types.js.map +0 -1
|
@@ -6,35 +6,52 @@
|
|
|
6
6
|
* For the full copyright and license information, please view the LICENSE
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
|
-
import type { SessionData,
|
|
9
|
+
import type { SessionData, SessionStoreContract } from '../types.ts';
|
|
10
10
|
/**
|
|
11
11
|
* Memory store is meant to be used for writing tests.
|
|
12
|
+
* All session data is stored in memory and will be lost when the process restarts.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const memoryStore = new MemoryStore()
|
|
16
|
+
* memoryStore.write('sess_abc123', { userId: 123 })
|
|
12
17
|
*/
|
|
13
|
-
export declare class MemoryStore implements
|
|
14
|
-
static sessions: Map<string, SessionData>;
|
|
18
|
+
export declare class MemoryStore implements SessionStoreContract {
|
|
15
19
|
/**
|
|
16
|
-
*
|
|
20
|
+
* Static map to store all session data in memory
|
|
17
21
|
*/
|
|
18
|
-
static
|
|
22
|
+
static sessions: Map<string, SessionData>;
|
|
19
23
|
/**
|
|
20
|
-
*
|
|
24
|
+
* Reads session value from memory
|
|
25
|
+
*
|
|
26
|
+
* @param sessionId - Session identifier
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* const data = store.read('sess_abc123')
|
|
21
30
|
*/
|
|
22
31
|
read(sessionId: string): SessionData | null;
|
|
23
32
|
/**
|
|
24
|
-
*
|
|
33
|
+
* Saves session value in memory for a given session id
|
|
34
|
+
*
|
|
35
|
+
* @param sessionId - Session identifier
|
|
36
|
+
* @param values - Session data to store
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* store.write('sess_abc123', { userId: 123, theme: 'dark' })
|
|
25
40
|
*/
|
|
26
41
|
write(sessionId: string, values: SessionData): void;
|
|
27
42
|
/**
|
|
28
|
-
*
|
|
43
|
+
* Removes a single session from memory
|
|
44
|
+
*
|
|
45
|
+
* @param sessionId - Session identifier to remove
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* store.destroy('sess_abc123')
|
|
29
49
|
*/
|
|
30
50
|
destroy(sessionId: string): void;
|
|
31
|
-
touch(): void;
|
|
32
|
-
/**
|
|
33
|
-
* Tag a session with a user ID
|
|
34
|
-
*/
|
|
35
|
-
tag(sessionId: string, userId: string): Promise<void>;
|
|
36
51
|
/**
|
|
37
|
-
*
|
|
52
|
+
* No-op for memory store as there's no expiry mechanism
|
|
53
|
+
*
|
|
54
|
+
* @param sessionId - Session identifier (unused)
|
|
38
55
|
*/
|
|
39
|
-
|
|
56
|
+
touch(_?: string): void;
|
|
40
57
|
}
|
|
@@ -7,35 +7,58 @@
|
|
|
7
7
|
* file that was distributed with this source code.
|
|
8
8
|
*/
|
|
9
9
|
import type { Connection } from '@adonisjs/redis/types';
|
|
10
|
-
import type {
|
|
10
|
+
import type { SessionStoreContract, SessionData } from '../types.ts';
|
|
11
11
|
/**
|
|
12
|
-
* Redis store to read/write session to Redis
|
|
12
|
+
* Redis store to read/write session data to Redis server.
|
|
13
|
+
* Provides fast, scalable session storage with automatic expiry.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const redisStore = new RedisStore(redisConnection, '2 hours')
|
|
13
17
|
*/
|
|
14
|
-
export declare class RedisStore implements
|
|
18
|
+
export declare class RedisStore implements SessionStoreContract {
|
|
15
19
|
#private;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new Redis store instance
|
|
22
|
+
*
|
|
23
|
+
* @param connection - Redis connection instance
|
|
24
|
+
* @param age - Session age in seconds or time expression (e.g. '2 hours')
|
|
25
|
+
*/
|
|
16
26
|
constructor(connection: Connection, age: string | number);
|
|
17
27
|
/**
|
|
18
|
-
*
|
|
28
|
+
* Reads session data from Redis
|
|
29
|
+
*
|
|
30
|
+
* @param sessionId - Session identifier
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const data = await store.read('sess_abc123')
|
|
19
34
|
*/
|
|
20
35
|
read(sessionId: string): Promise<SessionData | null>;
|
|
21
36
|
/**
|
|
22
|
-
*
|
|
37
|
+
* Writes session values to Redis with expiry
|
|
38
|
+
*
|
|
39
|
+
* @param sessionId - Session identifier
|
|
40
|
+
* @param values - Session data to store
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* await store.write('sess_abc123', { userId: 123 })
|
|
23
44
|
*/
|
|
24
|
-
write(sessionId: string, values:
|
|
45
|
+
write(sessionId: string, values: Object): Promise<void>;
|
|
25
46
|
/**
|
|
26
|
-
*
|
|
47
|
+
* Removes session data from Redis
|
|
48
|
+
*
|
|
49
|
+
* @param sessionId - Session identifier to remove
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* await store.destroy('sess_abc123')
|
|
27
53
|
*/
|
|
28
54
|
destroy(sessionId: string): Promise<void>;
|
|
29
55
|
/**
|
|
30
|
-
* Updates the
|
|
56
|
+
* Updates the session expiry time in Redis
|
|
57
|
+
*
|
|
58
|
+
* @param sessionId - Session identifier
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* await store.touch('sess_abc123')
|
|
31
62
|
*/
|
|
32
63
|
touch(sessionId: string): Promise<void>;
|
|
33
|
-
/**
|
|
34
|
-
* Tag a session with a user ID
|
|
35
|
-
*/
|
|
36
|
-
tag(sessionId: string, userId: string): Promise<void>;
|
|
37
|
-
/**
|
|
38
|
-
* Get all sessions for a given user ID (tag)
|
|
39
|
-
*/
|
|
40
|
-
tagged(userId: string): Promise<TaggedSession[]>;
|
|
41
64
|
}
|
package/build/src/types.d.ts
CHANGED
|
@@ -1,143 +1,212 @@
|
|
|
1
|
-
import { HttpContext } from '@adonisjs/core/http';
|
|
2
|
-
import { RedisConnections } from '@adonisjs/redis/types';
|
|
1
|
+
import { type HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import { type RedisConnections } from '@adonisjs/redis/types';
|
|
3
3
|
import type { CookieOptions } from '@adonisjs/core/types/http';
|
|
4
4
|
import type { DynamoDBClient, DynamoDBClientConfig } from '@aws-sdk/client-dynamodb';
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Union type of all values that can be stored in session data.
|
|
7
|
+
* These types are JSON serializable and safe for session storage.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* // All valid session values
|
|
11
|
+
* const stringVal: AllowedSessionValues = 'hello'
|
|
12
|
+
* const numberVal: AllowedSessionValues = 42
|
|
13
|
+
* const boolVal: AllowedSessionValues = true
|
|
14
|
+
* const objectVal: AllowedSessionValues = { key: 'value' }
|
|
15
|
+
* const dateVal: AllowedSessionValues = new Date()
|
|
16
|
+
* const arrayVal: AllowedSessionValues = [1, 2, 3]
|
|
7
17
|
*/
|
|
8
18
|
export type AllowedSessionValues = string | boolean | number | object | Date | Array<any>;
|
|
9
|
-
export type SessionData = Record<string, AllowedSessionValues>;
|
|
10
19
|
/**
|
|
11
|
-
*
|
|
20
|
+
* Shape of session data as a record of key-value pairs.
|
|
21
|
+
* The session data must be JSON serializable for persistence.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* const sessionData: SessionData = {
|
|
25
|
+
* userId: 123,
|
|
26
|
+
* username: 'john_doe',
|
|
27
|
+
* preferences: { theme: 'dark', language: 'en' },
|
|
28
|
+
* loginAt: new Date(),
|
|
29
|
+
* roles: ['user', 'editor']
|
|
30
|
+
* }
|
|
12
31
|
*/
|
|
13
|
-
export
|
|
14
|
-
id: string;
|
|
15
|
-
data: SessionData;
|
|
16
|
-
}
|
|
32
|
+
export type SessionData = Record<string, AllowedSessionValues>;
|
|
17
33
|
/**
|
|
18
|
-
*
|
|
34
|
+
* Contract interface that every session store implementation must follow.
|
|
35
|
+
* Defines the basic CRUD operations for session data persistence.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* class CustomStore implements SessionStoreContract {
|
|
39
|
+
* async read(sessionId: string) {
|
|
40
|
+
* // Read session data from your storage
|
|
41
|
+
* return await this.storage.get(sessionId)
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* async write(sessionId: string, data: SessionData) {
|
|
45
|
+
* // Write session data to your storage
|
|
46
|
+
* await this.storage.set(sessionId, data)
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* async destroy(sessionId: string) {
|
|
50
|
+
* // Remove session data from storage
|
|
51
|
+
* await this.storage.delete(sessionId)
|
|
52
|
+
* }
|
|
53
|
+
*
|
|
54
|
+
* async touch(sessionId: string) {
|
|
55
|
+
* // Update expiry/last accessed time
|
|
56
|
+
* await this.storage.touch(sessionId)
|
|
57
|
+
* }
|
|
58
|
+
* }
|
|
19
59
|
*/
|
|
20
60
|
export interface SessionStoreContract {
|
|
21
61
|
/**
|
|
22
|
-
*
|
|
23
|
-
*
|
|
62
|
+
* Reads session data for a given session ID from the persistence store
|
|
63
|
+
*
|
|
64
|
+
* @param sessionId - Unique identifier for the session
|
|
24
65
|
*/
|
|
25
66
|
read(sessionId: string): Promise<SessionData | null> | SessionData | null;
|
|
26
67
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
68
|
+
* Writes session data for a given session ID to the persistence store
|
|
69
|
+
*
|
|
70
|
+
* @param sessionId - Unique identifier for the session
|
|
71
|
+
* @param data - Session data to store
|
|
29
72
|
*/
|
|
30
73
|
write(sessionId: string, data: SessionData): Promise<void> | void;
|
|
31
74
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
75
|
+
* Removes session data for a given session ID from the persistence store
|
|
76
|
+
*
|
|
77
|
+
* @param sessionId - Unique identifier for the session to remove
|
|
34
78
|
*/
|
|
35
79
|
destroy(sessionId: string): Promise<void> | void;
|
|
36
80
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
81
|
+
* Updates the lifetime/expiry of a session without modifying the session data
|
|
82
|
+
*
|
|
83
|
+
* @param sessionId - Unique identifier for the session to touch
|
|
39
84
|
*/
|
|
40
85
|
touch(sessionId: string): Promise<void> | void;
|
|
41
86
|
}
|
|
42
87
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
* Base configuration for managing sessions without
|
|
58
|
-
* stores.
|
|
88
|
+
* Base configuration interface for session management.
|
|
89
|
+
* Used by the session manager and middleware to control session behavior.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* const config: SessionConfig = {
|
|
93
|
+
* enabled: true,
|
|
94
|
+
* cookieName: 'my_app_session',
|
|
95
|
+
* age: '2 hours',
|
|
96
|
+
* cookie: {
|
|
97
|
+
* httpOnly: true,
|
|
98
|
+
* secure: true,
|
|
99
|
+
* sameSite: 'strict'
|
|
100
|
+
* }
|
|
101
|
+
* }
|
|
59
102
|
*/
|
|
60
103
|
export interface SessionConfig {
|
|
61
104
|
/**
|
|
62
|
-
*
|
|
105
|
+
* Whether session management is enabled for the application
|
|
63
106
|
*/
|
|
64
107
|
enabled: boolean;
|
|
65
108
|
/**
|
|
66
|
-
*
|
|
109
|
+
* Name of the cookie used to store the session ID
|
|
67
110
|
*/
|
|
68
111
|
cookieName: string;
|
|
69
112
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* it gets expired.
|
|
75
|
-
*/
|
|
76
|
-
clearWithBrowser: boolean;
|
|
77
|
-
/**
|
|
78
|
-
* How long the session data should be kept alive without any
|
|
79
|
-
* activity.
|
|
80
|
-
*
|
|
81
|
-
* The session id cookie will also live for the same duration, unless
|
|
82
|
-
* "clearWithBrowser" is enabled
|
|
83
|
-
*
|
|
84
|
-
* The value should be a time expression or a number in seconds
|
|
113
|
+
* Maximum age of the session data and session ID cookie.
|
|
114
|
+
* Session data is automatically cleaned up after this duration of inactivity.
|
|
115
|
+
* Value can be a time expression (e.g. '2 hours') or number of seconds.
|
|
116
|
+
* Cookie duration is also set to this value unless clearWithBrowser is enabled.
|
|
85
117
|
*/
|
|
86
118
|
age: string | number;
|
|
87
119
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
120
|
+
* Additional cookie options for the session ID cookie.
|
|
121
|
+
* Excludes maxAge and expires as they're controlled by age and clearWithBrowser.
|
|
90
122
|
*/
|
|
91
|
-
cookie:
|
|
123
|
+
cookie: Partial<CookieOptions>;
|
|
92
124
|
}
|
|
93
125
|
/**
|
|
94
|
-
* Configuration
|
|
126
|
+
* Configuration for file-based session storage.
|
|
127
|
+
* Sessions are stored as individual files on the filesystem.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* const fileConfig: FileStoreConfig = {
|
|
131
|
+
* location: './tmp/sessions'
|
|
132
|
+
* }
|
|
95
133
|
*/
|
|
96
134
|
export type FileStoreConfig = {
|
|
135
|
+
/**
|
|
136
|
+
* Directory path where session files should be stored
|
|
137
|
+
*/
|
|
97
138
|
location: string;
|
|
98
139
|
};
|
|
99
140
|
/**
|
|
100
|
-
* Configuration
|
|
141
|
+
* Configuration for Redis-based session storage.
|
|
142
|
+
* Sessions are stored in a Redis database with automatic expiry.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* const redisConfig: RedisStoreConfig = {
|
|
146
|
+
* connection: 'main' // Redis connection name from config/redis.ts
|
|
147
|
+
* }
|
|
101
148
|
*/
|
|
102
149
|
export type RedisStoreConfig = {
|
|
150
|
+
/**
|
|
151
|
+
* Name of the Redis connection to use (must match connection name in config/redis.ts)
|
|
152
|
+
*/
|
|
103
153
|
connection: keyof RedisConnections;
|
|
104
154
|
};
|
|
105
155
|
/**
|
|
106
|
-
* Configuration
|
|
156
|
+
* Configuration for AWS DynamoDB-based session storage.
|
|
157
|
+
* Sessions are stored in a DynamoDB table with TTL support.
|
|
158
|
+
* Supports either providing a DynamoDB client instance or client configuration.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* // Using existing client
|
|
162
|
+
* const configWithClient: DynamoDBStoreConfig = {
|
|
163
|
+
* client: dynamoClient,
|
|
164
|
+
* tableName: 'UserSessions',
|
|
165
|
+
* keyAttribute: 'sessionId'
|
|
166
|
+
* }
|
|
167
|
+
*
|
|
168
|
+
* // Using client configuration
|
|
169
|
+
* const configWithClientConfig: DynamoDBStoreConfig = {
|
|
170
|
+
* clientConfig: {
|
|
171
|
+
* region: 'us-east-1',
|
|
172
|
+
* credentials: { ... }
|
|
173
|
+
* },
|
|
174
|
+
* tableName: 'Sessions'
|
|
175
|
+
* }
|
|
107
176
|
*/
|
|
108
177
|
export type DynamoDBStoreConfig = ({
|
|
178
|
+
/**
|
|
179
|
+
* Pre-configured DynamoDB client instance
|
|
180
|
+
*/
|
|
109
181
|
client: DynamoDBClient;
|
|
110
182
|
} | {
|
|
183
|
+
/**
|
|
184
|
+
* Configuration options for creating a new DynamoDB client
|
|
185
|
+
*/
|
|
111
186
|
clientConfig: DynamoDBClientConfig;
|
|
112
187
|
}) & {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Configuration used by the database store.
|
|
118
|
-
*/
|
|
119
|
-
export interface DatabaseStoreConfig {
|
|
120
|
-
connectionName?: string;
|
|
188
|
+
/**
|
|
189
|
+
* DynamoDB table name for storing sessions (defaults to "Session")
|
|
190
|
+
*/
|
|
121
191
|
tableName?: string;
|
|
122
192
|
/**
|
|
123
|
-
*
|
|
124
|
-
* sessions will be triggered on any given request.
|
|
125
|
-
*
|
|
126
|
-
* For example, 2 means 2% chance.
|
|
127
|
-
* Set to 0 to disable garbage collection.
|
|
128
|
-
*
|
|
129
|
-
* Defaults to 2 (2% chance)
|
|
193
|
+
* Attribute name for the session ID in the table (defaults to "key")
|
|
130
194
|
*/
|
|
131
|
-
|
|
132
|
-
}
|
|
195
|
+
keyAttribute?: string;
|
|
196
|
+
};
|
|
133
197
|
/**
|
|
134
|
-
* Factory function
|
|
198
|
+
* Factory function type for creating session store instances.
|
|
199
|
+
* Used by the session manager to instantiate stores per HTTP request.
|
|
200
|
+
*
|
|
201
|
+
* @param ctx - HTTP context for the current request
|
|
202
|
+
* @param sessionConfig - Session configuration
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* const storeFactory: SessionStoreFactory = (ctx, sessionConfig) => {
|
|
206
|
+
* return new CustomStore({
|
|
207
|
+
* connection: ctx.database,
|
|
208
|
+
* maxAge: sessionConfig.age
|
|
209
|
+
* })
|
|
210
|
+
* }
|
|
135
211
|
*/
|
|
136
212
|
export type SessionStoreFactory = (ctx: HttpContext, sessionConfig: SessionConfig) => SessionStoreContract;
|
|
137
|
-
/**
|
|
138
|
-
* Resolved session config after processing by defineConfig
|
|
139
|
-
*/
|
|
140
|
-
export interface ResolvedSessionConfig extends SessionConfig {
|
|
141
|
-
store: string;
|
|
142
|
-
stores: Record<string, SessionStoreFactory>;
|
|
143
|
-
}
|
package/build/src/types.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
|
@@ -1,91 +1,183 @@
|
|
|
1
|
-
import type { AllowedSessionValues, SessionData } from './types.
|
|
1
|
+
import type { AllowedSessionValues, SessionData } from './types.ts';
|
|
2
2
|
/**
|
|
3
|
-
* Readonly session store
|
|
3
|
+
* Readonly session store that provides read-only access to session values.
|
|
4
|
+
* Used for sharing session data with templates and other readonly contexts.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* const readOnlyStore = new ReadOnlyValuesStore({ user: 'john', theme: 'dark' })
|
|
8
|
+
* const user = readOnlyStore.get('user')
|
|
9
|
+
* const hasTheme = readOnlyStore.has('theme')
|
|
4
10
|
*/
|
|
5
11
|
export declare class ReadOnlyValuesStore {
|
|
6
12
|
/**
|
|
7
|
-
* Underlying store values
|
|
13
|
+
* Underlying store values containing session data
|
|
8
14
|
*/
|
|
9
15
|
protected values: SessionData;
|
|
10
16
|
/**
|
|
11
|
-
*
|
|
17
|
+
* Returns true if the store is empty
|
|
12
18
|
*/
|
|
13
19
|
get isEmpty(): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new readonly values store
|
|
22
|
+
*
|
|
23
|
+
* @param values - Initial session data or null
|
|
24
|
+
*/
|
|
14
25
|
constructor(values: SessionData | null);
|
|
15
26
|
/**
|
|
16
|
-
*
|
|
27
|
+
* Gets value for a given key
|
|
28
|
+
*
|
|
29
|
+
* @param key - The key or key path to retrieve
|
|
30
|
+
* @param defaultValue - Default value if key doesn't exist
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* store.get('username', 'guest')
|
|
34
|
+
* store.get(['user', 'preferences', 'theme'], 'light')
|
|
17
35
|
*/
|
|
18
36
|
get(key: string | string[], defaultValue?: any): any;
|
|
19
37
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
38
|
+
* Checks if a value exists. Includes extra guards to check arrays for length.
|
|
39
|
+
*
|
|
40
|
+
* @param key - The key or key path to check
|
|
41
|
+
* @param checkForArraysLength - Whether to check array length (default: true)
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* store.has('username') // Check if key exists
|
|
45
|
+
* store.has(['user', 'roles']) // Check nested key
|
|
46
|
+
* store.has('items', false) // Don't check array length
|
|
22
47
|
*/
|
|
23
48
|
has(key: string | string[], checkForArraysLength?: boolean): boolean;
|
|
24
49
|
/**
|
|
25
|
-
*
|
|
50
|
+
* Gets all values from the store
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* const allData = store.all()
|
|
26
54
|
*/
|
|
27
55
|
all(): any;
|
|
28
56
|
/**
|
|
29
57
|
* Returns object representation of values
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* const obj = store.toObject()
|
|
30
61
|
*/
|
|
31
62
|
toObject(): any;
|
|
32
63
|
/**
|
|
33
|
-
* Returns the store values
|
|
64
|
+
* Returns the store values as JSON-serializable data
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* const json = store.toJSON()
|
|
34
68
|
*/
|
|
35
69
|
toJSON(): any;
|
|
36
70
|
/**
|
|
37
71
|
* Returns string representation of the store
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* const str = store.toString()
|
|
38
75
|
*/
|
|
39
76
|
toString(): string;
|
|
40
77
|
}
|
|
41
78
|
/**
|
|
42
79
|
* Session store encapsulates the session data and offers a
|
|
43
80
|
* declarative API to mutate it.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* const store = new ValuesStore({ counter: 0 })
|
|
84
|
+
* store.set('username', 'john')
|
|
85
|
+
* store.increment('counter')
|
|
86
|
+
* const hasData = store.hasBeenModified
|
|
44
87
|
*/
|
|
45
88
|
export declare class ValuesStore extends ReadOnlyValuesStore {
|
|
46
89
|
#private;
|
|
90
|
+
/**
|
|
91
|
+
* Creates a new values store
|
|
92
|
+
*
|
|
93
|
+
* @param values - Initial session data or null
|
|
94
|
+
*/
|
|
47
95
|
constructor(values: SessionData | null);
|
|
48
96
|
/**
|
|
49
|
-
*
|
|
97
|
+
* Returns true if the store has been modified
|
|
50
98
|
*/
|
|
51
99
|
get hasBeenModified(): boolean;
|
|
52
100
|
/**
|
|
53
|
-
*
|
|
101
|
+
* Sets a key/value pair in the store
|
|
102
|
+
*
|
|
103
|
+
* @param key - The key or key path to set
|
|
104
|
+
* @param value - The value to set
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* store.set('username', 'john')
|
|
108
|
+
* store.set(['user', 'preferences'], { theme: 'dark' })
|
|
54
109
|
*/
|
|
55
110
|
set(key: string | string[], value: AllowedSessionValues): void;
|
|
56
111
|
/**
|
|
57
|
-
*
|
|
112
|
+
* Removes a key from the store
|
|
113
|
+
*
|
|
114
|
+
* @param key - The key or key path to remove
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* store.unset('temp_data')
|
|
118
|
+
* store.unset(['user', 'cache'])
|
|
58
119
|
*/
|
|
59
120
|
unset(key: string | string[]): void;
|
|
60
121
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
122
|
+
* Pulls value from the store. Same as calling store.get then store.unset.
|
|
123
|
+
*
|
|
124
|
+
* @param key - The key or key path to pull
|
|
125
|
+
* @param defaultValue - Default value if key doesn't exist
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* const message = store.pull('notification', 'No messages')
|
|
129
|
+
* const data = store.pull(['temp', 'data'])
|
|
63
130
|
*/
|
|
64
131
|
pull(key: string | string[], defaultValue?: any): any;
|
|
65
132
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
133
|
+
* Increments a numeric value. Raises an error when underlying value is not a number.
|
|
134
|
+
*
|
|
135
|
+
* @param key - The key or key path to increment
|
|
136
|
+
* @param steps - Number of steps to increment (default: 1)
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* store.increment('page_views') // Increments by 1
|
|
140
|
+
* store.increment('score', 10) // Increments by 10
|
|
68
141
|
*/
|
|
69
142
|
increment(key: string | string[], steps?: number): void;
|
|
70
143
|
/**
|
|
71
|
-
*
|
|
72
|
-
*
|
|
144
|
+
* Decrements a numeric value. Raises an error when underlying value is not a number.
|
|
145
|
+
*
|
|
146
|
+
* @param key - The key or key path to decrement
|
|
147
|
+
* @param steps - Number of steps to decrement (default: 1)
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* store.decrement('attempts') // Decrements by 1
|
|
151
|
+
* store.decrement('credits', 5) // Decrements by 5
|
|
73
152
|
*/
|
|
74
153
|
decrement(key: string | string[], steps?: number): void;
|
|
75
154
|
/**
|
|
76
|
-
*
|
|
155
|
+
* Overwrites existing store data with new values
|
|
156
|
+
*
|
|
157
|
+
* @param values - New values to replace existing data
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* store.update({ username: 'jane', theme: 'light' })
|
|
77
161
|
*/
|
|
78
162
|
update(values: {
|
|
79
163
|
[key: string]: any;
|
|
80
164
|
}): void;
|
|
81
165
|
/**
|
|
82
|
-
*
|
|
166
|
+
* Merges values with existing store data
|
|
167
|
+
*
|
|
168
|
+
* @param values - Values to merge with existing data
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* store.merge({ newField: 'value', existingField: 'updated' })
|
|
83
172
|
*/
|
|
84
173
|
merge(values: {
|
|
85
174
|
[key: string]: any;
|
|
86
175
|
}): any;
|
|
87
176
|
/**
|
|
88
|
-
*
|
|
177
|
+
* Resets store by clearing all values
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* store.clear() // Removes all data from store
|
|
89
181
|
*/
|
|
90
182
|
clear(): void;
|
|
91
183
|
}
|