@adonisjs/session 7.0.0-8 → 7.0.0

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.
Files changed (76) hide show
  1. package/build/chunk-2X5L327N.js +28 -0
  2. package/build/chunk-2X5L327N.js.map +1 -0
  3. package/build/chunk-7YIO32ZH.js +134 -0
  4. package/build/chunk-7YIO32ZH.js.map +1 -0
  5. package/build/chunk-K4OSGJVW.js +402 -0
  6. package/build/chunk-K4OSGJVW.js.map +1 -0
  7. package/build/chunk-S6P3TBEK.js +85 -0
  8. package/build/chunk-S6P3TBEK.js.map +1 -0
  9. package/build/chunk-TE5JP3SX.js +151 -0
  10. package/build/chunk-TE5JP3SX.js.map +1 -0
  11. package/build/chunk-WBAYBMJJ.js +15 -0
  12. package/build/chunk-WBAYBMJJ.js.map +1 -0
  13. package/build/config/session.stub +51 -0
  14. package/build/cookie-H7KRZB4T.js +56 -0
  15. package/build/cookie-H7KRZB4T.js.map +1 -0
  16. package/build/factories/main.d.ts +1 -0
  17. package/build/factories/main.js +50 -0
  18. package/build/factories/main.js.map +1 -0
  19. package/build/factories/session_middleware_factory.d.ts +27 -0
  20. package/build/file-YO7C2QWO.js +112 -0
  21. package/build/file-YO7C2QWO.js.map +1 -0
  22. package/build/index.d.ts +1 -3
  23. package/build/index.js +18 -14
  24. package/build/index.js.map +1 -0
  25. package/build/providers/session_provider.d.ts +22 -5
  26. package/build/providers/session_provider.js +51 -59
  27. package/build/providers/session_provider.js.map +1 -0
  28. package/build/redis-KDWIBKUQ.js +58 -0
  29. package/build/redis-KDWIBKUQ.js.map +1 -0
  30. package/build/src/client.d.ts +24 -19
  31. package/build/src/client.js +9 -81
  32. package/build/src/client.js.map +1 -0
  33. package/build/src/debug.d.ts +1 -1
  34. package/build/src/define_config.d.ts +24 -3
  35. package/build/src/{edge_plugin_adonisjs_session.d.ts → plugins/edge.d.ts} +1 -1
  36. package/build/src/plugins/edge.js +102 -0
  37. package/build/src/plugins/edge.js.map +1 -0
  38. package/build/src/plugins/japa/api_client.d.ts +75 -0
  39. package/build/src/plugins/japa/api_client.js +100 -0
  40. package/build/src/plugins/japa/api_client.js.map +1 -0
  41. package/build/src/plugins/japa/browser_client.d.ts +36 -0
  42. package/build/src/plugins/japa/browser_client.js +88 -0
  43. package/build/src/plugins/japa/browser_client.js.map +1 -0
  44. package/build/src/session.d.ts +21 -12
  45. package/build/src/session_middleware.d.ts +7 -4
  46. package/build/src/session_middleware.js +10 -59
  47. package/build/src/session_middleware.js.map +1 -0
  48. package/build/src/{drivers → stores}/cookie.d.ts +4 -4
  49. package/build/src/{drivers → stores}/file.d.ts +4 -4
  50. package/build/src/{drivers → stores}/memory.d.ts +3 -3
  51. package/build/src/{drivers → stores}/redis.d.ts +5 -5
  52. package/build/src/{types/main.d.ts → types.d.ts} +11 -31
  53. package/build/src/types.js +1 -0
  54. package/build/src/types.js.map +1 -0
  55. package/build/src/{store.d.ts → values_store.d.ts} +10 -10
  56. package/package.json +75 -40
  57. package/build/configure.js +0 -45
  58. package/build/src/debug.js +0 -10
  59. package/build/src/define_config.js +0 -42
  60. package/build/src/drivers/cookie.js +0 -60
  61. package/build/src/drivers/file.js +0 -133
  62. package/build/src/drivers/memory.js +0 -33
  63. package/build/src/drivers/redis.js +0 -70
  64. package/build/src/drivers_collection.d.ts +0 -22
  65. package/build/src/drivers_collection.js +0 -38
  66. package/build/src/edge_plugin_adonisjs_session.js +0 -92
  67. package/build/src/errors.js +0 -17
  68. package/build/src/helpers.d.ts +0 -6
  69. package/build/src/helpers.js +0 -43
  70. package/build/src/session.js +0 -384
  71. package/build/src/store.js +0 -159
  72. package/build/src/types/extended.d.ts +0 -19
  73. package/build/src/types/extended.js +0 -9
  74. package/build/src/types/main.js +0 -9
  75. package/build/stubs/config.stub +0 -49
  76. package/build/stubs/main.js +0 -10
