@alwatr/session-storage 1.0.0 โ 2.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.
- package/dist/main.js +5 -0
- package/dist/main.js.map +11 -0
- package/package.json +20 -23
- package/src/facade.ts +25 -0
- package/src/main.ts +3 -0
- package/src/session-storage.provider.ts +154 -0
- package/src/type.ts +12 -0
- package/CHANGELOG.md +0 -23
- package/dist/main.cjs +0 -4
- package/dist/main.cjs.map +0 -7
- package/dist/main.mjs +0 -4
- package/dist/main.mjs.map +0 -7
package/dist/main.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/* ๐ฆ @alwatr/session-storage v2.0.0 */
|
|
2
|
+
import{createLogger as B}from"@alwatr/logger";class z{static version="2.0.0";key__;logger_;constructor(x){this.key__=x.name,this.logger_=B(`session-storage-provider: ${this.key__}`),this.logger_.logMethodArgs?.("constructor",x)}static has(x){return sessionStorage.getItem(x)!==null}has(){return sessionStorage.getItem(this.key__)!==null}read(){let x=null;try{x=sessionStorage.getItem(this.key__)}catch(b){this.logger_.error("read","read_session_storage_error",{err:b})}if(!x)return this.logger_.logMethod?.("read//no_value"),null;try{let b=JSON.parse(x);return this.logger_.logMethodFull?.("read//value",void 0,{parsed:b}),b}catch(b){return this.logger_.error("read","read_parse_error",{err:b}),null}}write(x){this.logger_.logMethodArgs?.("write",{value:x});let b;try{b=JSON.stringify(x)}catch(j){throw this.logger_.error("write","write_stringify_error",{err:j}),Error("write_stringify_error")}try{sessionStorage.setItem(this.key__,b)}catch(j){this.logger_.error("write","write_session_storage_error",{err:j})}}remove(){this.logger_.logMethod?.("remove"),sessionStorage.removeItem(this.key__)}}function I(x){return new z(x)}export{I as createSessionStorageProvider,z as SessionStorageProvider};
|
|
3
|
+
|
|
4
|
+
//# debugId=CF722D8BCB54E18D64756E2164756E21
|
|
5
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/session-storage.provider.ts", "../src/facade.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { createLogger } from '@alwatr/logger';\n\nimport type { SessionStorageProviderConfig } from './type.js';\n\n/**\n * A provider class for managing a specific item in sessionStorage.\n * It encapsulates the logic for key generation and serialization.\n *\n * @example\n * ```typescript\n * const formDraft = new SessionStorageProvider<{title: string; body: string}>({\n * name: 'post-form-draft',\n * });\n *\n * // Write a draft\n * formDraft.write({ title: 'Hello', body: 'World' });\n *\n * // Read the draft\n * const draft = formDraft.read();\n * console.log(draft); // { title: 'Hello', body: 'World' }\n *\n * // Remove the draft\n * formDraft.remove();\n * ```\n */\nexport class SessionStorageProvider<T extends JsonValue> {\n public static readonly version = __package_version__;\n\n private readonly key__: string;\n protected readonly logger_;\n\n constructor(config: SessionStorageProviderConfig) {\n this.key__ = config.name;\n this.logger_ = createLogger(`session-storage-provider: ${this.key__}`);\n this.logger_.logMethodArgs?.('constructor', config);\n }\n\n /**\n * Checks if an item exists in sessionStorage for the given key.\n * This static method allows checking for existence without instantiating the provider.\n *\n * @param key - The storage key.\n * @returns `true` if the item exists in sessionStorage, otherwise `false`.\n *\n * @example\n * ```typescript\n * const exists = SessionStorageProvider.has('user-form');\n * ```\n */\n public static has(key: string): boolean {\n return sessionStorage.getItem(key) !== null;\n }\n\n /**\n * Checks if the current item exists in sessionStorage.\n *\n * @returns `true` if the item exists in sessionStorage, otherwise `false`.\n *\n * @example\n * ```typescript\n * const provider = new SessionStorageProvider({name: 'profile'});\n * if (provider.has()) {\n * // Item exists\n * }\n * ```\n */\n public has(): boolean {\n return sessionStorage.getItem(this.key__) !== null;\n }\n\n /**\n * Reads and parses the value from sessionStorage.\n * Returns `null` if the item does not exist or contains invalid JSON.\n *\n * @returns The parsed value of type `T`, or `null`.\n *\n * @example\n * ```typescript\n * const value = provider.read();\n * if (value !== null) {\n * console.log('Restored value:', value);\n * }\n * ```\n */\n public read(): T | null {\n let raw: string | null = null;\n\n try {\n raw = sessionStorage.getItem(this.key__);\n }\n catch (err) {\n this.logger_.error('read', 'read_session_storage_error', { err });\n }\n\n if (!raw) {\n this.logger_.logMethod?.('read//no_value');\n return null;\n }\n\n try {\n const parsed = JSON.parse(raw) as T;\n this.logger_.logMethodFull?.('read//value', undefined, { parsed });\n return parsed;\n }\n catch (err) {\n this.logger_.error('read', 'read_parse_error', { err });\n return null;\n }\n }\n\n /**\n * Serializes and writes a value to sessionStorage.\n * Throws if the value cannot be serialized.\n *\n * @param value The value to persist.\n *\n * @example\n * ```typescript\n * provider.write({ step: 3, answers: [true, false, true] });\n * ```\n */\n public write(value: T): void {\n this.logger_.logMethodArgs?.('write', { value });\n\n let valueStr: string;\n try {\n valueStr = JSON.stringify(value);\n }\n catch (err) {\n this.logger_.error('write', 'write_stringify_error', { err });\n throw new Error('write_stringify_error');\n }\n\n try {\n sessionStorage.setItem(this.key__, valueStr);\n }\n catch (err) {\n this.logger_.error('write', 'write_session_storage_error', { err });\n }\n }\n\n /**\n * Removes the item from sessionStorage.\n *\n * @example\n * ```typescript\n * provider.remove(); // Clears the stored value for this key.\n * ```\n */\n public remove(): void {\n this.logger_.logMethod?.('remove');\n sessionStorage.removeItem(this.key__);\n }\n}\n",
|
|
6
|
+
"import { SessionStorageProvider } from './session-storage.provider.js';\n\nimport type { SessionStorageProviderConfig } from './type.js';\n\n/**\n * Factory function to create a new SessionStorageProvider.\n *\n * @param config - The provider configuration.\n * @returns An instance of SessionStorageProvider.\n *\n * @example\n * ```typescript\n * const formData = createSessionStorageProvider({ name: 'multi-step-form' });\n *\n * // Write new data\n * formData.write({ step: 2, answers: { q1: 'yes' } });\n *\n * // Read the current data\n * const current = formData.read();\n * console.log(current); // { step: 2, answers: { q1: 'yes' } }\n * ```\n */\nexport function createSessionStorageProvider<T extends JsonValue>(config: SessionStorageProviderConfig): SessionStorageProvider<T> {\n return new SessionStorageProvider<T>(config);\n}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K,KAAK,EAExC,CCnIO,SAAS,CAAiD,CAAC,EAAiE,CACjI,OAAO,IAAI,EAA0B,CAAM",
|
|
9
|
+
"debugId": "CF722D8BCB54E18D64756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
package/package.json
CHANGED
|
@@ -1,29 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/session-storage",
|
|
3
3
|
"description": "A modern, simple, and robust solution for managing versioned JSON objects in the browser's `sessionStorage`. This package provides a clean, class-based API with a factory function to ensure your application's data persistence is safe, maintainable, and future-proof within a single tab.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0",
|
|
5
5
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
6
|
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@alwatr/logger": "
|
|
8
|
+
"@alwatr/logger": "7.0.0"
|
|
9
9
|
},
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@alwatr/nano-build": "
|
|
12
|
-
"@alwatr/prettier-config": "
|
|
13
|
-
"@alwatr/tsconfig-base": "
|
|
14
|
-
"@alwatr/type-helper": "
|
|
15
|
-
"@jest/globals": "^30.2.0",
|
|
11
|
+
"@alwatr/nano-build": "7.0.0",
|
|
12
|
+
"@alwatr/prettier-config": "7.0.0",
|
|
13
|
+
"@alwatr/tsconfig-base": "7.0.0",
|
|
14
|
+
"@alwatr/type-helper": "8.0.0",
|
|
16
15
|
"typescript": "^5.9.3"
|
|
17
16
|
},
|
|
18
17
|
"exports": {
|
|
19
18
|
".": {
|
|
20
19
|
"types": "./dist/main.d.ts",
|
|
21
|
-
"
|
|
22
|
-
"require": "./dist/main.cjs"
|
|
20
|
+
"default": "./dist/main.js"
|
|
23
21
|
}
|
|
24
22
|
},
|
|
25
23
|
"files": [
|
|
26
|
-
"**/*.{js,mjs,cjs,map,d.ts,html,
|
|
24
|
+
"**/*.{js,mjs,cjs,ts,map,d.ts,html,LEGAL.txt}",
|
|
25
|
+
"README.md",
|
|
27
26
|
"LICENSE",
|
|
28
27
|
"!demo/**/*",
|
|
29
28
|
"!**/*.test.js"
|
|
@@ -48,8 +47,6 @@
|
|
|
48
47
|
"versioning"
|
|
49
48
|
],
|
|
50
49
|
"license": "MPL-2.0",
|
|
51
|
-
"main": "./dist/main.cjs",
|
|
52
|
-
"module": "./dist/main.mjs",
|
|
53
50
|
"prettier": "@alwatr/prettier-config",
|
|
54
51
|
"publishConfig": {
|
|
55
52
|
"access": "public"
|
|
@@ -60,21 +57,21 @@
|
|
|
60
57
|
"directory": "packages/session-storage"
|
|
61
58
|
},
|
|
62
59
|
"scripts": {
|
|
63
|
-
"b": "
|
|
64
|
-
"build": "
|
|
65
|
-
"build:es": "nano-build --preset=module",
|
|
60
|
+
"b": "bun run build",
|
|
61
|
+
"build": "bun run build:ts && bun run build:es",
|
|
62
|
+
"build:es": "nano-build --preset=module src/main.ts",
|
|
66
63
|
"build:ts": "tsc --build",
|
|
67
|
-
"c": "
|
|
68
|
-
"cb": "
|
|
64
|
+
"c": "bun run clean",
|
|
65
|
+
"cb": "bun run clean && bun run build",
|
|
69
66
|
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
70
|
-
"d": "
|
|
71
|
-
"w": "
|
|
72
|
-
"watch": "
|
|
73
|
-
"watch:es": "
|
|
74
|
-
"watch:ts": "
|
|
67
|
+
"d": "bun run build:es && bun",
|
|
68
|
+
"w": "bun run watch",
|
|
69
|
+
"watch": "bun run watch:ts & bun run watch:es",
|
|
70
|
+
"watch:es": "bun run build:es --watch",
|
|
71
|
+
"watch:ts": "bun run build:ts --watch --preserveWatchOutput"
|
|
75
72
|
},
|
|
76
73
|
"sideEffects": false,
|
|
77
74
|
"type": "module",
|
|
78
75
|
"types": "./dist/main.d.ts",
|
|
79
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "056102a1c8a563bbae8a290b6830450f467322a3"
|
|
80
77
|
}
|
package/src/facade.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SessionStorageProvider } from './session-storage.provider.js';
|
|
2
|
+
|
|
3
|
+
import type { SessionStorageProviderConfig } from './type.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Factory function to create a new SessionStorageProvider.
|
|
7
|
+
*
|
|
8
|
+
* @param config - The provider configuration.
|
|
9
|
+
* @returns An instance of SessionStorageProvider.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* const formData = createSessionStorageProvider({ name: 'multi-step-form' });
|
|
14
|
+
*
|
|
15
|
+
* // Write new data
|
|
16
|
+
* formData.write({ step: 2, answers: { q1: 'yes' } });
|
|
17
|
+
*
|
|
18
|
+
* // Read the current data
|
|
19
|
+
* const current = formData.read();
|
|
20
|
+
* console.log(current); // { step: 2, answers: { q1: 'yes' } }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function createSessionStorageProvider<T extends JsonValue>(config: SessionStorageProviderConfig): SessionStorageProvider<T> {
|
|
24
|
+
return new SessionStorageProvider<T>(config);
|
|
25
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { createLogger } from '@alwatr/logger';
|
|
2
|
+
|
|
3
|
+
import type { SessionStorageProviderConfig } from './type.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A provider class for managing a specific item in sessionStorage.
|
|
7
|
+
* It encapsulates the logic for key generation and serialization.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const formDraft = new SessionStorageProvider<{title: string; body: string}>({
|
|
12
|
+
* name: 'post-form-draft',
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* // Write a draft
|
|
16
|
+
* formDraft.write({ title: 'Hello', body: 'World' });
|
|
17
|
+
*
|
|
18
|
+
* // Read the draft
|
|
19
|
+
* const draft = formDraft.read();
|
|
20
|
+
* console.log(draft); // { title: 'Hello', body: 'World' }
|
|
21
|
+
*
|
|
22
|
+
* // Remove the draft
|
|
23
|
+
* formDraft.remove();
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export class SessionStorageProvider<T extends JsonValue> {
|
|
27
|
+
public static readonly version = __package_version__;
|
|
28
|
+
|
|
29
|
+
private readonly key__: string;
|
|
30
|
+
protected readonly logger_;
|
|
31
|
+
|
|
32
|
+
constructor(config: SessionStorageProviderConfig) {
|
|
33
|
+
this.key__ = config.name;
|
|
34
|
+
this.logger_ = createLogger(`session-storage-provider: ${this.key__}`);
|
|
35
|
+
this.logger_.logMethodArgs?.('constructor', config);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Checks if an item exists in sessionStorage for the given key.
|
|
40
|
+
* This static method allows checking for existence without instantiating the provider.
|
|
41
|
+
*
|
|
42
|
+
* @param key - The storage key.
|
|
43
|
+
* @returns `true` if the item exists in sessionStorage, otherwise `false`.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const exists = SessionStorageProvider.has('user-form');
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
public static has(key: string): boolean {
|
|
51
|
+
return sessionStorage.getItem(key) !== null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the current item exists in sessionStorage.
|
|
56
|
+
*
|
|
57
|
+
* @returns `true` if the item exists in sessionStorage, otherwise `false`.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const provider = new SessionStorageProvider({name: 'profile'});
|
|
62
|
+
* if (provider.has()) {
|
|
63
|
+
* // Item exists
|
|
64
|
+
* }
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
public has(): boolean {
|
|
68
|
+
return sessionStorage.getItem(this.key__) !== null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Reads and parses the value from sessionStorage.
|
|
73
|
+
* Returns `null` if the item does not exist or contains invalid JSON.
|
|
74
|
+
*
|
|
75
|
+
* @returns The parsed value of type `T`, or `null`.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const value = provider.read();
|
|
80
|
+
* if (value !== null) {
|
|
81
|
+
* console.log('Restored value:', value);
|
|
82
|
+
* }
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
public read(): T | null {
|
|
86
|
+
let raw: string | null = null;
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
raw = sessionStorage.getItem(this.key__);
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
this.logger_.error('read', 'read_session_storage_error', { err });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!raw) {
|
|
96
|
+
this.logger_.logMethod?.('read//no_value');
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
const parsed = JSON.parse(raw) as T;
|
|
102
|
+
this.logger_.logMethodFull?.('read//value', undefined, { parsed });
|
|
103
|
+
return parsed;
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
this.logger_.error('read', 'read_parse_error', { err });
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Serializes and writes a value to sessionStorage.
|
|
113
|
+
* Throws if the value cannot be serialized.
|
|
114
|
+
*
|
|
115
|
+
* @param value The value to persist.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* provider.write({ step: 3, answers: [true, false, true] });
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
public write(value: T): void {
|
|
123
|
+
this.logger_.logMethodArgs?.('write', { value });
|
|
124
|
+
|
|
125
|
+
let valueStr: string;
|
|
126
|
+
try {
|
|
127
|
+
valueStr = JSON.stringify(value);
|
|
128
|
+
}
|
|
129
|
+
catch (err) {
|
|
130
|
+
this.logger_.error('write', 'write_stringify_error', { err });
|
|
131
|
+
throw new Error('write_stringify_error');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
sessionStorage.setItem(this.key__, valueStr);
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
this.logger_.error('write', 'write_session_storage_error', { err });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Removes the item from sessionStorage.
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* provider.remove(); // Clears the stored value for this key.
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
public remove(): void {
|
|
151
|
+
this.logger_.logMethod?.('remove');
|
|
152
|
+
sessionStorage.removeItem(this.key__);
|
|
153
|
+
}
|
|
154
|
+
}
|
package/src/type.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type {} from '@alwatr/nano-build';
|
|
2
|
+
import type {} from '@alwatr/type-helper';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for a session storage provider.
|
|
6
|
+
*/
|
|
7
|
+
export interface SessionStorageProviderConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The unique name for the storage item.
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## 1.0.0 (2026-02-18)
|
|
7
|
-
|
|
8
|
-
### โจ Features
|
|
9
|
-
|
|
10
|
-
* **session-storage:** add new package for managing sessionStorage with versioning ([55b1a28](https://github.com/Alwatr/nanolib/commit/55b1a280f3534aea6a2a1ab857327d3f020eaf1b))
|
|
11
|
-
* **session-storage:** export types from type.js in main.ts ([db0c040](https://github.com/Alwatr/nanolib/commit/db0c0400d267df28fd5ea8beda42e332bcd45f87))
|
|
12
|
-
* **session-storage:** update createSessionStorageProvider to accept config object ([c80f057](https://github.com/Alwatr/nanolib/commit/c80f0575f74c98ff910e96af49e1d68728856f05))
|
|
13
|
-
|
|
14
|
-
### ๐ Bug Fixes
|
|
15
|
-
|
|
16
|
-
* **session-storage:** rename key_ to key__ for consistency and update logging ([6ac3820](https://github.com/Alwatr/nanolib/commit/6ac3820fd5d810e742c3b490f22fec67ee1b77ca))
|
|
17
|
-
|
|
18
|
-
### ๐งน Miscellaneous Chores
|
|
19
|
-
|
|
20
|
-
* fix pre version ([d9c8b97](https://github.com/Alwatr/nanolib/commit/d9c8b97eaaa9388d843d0ed24c426be68248972f))
|
|
21
|
-
* remove CHANGELOG.md as it is no longer needed ([94219bf](https://github.com/Alwatr/nanolib/commit/94219bf23309e6d05d5d57aca95ac01d8cdde46f))
|
|
22
|
-
* update keywords in package.json files for better categorization ([cf0ddb6](https://github.com/Alwatr/nanolib/commit/cf0ddb66eab7e2a87158279e455d71c495b30834))
|
|
23
|
-
* update LICENSE file with complete text of Mozilla Public License v2.0 ([23815d3](https://github.com/Alwatr/nanolib/commit/23815d3bc29545996385f4702166df9a080504de))
|
package/dist/main.cjs
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
/** ๐ฆ @alwatr/session-storage v1.0.0 */
|
|
2
|
-
__dev_mode__: console.debug("๐ฆ @alwatr/session-storage v1.0.0");
|
|
3
|
-
"use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{SessionStorageProvider:()=>SessionStorageProvider,createSessionStorageProvider:()=>createSessionStorageProvider});module.exports=__toCommonJS(main_exports);var import_logger=require("@alwatr/logger");var SessionStorageProvider=class{static{this.version="1.0.0"}constructor(config){this.key__=config.name;this.logger_=(0,import_logger.createLogger)(`session-storage-provider: ${this.key__}`);this.logger_.logMethodArgs?.("constructor",config)}static has(key){return sessionStorage.getItem(key)!==null}has(){return sessionStorage.getItem(this.key__)!==null}read(){let raw=null;try{raw=sessionStorage.getItem(this.key__)}catch(err){this.logger_.error("read","read_session_storage_error",{err})}if(!raw){this.logger_.logMethod?.("read//no_value");return null}try{const parsed=JSON.parse(raw);this.logger_.logMethodFull?.("read//value",void 0,{parsed});return parsed}catch(err){this.logger_.error("read","read_parse_error",{err});return null}}write(value){this.logger_.logMethodArgs?.("write",{value});let valueStr;try{valueStr=JSON.stringify(value)}catch(err){this.logger_.error("write","write_stringify_error",{err});throw new Error("write_stringify_error")}try{sessionStorage.setItem(this.key__,valueStr)}catch(err){this.logger_.error("write","write_session_storage_error",{err})}}remove(){this.logger_.logMethod?.("remove");sessionStorage.removeItem(this.key__)}};function createSessionStorageProvider(config){return new SessionStorageProvider(config)}0&&(module.exports={SessionStorageProvider,createSessionStorageProvider});
|
|
4
|
-
//# sourceMappingURL=main.cjs.map
|
package/dist/main.cjs.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/main.ts", "../src/session-storage.provider.ts", "../src/facade.ts"],
|
|
4
|
-
"sourcesContent": ["export * from './session-storage.provider.js';\nexport * from './facade.js';\nexport type * from './type.js';\n", "import { createLogger } from '@alwatr/logger';\n\nimport type { SessionStorageProviderConfig } from './type.js';\n\n/**\n * A provider class for managing a specific item in sessionStorage.\n * It encapsulates the logic for key generation and serialization.\n *\n * @example\n * ```typescript\n * const formDraft = new SessionStorageProvider<{title: string; body: string}>({\n * name: 'post-form-draft',\n * });\n *\n * // Write a draft\n * formDraft.write({ title: 'Hello', body: 'World' });\n *\n * // Read the draft\n * const draft = formDraft.read();\n * console.log(draft); // { title: 'Hello', body: 'World' }\n *\n * // Remove the draft\n * formDraft.remove();\n * ```\n */\nexport class SessionStorageProvider<T extends JsonValue> {\n public static readonly version = __package_version__;\n\n private readonly key__: string;\n protected readonly logger_;\n\n constructor(config: SessionStorageProviderConfig) {\n this.key__ = config.name;\n this.logger_ = createLogger(`session-storage-provider: ${this.key__}`);\n this.logger_.logMethodArgs?.('constructor', config);\n }\n\n /**\n * Checks if an item exists in sessionStorage for the given key.\n * This static method allows checking for existence without instantiating the provider.\n *\n * @param key - The storage key.\n * @returns `true` if the item exists in sessionStorage, otherwise `false`.\n *\n * @example\n * ```typescript\n * const exists = SessionStorageProvider.has('user-form');\n * ```\n */\n public static has(key: string): boolean {\n return sessionStorage.getItem(key) !== null;\n }\n\n /**\n * Checks if the current item exists in sessionStorage.\n *\n * @returns `true` if the item exists in sessionStorage, otherwise `false`.\n *\n * @example\n * ```typescript\n * const provider = new SessionStorageProvider({name: 'profile'});\n * if (provider.has()) {\n * // Item exists\n * }\n * ```\n */\n public has(): boolean {\n return sessionStorage.getItem(this.key__) !== null;\n }\n\n /**\n * Reads and parses the value from sessionStorage.\n * Returns `null` if the item does not exist or contains invalid JSON.\n *\n * @returns The parsed value of type `T`, or `null`.\n *\n * @example\n * ```typescript\n * const value = provider.read();\n * if (value !== null) {\n * console.log('Restored value:', value);\n * }\n * ```\n */\n public read(): T | null {\n let raw: string | null = null;\n\n try {\n raw = sessionStorage.getItem(this.key__);\n }\n catch (err) {\n this.logger_.error('read', 'read_session_storage_error', { err });\n }\n\n if (!raw) {\n this.logger_.logMethod?.('read//no_value');\n return null;\n }\n\n try {\n const parsed = JSON.parse(raw) as T;\n this.logger_.logMethodFull?.('read//value', undefined, { parsed });\n return parsed;\n }\n catch (err) {\n this.logger_.error('read', 'read_parse_error', { err });\n return null;\n }\n }\n\n /**\n * Serializes and writes a value to sessionStorage.\n * Throws if the value cannot be serialized.\n *\n * @param value The value to persist.\n *\n * @example\n * ```typescript\n * provider.write({ step: 3, answers: [true, false, true] });\n * ```\n */\n public write(value: T): void {\n this.logger_.logMethodArgs?.('write', { value });\n\n let valueStr: string;\n try {\n valueStr = JSON.stringify(value);\n }\n catch (err) {\n this.logger_.error('write', 'write_stringify_error', { err });\n throw new Error('write_stringify_error');\n }\n\n try {\n sessionStorage.setItem(this.key__, valueStr);\n }\n catch (err) {\n this.logger_.error('write', 'write_session_storage_error', { err });\n }\n }\n\n /**\n * Removes the item from sessionStorage.\n *\n * @example\n * ```typescript\n * provider.remove(); // Clears the stored value for this key.\n * ```\n */\n public remove(): void {\n this.logger_.logMethod?.('remove');\n sessionStorage.removeItem(this.key__);\n }\n}\n", "import { SessionStorageProvider } from './session-storage.provider.js';\n\nimport type { SessionStorageProviderConfig } from './type.js';\n\n/**\n * Factory function to create a new SessionStorageProvider.\n *\n * @param config - The provider configuration.\n * @returns An instance of SessionStorageProvider.\n *\n * @example\n * ```typescript\n * const formData = createSessionStorageProvider({ name: 'multi-step-form' });\n *\n * // Write new data\n * formData.write({ step: 2, answers: { q1: 'yes' } });\n *\n * // Read the current data\n * const current = formData.read();\n * console.log(current); // { step: 2, answers: { q1: 'yes' } }\n * ```\n */\nexport function createSessionStorageProvider<T extends JsonValue>(config: SessionStorageProviderConfig): SessionStorageProvider<T> {\n return new SessionStorageProvider<T>(config);\n}\n"],
|
|
5
|
-
"mappings": ";;qqBAAA,uMCAA,kBAA6B,0BAyBtB,IAAM,uBAAN,KAAkD,CACvD,YAAuB,QAAU,QAKjC,YAAY,OAAsC,CAChD,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
|
-
"names": []
|
|
7
|
-
}
|
package/dist/main.mjs
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
/** ๐ฆ @alwatr/session-storage v1.0.0 */
|
|
2
|
-
__dev_mode__: console.debug("๐ฆ @alwatr/session-storage v1.0.0");
|
|
3
|
-
import{createLogger}from"@alwatr/logger";var SessionStorageProvider=class{static{this.version="1.0.0"}constructor(config){this.key__=config.name;this.logger_=createLogger(`session-storage-provider: ${this.key__}`);this.logger_.logMethodArgs?.("constructor",config)}static has(key){return sessionStorage.getItem(key)!==null}has(){return sessionStorage.getItem(this.key__)!==null}read(){let raw=null;try{raw=sessionStorage.getItem(this.key__)}catch(err){this.logger_.error("read","read_session_storage_error",{err})}if(!raw){this.logger_.logMethod?.("read//no_value");return null}try{const parsed=JSON.parse(raw);this.logger_.logMethodFull?.("read//value",void 0,{parsed});return parsed}catch(err){this.logger_.error("read","read_parse_error",{err});return null}}write(value){this.logger_.logMethodArgs?.("write",{value});let valueStr;try{valueStr=JSON.stringify(value)}catch(err){this.logger_.error("write","write_stringify_error",{err});throw new Error("write_stringify_error")}try{sessionStorage.setItem(this.key__,valueStr)}catch(err){this.logger_.error("write","write_session_storage_error",{err})}}remove(){this.logger_.logMethod?.("remove");sessionStorage.removeItem(this.key__)}};function createSessionStorageProvider(config){return new SessionStorageProvider(config)}export{SessionStorageProvider,createSessionStorageProvider};
|
|
4
|
-
//# sourceMappingURL=main.mjs.map
|
package/dist/main.mjs.map
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../src/session-storage.provider.ts", "../src/facade.ts"],
|
|
4
|
-
"sourcesContent": ["import { createLogger } from '@alwatr/logger';\n\nimport type { SessionStorageProviderConfig } from './type.js';\n\n/**\n * A provider class for managing a specific item in sessionStorage.\n * It encapsulates the logic for key generation and serialization.\n *\n * @example\n * ```typescript\n * const formDraft = new SessionStorageProvider<{title: string; body: string}>({\n * name: 'post-form-draft',\n * });\n *\n * // Write a draft\n * formDraft.write({ title: 'Hello', body: 'World' });\n *\n * // Read the draft\n * const draft = formDraft.read();\n * console.log(draft); // { title: 'Hello', body: 'World' }\n *\n * // Remove the draft\n * formDraft.remove();\n * ```\n */\nexport class SessionStorageProvider<T extends JsonValue> {\n public static readonly version = __package_version__;\n\n private readonly key__: string;\n protected readonly logger_;\n\n constructor(config: SessionStorageProviderConfig) {\n this.key__ = config.name;\n this.logger_ = createLogger(`session-storage-provider: ${this.key__}`);\n this.logger_.logMethodArgs?.('constructor', config);\n }\n\n /**\n * Checks if an item exists in sessionStorage for the given key.\n * This static method allows checking for existence without instantiating the provider.\n *\n * @param key - The storage key.\n * @returns `true` if the item exists in sessionStorage, otherwise `false`.\n *\n * @example\n * ```typescript\n * const exists = SessionStorageProvider.has('user-form');\n * ```\n */\n public static has(key: string): boolean {\n return sessionStorage.getItem(key) !== null;\n }\n\n /**\n * Checks if the current item exists in sessionStorage.\n *\n * @returns `true` if the item exists in sessionStorage, otherwise `false`.\n *\n * @example\n * ```typescript\n * const provider = new SessionStorageProvider({name: 'profile'});\n * if (provider.has()) {\n * // Item exists\n * }\n * ```\n */\n public has(): boolean {\n return sessionStorage.getItem(this.key__) !== null;\n }\n\n /**\n * Reads and parses the value from sessionStorage.\n * Returns `null` if the item does not exist or contains invalid JSON.\n *\n * @returns The parsed value of type `T`, or `null`.\n *\n * @example\n * ```typescript\n * const value = provider.read();\n * if (value !== null) {\n * console.log('Restored value:', value);\n * }\n * ```\n */\n public read(): T | null {\n let raw: string | null = null;\n\n try {\n raw = sessionStorage.getItem(this.key__);\n }\n catch (err) {\n this.logger_.error('read', 'read_session_storage_error', { err });\n }\n\n if (!raw) {\n this.logger_.logMethod?.('read//no_value');\n return null;\n }\n\n try {\n const parsed = JSON.parse(raw) as T;\n this.logger_.logMethodFull?.('read//value', undefined, { parsed });\n return parsed;\n }\n catch (err) {\n this.logger_.error('read', 'read_parse_error', { err });\n return null;\n }\n }\n\n /**\n * Serializes and writes a value to sessionStorage.\n * Throws if the value cannot be serialized.\n *\n * @param value The value to persist.\n *\n * @example\n * ```typescript\n * provider.write({ step: 3, answers: [true, false, true] });\n * ```\n */\n public write(value: T): void {\n this.logger_.logMethodArgs?.('write', { value });\n\n let valueStr: string;\n try {\n valueStr = JSON.stringify(value);\n }\n catch (err) {\n this.logger_.error('write', 'write_stringify_error', { err });\n throw new Error('write_stringify_error');\n }\n\n try {\n sessionStorage.setItem(this.key__, valueStr);\n }\n catch (err) {\n this.logger_.error('write', 'write_session_storage_error', { err });\n }\n }\n\n /**\n * Removes the item from sessionStorage.\n *\n * @example\n * ```typescript\n * provider.remove(); // Clears the stored value for this key.\n * ```\n */\n public remove(): void {\n this.logger_.logMethod?.('remove');\n sessionStorage.removeItem(this.key__);\n }\n}\n", "import { SessionStorageProvider } from './session-storage.provider.js';\n\nimport type { SessionStorageProviderConfig } from './type.js';\n\n/**\n * Factory function to create a new SessionStorageProvider.\n *\n * @param config - The provider configuration.\n * @returns An instance of SessionStorageProvider.\n *\n * @example\n * ```typescript\n * const formData = createSessionStorageProvider({ name: 'multi-step-form' });\n *\n * // Write new data\n * formData.write({ step: 2, answers: { q1: 'yes' } });\n *\n * // Read the current data\n * const current = formData.read();\n * console.log(current); // { step: 2, answers: { q1: 'yes' } }\n * ```\n */\nexport function createSessionStorageProvider<T extends JsonValue>(config: SessionStorageProviderConfig): SessionStorageProvider<T> {\n return new SessionStorageProvider<T>(config);\n}\n"],
|
|
5
|
-
"mappings": ";;AAAA,OAAS,iBAAoB,iBAyBtB,IAAM,uBAAN,KAAkD,CACvD,YAAuB,QAAU,QAKjC,YAAY,OAAsC,CAChD,KAAK,MAAQ,OAAO,KACpB,KAAK,QAAU,aAAa,6BAA6B,KAAK,KAAK,EAAE,EACrE,KAAK,QAAQ,gBAAgB,cAAe,MAAM,CACpD,CAcA,OAAc,IAAI,IAAsB,CACtC,OAAO,eAAe,QAAQ,GAAG,IAAM,IACzC,CAeO,KAAe,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