@adonisjs/session 7.0.0-2 → 7.0.0-3
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/README.md +5 -8
- package/build/index.d.ts +5 -3
- package/build/index.js +5 -3
- package/build/providers/session_provider.d.ts +5 -8
- package/build/providers/session_provider.js +13 -35
- package/build/src/client.d.ts +13 -25
- package/build/src/client.js +24 -43
- package/build/src/debug.d.ts +3 -0
- package/build/src/debug.js +10 -0
- package/build/src/define_config.d.ts +6 -3
- package/build/src/define_config.js +34 -5
- package/build/src/drivers/cookie.d.ts +7 -9
- package/build/src/drivers/cookie.js +10 -6
- package/build/src/drivers/file.d.ts +11 -14
- package/build/src/drivers/file.js +64 -41
- package/build/src/drivers/memory.d.ts +4 -8
- package/build/src/drivers/memory.js +0 -3
- package/build/src/drivers/redis.d.ts +4 -6
- package/build/src/drivers/redis.js +24 -28
- package/build/src/drivers_collection.d.ts +22 -0
- package/build/src/drivers_collection.js +38 -0
- package/build/src/errors.d.ts +8 -0
- package/build/src/errors.js +17 -0
- package/build/src/helpers.d.ts +6 -0
- package/build/src/helpers.js +37 -0
- package/build/src/session.d.ts +86 -59
- package/build/src/session.js +221 -221
- package/build/src/session_middleware.d.ts +19 -5
- package/build/src/session_middleware.js +42 -6
- package/build/src/store.d.ts +17 -14
- package/build/src/store.js +33 -17
- package/build/src/types/extended.d.ts +19 -0
- package/build/src/types/main.d.ts +106 -0
- package/build/src/types/main.js +9 -0
- package/package.json +23 -20
- package/build/src/bindings/api_client.d.ts +0 -2
- package/build/src/bindings/api_client.js +0 -135
- package/build/src/bindings/http_context.d.ts +0 -5
- package/build/src/bindings/http_context.js +0 -17
- package/build/src/bindings/types.d.ts +0 -77
- package/build/src/session_manager.d.ts +0 -38
- package/build/src/session_manager.js +0 -149
- package/build/src/types.d.ts +0 -61
- package/build/src/types.js +0 -1
- /package/build/src/{bindings/types.js → types/extended.js} +0 -0
|
@@ -1,12 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/session
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { ExceptionHandler } from '@adonisjs/core/http';
|
|
10
|
+
import { Session } from './session.js';
|
|
11
|
+
import sessionDriversList from './drivers_collection.js';
|
|
12
|
+
/**
|
|
13
|
+
* Overwriting validation exception renderer
|
|
14
|
+
*/
|
|
15
|
+
const originalErrorHandler = ExceptionHandler.prototype.renderValidationErrorAsHTML;
|
|
16
|
+
ExceptionHandler.macro('renderValidationErrorAsHTML', async function (error, ctx) {
|
|
17
|
+
if (ctx.session) {
|
|
18
|
+
ctx.session.flashValidationErrors(error);
|
|
19
|
+
ctx.response.redirect('back', true);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return originalErrorHandler(error, ctx);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* Session middleware is used to initiate the session store
|
|
27
|
+
* and commit its values during an HTTP request
|
|
28
|
+
*/
|
|
1
29
|
export default class SessionMiddleware {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
30
|
+
#config;
|
|
31
|
+
#emitter;
|
|
32
|
+
constructor(config, emitter) {
|
|
33
|
+
this.#config = config;
|
|
34
|
+
this.#emitter = emitter;
|
|
5
35
|
}
|
|
6
36
|
async handle(ctx, next) {
|
|
7
|
-
if (!this.
|
|
8
|
-
return;
|
|
37
|
+
if (!this.#config.enabled) {
|
|
38
|
+
return next();
|
|
9
39
|
}
|
|
40
|
+
const driver = sessionDriversList.create(this.#config.driver, this.#config, ctx);
|
|
41
|
+
ctx.session = new Session(this.#config, driver, this.#emitter, ctx);
|
|
10
42
|
/**
|
|
11
43
|
* Initiate session store
|
|
12
44
|
*/
|
|
@@ -14,10 +46,14 @@ export default class SessionMiddleware {
|
|
|
14
46
|
/**
|
|
15
47
|
* Call next middlewares or route handler
|
|
16
48
|
*/
|
|
17
|
-
await next();
|
|
49
|
+
const response = await next();
|
|
18
50
|
/**
|
|
19
51
|
* Commit store mutations
|
|
20
52
|
*/
|
|
21
53
|
await ctx.session.commit();
|
|
54
|
+
/**
|
|
55
|
+
* Return response
|
|
56
|
+
*/
|
|
57
|
+
return response;
|
|
22
58
|
}
|
|
23
59
|
}
|
package/build/src/store.d.ts
CHANGED
|
@@ -1,32 +1,27 @@
|
|
|
1
|
-
import type { AllowedSessionValues } from './types.js';
|
|
1
|
+
import type { AllowedSessionValues, SessionData } from './types/main.js';
|
|
2
2
|
/**
|
|
3
|
-
* Session store
|
|
3
|
+
* Session store encapsulates the session data and offers a
|
|
4
|
+
* declarative API to mutate it.
|
|
4
5
|
*/
|
|
5
6
|
export declare class Store {
|
|
6
7
|
#private;
|
|
7
|
-
constructor(values:
|
|
8
|
-
[key: string]: any;
|
|
9
|
-
} | null);
|
|
8
|
+
constructor(values: SessionData | null);
|
|
10
9
|
/**
|
|
11
10
|
* Find if store is empty or not
|
|
12
11
|
*/
|
|
13
12
|
get isEmpty(): boolean;
|
|
14
13
|
/**
|
|
15
|
-
*
|
|
14
|
+
* Find if the store has been modified.
|
|
16
15
|
*/
|
|
17
|
-
|
|
16
|
+
get hasBeenModified(): boolean;
|
|
18
17
|
/**
|
|
19
|
-
*
|
|
18
|
+
* Set key/value pair
|
|
20
19
|
*/
|
|
21
|
-
|
|
20
|
+
set(key: string, value: AllowedSessionValues): void;
|
|
22
21
|
/**
|
|
23
22
|
* Remove key
|
|
24
23
|
*/
|
|
25
24
|
unset(key: string): void;
|
|
26
|
-
/**
|
|
27
|
-
* Reset store by clearing it's values.
|
|
28
|
-
*/
|
|
29
|
-
clear(): void;
|
|
30
25
|
/**
|
|
31
26
|
* Pull value from the store. It is same as calling
|
|
32
27
|
* store.get and then store.unset
|
|
@@ -43,7 +38,7 @@ export declare class Store {
|
|
|
43
38
|
*/
|
|
44
39
|
decrement(key: string, steps?: number): void;
|
|
45
40
|
/**
|
|
46
|
-
* Overwrite
|
|
41
|
+
* Overwrite existing store data with new values.
|
|
47
42
|
*/
|
|
48
43
|
update(values: {
|
|
49
44
|
[key: string]: any;
|
|
@@ -54,6 +49,14 @@ export declare class Store {
|
|
|
54
49
|
merge(values: {
|
|
55
50
|
[key: string]: any;
|
|
56
51
|
}): any;
|
|
52
|
+
/**
|
|
53
|
+
* Reset store by clearing it's values.
|
|
54
|
+
*/
|
|
55
|
+
clear(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Get value for a given key
|
|
58
|
+
*/
|
|
59
|
+
get(key: string, defaultValue?: any): any;
|
|
57
60
|
/**
|
|
58
61
|
* A boolean to know if value exists. Extra guards to check
|
|
59
62
|
* arrays for it's length as well.
|
package/build/src/store.js
CHANGED
|
@@ -6,16 +6,22 @@
|
|
|
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 { Exception } from '@poppinss/utils';
|
|
10
9
|
import lodash from '@poppinss/utils/lodash';
|
|
10
|
+
import { RuntimeException } from '@poppinss/utils';
|
|
11
11
|
/**
|
|
12
|
-
* Session store
|
|
12
|
+
* Session store encapsulates the session data and offers a
|
|
13
|
+
* declarative API to mutate it.
|
|
13
14
|
*/
|
|
14
15
|
export class Store {
|
|
15
16
|
/**
|
|
16
17
|
* Underlying store values
|
|
17
18
|
*/
|
|
18
19
|
#values;
|
|
20
|
+
/**
|
|
21
|
+
* A boolean to know if store has been
|
|
22
|
+
* modified
|
|
23
|
+
*/
|
|
24
|
+
#modified = false;
|
|
19
25
|
constructor(values) {
|
|
20
26
|
this.#values = values || {};
|
|
21
27
|
}
|
|
@@ -26,29 +32,25 @@ export class Store {
|
|
|
26
32
|
return !this.#values || Object.keys(this.#values).length === 0;
|
|
27
33
|
}
|
|
28
34
|
/**
|
|
29
|
-
*
|
|
35
|
+
* Find if the store has been modified.
|
|
30
36
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
37
|
+
get hasBeenModified() {
|
|
38
|
+
return this.#modified;
|
|
33
39
|
}
|
|
34
40
|
/**
|
|
35
|
-
*
|
|
41
|
+
* Set key/value pair
|
|
36
42
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
set(key, value) {
|
|
44
|
+
this.#modified = true;
|
|
45
|
+
lodash.set(this.#values, key, value);
|
|
39
46
|
}
|
|
40
47
|
/**
|
|
41
48
|
* Remove key
|
|
42
49
|
*/
|
|
43
50
|
unset(key) {
|
|
51
|
+
this.#modified = true;
|
|
44
52
|
lodash.unset(this.#values, key);
|
|
45
53
|
}
|
|
46
|
-
/**
|
|
47
|
-
* Reset store by clearing it's values.
|
|
48
|
-
*/
|
|
49
|
-
clear() {
|
|
50
|
-
this.update({});
|
|
51
|
-
}
|
|
52
54
|
/**
|
|
53
55
|
* Pull value from the store. It is same as calling
|
|
54
56
|
* store.get and then store.unset
|
|
@@ -66,7 +68,7 @@ export class Store {
|
|
|
66
68
|
increment(key, steps = 1) {
|
|
67
69
|
const value = this.get(key, 0);
|
|
68
70
|
if (typeof value !== 'number') {
|
|
69
|
-
throw new
|
|
71
|
+
throw new RuntimeException(`Cannot increment "${key}". Existing value is not a number`);
|
|
70
72
|
}
|
|
71
73
|
this.set(key, value + steps);
|
|
72
74
|
}
|
|
@@ -77,22 +79,36 @@ export class Store {
|
|
|
77
79
|
decrement(key, steps = 1) {
|
|
78
80
|
const value = this.get(key, 0);
|
|
79
81
|
if (typeof value !== 'number') {
|
|
80
|
-
throw new
|
|
82
|
+
throw new RuntimeException(`Cannot decrement "${key}". Existing value is not a number`);
|
|
81
83
|
}
|
|
82
84
|
this.set(key, value - steps);
|
|
83
85
|
}
|
|
84
86
|
/**
|
|
85
|
-
* Overwrite
|
|
87
|
+
* Overwrite existing store data with new values.
|
|
86
88
|
*/
|
|
87
89
|
update(values) {
|
|
90
|
+
this.#modified = true;
|
|
88
91
|
this.#values = values;
|
|
89
92
|
}
|
|
90
93
|
/**
|
|
91
94
|
* Update to merge values
|
|
92
95
|
*/
|
|
93
96
|
merge(values) {
|
|
97
|
+
this.#modified = true;
|
|
94
98
|
lodash.merge(this.#values, values);
|
|
95
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Reset store by clearing it's values.
|
|
102
|
+
*/
|
|
103
|
+
clear() {
|
|
104
|
+
this.update({});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Get value for a given key
|
|
108
|
+
*/
|
|
109
|
+
get(key, defaultValue) {
|
|
110
|
+
return lodash.get(this.#values, key, defaultValue);
|
|
111
|
+
}
|
|
96
112
|
/**
|
|
97
113
|
* A boolean to know if value exists. Extra guards to check
|
|
98
114
|
* arrays for it's length as well.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Session } from '../session.js';
|
|
2
|
+
/**
|
|
3
|
+
* Events emitted by the session class
|
|
4
|
+
*/
|
|
5
|
+
declare module '@adonisjs/core/types' {
|
|
6
|
+
interface EventsList {
|
|
7
|
+
'session:initiated': {
|
|
8
|
+
session: Session;
|
|
9
|
+
};
|
|
10
|
+
'session:committed': {
|
|
11
|
+
session: Session;
|
|
12
|
+
};
|
|
13
|
+
'session:migrated': {
|
|
14
|
+
fromSessionId: string;
|
|
15
|
+
toSessionId: string;
|
|
16
|
+
session: Session;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import { RedisConnections } from '@adonisjs/redis/types';
|
|
3
|
+
import type { CookieOptions } from '@adonisjs/core/types/http';
|
|
4
|
+
import type { FileDriver } from '../drivers/file.js';
|
|
5
|
+
import type { RedisDriver } from '../drivers/redis.js';
|
|
6
|
+
import type { MemoryDriver } from '../drivers/memory.js';
|
|
7
|
+
import type { CookieDriver } from '../drivers/cookie.js';
|
|
8
|
+
/**
|
|
9
|
+
* The values allowed by the `session.put` method
|
|
10
|
+
*/
|
|
11
|
+
export type AllowedSessionValues = string | boolean | number | object | Date | Array<any>;
|
|
12
|
+
export type SessionData = Record<string, AllowedSessionValues>;
|
|
13
|
+
/**
|
|
14
|
+
* Session drivers must implement the session driver contract.
|
|
15
|
+
*/
|
|
16
|
+
export interface SessionDriverContract {
|
|
17
|
+
/**
|
|
18
|
+
* The read method is used to read the data from the persistence
|
|
19
|
+
* store and return it back as an object
|
|
20
|
+
*/
|
|
21
|
+
read(sessionId: string): Promise<SessionData | null> | SessionData | null;
|
|
22
|
+
/**
|
|
23
|
+
* The write method is used to write the session data into the
|
|
24
|
+
* persistence store.
|
|
25
|
+
*/
|
|
26
|
+
write(sessionId: string, data: SessionData): Promise<void> | void;
|
|
27
|
+
/**
|
|
28
|
+
* The destroy method is used to destroy the session by removing
|
|
29
|
+
* its data from the persistence store
|
|
30
|
+
*/
|
|
31
|
+
destroy(sessionId: string): Promise<void> | void;
|
|
32
|
+
/**
|
|
33
|
+
* The touch method should update the lifetime of session id without
|
|
34
|
+
* making changes to the session data.
|
|
35
|
+
*/
|
|
36
|
+
touch(sessionId: string): Promise<void> | void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Shape of session config.
|
|
40
|
+
*/
|
|
41
|
+
export interface SessionConfig {
|
|
42
|
+
/**
|
|
43
|
+
* Enable/disable sessions temporarily
|
|
44
|
+
*/
|
|
45
|
+
enabled: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* The drivers to use
|
|
48
|
+
*/
|
|
49
|
+
driver: keyof SessionDriversList;
|
|
50
|
+
/**
|
|
51
|
+
* The name of the cookie for storing the session id.
|
|
52
|
+
*/
|
|
53
|
+
cookieName: string;
|
|
54
|
+
/**
|
|
55
|
+
* When set to true, the session id cookie will be removed
|
|
56
|
+
* when the user closes the browser.
|
|
57
|
+
*
|
|
58
|
+
* However, the persisted data will continue to exist until
|
|
59
|
+
* it gets expired.
|
|
60
|
+
*/
|
|
61
|
+
clearWithBrowser: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* How long the session data should be kept alive without any
|
|
64
|
+
* activity.
|
|
65
|
+
*
|
|
66
|
+
* The session id cookie will also live for the same duration, unless
|
|
67
|
+
* "clearWithBrowser" is enabled
|
|
68
|
+
*
|
|
69
|
+
* The value should be a time expression or a number in seconds
|
|
70
|
+
*/
|
|
71
|
+
age: string | number;
|
|
72
|
+
/**
|
|
73
|
+
* Configuration used by the cookie driver and for storing the
|
|
74
|
+
* session id cookie.
|
|
75
|
+
*/
|
|
76
|
+
cookie: Omit<Partial<CookieOptions>, 'maxAge' | 'expires'>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Configuration used by the file driver.
|
|
80
|
+
*/
|
|
81
|
+
export type FileDriverConfig = {
|
|
82
|
+
location: string;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Configuration used by the redis driver.
|
|
86
|
+
*/
|
|
87
|
+
export type RedisDriverConfig = {
|
|
88
|
+
connection: keyof RedisConnections;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Extending session config with the drivers config
|
|
92
|
+
*/
|
|
93
|
+
export interface SessionConfig {
|
|
94
|
+
file?: FileDriverConfig;
|
|
95
|
+
redis?: RedisDriverConfig;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* List of the session drivers. The list can be extended using
|
|
99
|
+
* declaration merging
|
|
100
|
+
*/
|
|
101
|
+
export interface SessionDriversList {
|
|
102
|
+
file: (config: SessionConfig, ctx: HttpContext) => FileDriver;
|
|
103
|
+
cookie: (config: SessionConfig, ctx: HttpContext) => CookieDriver;
|
|
104
|
+
redis: (config: SessionConfig, ctx: HttpContext) => RedisDriver;
|
|
105
|
+
memory: (config: SessionConfig, ctx: HttpContext) => MemoryDriver;
|
|
106
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/session",
|
|
3
3
|
"description": "Session provider for AdonisJS",
|
|
4
|
-
"version": "7.0.0-
|
|
4
|
+
"version": "7.0.0-3",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=18.16.0"
|
|
7
7
|
},
|
|
@@ -11,20 +11,21 @@
|
|
|
11
11
|
"build/src",
|
|
12
12
|
"build/stubs",
|
|
13
13
|
"build/providers",
|
|
14
|
-
"build/index.d.ts",
|
|
15
|
-
"build/index.js",
|
|
16
14
|
"build/configure.d.ts",
|
|
17
|
-
"build/configure.js"
|
|
15
|
+
"build/configure.js",
|
|
16
|
+
"build/index.d.ts",
|
|
17
|
+
"build/index.js"
|
|
18
18
|
],
|
|
19
19
|
"exports": {
|
|
20
20
|
".": "./build/index.js",
|
|
21
21
|
"./session_provider": "./build/providers/session_provider.js",
|
|
22
22
|
"./session_middleware": "./build/src/session_middleware.js",
|
|
23
|
+
"./client": "./build/src/client.js",
|
|
23
24
|
"./types": "./build/src/types.js"
|
|
24
25
|
},
|
|
25
26
|
"scripts": {
|
|
26
27
|
"pretest": "npm run lint",
|
|
27
|
-
"test": "c8 npm run quick:test",
|
|
28
|
+
"test": "cross-env NODE_DEBUG=adonisjs:session c8 npm run quick:test",
|
|
28
29
|
"clean": "del-cli build",
|
|
29
30
|
"typecheck": "tsc --noEmit",
|
|
30
31
|
"copy:templates": "copyfiles \"stubs/**/*.stub\" build",
|
|
@@ -35,29 +36,29 @@
|
|
|
35
36
|
"format": "prettier --write .",
|
|
36
37
|
"release": "np",
|
|
37
38
|
"version": "npm run build",
|
|
38
|
-
"sync-labels": "github-label-sync --labels .github/labels.json adonisjs/
|
|
39
|
+
"sync-labels": "github-label-sync --labels .github/labels.json adonisjs/session",
|
|
39
40
|
"quick:test": "node --enable-source-maps --loader=ts-node/esm bin/test.ts"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
|
-
"@adonisjs/
|
|
43
|
-
"@adonisjs/core": "6.1.5-10",
|
|
43
|
+
"@adonisjs/core": "^6.1.5-14",
|
|
44
44
|
"@adonisjs/eslint-config": "^1.1.8",
|
|
45
45
|
"@adonisjs/prettier-config": "^1.1.8",
|
|
46
|
-
"@adonisjs/redis": "8.0.0-
|
|
46
|
+
"@adonisjs/redis": "^8.0.0-8",
|
|
47
47
|
"@adonisjs/tsconfig": "^1.1.8",
|
|
48
|
-
"@
|
|
49
|
-
"@japa/
|
|
50
|
-
"@japa/
|
|
51
|
-
"@japa/
|
|
52
|
-
"@japa/
|
|
53
|
-
"@japa/
|
|
54
|
-
"@poppinss/dev-utils": "^2.0.3",
|
|
48
|
+
"@japa/api-client": "^2.0.0-0",
|
|
49
|
+
"@japa/assert": "^2.0.0-1",
|
|
50
|
+
"@japa/file-system": "^2.0.0-1",
|
|
51
|
+
"@japa/plugin-adonisjs": "^2.0.0-1",
|
|
52
|
+
"@japa/runner": "^3.0.0-6",
|
|
53
|
+
"@japa/snapshot": "^2.0.0-1",
|
|
55
54
|
"@swc/core": "^1.3.70",
|
|
56
55
|
"@types/node": "^20.4.2",
|
|
57
56
|
"@types/set-cookie-parser": "^2.4.3",
|
|
58
57
|
"@types/supertest": "^2.0.12",
|
|
58
|
+
"@vinejs/vine": "^1.6.0",
|
|
59
59
|
"c8": "^8.0.0",
|
|
60
60
|
"copyfiles": "^2.4.1",
|
|
61
|
+
"cross-env": "^7.0.3",
|
|
61
62
|
"del-cli": "^5.0.0",
|
|
62
63
|
"eslint": "^8.45.0",
|
|
63
64
|
"github-label-sync": "^2.3.1",
|
|
@@ -70,11 +71,11 @@
|
|
|
70
71
|
"typescript": "^5.1.6"
|
|
71
72
|
},
|
|
72
73
|
"dependencies": {
|
|
73
|
-
"@poppinss/utils": "6.5.0-
|
|
74
|
+
"@poppinss/utils": "^6.5.0-5"
|
|
74
75
|
},
|
|
75
76
|
"peerDependencies": {
|
|
76
|
-
"@adonisjs/core": "6.1.5-
|
|
77
|
-
"@adonisjs/redis": "8.0.0-
|
|
77
|
+
"@adonisjs/core": "^6.1.5-12",
|
|
78
|
+
"@adonisjs/redis": "^8.0.0-7"
|
|
78
79
|
},
|
|
79
80
|
"peerDependenciesMeta": {
|
|
80
81
|
"@adonisjs/redis": {
|
|
@@ -124,7 +125,9 @@
|
|
|
124
125
|
],
|
|
125
126
|
"exclude": [
|
|
126
127
|
"tests/**",
|
|
127
|
-
"stubs/**"
|
|
128
|
+
"stubs/**",
|
|
129
|
+
"factories/**",
|
|
130
|
+
"bin/**"
|
|
128
131
|
]
|
|
129
132
|
}
|
|
130
133
|
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { ApiRequest, ApiResponse, ApiClient } from '@japa/api-client';
|
|
10
|
-
import { inspect } from 'node:util';
|
|
11
|
-
export function extendApiClient(sessionManager) {
|
|
12
|
-
/**
|
|
13
|
-
* Set "sessionClient" on the api request
|
|
14
|
-
*/
|
|
15
|
-
ApiRequest.getter('sessionClient', function () {
|
|
16
|
-
return sessionManager.client();
|
|
17
|
-
}, true);
|
|
18
|
-
/**
|
|
19
|
-
* Send session values in the request
|
|
20
|
-
*/
|
|
21
|
-
ApiRequest.macro('session', function (session) {
|
|
22
|
-
if (!this.sessionClient.isEnabled()) {
|
|
23
|
-
throw new Error('Cannot set session. Make sure to enable it inside "config/session" file');
|
|
24
|
-
}
|
|
25
|
-
this.sessionClient.merge(session);
|
|
26
|
-
return this;
|
|
27
|
-
});
|
|
28
|
-
/**
|
|
29
|
-
* Send flash messages in the request
|
|
30
|
-
*/
|
|
31
|
-
ApiRequest.macro('flashMessages', function (messages) {
|
|
32
|
-
if (!this.sessionClient.isEnabled()) {
|
|
33
|
-
throw new Error('Cannot set flash messages. Make sure to enable the session inside "config/session" file');
|
|
34
|
-
}
|
|
35
|
-
this.sessionClient.flashMessages.merge(messages);
|
|
36
|
-
return this;
|
|
37
|
-
});
|
|
38
|
-
/**
|
|
39
|
-
* Returns reference to the session data from the session
|
|
40
|
-
* jar
|
|
41
|
-
*/
|
|
42
|
-
ApiResponse.macro('session', function () {
|
|
43
|
-
return this.sessionJar.session;
|
|
44
|
-
});
|
|
45
|
-
/**
|
|
46
|
-
* Returns reference to the flash messages from the session
|
|
47
|
-
* jar
|
|
48
|
-
*/
|
|
49
|
-
ApiResponse.macro('flashMessages', function () {
|
|
50
|
-
return this.sessionJar.flashMessages || {};
|
|
51
|
-
});
|
|
52
|
-
/**
|
|
53
|
-
* Assert response to contain a given session and optionally
|
|
54
|
-
* has the expected value
|
|
55
|
-
*/
|
|
56
|
-
ApiResponse.macro('assertSession', function (name, value) {
|
|
57
|
-
this.ensureHasAssert();
|
|
58
|
-
this.assert.property(this.session(), name);
|
|
59
|
-
if (value !== undefined) {
|
|
60
|
-
this.assert.deepEqual(this.session()[name], value);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
/**
|
|
64
|
-
* Assert response to not contain a given session
|
|
65
|
-
*/
|
|
66
|
-
ApiResponse.macro('assertSessionMissing', function (name) {
|
|
67
|
-
this.ensureHasAssert();
|
|
68
|
-
this.assert.notProperty(this.session(), name);
|
|
69
|
-
});
|
|
70
|
-
/**
|
|
71
|
-
* Assert response to contain a given flash message and optionally
|
|
72
|
-
* has the expected value
|
|
73
|
-
*/
|
|
74
|
-
ApiResponse.macro('assertFlashMessage', function (name, value) {
|
|
75
|
-
this.ensureHasAssert();
|
|
76
|
-
this.assert.property(this.flashMessages(), name);
|
|
77
|
-
if (value !== undefined) {
|
|
78
|
-
this.assert.deepEqual(this.flashMessages()[name], value);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
/**
|
|
82
|
-
* Assert response to not contain a given session
|
|
83
|
-
*/
|
|
84
|
-
ApiResponse.macro('assertFlashMissing', function (name) {
|
|
85
|
-
this.ensureHasAssert();
|
|
86
|
-
this.assert.notProperty(this.flashMessages(), name);
|
|
87
|
-
});
|
|
88
|
-
/**
|
|
89
|
-
* Dump session to the console
|
|
90
|
-
*/
|
|
91
|
-
ApiResponse.macro('dumpSession', function (options) {
|
|
92
|
-
const inspectOptions = { depth: 2, showHidden: false, colors: true, ...options };
|
|
93
|
-
console.log(`"session" => ${inspect(this.session(), inspectOptions)}`);
|
|
94
|
-
console.log(`"flashMessages" => ${inspect(this.flashMessages(), inspectOptions)}`);
|
|
95
|
-
return this;
|
|
96
|
-
});
|
|
97
|
-
/**
|
|
98
|
-
* Adding hooks directly on the request object moves the hooks to
|
|
99
|
-
* the end of the queue (basically after the globally hooks)
|
|
100
|
-
*/
|
|
101
|
-
ApiClient.onRequest((req) => {
|
|
102
|
-
/**
|
|
103
|
-
* Hook into request and persist session data to be available
|
|
104
|
-
* on the server during the request.
|
|
105
|
-
*/
|
|
106
|
-
req.setup(async (request) => {
|
|
107
|
-
/**
|
|
108
|
-
* Persist session data and set the session id within the
|
|
109
|
-
* cookie
|
|
110
|
-
*/
|
|
111
|
-
const { cookieName, sessionId } = await request.sessionClient.commit();
|
|
112
|
-
request.withCookie(cookieName, sessionId);
|
|
113
|
-
/**
|
|
114
|
-
* Cleanup if request has error. Otherwise the teardown
|
|
115
|
-
* hook will clear
|
|
116
|
-
*/
|
|
117
|
-
return async (error) => {
|
|
118
|
-
if (error) {
|
|
119
|
-
await request.sessionClient.forget();
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
});
|
|
123
|
-
/**
|
|
124
|
-
* Load messages from the session store and keep a reference to it
|
|
125
|
-
* inside the response object.
|
|
126
|
-
*
|
|
127
|
-
* We also destroy the session after getting a copy of the session
|
|
128
|
-
* data
|
|
129
|
-
*/
|
|
130
|
-
req.teardown(async (response) => {
|
|
131
|
-
response.sessionJar = await response.request.sessionClient.load(response.cookies());
|
|
132
|
-
await response.request.sessionClient.forget();
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/session
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { HttpContext } from '@adonisjs/core/http';
|
|
10
|
-
/**
|
|
11
|
-
* Extends HttpContext class with the ally getter
|
|
12
|
-
*/
|
|
13
|
-
export function extendHttpContext(session) {
|
|
14
|
-
HttpContext.getter('session', function () {
|
|
15
|
-
return session.create(this);
|
|
16
|
-
}, true);
|
|
17
|
-
}
|