@@ -0,0 +1,100 @@
1
+ import {
2
+ SessionClient
3
+ } from "../../../chunk-S6P3TBEK.js";
4
+ import "../../../chunk-TE5JP3SX.js";
5
+ import "../../../chunk-WBAYBMJJ.js";
6
+
7
+ // src/plugins/japa/api_client.ts
8
+ import lodash from "@poppinss/utils/lodash";
9
+ import { configProvider } from "@adonisjs/core";
10
+ import { RuntimeException } from "@poppinss/utils";
11
+ import { ApiClient, ApiRequest, ApiResponse } from "@japa/api-client";
12
+ var sessionApiClient = (app) => {
13
+ const pluginFn = async function() {
14
+ const sessionConfigProvider = app.config.get("session", {});
15
+ const config = await configProvider.resolve(app, sessionConfigProvider);
16
+ if (!config) {
17
+ throw new RuntimeException(
18
+ 'Invalid "config/session.ts" file. Make sure you are using the "defineConfig" method'
19
+ );
20
+ }
21
+ ApiRequest.getter(
22
+ "sessionClient",
23
+ function() {
24
+ return new SessionClient(config.stores.memory());
25
+ },
26
+ true
27
+ );
28
+ ApiRequest.macro("withSession", function(data) {
29
+ this.sessionClient.merge(data);
30
+ return this;
31
+ });
32
+ ApiRequest.macro("withFlashMessages", function(data) {
33
+ this.sessionClient.flash(data);
34
+ return this;
35
+ });
36
+ ApiResponse.macro("session", function(key) {
37
+ return key ? lodash.get(this.sessionBag.values, key) : this.sessionBag.values;
38
+ });
39
+ ApiResponse.macro("flashMessages", function() {
40
+ return this.sessionBag.flashMessages;
41
+ });
42
+ ApiResponse.macro("flashMessage", function(key) {
43
+ return lodash.get(this.sessionBag.flashMessages, key);
44
+ });
45
+ ApiResponse.macro("assertSession", function(key, value) {
46
+ this.assert.property(this.session(), key);
47
+ if (value !== void 0) {
48
+ this.assert.deepEqual(this.session(key), value);
49
+ }
50
+ });
51
+ ApiResponse.macro("assertSessionMissing", function(key) {
52
+ this.assert.notProperty(this.session(), key);
53
+ });
54
+ ApiResponse.macro("assertFlashMessage", function(key, value) {
55
+ this.assert.property(this.flashMessages(), key);
56
+ if (value !== void 0) {
57
+ this.assert.deepEqual(this.flashMessage(key), value);
58
+ }
59
+ });
60
+ ApiResponse.macro("assertFlashMissing", function(key) {
61
+ this.assert.notProperty(this.flashMessages(), key);
62
+ });
63
+ ApiResponse.macro("assertHasValidationError", function(field) {
64
+ this.assert.property(this.flashMessage("errors"), field);
65
+ });
66
+ ApiResponse.macro("assertDoesNotHaveValidationError", function(field) {
67
+ this.assert.notProperty(this.flashMessage("errors"), field);
68
+ });
69
+ ApiResponse.macro("assertValidationError", function(field, message) {
70
+ this.assert.include(this.flashMessage("errors")?.[field] || [], message);
71
+ });
72
+ ApiResponse.macro("assertValidationErrors", function(field, messages) {
73
+ this.assert.deepEqual(this.flashMessage("errors")?.[field] || [], messages);
74
+ });
75
+ ApiClient.onRequest((request) => {
76
+ request.setup(async () => {
77
+ request.withCookie(config.cookieName, request.sessionClient.sessionId);
78
+ await request.sessionClient.commit();
79
+ return async (error) => {
80
+ if (error) {
81
+ await request.sessionClient.destroy();
82
+ }
83
+ };
84
+ });
85
+ request.teardown(async (response) => {
86
+ const sessionId = response.cookie(config.cookieName);
87
+ response.sessionBag = sessionId ? await response.request.sessionClient.load(sessionId.value) : {
88
+ values: {},
89
+ flashMessages: {}
90
+ };
91
+ await request.sessionClient.destroy(sessionId?.value);
92
+ });
93
+ });
94
+ };
95
+ return pluginFn;
96
+ };
97
+ export {
98
+ sessionApiClient
99
+ };
100
+ //# sourceMappingURL=api_client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/plugins/japa/api_client.ts"],"sourcesContent":["/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport lodash from '@poppinss/utils/lodash'\nimport { configProvider } from '@adonisjs/core'\nimport { RuntimeException } from '@poppinss/utils'\nimport type { PluginFn } from '@japa/runner/types'\nimport type { ApplicationService } from '@adonisjs/core/types'\nimport { ApiClient, ApiRequest, ApiResponse } from '@japa/api-client'\n\nimport { SessionClient } from '../../client.js'\nimport type { SessionData } from '../../types.js'\n\ndeclare module '@japa/api-client' {\n export interface ApiRequest {\n sessionClient: SessionClient\n\n /**\n * Make HTTP request along with the provided session data\n */\n withSession(values: SessionData): this\n\n /**\n * Make HTTP request along with the provided session flash\n * messages.\n */\n withFlashMessages(values: SessionData): this\n }\n\n export interface ApiResponse {\n sessionBag: {\n values: SessionData\n flashMessages: SessionData\n }\n\n /**\n * Get session data from the HTTP response\n */\n session(key?: string): any\n\n /**\n * Get flash messages from the HTTP response\n */\n flashMessages(): SessionData\n\n /**\n * Get flash messages for a specific key from the HTTP response\n */\n flashMessage(key: string): SessionData\n\n /**\n * Assert session key-value pair exists\n */\n assertSession(key: string, value?: any): void\n\n /**\n * Assert key is missing in session store\n */\n assertSessionMissing(key: string): void\n\n /**\n * Assert flash message key-value pair exists\n */\n assertFlashMessage(key: string, value?: any): void\n\n /**\n * Assert key is missing flash messages store\n */\n assertFlashMissing(key: string): void\n\n /**\n * Assert flash messages has validation errors for\n * the given field\n */\n assertHasValidationError(field: string): void\n\n /**\n * Assert flash messages does not have validation errors\n * for the given field\n */\n assertDoesNotHaveValidationError(field: string): void\n\n /**\n * Assert error message for a given field\n */\n assertValidationError(field: string, message: string): void\n\n /**\n * Assert all error messages for a given field\n */\n assertValidationErrors(field: string, messages: string[]): void\n }\n}\n\n/**\n * Hooks AdonisJS Session with the Japa API client\n * plugin\n */\nexport const sessionApiClient = (app: ApplicationService) => {\n const pluginFn: PluginFn = async function () {\n const sessionConfigProvider = app.config.get('session', {})\n\n /**\n * Resolve config from the provider\n */\n const config = await configProvider.resolve<any>(app, sessionConfigProvider)\n if (!config) {\n throw new RuntimeException(\n 'Invalid \"config/session.ts\" file. Make sure you are using the \"defineConfig\" method'\n )\n }\n\n /**\n * Stick an singleton session client to APIRequest. The session\n * client is used to keep a track of session data we have\n * to send during the request.\n */\n ApiRequest.getter(\n 'sessionClient',\n function () {\n return new SessionClient(config.stores.memory())\n },\n true\n )\n\n /**\n * Define session data\n */\n ApiRequest.macro('withSession', function (this: ApiRequest, data) {\n this.sessionClient.merge(data)\n return this\n })\n\n /**\n * Define flash messages\n */\n ApiRequest.macro('withFlashMessages', function (this: ApiRequest, data) {\n this.sessionClient.flash(data)\n return this\n })\n\n /**\n * Get session data\n */\n ApiResponse.macro('session', function (this: ApiResponse, key) {\n return key ? lodash.get(this.sessionBag.values, key) : this.sessionBag.values\n })\n\n /**\n * Get flash messages\n */\n ApiResponse.macro('flashMessages', function (this: ApiResponse) {\n return this.sessionBag.flashMessages\n })\n ApiResponse.macro('flashMessage', function (this: ApiResponse, key) {\n return lodash.get(this.sessionBag.flashMessages, key)\n })\n\n /**\n * Response session assertions\n */\n ApiResponse.macro('assertSession', function (this: ApiResponse, key, value) {\n this.assert!.property(this.session(), key)\n if (value !== undefined) {\n this.assert!.deepEqual(this.session(key), value)\n }\n })\n ApiResponse.macro('assertSessionMissing', function (this: ApiResponse, key) {\n this.assert!.notProperty(this.session(), key)\n })\n ApiResponse.macro('assertFlashMessage', function (this: ApiResponse, key, value) {\n this.assert!.property(this.flashMessages(), key)\n if (value !== undefined) {\n this.assert!.deepEqual(this.flashMessage(key), value)\n }\n })\n ApiResponse.macro('assertFlashMissing', function (this: ApiResponse, key) {\n this.assert!.notProperty(this.flashMessages(), key)\n })\n ApiResponse.macro('assertHasValidationError', function (this: ApiResponse, field) {\n this.assert!.property(this.flashMessage('errors'), field)\n })\n ApiResponse.macro('assertDoesNotHaveValidationError', function (this: ApiResponse, field) {\n this.assert!.notProperty(this.flashMessage('errors'), field)\n })\n ApiResponse.macro('assertValidationError', function (this: ApiResponse, field, message) {\n this.assert!.include(this.flashMessage('errors')?.[field] || [], message)\n })\n ApiResponse.macro('assertValidationErrors', function (this: ApiResponse, field, messages) {\n this.assert!.deepEqual(this.flashMessage('errors')?.[field] || [], messages)\n })\n\n /**\n * We define the hook using the \"request.setup\" method because we\n * want to allow other Japa hooks to mutate the session store\n * without running into race conditions\n */\n ApiClient.onRequest((request) => {\n request.setup(async () => {\n /**\n * Set cookie\n */\n request.withCookie(config.cookieName, request.sessionClient.sessionId)\n\n /**\n * Persist data\n */\n await request.sessionClient.commit()\n\n /**\n * Cleanup if request fails\n */\n return async (error: any) => {\n if (error) {\n await request.sessionClient.destroy()\n }\n }\n })\n\n request.teardown(async (response) => {\n const sessionId = response.cookie(config.cookieName)\n\n /**\n * Reading session data from the response cookie\n */\n response.sessionBag = sessionId\n ? await response.request.sessionClient.load(sessionId.value)\n : {\n values: {},\n flashMessages: {},\n }\n\n /**\n * Cleanup state\n */\n await request.sessionClient.destroy(sessionId?.value)\n })\n })\n }\n\n return pluginFn\n}\n"],"mappings":";;;;;;;AASA,OAAO,YAAY;AACnB,SAAS,sBAAsB;AAC/B,SAAS,wBAAwB;AAGjC,SAAS,WAAW,YAAY,mBAAmB;AA0F5C,IAAM,mBAAmB,CAAC,QAA4B;AAC3D,QAAM,WAAqB,iBAAkB;AAC3C,UAAM,wBAAwB,IAAI,OAAO,IAAI,WAAW,CAAC,CAAC;AAK1D,UAAM,SAAS,MAAM,eAAe,QAAa,KAAK,qBAAqB;AAC3E,QAAI,CAAC,QAAQ;AACX,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAOA,eAAW;AAAA,MACT;AAAA,MACA,WAAY;AACV,eAAO,IAAI,cAAc,OAAO,OAAO,OAAO,CAAC;AAAA,MACjD;AAAA,MACA;AAAA,IACF;AAKA,eAAW,MAAM,eAAe,SAA4B,MAAM;AAChE,WAAK,cAAc,MAAM,IAAI;AAC7B,aAAO;AAAA,IACT,CAAC;AAKD,eAAW,MAAM,qBAAqB,SAA4B,MAAM;AACtE,WAAK,cAAc,MAAM,IAAI;AAC7B,aAAO;AAAA,IACT,CAAC;AAKD,gBAAY,MAAM,WAAW,SAA6B,KAAK;AAC7D,aAAO,MAAM,OAAO,IAAI,KAAK,WAAW,QAAQ,GAAG,IAAI,KAAK,WAAW;AAAA,IACzE,CAAC;AAKD,gBAAY,MAAM,iBAAiB,WAA6B;AAC9D,aAAO,KAAK,WAAW;AAAA,IACzB,CAAC;AACD,gBAAY,MAAM,gBAAgB,SAA6B,KAAK;AAClE,aA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CAAC,YAAY;AAC/B,cAAQ,MAAM,YAAY;AAIxB,gBAAQ,WAAW,OAAO,YAAY,QAAQ,cAAc,SAAS;AAKrE,cAAM,QAAQ,cAAc,OAAO;AAKnC,eAAO,OAAO,UAAe;AAC3B,cAAI,OAAO;AACT,kBAAM,QAAQ,cAAc,QAAQ;AAAA,UACtC;AAAA,QACF;AAAA,MACF,CAAC;AAED,cAAQ,SAAS,OAAO,aAAa;AACnC,cAAM,YAAY,SAAS,OAAO,OAAO,UAAU;AAKnD,iBAAS,aAAa,YAClB,MAAM,SAAS,QAAQ,cAAc,KAAK,UAAU,KAAK,IACzD;AAAA,UACE,QAAQ,CAAC;AAAA,UACT,eAAe,CAAC;AAAA,QAClB;AAKJ,cAAM,QAAQ,cAAc,QAAQ,WAAW,KAAK;AAAA,MACtD,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
@@ -0,0 +1,36 @@
1
+ import type { PluginFn } from '@japa/runner/types';
2
+ import type { ApplicationService } from '@adonisjs/core/types';
3
+ import type { CookieOptions as AdonisCookieOptions } from '@adonisjs/core/types/http';
4
+ import { SessionClient } from '../../client.js';
5
+ import type { SessionData } from '../../types.js';
6
+ declare module 'playwright' {
7
+ interface BrowserContext {
8
+ sessionClient: SessionClient;
9
+ /**
10
+ * Initiate session. The session id cookie will be defined
11
+ * if missing
12
+ */
13
+ initiateSession(options?: Partial<AdonisCookieOptions>): Promise<void>;
14
+ /**
15
+ * Returns data from the session store
16
+ */
17
+ getSession(): Promise<any>;
18
+ /**
19
+ * Returns data from the session store
20
+ */
21
+ getFlashMessages(): Promise<any>;
22
+ /**
23
+ * Set session data
24
+ */
25
+ setSession(values: SessionData): Promise<void>;
26
+ /**
27
+ * Set flash messages
28
+ */
29
+ setFlashMessages(values: SessionData): Promise<void>;
30
+ }
31
+ }
32
+ /**
33
+ * Hooks AdonisJS Session with the Japa browser client
34
+ * plugin
35
+ */
36
+ export declare const sessionBrowserClient: (app: ApplicationService) => PluginFn;
@@ -0,0 +1,88 @@
1
+ import {
2
+ SessionClient
3
+ } from "../../../chunk-S6P3TBEK.js";
4
+ import "../../../chunk-TE5JP3SX.js";
5
+ import "../../../chunk-WBAYBMJJ.js";
6
+
7
+ // src/plugins/japa/browser_client.ts
8
+ import { configProvider } from "@adonisjs/core";
9
+ import { RuntimeException } from "@poppinss/utils";
10
+ import { decoratorsCollection } from "@japa/browser-client";
11
+ function transformSameSiteOption(sameSite) {
12
+ if (!sameSite) {
13
+ return;
14
+ }
15
+ if (sameSite === true || sameSite === "strict") {
16
+ return "Strict";
17
+ }
18
+ if (sameSite === "lax") {
19
+ return "Lax";
20
+ }
21
+ if (sameSite === "none") {
22
+ return "None";
23
+ }
24
+ }
25
+ function getSessionCookieOptions(config, cookieOptions) {
26
+ const options = { ...config.cookie, ...cookieOptions };
27
+ return {
28
+ ...options,
29
+ expires: void 0,
30
+ sameSite: transformSameSiteOption(options.sameSite)
31
+ };
32
+ }
33
+ var sessionBrowserClient = (app) => {
34
+ const pluginFn = async function() {
35
+ const sessionConfigProvider = app.config.get("session", {});
36
+ const config = await configProvider.resolve(app, sessionConfigProvider);
37
+ if (!config) {
38
+ throw new RuntimeException(
39
+ 'Invalid "config/session.ts" file. Make sure you are using the "defineConfig" method'
40
+ );
41
+ }
42
+ decoratorsCollection.register({
43
+ context(context) {
44
+ context.sessionClient = new SessionClient(config.stores.memory());
45
+ context.initiateSession = async function(options) {
46
+ const sessionId = await context.getCookie(config.cookieName);
47
+ if (sessionId) {
48
+ context.sessionClient.sessionId = sessionId;
49
+ return;
50
+ }
51
+ await context.setCookie(
52
+ config.cookieName,
53
+ context.sessionClient.sessionId,
54
+ getSessionCookieOptions(config, options)
55
+ );
56
+ };
57
+ context.getSession = async function() {
58
+ await context.initiateSession();
59
+ const sessionData = await context.sessionClient.load();
60
+ return sessionData.values;
61
+ };
62
+ context.getFlashMessages = async function() {
63
+ await context.initiateSession();
64
+ const sessionData = await context.sessionClient.load();
65
+ return sessionData.flashMessages;
66
+ };
67
+ context.setSession = async function(values) {
68
+ await context.initiateSession();
69
+ context.sessionClient.merge(values);
70
+ await context.sessionClient.commit();
71
+ };
72
+ context.setFlashMessages = async function(values) {
73
+ await context.initiateSession();
74
+ context.sessionClient.flash(values);
75
+ await context.sessionClient.commit();
76
+ };
77
+ context.on("close", async function() {
78
+ await context.sessionClient.destroy();
79
+ });
80
+ }
81
+ });
82
+ };
83
+ return pluginFn;
84
+ };
85
+ export {
86
+ sessionBrowserClient
87
+ };
88
+ //# sourceMappingURL=browser_client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../../src/plugins/japa/browser_client.ts"],"sourcesContent":["/*\n * @adonisjs/session\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport { configProvider } from '@adonisjs/core'\nimport { RuntimeException } from '@poppinss/utils'\nimport type { PluginFn } from '@japa/runner/types'\nimport { decoratorsCollection } from '@japa/browser-client'\nimport type { ApplicationService } from '@adonisjs/core/types'\nimport type { CookieOptions as AdonisCookieOptions } from '@adonisjs/core/types/http'\n\nimport { SessionClient } from '../../client.js'\nimport type { SessionConfig, SessionData } from '../../types.js'\n\ndeclare module 'playwright' {\n export interface BrowserContext {\n sessionClient: SessionClient\n\n /**\n * Initiate session. The session id cookie will be defined\n * if missing\n */\n initiateSession(options?: Partial<AdonisCookieOptions>): Promise<void>\n\n /**\n * Returns data from the session store\n */\n getSession(): Promise<any>\n\n /**\n * Returns data from the session store\n */\n getFlashMessages(): Promise<any>\n\n /**\n * Set session data\n */\n setSession(values: SessionData): Promise<void>\n\n /**\n * Set flash messages\n */\n setFlashMessages(values: SessionData): Promise<void>\n }\n}\n\n/**\n * Transforming AdonisJS same site option to playwright\n * same site option.\n */\nfunction transformSameSiteOption(sameSite?: AdonisCookieOptions['sameSite']) {\n if (!sameSite) {\n return\n }\n\n if (sameSite === true || sameSite === 'strict') {\n return 'Strict' as const\n }\n\n if (sameSite === 'lax') {\n return 'Lax' as const\n }\n\n if (sameSite === 'none') {\n return 'None' as const\n }\n}\n\n/**\n * Transforming AdonisJS session config to playwright cookie options.\n */\nfunction getSessionCookieOptions(\n config: SessionConfig,\n cookieOptions?: Partial<AdonisCookieOptions>\n) {\n const options = { ...config.cookie, ...cookieOptions }\n return {\n ...options,\n expires: undefined,\n sameSite: transformSameSiteOption(options.sameSite),\n }\n}\n\n/**\n * Hooks AdonisJS Session with the Japa browser client\n * plugin\n */\nexport const sessionBrowserClient = (app: ApplicationService) => {\n const pluginFn: PluginFn = async function () {\n const sessionConfigProvider = app.config.get('session', {})\n\n /**\n * Resolve config from the provider\n */\n const config = await configProvider.resolve<any>(app, sessionConfigProvider)\n if (!config) {\n throw new RuntimeException(\n 'Invalid \"config/session.ts\" file. Make sure you are using the \"defineConfig\" method'\n )\n }\n\n decoratorsCollection.register({\n context(context) {\n /**\n * Reference to session client per browser context\n */\n context.sessionClient = new SessionClient(config.stores.memory())\n\n /**\n * Initiating session store\n */\n context.initiateSession = async function (options) {\n const sessionId = await context.getCookie(config.cookieName)\n if (sessionId) {\n context.sessionClient.sessionId = sessionId\n return\n }\n\n await context.setCookie(\n config.cookieName,\n context.sessionClient.sessionId,\n getSessionCookieOptions(config, options)\n )\n }\n\n /**\n * Returns session data\n */\n context.getSession = async function () {\n await context.initiateSession()\n const sessionData = await context.sessionClient.load()\n return sessionData.values\n }\n\n /**\n * Returns flash messages from the data store\n */\n context.getFlashMessages = async function () {\n await context.initiateSession()\n const sessionData = await context.sessionClient.load()\n return sessionData.flashMessages\n }\n\n /**\n * Set session data\n */\n context.setSession = async function (values) {\n await context.initiateSession()\n context.sessionClient.merge(values)\n await context.sessionClient.commit()\n }\n\n /**\n * Set flash messages\n */\n context.setFlashMessages = async function (values) {\n await context.initiateSession()\n context.sessionClient.flash(values)\n await context.sessionClient.commit()\n }\n\n /**\n * Destroy session when context is closed\n */\n context.on('close', async function () {\n await context.sessionClient.destroy()\n })\n },\n })\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Ac,KAAK;AACrD,iBAAO,YAAY;AAAA,QACrB;AAKA,gBAAQ,mBAAmB,iBAAkB;AAC3C,gBAAM,QAAQ,gBAAgB;AAC9B,gBAAM,cAAc,MAAM,QAAQ,cAAc,KAAK;AACrD,iBAAO,YAAY;AAAA,QACrB;AAKA,gBAAQ,aAAa,eAAgB,QAAQ;AAC3C,gBAAM,QAAQ,gBAAgB;AAC9B,kBAAQ,cAAc,MAAM,MAAM;AAClC,gBAAM,QAAQ,cAAc,OAAO;AAAA,QACrC;AAKA,gBAAQ,mBAAmB,eAAgB,QAAQ;AACjD,gBAAM,QAAQ,gBAAgB;AAC9B,kBAAQ,cAAc,MAAM,MAAM;AAClC,gBAAM,QAAQ,cAAc,OAAO;AAAA,QACrC;AAKA,gBAAQ,GAAG,SAAS,iBAAkB;AACpC,gBAAM,QAAQ,cAAc,QAAQ;AAAA,QACtC,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
@@ -1,8 +1,8 @@
1
- import { EmitterService } from '@adonisjs/core/types';
2
1
  import type { HttpContext } from '@adonisjs/core/http';
