@nexim/alpine 0.0.1 → 1.0.0-alpha.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/CHANGELOG.md CHANGED
@@ -3,6 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.0.0-alpha.0](https://github.com/the-nexim/nanolib/compare/@nexim/alpine@0.0.1...@nexim/alpine@1.0.0-alpha.0) (2024-12-10)
7
+
8
+ ### Features
9
+
10
+ * **alpine:** add store class ([8f12bd1](https://github.com/the-nexim/nanolib/commit/8f12bd1edfee62e4e92c1bbef67584b4aea05533)) by @
11
+
6
12
  ## 0.0.1 (2024-12-10)
7
13
 
8
14
  ### Code Refactoring
package/README.md CHANGED
@@ -40,6 +40,38 @@ const store = alpineStoreGenerator({
40
40
  console.log(store.type); // Output: root
41
41
  ```
42
42
 
43
+ ### AlpineStore
44
+
45
+ Provides a Alpine.js pure store implementation with logger.
46
+
47
+ #### Constructor
48
+
49
+ Creates an instance of `AlpineStore`.
50
+
51
+ - **config**: The configuration object for the store.
52
+ - **name**: The name of the store.
53
+ - **defaultValue**: The default value of the store.
54
+
55
+ ### Properties
56
+
57
+ - **store**: alpine store proxy.
58
+
59
+ #### Example Usage
60
+
61
+ ```ts
62
+ import {AlpineStore} from '@nexim/alpine';
63
+
64
+ const {store} = new AlpineStore({
65
+ name: 'myStore',
66
+ defaultValue: {data: 'root'},
67
+ });
68
+
69
+ console.log(store.data); // Output: { data: 'root' }
70
+ store.data = 'user';
71
+
72
+ console.log(store.data); // Output: { data: 'user' }
73
+ ```
74
+
43
75
  ### AlpineStoreWithBackup
44
76
 
45
77
  Extends `AlpineStore` to add backup and restore functionality with local storage support and expiration handling.
@@ -54,6 +86,10 @@ Creates an instance of `AlpineStoreWithBackup`.
54
86
  - **defaultValue**: The default value of the store.
55
87
  - **expireDuration**: Optional. The duration after which the store expires.
56
88
 
89
+ ### Properties
90
+
91
+ - **store**: alpine store proxy.
92
+
57
93
  #### Methods
58
94
 
59
95
  - **save()**: Saves the current data to local storage. If the data is null, it clears the stored data. Also updates the expiration time.
@@ -79,3 +115,7 @@ console.log(storeWithBackup.store.data); // Output: { data: 'user' }
79
115
  storeWithBackup.clear();
80
116
  console.log(storeWithBackup.store.data); // Output: { data: 'root' }
81
117
  ```
118
+
119
+ ### TODO
120
+
121
+ - Analyze [@alwatr/context](https://github.com/Alwatr/flux/tree/next/packages/context) for use here.
package/dist/main.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /* @nexim/alpine v0.0.1 */
1
+ /* @nexim/alpine v1.0.0-alpha.0 */
2
2
  "use strict";
3
3
  var __create = Object.create;
4
4
  var __defProp = Object.defineProperty;
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  // src/main.ts
32
32
  var main_exports = {};
33
33
  __export(main_exports, {
34
+ AlpineStore: () => AlpineStore,
34
35
  AlpineStoreWithBackup: () => AlpineStoreWithBackup,
35
36
  alpineStoreGenerator: () => alpineStoreGenerator
36
37
  });
@@ -48,12 +49,38 @@ function alpineStoreGenerator(config) {
48
49
  return store;
49
50
  }
50
51
 
52
+ // src/store/store.ts
53
+ var import_logger2 = require("@alwatr/logger");
54
+ var AlpineStore = class {
55
+ /**
56
+ * Provides a Alpine.js pure store implementation with logger.
57
+ *
58
+ * @param {AlpineStoreOptions} config - Configuration object.
59
+ *
60
+ * @example
61
+ * import {AlpineStore} from '@nexim/alpine';
62
+ *
63
+ * const {store} = new AlpineStore({
64
+ * name: 'myStore',
65
+ * defaultValue: {data: 'root'},
66
+ * });
67
+ * console.log(store.data); // Output: { data: 'root' }
68
+ *
69
+ * store.data = 'user';
70
+ * console.log(store.data); // Output: { data: 'user' }
71
+ */
72
+ constructor(config) {
73
+ this.logger_ = (0, import_logger2.createLogger)(`${"@nexim/alpine"}:${config.name}`);
74
+ this.logger_.logMethodArgs?.("constructor", config);
75
+ this.store = config.defaultValue;
76
+ }
77
+ };
78
+
51
79
  // src/store/store-with-backup.ts
52
80
  var import_local_storage = require("@alwatr/local-storage");
53
- var import_logger2 = require("@alwatr/logger");
54
81
  var import_parse_duration = require("@alwatr/parse-duration");
55
82
  var schemaVersion = 1;
56
- var AlpineStoreWithBackup = class {
83
+ var AlpineStoreWithBackup = class extends AlpineStore {
57
84
  /**
58
85
  * Provides a Alpine.js store implementation with backup and expiration.
59
86
  *
@@ -78,6 +105,7 @@ var AlpineStoreWithBackup = class {
78
105
  * console.log(storeWithBackup.store.data); // Output: { data: 'root' }
79
106
  */
80
107
  constructor(config__) {
108
+ super(config__);
81
109
  this.config__ = config__;
82
110
  /**
83
111
  * Keys for storing data and expire time in local storage with version.
@@ -86,9 +114,6 @@ var AlpineStoreWithBackup = class {
86
114
  data: `[${"@nexim/alpine"}:data:sv${schemaVersion}]:${this.config__.name}`,
87
115
  expireTime: `[${"@nexim/alpine"}:expire-time:sv${schemaVersion}]:${this.config__.name}`
88
116
  };
89
- this.logger_ = (0, import_logger2.createLogger)(`[${"@nexim/alpine"}]:${config__.name}`);
90
- this.logger_.logMethodArgs?.("constructor", config__);
91
- this.store = config__.defaultValue;
92
117
  if (this.config__.expireDuration !== null) {
93
118
  this.handleDataExpiration__();
94
119
  }
@@ -155,9 +180,10 @@ var AlpineStoreWithBackup = class {
155
180
  };
156
181
 
157
182
  // src/main.ts
158
- __dev_mode__: import_package_tracer.packageTracer.add("@nexim/alpine", "0.0.1");
183
+ __dev_mode__: import_package_tracer.packageTracer.add("@nexim/alpine", "1.0.0-alpha.0");
159
184
  // Annotate the CommonJS export names for ESM import in node:
160
185
  0 && (module.exports = {
186
+ AlpineStore,
161
187
  AlpineStoreWithBackup,
162
188
  alpineStoreGenerator
163
189
  });
package/dist/main.cjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store-with-backup.ts"],
4
- "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store-with-backup.js';\n", "import {createLogger} from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @template T - The type of the default value.\n * @property {string} name - The name of the store.\n * @property {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @template T - The type of the data.\n * @param {AlpineStoreGeneratorOptions} config - The configuration object for the store.\n * @returns {T} - The initialized store instance.\n *\n * @example\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *\n * @description\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see https://alpinejs.dev/globals/alpine-store\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', config);\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import {localJsonStorage} from '@alwatr/local-storage';\nimport {type AlwatrLogger, createLogger} from '@alwatr/logger';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n data: DictionaryReq<any> | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {number} version - The version of the store.\n * @param {T} defaultValue - The default value of the store.\n * @param {Duration} [expireDuration] - Optional. The duration after which the store expires.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n name: string;\n version: number;\n defaultValue: T;\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param {AlpineStoreWithBackupOptions} config__ - Configuration object.\n *\n * @example\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n this.logger_ = createLogger(`[${__package_name__}]:${config__.name}`);\n this.logger_.logMethodArgs?.('constructor', config__);\n\n this.store = config__.defaultValue;\n\n if (this.config__.expireDuration !== null) {\n this.handleDataExpiration__();\n }\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', {data: this.store.data});\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n const expireDuration = localJsonStorage.getItem<{time: number}>(\n this.localStorageKey__.expireTime,\n {time: 0},\n this.config__.version,\n ).time;\n\n if (expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<T>(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);\n this.store.data = newData.data;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, {time: newExpireTime}, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', {newExpireTime});\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA4B;;;ACA5B,oBAA2B;AAC3B,sBAAmB;AAEnB,IAAM,aAAS,4BAAa,eAAgB;AAqCrC,SAAS,qBAA8C,QAA2C;AACvG,SAAO,gBAAgB,wBAAwB,MAAM;AAErD,kBAAAA,QAAO,MAAM,OAAO,MAAM,OAAO,YAAY;AAG7C,QAAM,QAAQ,gBAAAA,QAAO,MAAM,OAAO,IAAI;AACtC,SAAO;AACT;;;AChD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B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMQ,yBAA+B;AACrC,SAAK,QAAQ,YAAY,wBAAwB;AAEjD,UAAM,iBAAiB,sCAAiB;AAAA,MACtC,KAAK,kBAAkB;AAAA,MACvB,EAAC,MAAM,EAAC;AAAA,MACR,KAAK,SAAS;AAAA,IAChB,EAAE;AAEF,QAAI,iBAAiB,KAAK,IAAI,GAAG;AAC/B,WAAK,MAAM;AAAA,IACb;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,SAAe;AACrB,SAAK,QAAQ,YAAY,QAAQ;AAEjC,UAAM,UAAU,sCAAiB,QAAW,KAAK,kBAAkB,MAAM,KAAK,SAAS,cAAc,KAAK,SAAS,OAAO;AAC1H,SAAK,MAAM,OAAO,QAAQ;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKQ,qBAA2B;AACjC,QAAI,KAAK,SAAS,kBAAkB,KAAM;AAC1C,SAAK,QAAQ,YAAY,oBAAoB;AAE7C,UAAM,gBAAgB,KAAK,IAAI,QAAI,qCAAc,KAAK,SAAS,cAAc;AAC7E,0CAAiB,QAAQ,KAAK,kBAAkB,YAAY,EAAC,MAAM,cAAa,GAAG,KAAK,SAAS,OAAO;AAExG,SAAK,QAAQ,WAAW,uBAAuB,EAAC,cAAa,CAAC;AAAA,EAChE;AACF;;;AF9JA,aAAc,qCAAc,IAAI,iBAAkB,OAAmB;",
3
+ "sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store.ts", "../src/store/store-with-backup.ts"],
4
+ "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store.js';\nexport * from './store/store-with-backup.js';\n", "import {createLogger} from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @template T - The type of the default value.\n * @property {string} name - The name of the store.\n * @property {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @template T - The type of the data.\n * @param {AlpineStoreGeneratorOptions} config - The configuration object for the store.\n * @returns {T} - The initialized store instance.\n *\n * @example\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *\n * @description\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see https://alpinejs.dev/globals/alpine-store\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', config);\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import {type AlwatrLogger, createLogger} from '@alwatr/logger';\n\n/**\n * AlpineStore Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Provides a Alpine.js pure store implementation with logger.\n */\nexport class AlpineStore<T extends DictionaryReq> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Provides a Alpine.js pure store implementation with logger.\n *\n * @param {AlpineStoreOptions} config - Configuration object.\n *\n * @example\n * import {AlpineStore} from '@nexim/alpine';\n *\n * const {store} = new AlpineStore({\n * name: 'myStore',\n * defaultValue: {data: 'root'},\n * });\n * console.log(store.data); // Output: { data: 'root' }\n *\n * store.data = 'user';\n * console.log(store.data); // Output: { data: 'user' }\n */\n constructor(config: AlpineStoreOptions<T>) {\n this.logger_ = createLogger(`${__package_name__}:${config.name}`);\n this.logger_.logMethodArgs?.('constructor', config);\n\n this.store = config.defaultValue;\n }\n}\n", "import {localJsonStorage} from '@alwatr/local-storage';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\nimport {AlpineStore} from './store.js';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n data: DictionaryReq<any> | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {number} version - The version of the store.\n * @param {T} defaultValue - The default value of the store.\n * @param {Duration} [expireDuration] - Optional. The duration after which the store expires.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n name: string;\n version: number;\n defaultValue: T;\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> extends AlpineStore<T> {\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param {AlpineStoreWithBackupOptions} config__ - Configuration object.\n *\n * @example\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n super(config__);\n\n if (this.config__.expireDuration !== null) {\n this.handleDataExpiration__();\n }\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', {data: this.store.data});\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n const expireDuration = localJsonStorage.getItem<{time: number}>(\n this.localStorageKey__.expireTime,\n {time: 0},\n this.config__.version,\n ).time;\n\n if (expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<T>(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);\n this.store.data = newData.data;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, {time: newExpireTime}, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', {newExpireTime});\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,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;AAExG,SAAK,QAAQ,WAAW,uBAAuB,EAAC,cAAa,CAAC;AAAA,EAChE;AACF;;;AHrJA,aAAc,qCAAc,IAAI,iBAAkB,eAAmB;",
6
6
  "names": ["alpine", "import_logger"]
7
7
  }
package/dist/main.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './store/store-generator.js';
2
+ export * from './store/store.js';
2
3
  export * from './store/store-with-backup.js';
3
4
  //# sourceMappingURL=main.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAIA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8BAA8B,CAAC"}
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAIA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kBAAkB,CAAC;AACjC,cAAc,8BAA8B,CAAC"}
package/dist/main.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /* @nexim/alpine v0.0.1 */
1
+ /* @nexim/alpine v1.0.0-alpha.0 */
2
2
 
3
3
  // src/main.ts
4
4
  import { packageTracer } from "@alwatr/package-tracer";
@@ -14,12 +14,38 @@ function alpineStoreGenerator(config) {
14
14
  return store;
15
15
  }
16
16
 
17
+ // src/store/store.ts
18
+ import { createLogger as createLogger2 } from "@alwatr/logger";
19
+ var AlpineStore = class {
20
+ /**
21
+ * Provides a Alpine.js pure store implementation with logger.
22
+ *
23
+ * @param {AlpineStoreOptions} config - Configuration object.
24
+ *
25
+ * @example
26
+ * import {AlpineStore} from '@nexim/alpine';
27
+ *
28
+ * const {store} = new AlpineStore({
29
+ * name: 'myStore',
30
+ * defaultValue: {data: 'root'},
31
+ * });
32
+ * console.log(store.data); // Output: { data: 'root' }
33
+ *
34
+ * store.data = 'user';
35
+ * console.log(store.data); // Output: { data: 'user' }
36
+ */
37
+ constructor(config) {
38
+ this.logger_ = createLogger2(`${"@nexim/alpine"}:${config.name}`);
39
+ this.logger_.logMethodArgs?.("constructor", config);
40
+ this.store = config.defaultValue;
41
+ }
42
+ };
43
+
17
44
  // src/store/store-with-backup.ts
18
45
  import { localJsonStorage } from "@alwatr/local-storage";
19
- import { createLogger as createLogger2 } from "@alwatr/logger";
20
46
  import { parseDuration } from "@alwatr/parse-duration";
21
47
  var schemaVersion = 1;
22
- var AlpineStoreWithBackup = class {
48
+ var AlpineStoreWithBackup = class extends AlpineStore {
23
49
  /**
24
50
  * Provides a Alpine.js store implementation with backup and expiration.
25
51
  *
@@ -44,6 +70,7 @@ var AlpineStoreWithBackup = class {
44
70
  * console.log(storeWithBackup.store.data); // Output: { data: 'root' }
45
71
  */
46
72
  constructor(config__) {
73
+ super(config__);
47
74
  this.config__ = config__;
48
75
  /**
49
76
  * Keys for storing data and expire time in local storage with version.
@@ -52,9 +79,6 @@ var AlpineStoreWithBackup = class {
52
79
  data: `[${"@nexim/alpine"}:data:sv${schemaVersion}]:${this.config__.name}`,
53
80
  expireTime: `[${"@nexim/alpine"}:expire-time:sv${schemaVersion}]:${this.config__.name}`
54
81
  };
55
- this.logger_ = createLogger2(`[${"@nexim/alpine"}]:${config__.name}`);
56
- this.logger_.logMethodArgs?.("constructor", config__);
57
- this.store = config__.defaultValue;
58
82
  if (this.config__.expireDuration !== null) {
59
83
  this.handleDataExpiration__();
60
84
  }
@@ -121,8 +145,9 @@ var AlpineStoreWithBackup = class {
121
145
  };
122
146
 
123
147
  // src/main.ts
124
- __dev_mode__: packageTracer.add("@nexim/alpine", "0.0.1");
148
+ __dev_mode__: packageTracer.add("@nexim/alpine", "1.0.0-alpha.0");
125
149
  export {
150
+ AlpineStore,
126
151
  AlpineStoreWithBackup,
127
152
  alpineStoreGenerator
128
153
  };
package/dist/main.mjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store-with-backup.ts"],
4
- "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store-with-backup.js';\n", "import {createLogger} from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @template T - The type of the default value.\n * @property {string} name - The name of the store.\n * @property {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @template T - The type of the data.\n * @param {AlpineStoreGeneratorOptions} config - The configuration object for the store.\n * @returns {T} - The initialized store instance.\n *\n * @example\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *\n * @description\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see https://alpinejs.dev/globals/alpine-store\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', config);\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import {localJsonStorage} from '@alwatr/local-storage';\nimport {type AlwatrLogger, createLogger} from '@alwatr/logger';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n data: DictionaryReq<any> | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {number} version - The version of the store.\n * @param {T} defaultValue - The default value of the store.\n * @param {Duration} [expireDuration] - Optional. The duration after which the store expires.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n name: string;\n version: number;\n defaultValue: T;\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param {AlpineStoreWithBackupOptions} config__ - Configuration object.\n *\n * @example\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n this.logger_ = createLogger(`[${__package_name__}]:${config__.name}`);\n this.logger_.logMethodArgs?.('constructor', config__);\n\n this.store = config__.defaultValue;\n\n if (this.config__.expireDuration !== null) {\n this.handleDataExpiration__();\n }\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', {data: this.store.data});\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n const expireDuration = localJsonStorage.getItem<{time: number}>(\n this.localStorageKey__.expireTime,\n {time: 0},\n this.config__.version,\n ).time;\n\n if (expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<T>(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);\n this.store.data = newData.data;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, {time: newExpireTime}, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', {newExpireTime});\n }\n}\n"],
5
- "mappings": ";;;AAAA,SAAQ,qBAAoB;;;ACA5B,SAAQ,oBAAmB;AAC3B,OAAO,YAAY;AAEnB,IAAM,SAAS,aAAa,eAAgB;AAqCrC,SAAS,qBAA8C,QAA2C;AACvG,SAAO,gBAAgB,wBAAwB,MAAM;AAErD,SAAO,MAAM,OAAO,MAAM,OAAO,YAAY;AAG7C,QAAM,QAAQ,OAAO,MAAM,OAAO,IAAI;AACtC,SAAO;AACT;;;AChDA,SAAQ,wBAAuB;AAC/B,SAA2B,gBAAAA,qBAAmB;AAC9C,SAAQ,qBAAmC;AA+B3C,IAAM,gBAAgB;AAKf,IAAM,wBAAN,MAAiE;AA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B,iBAAiB;AAAA,MACtC,KAAK,kBAAkB;AAAA,MACvB,EAAC,MAAM,EAAC;AAAA,MACR,KAAK,SAAS;AAAA,IAChB,EAAE;AAEF,QAAI,iBAAiB,KAAK,IAAI,GAAG;AAC/B,WAAK,MAAM;AAAA,IACb;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOQ,SAAe;AACrB,SAAK,QAAQ,YAAY,QAAQ;AAEjC,UAAM,UAAU,iBAAiB,QAAW,KAAK,kBAAkB,MAAM,KAAK,SAAS,cAAc,KAAK,SAAS,OAAO;AAC1H,SAAK,MAAM,OAAO,QAAQ;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA,EAKQ,qBAA2B;AACjC,QAAI,KAAK,SAAS,kBAAkB,KAAM;AAC1C,SAAK,QAAQ,YAAY,oBAAoB;AAE7C,UAAM,gBAAgB,KAAK,IAAI,IAAI,cAAc,KAAK,SAAS,cAAc;AAC7E,qBAAiB,QAAQ,KAAK,kBAAkB,YAAY,EAAC,MAAM,cAAa,GAAG,KAAK,SAAS,OAAO;AAExG,SAAK,QAAQ,WAAW,uBAAuB,EAAC,cAAa,CAAC;AAAA,EAChE;AACF;;;AF9JA,aAAc,eAAc,IAAI,iBAAkB,OAAmB;",
3
+ "sources": ["../src/main.ts", "../src/store/store-generator.ts", "../src/store/store.ts", "../src/store/store-with-backup.ts"],
4
+ "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\nexport * from './store/store-generator.js';\nexport * from './store/store.js';\nexport * from './store/store-with-backup.js';\n", "import {createLogger} from '@alwatr/logger';\nimport alpine from 'alpinejs';\n\nconst logger = createLogger(__package_name__);\n\n/**\n * alpineStoreGenerator Options.\n *\n * @template T - The type of the default value.\n * @property {string} name - The name of the store.\n * @property {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreGeneratorOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Generates an Alpine.js store with default value.\n *\n * @template T - The type of the data.\n * @param {AlpineStoreGeneratorOptions} config - The configuration object for the store.\n * @returns {T} - The initialized store instance.\n *\n * @example\n * const store = alpineStoreGenerator({\n * name: 'user',\n * defaultValue: {type: 'root'},\n * });\n *\n * console.log(store.type); // Output: root\n *\n * @description\n * This function uses Alpine.js to create a reactive store with a default value.\n * The store is identified by a unique name and can be accessed and manipulated\n * throughout the application. Alpine.js stores provide a simple way to manage\n * data in your application, making it easy to keep your UI in sync with your data.\n *\n * @see https://alpinejs.dev/globals/alpine-store\n */\nexport function alpineStoreGenerator<T extends DictionaryReq>(config: AlpineStoreGeneratorOptions<T>): T {\n logger.logMethodArgs?.('alpineStoreGenerator', config);\n\n alpine.store(config.name, config.defaultValue);\n\n // Get store Proxy\n const store = alpine.store(config.name) as T;\n return store;\n}\n", "import {type AlwatrLogger, createLogger} from '@alwatr/logger';\n\n/**\n * AlpineStore Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {T} defaultValue - The default value of the store.\n */\nexport type AlpineStoreOptions<T extends DictionaryReq> = {\n name: string;\n defaultValue: T;\n};\n\n/**\n * Provides a Alpine.js pure store implementation with logger.\n */\nexport class AlpineStore<T extends DictionaryReq> {\n /**\n * The store's data.\n */\n store: T;\n\n protected logger_: AlwatrLogger;\n\n /**\n * Provides a Alpine.js pure store implementation with logger.\n *\n * @param {AlpineStoreOptions} config - Configuration object.\n *\n * @example\n * import {AlpineStore} from '@nexim/alpine';\n *\n * const {store} = new AlpineStore({\n * name: 'myStore',\n * defaultValue: {data: 'root'},\n * });\n * console.log(store.data); // Output: { data: 'root' }\n *\n * store.data = 'user';\n * console.log(store.data); // Output: { data: 'user' }\n */\n constructor(config: AlpineStoreOptions<T>) {\n this.logger_ = createLogger(`${__package_name__}:${config.name}`);\n this.logger_.logMethodArgs?.('constructor', config);\n\n this.store = config.defaultValue;\n }\n}\n", "import {localJsonStorage} from '@alwatr/local-storage';\nimport {parseDuration, type Duration} from '@alwatr/parse-duration';\n\nimport {AlpineStore} from './store.js';\n\n/**\n * Type for the store's data to extends from them.\n */\nexport type AlpineStoreWithBackupType = {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n data: DictionaryReq<any> | null;\n};\n\n/**\n * AlpineStoreWithBackup Options.\n *\n * @template T - The type of the store value.\n * @param {string} name - The name of the store.\n * @param {number} version - The version of the store.\n * @param {T} defaultValue - The default value of the store.\n * @param {Duration} [expireDuration] - Optional. The duration after which the store expires.\n */\nexport type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> = {\n name: string;\n version: number;\n defaultValue: T;\n expireDuration?: Duration;\n};\n\n/**\n * Version of the schema for storing data in local storage.\n *\n * Change when this schema changes.\n */\nconst schemaVersion = 1;\n\n/**\n * Provides a Alpine.js store implementation with backup and expiration.\n */\nexport class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> extends AlpineStore<T> {\n /**\n * Keys for storing data and expire time in local storage with version.\n */\n private localStorageKey__ = {\n data: `[${__package_name__}:data:sv${schemaVersion}]:${this.config__.name}`,\n expireTime: `[${__package_name__}:expire-time:sv${schemaVersion}]:${this.config__.name}`,\n };\n\n /**\n * Provides a Alpine.js store implementation with backup and expiration.\n *\n * @param {AlpineStoreWithBackupOptions} config__ - Configuration object.\n *\n * @example\n * import {AlpineStoreWithBackup} from '@nexim/alpine';\n *\n * const storeWithBackup = new AlpineStoreWithBackup({\n * name: 'myStoreWithBackup',\n * version: 1,\n * defaultValue: {data: 'root'},\n * expireDuration: '1d',\n * });\n *\n * storeWithBackup.store.data = 'user';\n *\n * storeWithBackup.save();\n * console.log(storeWithBackup.store.data); // Output: { data: 'user' }\n *\n * storeWithBackup.clear();\n * console.log(storeWithBackup.store.data); // Output: { data: 'root' }\n */\n constructor(private config__: AlpineStoreWithBackupOptions<T>) {\n super(config__);\n\n if (this.config__.expireDuration !== null) {\n this.handleDataExpiration__();\n }\n this.load__();\n }\n\n /**\n * Saves the current data to local storage. If the data is null, it clears the stored data.\n *\n * Also updates the expiration time.\n */\n save(): void {\n this.logger_.logMethodArgs?.('save', {data: this.store.data});\n\n if (this.store.data === null) {\n this.clear();\n return;\n }\n\n localJsonStorage.setItem(this.localStorageKey__.data, this.store.data, this.config__.version);\n this.updateExpireTime__();\n }\n\n /**\n * Clears the stored data.\n */\n clear(): void {\n this.logger_.logMethod?.('clear');\n\n localJsonStorage.removeItem(this.localStorageKey__.data, this.config__.version);\n localJsonStorage.removeItem(this.localStorageKey__.expireTime, this.config__.version);\n\n this.store = this.config__.defaultValue;\n }\n\n /**\n * Handles the expiration duration by checking if the stored data has expired.\n * If expired, it clears the stored data.\n */\n private handleDataExpiration__(): void {\n this.logger_.logMethod?.('handleDataExpiration__');\n\n const expireDuration = localJsonStorage.getItem<{time: number}>(\n this.localStorageKey__.expireTime,\n {time: 0},\n this.config__.version,\n ).time;\n\n if (expireDuration < Date.now()) {\n this.clear();\n }\n }\n\n /**\n * Loads data from local storage and updates the store's data.\n *\n * When data is not found or invalid in local storage, it uses the default value.\n */\n private load__(): void {\n this.logger_.logMethod?.('load__');\n\n const newData = localJsonStorage.getItem<T>(this.localStorageKey__.data, this.config__.defaultValue, this.config__.version);\n this.store.data = newData.data;\n }\n\n /**\n * Updates the expiration time in local storage to the current time plus the configured expiration duration.\n */\n private updateExpireTime__(): void {\n if (this.config__.expireDuration == null) return;\n this.logger_.logMethod?.('updateExpireTime__');\n\n const newExpireTime = Date.now() + parseDuration(this.config__.expireDuration);\n localJsonStorage.setItem(this.localStorageKey__.expireTime, {time: newExpireTime}, this.config__.version);\n\n this.logger_.logOther?.('updated_expire_time', {newExpireTime});\n }\n}\n"],
5
+ "mappings": ";;;AAAA,SAAQ,qBAAoB;;;ACA5B,SAAQ,oBAAmB;AAC3B,OAAO,YAAY;AAEnB,IAAM,SAAS,aAAa,eAAgB;AAqCrC,SAAS,qBAA8C,QAA2C;AACvG,SAAO,gBAAgB,wBAAwB,MAAM;AAErD,SAAO,MAAM,OAA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5 @@
1
- import { type AlwatrLogger } from '@alwatr/logger';
2
1
  import { type Duration } from '@alwatr/parse-duration';
2
+ import { AlpineStore } from './store.js';
3
3
  /**
4
4
  * Type for the store's data to extends from them.
5
5
  */
@@ -24,13 +24,8 @@ export type AlpineStoreWithBackupOptions<T extends AlpineStoreWithBackupType> =
24
24
  /**
25
25
  * Provides a Alpine.js store implementation with backup and expiration.
26
26
  */
27
- export declare class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> {
27
+ export declare class AlpineStoreWithBackup<T extends AlpineStoreWithBackupType> extends AlpineStore<T> {
28
28
  private config__;
29
- /**
30
- * The store's data.
31
- */
32
- store: T;
33
- protected logger_: AlwatrLogger;
34
29
  /**
35
30
  * Keys for storing data and expire time in local storage with version.
36
31
  */
@@ -1 +1 @@
1
- {"version":3,"file":"store-with-backup.d.ts","sourceRoot":"","sources":["../../src/store/store-with-backup.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,KAAK,YAAY,EAAe,MAAM,gBAAgB,CAAC;AAC/D,OAAO,EAAgB,KAAK,QAAQ,EAAC,MAAM,wBAAwB,CAAC;AAEpE;;GAEG;AACH,MAAM,MAAM,yBAAyB,GAAG;IAEtC,IAAI,EAAE,aAAa,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;CACjC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,MAAM,4BAA4B,CAAC,CAAC,SAAS,yBAAyB,IAAI;IAC9E,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,CAAC,CAAC;IAChB,cAAc,CAAC,EAAE,QAAQ,CAAC;CAC3B,CAAC;AASF;;GAEG;AACH,qBAAa,qBAAqB,CAAC,CAAC,SAAS,yBAAyB;IAuCxD,OAAO,CAAC,QAAQ;IAtC5B;;OAEG;IACH,KAAK,EAAE,CAAC,CAAC;IAET,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC;IAEhC;;OAEG;IACH,OAAO,CAAC,i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C,MAAM;IAOd;;OAEG;IACH,OAAO,CAAC,kBAAkB;CAS3B"}
@@ -0,0 +1,41 @@
1
+ import { type AlwatrLogger } from '@alwatr/logger';
2
+ /**
3
+ * AlpineStore Options.
4
+ *
5
+ * @template T - The type of the store value.
6
+ * @param {string} name - The name of the store.
7
+ * @param {T} defaultValue - The default value of the store.
8
+ */
9
+ export type AlpineStoreOptions<T extends DictionaryReq> = {
10
+ name: string;
11
+ defaultValue: T;
12
+ };
13
+ /**
14
+ * Provides a Alpine.js pure store implementation with logger.
15
+ */
16
+ export declare class AlpineStore<T extends DictionaryReq> {
17
+ /**
18
+ * The store's data.
19
+ */
20
+ store: T;
21
+ protected logger_: AlwatrLogger;
22
+ /**
23
+ * Provides a Alpine.js pure store implementation with logger.
24
+ *
25
+ * @param {AlpineStoreOptions} config - Configuration object.
26
+ *
27
+ * @example
28
+ * import {AlpineStore} from '@nexim/alpine';
29
+ *
30
+ * const {store} = new AlpineStore({
31
+ * name: 'myStore',
32
+ * defaultValue: {data: 'root'},
33
+ * });
34
+ * console.log(store.data); // Output: { data: 'root' }
35
+ *
36
+ * store.data = 'user';
37
+ * console.log(store.data); // Output: { data: 'user' }
38
+ */
39
+ constructor(config: AlpineStoreOptions<T>);
40
+ }
41
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../src/store/store.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,YAAY,EAAe,MAAM,gBAAgB,CAAC;AAE/D;;;;;;GAMG;AACH,MAAM,MAAM,kBAAkB,CAAC,CAAC,SAAS,aAAa,IAAI;IACxD,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,CAAC,CAAC;CACjB,CAAC;AAEF;;GAEG;AACH,qBAAa,WAAW,CAAC,CAAC,SAAS,aAAa;IAC9C;;OAEG;IACH,KAAK,EAAE,CAAC,CAAC;IAET,SAAS,CAAC,OAAO,EAAE,YAAY,CAAC;IAEhC;;;;;;;;;;;;;;;;OAgBG;gBACS,MAAM,EAAE,kBAAkB,CAAC,CAAC,CAAC;CAM1C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nexim/alpine",
3
- "version": "0.0.1",
3
+ "version": "1.0.0-alpha.0",
4
4
  "description": "Utility functions to enhance Alpine.js usage with backup support.",
5
5
  "keywords": [
6
6
  "alpinejs",
@@ -74,5 +74,5 @@
74
74
  "publishConfig": {
75
75
  "access": "public"
76
76
  },
77
- "gitHead": "a02afd5953a471c872d63e4eb2e7ccbf90c21abb"
77
+ "gitHead": "9bcba09ec53483b8fd6428a3cd1e5461b81e925e"
78
78
  }