qortex-core 0.3.0-beta.3 → 0.3.0-beta.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qortex-core",
3
- "version": "0.3.0-beta.3",
3
+ "version": "0.3.0-beta.4",
4
4
  "description": "Framework-agnostic query cache & fetch registry (MFE friendly).",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -11,6 +11,11 @@
11
11
  "types": "./index.d.ts",
12
12
  "import": "./index.mjs",
13
13
  "require": "./index.js"
14
+ },
15
+ "./persister-export": {
16
+ "types": "./persister-export.d.ts",
17
+ "import": "./persister-export.mjs",
18
+ "require": "./persister-export.js"
14
19
  }
15
20
  },
16
21
  "publishConfig": {
@@ -20,6 +25,9 @@
20
25
  "index.js",
21
26
  "index.mjs",
22
27
  "index.d.ts",
28
+ "persister-export.js",
29
+ "persister-export.mjs",
30
+ "persister-export.d.ts",
23
31
  "README.md"
24
32
  ],
25
33
  "repository": {
@@ -0,0 +1,48 @@
1
+ import { P as PersisterConfig, B as BasePersister } from './types-14ced1fe.js';
2
+ export { e as Persister } from './types-14ced1fe.js';
3
+
4
+ /**
5
+ * Creates a persister instance for data persistence.
6
+ *
7
+ * This factory function creates a persister that can save and load query state data
8
+ * to/from browser storage (localStorage or sessionStorage). The persister handles
9
+ * serialization, validation, burst key management, and debounced syncing automatically.
10
+ *
11
+ * @param {'local' | 'session'} type - The type of storage to use
12
+ * - `'local'`: Uses localStorage for persistent data across browser sessions
13
+ * - `'session'`: Uses sessionStorage for data that persists only during the current session
14
+ * @param {PersisterConfig} [config] - Optional configuration for the persister
15
+ * @returns {Persister | undefined} A persister instance, or undefined if storage is not available
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Basic usage with localStorage
20
+ * const persister = createPersister('local');
21
+ *
22
+ * // With custom configuration
23
+ * const persister = createPersister('local', {
24
+ * burstKey: 'v1.0.0',
25
+ * prefix: 'my_app',
26
+ * debounceTime: 50
27
+ * });
28
+ *
29
+ * // Use with query manager
30
+ * setDefaultConfig({ persister });
31
+ * ```
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // Session storage for temporary data
36
+ * const sessionPersister = createPersister('session', {
37
+ * prefix: 'temp_data',
38
+ * debounceTime: 200
39
+ * });
40
+ *
41
+ * setDefaultConfig({ persister: sessionPersister });
42
+ * ```
43
+ *
44
+ * @throws {Error} If an invalid storage type is provided
45
+ */
46
+ declare const createPersister: (type: "local" | "session", config?: PersisterConfig) => BasePersister | undefined;
47
+
48
+ export { PersisterConfig, createPersister };
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ function g(e){return typeof e=="string"&&["idle","fetching","success","error"].includes(e)}function h(e){return typeof e=="string"&&["shallow","deep"].includes(e)}function b(e){return e===!1||typeof e=="string"&&["always","stale"].includes(e)}function Q(e){return !(!e||typeof e!="object"||!g(e.status)||typeof e.staleTime!="number"||e.staleTime<0||typeof e.isInvalidated!="boolean"||!h(e.equalityStrategy)||!b(e.refetchOnSubscribe))}function n(e){if(!e||typeof e!="object"||!e.entries||typeof e.entries!="object")return !1;for(let[t,r]of Object.entries(e.entries))if(typeof t!="string"||!Q(r))return !1;return !(typeof e.burstKey!="string"||typeof e.timestamp!="number"||e.timestamp<0)}function l(e){let{fetcher:t,equalityFn:r,fetchPromise:s,refetch:i,...a}=e;return a}function u(e,t){return {...t??{},...e}}function y(e){try{let t=JSON.parse(e);return n(t)?t:null}catch{return null}}var f=()=>{console.warn("[Qortex] No storage found, persister will not be able to persist data");},c=()=>{console.warn("[Qortex] Persister is being set after queries have been used. This may cause data inconsistency. It is recommended to set the persister before any query usage.");};var o=class{constructor(t,r){this.syncTimeout=null;this.debounceTime=100;this.burstKey="0.3.0";this.storageKey="qortex";this.storage=t,this.burstKey=r?.burstKey??this.burstKey,this.storageKey=r?.prefix??this.storageKey,this.debounceTime=r?.debounceTime??this.debounceTime;}save(t){try{let r={entries:{},burstKey:this.burstKey,timestamp:Date.now()};for(let[i,a]of Object.entries(t))r.entries[i]=a;let s=JSON.stringify(r);this.storage.setItem(this.storageKey,s);}catch(r){console.warn("[Qortex] Failed to persist state:",r);}}load(t,r){r&&c();try{let s=this.storage.getItem(this.storageKey);if(!s)return;let i=y(s);if(!i){console.warn("[Qortex] Invalid persisted state format, clearing cache"),this.clear();return}if(i.burstKey!==this.burstKey){console.warn("[Qortex] Burst key mismatch, clearing cache"),this.clear();return}for(let[a,p]of Object.entries(i.entries)){let S=p,d=t.get(a),m=u(S,d);t.set(a,m);}}catch(s){console.warn("[Qortex] Failed to load persisted state:",s),this.clear();}}clear(){try{this.storage.removeItem(this.storageKey);}catch(t){console.warn("[Qortex] Failed to clear persisted data:",t);}}sync(t){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>{let r={};for(let[s,i]of t.entries())r[s]=l(i);this.save(r);},this.debounceTime);}};var P=e=>{switch(e){case"local":if(typeof localStorage<"u")return window.localStorage;break;case"session":if(typeof sessionStorage<"u")return window.sessionStorage;break;default:throw new Error(`Invalid storage type: ${e}`)}},w=(e,t)=>{let r=P(e);if(!r){f();return}return new o(r,t)};
4
+
5
+ exports.createPersister = w;
@@ -0,0 +1,3 @@
1
+ function g(e){return typeof e=="string"&&["idle","fetching","success","error"].includes(e)}function h(e){return typeof e=="string"&&["shallow","deep"].includes(e)}function b(e){return e===!1||typeof e=="string"&&["always","stale"].includes(e)}function Q(e){return !(!e||typeof e!="object"||!g(e.status)||typeof e.staleTime!="number"||e.staleTime<0||typeof e.isInvalidated!="boolean"||!h(e.equalityStrategy)||!b(e.refetchOnSubscribe))}function n(e){if(!e||typeof e!="object"||!e.entries||typeof e.entries!="object")return !1;for(let[t,r]of Object.entries(e.entries))if(typeof t!="string"||!Q(r))return !1;return !(typeof e.burstKey!="string"||typeof e.timestamp!="number"||e.timestamp<0)}function l(e){let{fetcher:t,equalityFn:r,fetchPromise:s,refetch:i,...a}=e;return a}function u(e,t){return {...t??{},...e}}function y(e){try{let t=JSON.parse(e);return n(t)?t:null}catch{return null}}var f=()=>{console.warn("[Qortex] No storage found, persister will not be able to persist data");},c=()=>{console.warn("[Qortex] Persister is being set after queries have been used. This may cause data inconsistency. It is recommended to set the persister before any query usage.");};var o=class{constructor(t,r){this.syncTimeout=null;this.debounceTime=100;this.burstKey="0.3.0";this.storageKey="qortex";this.storage=t,this.burstKey=r?.burstKey??this.burstKey,this.storageKey=r?.prefix??this.storageKey,this.debounceTime=r?.debounceTime??this.debounceTime;}save(t){try{let r={entries:{},burstKey:this.burstKey,timestamp:Date.now()};for(let[i,a]of Object.entries(t))r.entries[i]=a;let s=JSON.stringify(r);this.storage.setItem(this.storageKey,s);}catch(r){console.warn("[Qortex] Failed to persist state:",r);}}load(t,r){r&&c();try{let s=this.storage.getItem(this.storageKey);if(!s)return;let i=y(s);if(!i){console.warn("[Qortex] Invalid persisted state format, clearing cache"),this.clear();return}if(i.burstKey!==this.burstKey){console.warn("[Qortex] Burst key mismatch, clearing cache"),this.clear();return}for(let[a,p]of Object.entries(i.entries)){let S=p,d=t.get(a),m=u(S,d);t.set(a,m);}}catch(s){console.warn("[Qortex] Failed to load persisted state:",s),this.clear();}}clear(){try{this.storage.removeItem(this.storageKey);}catch(t){console.warn("[Qortex] Failed to clear persisted data:",t);}}sync(t){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>{let r={};for(let[s,i]of t.entries())r[s]=l(i);this.save(r);},this.debounceTime);}};var P=e=>{switch(e){case"local":if(typeof localStorage<"u")return window.localStorage;break;case"session":if(typeof sessionStorage<"u")return window.sessionStorage;break;default:throw new Error(`Invalid storage type: ${e}`)}},w=(e,t)=>{let r=P(e);if(!r){f();return}return new o(r,t)};
2
+
3
+ export { w as createPersister };