3
- import { Store } from './store.js';
4
- import type { SessionData, SessionConfig, AllowedSessionValues, SessionDriverContract } from './types/main.js';
5
- import { HttpError } from '@adonisjs/core/types/http';
2
+ import type { EmitterService } from '@adonisjs/core/types';
3
+ import type { HttpError } from '@adonisjs/core/types/http';
4
+ import { ValuesStore } from './values_store.js';
5
+ import type { SessionData, SessionConfig, SessionStoreFactory, AllowedSessionValues } from './types.js';
6
6
  /**
7
7
  * The session class exposes the API to read and write values to
8
8
  * the session store.
@@ -16,11 +16,11 @@ export declare class Session {
16
16
  * Store of flash messages that be written during the
17
17
  * HTTP request
18
18
  */
19
- responseFlashMessages: Store;
19
+ responseFlashMessages: ValuesStore;
20
20
  /**
21
21
  * Store of flash messages for the current HTTP request.
22
22
  */
23
- flashMessages: Store;
23
+ flashMessages: ValuesStore;
24
24
  /**
25
25
  * The key to use for storing flash messages inside
26
26
  * the session store.
@@ -58,7 +58,7 @@ export declare class Session {
58
58
  * modified
59
59
  */
60
60
  get hasBeenModified(): boolean;
61
- constructor(config: SessionConfig, driver: SessionDriverContract, emitter: EmitterService, ctx: HttpContext);
61
+ constructor(config: SessionConfig, storeFactory: SessionStoreFactory, emitter: EmitterService, ctx: HttpContext);
62
62
  /**
63
63
  * Initiates the session store. The method results in a noop
64
64
  * when called multiple times
@@ -111,16 +111,25 @@ export declare class Session {
111
111
  * Empty the session store
112
112
  */
113
113
  clear(): void;
114
- /**
115
- * Flash validation error messages. Make sure the error
116
- * is an instance of VineJS ValidationException
117
- */
118
- flashValidationErrors(error: HttpError): void;
119
114
  /**
120
115
  * Add a key-value pair to flash messages
121
116
  */
122
117
  flash(key: string, value: AllowedSessionValues): void;
123
118
  flash(keyValue: SessionData): void;
119
+ /**
120
+ * Flash errors to the errorsBag. You can read these
121
+ * errors via the "@error" tag.
122
+ *
123
+ * Appends new messages to the existing collection.
124
+ */
125
+ flashErrors(errorsCollection: Record<string, string | string[]>): void;
126
+ /**
127
+ * Flash validation error messages. Make sure the error
128
+ * is an instance of VineJS ValidationException.
129
+ *
130
+ * Overrides existing inputErrors
131
+ */
132
+ flashValidationErrors(error: HttpError): void;
124
133
  /**
125
134
  * Flash form input data to the flash messages store
126
135
  */
@@ -2,11 +2,11 @@ import { EmitterService } from '@adonisjs/core/types';
2
2
  import type { NextFn } from '@adonisjs/core/types/http';
3
3
  import { HttpContext } from '@adonisjs/core/http';
4
4
  import { Session } from './session.js';
5
- import type { SessionConfig } from './types/main.js';
5
+ import type { SessionConfig, SessionStoreFactory } from './types.js';
6
6
  /**
7
7
  * HttpContext augmentations
8
8
  */
9
- declare module '@adonisjs/http-server' {
9
+ declare module '@adonisjs/core/http' {
10
10
  interface HttpContext {
11
11
  session: Session;
12
12
  }
@@ -15,8 +15,11 @@ declare module '@adonisjs/http-server' {
15
15
  * Session middleware is used to initiate the session store
16
16
  * and commit its values during an HTTP request
17
17
  */
18
- export default class SessionMiddleware {
18
+ export default class SessionMiddleware<KnownStores extends Record<string, SessionStoreFactory>> {
19
19
  #private;
20
- constructor(config: SessionConfig, emitter: EmitterService);
20
+ constructor(config: SessionConfig & {
21
+ store: keyof KnownStores;
22
+ stores: KnownStores;
23
+ }, emitter: EmitterService);
21
24
  handle(ctx: HttpContext, next: NextFn): Promise<any>;
22
25
  }
@@ -1,59 +1,10 @@
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
- */
29
- export default class SessionMiddleware {
30
- #config;
31
- #emitter;
32
- constructor(config, emitter) {
33
- this.#config = config;
34
- this.#emitter = emitter;
35
- }
36
- async handle(ctx, next) {
37
- if (!this.#config.enabled) {
38
- return next();
39
- }
40
- const driver = sessionDriversList.create(this.#config.driver, this.#config, ctx);
41
- ctx.session = new Session(this.#config, driver, this.#emitter, ctx);
42
- /**
43
- * Initiate session store
44
- */
45
- await ctx.session.initiate(false);
46
- /**
47
- * Call next middlewares or route handler
48
- */
49
- const response = await next();
50
- /**
51
- * Commit store mutations
52
- */
53
- await ctx.session.commit();
54
- /**
55
- * Return response
56
- */
57
- return response;
58
- }
59
- }
1
+ import {
2
+ SessionMiddleware
3
+ } from "../chunk-K4OSGJVW.js";
4
+ import "../chunk-2X5L327N.js";
5
+ import "../chunk-TE5JP3SX.js";
6
+ import "../chunk-WBAYBMJJ.js";
7
+ export {
8
+ SessionMiddleware as default
9
+ };
10
+ //# sourceMappingURL=session_middleware.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,11 +1,11 @@
1
1
  import type { HttpContext } from '@adonisjs/core/http';
2
- import { CookieOptions } from '@adonisjs/core/types/http';
3
- import type { SessionData, SessionDriverContract } from '../types/main.js';
2
+ import type { CookieOptions } from '@adonisjs/core/types/http';
3
+ import type { SessionData, SessionStoreContract } from '../types.js';
4
4
  /**
5
- * Cookie driver stores the session data inside an encrypted
5
+ * Cookie store stores the session data inside an encrypted
6
6
  * cookie.
7
7
  */
8
- export declare class CookieDriver implements SessionDriverContract {
8
+ export declare class CookieStore implements SessionStoreContract {
9
9
  #private;
10
10
  constructor(config: Partial<CookieOptions>, ctx: HttpContext);
11
11
  /**
@@ -6,14 +6,14 @@
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 { FileDriverConfig, SessionData, SessionDriverContract } from '../types/main.js';
9
+ import type { FileStoreConfig, SessionData, SessionStoreContract } from '../types.js';
10
10
  /**
11
- * File driver writes the session data on the file system as. Each session
11
+ * File store writes the session data on the file system as. Each session
12
12
  * id gets its own file.
13
13
  */
14
- export declare class FileDriver implements SessionDriverContract {
14
+ export declare class FileStore implements SessionStoreContract {
15
15
  #private;
16
- constructor(config: FileDriverConfig, age: string | number);
16
+ constructor(config: FileStoreConfig, age: string | number);
17
17
  /**
18
18
  * Reads the session data from the disk.
19
19
  */
@@ -6,11 +6,11 @@
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, SessionDriverContract } from '../types/main.js';
9
+ import type { SessionData, SessionStoreContract } from '../types.js';
10
10
  /**
11
- * Memory driver is meant to be used for writing tests.
11
+ * Memory store is meant to be used for writing tests.
12
12
  */
13
- export declare class MemoryDriver implements SessionDriverContract {
13
+ export declare class MemoryStore implements SessionStoreContract {
14
14
  static sessions: Map<string, SessionData>;
15
15
  /**
16
16
  * Read session id value from the memory
@@ -6,14 +6,14 @@
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 { RedisService } from '@adonisjs/redis/types';
10
- import type { SessionDriverContract, RedisDriverConfig, SessionData } from '../types/main.js';
9
+ import type { Connection } from '@adonisjs/redis/types';
10
+ import type { SessionStoreContract, SessionData } from '../types.js';
11
11
  /**
12
- * File driver to read/write session to filesystem
12
+ * File store to read/write session to filesystem
13
13
  */
14
- export declare class RedisDriver implements SessionDriverContract {
14
+ export declare class RedisStore implements SessionStoreContract {
15
15
  #private;
16
- constructor(redis: RedisService, config: RedisDriverConfig, age: string | number);
16
+ constructor(connection: Connection, age: string | number);
17
17
  /**
18
18
  * Returns file contents. A new file will be created if it's
19
19
  * missing.
@@ -1,19 +1,15 @@
1
- import type { HttpContext } from '@adonisjs/core/http';
1
+ import { HttpContext } from '@adonisjs/core/http';
2
2
  import { RedisConnections } from '@adonisjs/redis/types';
3
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
4
  /**
9
5
  * The values allowed by the `session.put` method
10
6
  */
11
7
  export type AllowedSessionValues = string | boolean | number | object | Date | Array<any>;
12
8
  export type SessionData = Record<string, AllowedSessionValues>;
13
9
  /**
14
- * Session drivers must implement the session driver contract.
10
+ * Session stores must implement the session store contract.
15
11
  */
16
- export interface SessionDriverContract {
12
+ export interface SessionStoreContract {
17
13
  /**
18
14
  * The read method is used to read the data from the persistence
19
15
  * store and return it back as an object
@@ -36,17 +32,14 @@ export interface SessionDriverContract {
36
32
  touch(sessionId: string): Promise<void> | void;
37
33
  }
38
34
  /**
39
- * Shape of session config.
35
+ * Base configuration for managing sessions without
36
+ * stores.
40
37
  */
41
38
  export interface SessionConfig {
42
39
  /**
43
40
  * Enable/disable sessions temporarily
44
41
  */
45
42
  enabled: boolean;
46
- /**
47
- * The drivers to use
48
- */
49
- driver: keyof SessionDriversList;
50
43
  /**
51
44
  * The name of the cookie for storing the session id.
52
45
  */
@@ -76,31 +69,18 @@ export interface SessionConfig {
76
69
  cookie: Omit<Partial<CookieOptions>, 'maxAge' | 'expires'>;
77
70
  }
78
71
  /**
79
- * Configuration used by the file driver.
72
+ * Configuration used by the file store.
80
73
  */
81
- export type FileDriverConfig = {
74
+ export type FileStoreConfig = {
82
75
  location: string;
83
76
  };
84
77
  /**
85
- * Configuration used by the redis driver.
78
+ * Configuration used by the redis store.
86
79
  */
87
- export type RedisDriverConfig = {
80
+ export type RedisStoreConfig = {
88
81
  connection: keyof RedisConnections;
89
82
  };
90
83
  /**
91
- * Extending session config with the drivers config
84
+ * Factory function to instantiate session store
92
85
  */
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
- }
86
+ export type SessionStoreFactory = (ctx: HttpContext, sessionConfig: SessionConfig) => SessionStoreContract;
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,8 +1,8 @@
1
- import type { AllowedSessionValues, SessionData } from './types/main.js';
1
+ import type { AllowedSessionValues, SessionData } from './types.js';
2
2
  /**
3
3
  * Readonly session store
4
4
  */
5
- export declare class ReadOnlyStore {
5
+ export declare class ReadOnlyValuesStore {
6
6
  /**
7
7
  * Underlying store values
8
8
  */
@@ -15,12 +15,12 @@ export declare class ReadOnlyStore {
15
15
  /**
16
16
  * Get value for a given key
17
17
  */
18
- get(key: string, defaultValue?: any): any;
18
+ get(key: string | string[], defaultValue?: any): any;
19
19
  /**
20
20
  * A boolean to know if value exists. Extra guards to check
21
21
  * arrays for it's length as well.
22
22
  */
23
- has(key: string, checkForArraysLength?: boolean): boolean;
23
+ has(key: string | string[], checkForArraysLength?: boolean): boolean;
24
24
  /**
25
25
  * Get all values
26
26
  */
@@ -42,7 +42,7 @@ export declare class ReadOnlyStore {
42
42
  * Session store encapsulates the session data and offers a
43
43
  * declarative API to mutate it.
44
44
  */
45
- export declare class Store extends ReadOnlyStore {
45
+ export declare class ValuesStore extends ReadOnlyValuesStore {
46
46
  #private;
47
47
  constructor(values: SessionData | null);
48
48
  /**
@@ -52,26 +52,26 @@ export declare class Store extends ReadOnlyStore {
52
52
  /**
53
53
  * Set key/value pair
54
54
  */
55
- set(key: string, value: AllowedSessionValues): void;
55
+ set(key: string | string[], value: AllowedSessionValues): void;
56
56
  /**
57
57
  * Remove key
58
58
  */
59
- unset(key: string): void;
59
+ unset(key: string | string[]): void;
60
60
  /**
61
61
  * Pull value from the store. It is same as calling
62
62
  * store.get and then store.unset
63
63
  */
64
- pull(key: string, defaultValue?: any): any;
64
+ pull(key: string | string[], defaultValue?: any): any;
65
65
  /**
66
66
  * Increment number. The method raises an error when
67
67
  * nderlying value is not a number
68
68
  */
69
- increment(key: string, steps?: number): void;
69
+ increment(key: string | string[], steps?: number): void;
70
70
  /**
71
71
  * Increment number. The method raises an error when
72
72
  * nderlying value is not a number
73
73
  */
74
- decrement(key: string, steps?: number): void;
74
+ decrement(key: string | string[], steps?: number): void;
75
75
  /**
76
76
  * Overwrite existing store data with new values.
77
77
  */