@pixel-pulse/next-cache-brain-core 0.2.0 → 0.3.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/index.d.mts CHANGED
@@ -1,3 +1,17 @@
1
+ interface CacheEntry<T> {
2
+ key: string;
3
+ data: T;
4
+ expiry: number;
5
+ ttl: number;
6
+ }
7
+
8
+ type Listener = (data: any[]) => void;
9
+ declare const cacheEngine: {
10
+ subscribe(fn: Listener): () => boolean;
11
+ getValues(): CacheEntry<any>[];
12
+ notify(data?: any[]): void;
13
+ };
14
+
1
15
  type Strategy = "cache-first" | "network-first" | "swr" | "no-cache";
2
16
  type SmartFetchOptions = {
3
17
  strategy?: Strategy;
@@ -5,17 +19,27 @@ type SmartFetchOptions = {
5
19
  dedupe?: boolean;
6
20
  };
7
21
 
8
- declare function smartFetch(url: string, options?: SmartFetchOptions): Promise<any>;
22
+ /**
23
+ * smartFetch is the main entry point for the library.
24
+ * It handles deduplication, strategy selection, and DevTools notification.
25
+ */
26
+ declare function smartFetch<T = any>(url: string, options?: SmartFetchOptions): Promise<T>;
27
+
28
+ declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
9
29
 
10
30
  declare function clearCache(url?: string): void;
11
31
 
12
- interface CacheEntry<T> {
13
- key: string;
14
- data: T;
15
- expiry: number;
16
- ttl: number;
32
+ /**
33
+ * GLOBAL TYPE DEFINITION
34
+ * Informs TypeScript about our DevTools bridge on the window object.
35
+ */
36
+ declare global {
37
+ interface Window {
38
+ __NCB_DEVTOOLS__?: {
39
+ getCache: () => any[];
40
+ subscribe: (cb: () => void) => () => void;
41
+ };
42
+ }
17
43
  }
18
44
 
19
- declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
20
-
21
- export { type CacheEntry, type SmartFetchOptions, clearCache, setCache, smartFetch };
45
+ export { type CacheEntry, cacheEngine, clearCache, setCache, smartFetch };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,17 @@
1
+ interface CacheEntry<T> {
2
+ key: string;
3
+ data: T;
4
+ expiry: number;
5
+ ttl: number;
6
+ }
7
+
8
+ type Listener = (data: any[]) => void;
9
+ declare const cacheEngine: {
10
+ subscribe(fn: Listener): () => boolean;
11
+ getValues(): CacheEntry<any>[];
12
+ notify(data?: any[]): void;
13
+ };
14
+
1
15
  type Strategy = "cache-first" | "network-first" | "swr" | "no-cache";
2
16
  type SmartFetchOptions = {
3
17
  strategy?: Strategy;
@@ -5,17 +19,27 @@ type SmartFetchOptions = {
5
19
  dedupe?: boolean;
6
20
  };
7
21
 
8
- declare function smartFetch(url: string, options?: SmartFetchOptions): Promise<any>;
22
+ /**
23
+ * smartFetch is the main entry point for the library.
24
+ * It handles deduplication, strategy selection, and DevTools notification.
25
+ */
26
+ declare function smartFetch<T = any>(url: string, options?: SmartFetchOptions): Promise<T>;
27
+
28
+ declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
9
29
 
10
30
  declare function clearCache(url?: string): void;
11
31
 
12
- interface CacheEntry<T> {
13
- key: string;
14
- data: T;
15
- expiry: number;
16
- ttl: number;
32
+ /**
33
+ * GLOBAL TYPE DEFINITION
34
+ * Informs TypeScript about our DevTools bridge on the window object.
35
+ */
36
+ declare global {
37
+ interface Window {
38
+ __NCB_DEVTOOLS__?: {
39
+ getCache: () => any[];
40
+ subscribe: (cb: () => void) => () => void;
41
+ };
42
+ }
17
43
  }
18
44
 
19
- declare function setCache<T>(key: string, data: T, ttl: number): CacheEntry<T>;
20
-
21
- export { type CacheEntry, type SmartFetchOptions, clearCache, setCache, smartFetch };
45
+ export { type CacheEntry, cacheEngine, clearCache, setCache, smartFetch };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var h=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var F=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var N=(t,e)=>{for(var r in e)h(t,r,{get:e[r],enumerable:!0})},v=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of F(e))!D.call(t,o)&&o!==r&&h(t,o,{get:()=>e[o],enumerable:!(n=O(e,o))||n.enumerable});return t};var I=t=>v(h({},"__esModule",{value:!0}),t);var K={};N(K,{clearCache:()=>T,setCache:()=>c,smartFetch:()=>E});module.exports=I(K);var p=Symbol.for("ncb.cache"),m=Symbol.for("ncb.pending"),g=Symbol.for("ncb.inflight");globalThis[p]||(globalThis[p]=new Map);globalThis[m]||(globalThis[m]=new Map);globalThis[g]||(globalThis[g]=new Map);var s=globalThis[p],i=globalThis[m],f=globalThis[g];var l="ncb_cache_snapshot",y=new Set,w=t=>{t&&typeof window<"u"&&localStorage.setItem(l,JSON.stringify(t)),y.forEach(e=>e())},b=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:()=>{let e=t();if(e.length===0){let r=localStorage.getItem(l);return r?JSON.parse(r):[]}return e},subscribe(e){return y.add(e),()=>y.delete(e)}})};var u="ncb_persistent_storage";function c(t,e,r){let n={key:t,data:e,expiry:Date.now()+r,ttl:r};if(s.set(t,n),typeof window<"u"){let o=localStorage.getItem(u),a=o?JSON.parse(o):{};a[t]=n,localStorage.setItem(u,JSON.stringify(a))}return w(),n}async function x(t,e){let r=Date.now(),n=s.get(t);if(n&&n.expiry>r)return n.data;if(i.has(t))return i.get(t);let o=fetch(t).then(a=>a.json()).then(a=>(c(t,a,e),i.delete(t),a)).catch(a=>{throw i.delete(t),a});return i.set(t,o),o}async function C(t,e){try{let n=await(await fetch(t)).json();return c(t,n,e),n}catch(r){let n=s.get(t);if(n)return n.data;throw r}}async function S(t){return(await fetch(t)).json()}async function _(t,e){let r=s.get(t);if(r)return fetch(t).then(a=>a.json()).then(a=>c(t,a,e)).catch(a=>console.error("SWR Background Fetch Failed:",a)),r.data;let o=await(await fetch(t)).json();return c(t,o,e),o}async function E(t,e){let r=e?.ttl??1e4,n=e?.strategy??"cache-first",o=e?.dedupe??!1;if(o&&f.has(t))return f.get(t);let d=(async()=>{try{switch(n){case"network-first":return await C(t,r);case"swr":return await _(t,r);case"no-cache":return await S(t);default:return await x(t,r)}}finally{o&&f.delete(t)}})();return o&&f.set(t,d),d}function T(t){t?s.delete(t):s.clear()}var j="ncb_persistent_storage";if(typeof window<"u"){try{let t=localStorage.getItem(j);if(t){let e=JSON.parse(t);Object.values(e).forEach(r=>{r.expiry>Date.now()&&s.set(r.key,r)})}}catch(t){console.error("NCB Core: Hydration failed",t)}b(()=>Array.from(s.values()))}0&&(module.exports={clearCache,setCache,smartFetch});
1
+ "use strict";var d=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var D=(e,t)=>{for(var n in t)d(e,n,{get:t[n],enumerable:!0})},I=(e,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of N(t))!F.call(e,r)&&r!==n&&d(e,r,{get:()=>t[r],enumerable:!(o=O(t,r))||o.enumerable});return e};var B=e=>I(d({},"__esModule",{value:!0}),e);var P={};D(P,{cacheEngine:()=>c,clearCache:()=>T,setCache:()=>i,smartFetch:()=>x});module.exports=B(P);var g=new Set,c={subscribe(e){return g.add(e),e(this.getValues()),()=>g.delete(e)},getValues(){return Array.from(s.values())},notify(e){let t=e||this.getValues();g.forEach(n=>n(t))}};var y=Symbol.for("ncb.cache"),l=Symbol.for("ncb.pending"),w=Symbol.for("ncb.inflight");globalThis[y]||(globalThis[y]=new Map);globalThis[l]||(globalThis[l]=new Map);globalThis[w]||(globalThis[w]=new Map);var s=globalThis[y],m=globalThis[l],p=globalThis[w];var A="ncb_cache_snapshot",L="ncb-devtools-update";var h=()=>{if(typeof window>"u")return;let e=c.getValues();try{localStorage.setItem(A,JSON.stringify(e))}catch(t){console.warn("NCB: Persist failed",t)}c.notify(e),window.dispatchEvent(new CustomEvent(L,{detail:e}))};var E="ncb_persistent_storage";function i(e,t,n){let o={key:e,data:t,expiry:Date.now()+n,ttl:n};if(s.set(e,o),typeof window<"u")try{let r=localStorage.getItem(E),a=r?JSON.parse(r):{};a[e]=o,localStorage.setItem(E,JSON.stringify(a))}catch(r){console.warn("NCB: Persistence failed",r)}return h(),o}async function b(e,t){let n=Date.now(),o=s.get(e);if(o&&o.expiry>n)return o.data;if(m.has(e))return m.get(e);let r=fetch(e).then(a=>a.json()).then(a=>(i(e,a,t),m.delete(e),a)).catch(a=>{throw m.delete(e),a});return m.set(e,r),r}async function C(e,t){try{let o=await(await fetch(e)).json();return i(e,o,t),o}catch(n){let o=s.get(e);if(o)return o.data;throw n}}async function S(e){return(await fetch(e)).json()}async function _(e,t){let n=s.get(e);if(n)return fetch(e).then(a=>a.json()).then(a=>i(e,a,t)).catch(a=>console.error("SWR Background Fetch Failed:",a)),n.data;let r=await(await fetch(e)).json();return i(e,r,t),r}async function x(e,t){let n=t?.ttl??1e4,o=t?.strategy??"cache-first",r=t?.dedupe??!0;if(r&&p.has(e))return p.get(e);let u=(async()=>{try{let f;switch(o){case"network-first":f=await C(e,n);break;case"swr":f=await _(e,n);break;case"no-cache":f=await S(e);break;default:f=await b(e,n);break}return h(),f}catch(f){throw console.error(`[NextCacheBrain] Fetch failed for ${e}:`,f),f}finally{r&&p.delete(e)}})();return r&&p.set(e,u),u}var v="ncb_persistent_storage";function T(e){if(e){if(s.delete(e),typeof window<"u"){let t=Array.from(s.values());localStorage.setItem(v,JSON.stringify(t))}}else s.clear(),typeof window<"u"&&localStorage.removeItem(v);h()}if(typeof window<"u"){try{let e=localStorage.getItem("ncb_persistent_storage");if(e){let t=JSON.parse(e);Object.values(t).forEach(n=>{n.expiry>Date.now()&&s.set(n.key,n)}),c.notify()}}catch(e){console.error("NCB Core: Hydration failed",e)}window.__NCB_DEVTOOLS__={getCache:()=>c.getValues(),subscribe:e=>c.subscribe(e)}}0&&(module.exports={cacheEngine,clearCache,setCache,smartFetch});
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var h=Symbol.for("ncb.cache"),p=Symbol.for("ncb.pending"),m=Symbol.for("ncb.inflight");globalThis[h]||(globalThis[h]=new Map);globalThis[p]||(globalThis[p]=new Map);globalThis[m]||(globalThis[m]=new Map);var a=globalThis[h],i=globalThis[p],f=globalThis[m];var d="ncb_cache_snapshot",g=new Set,l=t=>{t&&typeof window<"u"&&localStorage.setItem(d,JSON.stringify(t)),g.forEach(e=>e())},w=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:()=>{let e=t();if(e.length===0){let r=localStorage.getItem(d);return r?JSON.parse(r):[]}return e},subscribe(e){return g.add(e),()=>g.delete(e)}})};var b="ncb_persistent_storage";function c(t,e,r){let n={key:t,data:e,expiry:Date.now()+r,ttl:r};if(a.set(t,n),typeof window<"u"){let s=localStorage.getItem(b),o=s?JSON.parse(s):{};o[t]=n,localStorage.setItem(b,JSON.stringify(o))}return l(),n}async function u(t,e){let r=Date.now(),n=a.get(t);if(n&&n.expiry>r)return n.data;if(i.has(t))return i.get(t);let s=fetch(t).then(o=>o.json()).then(o=>(c(t,o,e),i.delete(t),o)).catch(o=>{throw i.delete(t),o});return i.set(t,s),s}async function x(t,e){try{let n=await(await fetch(t)).json();return c(t,n,e),n}catch(r){let n=a.get(t);if(n)return n.data;throw r}}async function C(t){return(await fetch(t)).json()}async function S(t,e){let r=a.get(t);if(r)return fetch(t).then(o=>o.json()).then(o=>c(t,o,e)).catch(o=>console.error("SWR Background Fetch Failed:",o)),r.data;let s=await(await fetch(t)).json();return c(t,s,e),s}async function _(t,e){let r=e?.ttl??1e4,n=e?.strategy??"cache-first",s=e?.dedupe??!1;if(s&&f.has(t))return f.get(t);let y=(async()=>{try{switch(n){case"network-first":return await x(t,r);case"swr":return await S(t,r);case"no-cache":return await C(t);default:return await u(t,r)}}finally{s&&f.delete(t)}})();return s&&f.set(t,y),y}function E(t){t?a.delete(t):a.clear()}var T="ncb_persistent_storage";if(typeof window<"u"){try{let t=localStorage.getItem(T);if(t){let e=JSON.parse(t);Object.values(e).forEach(r=>{r.expiry>Date.now()&&a.set(r.key,r)})}}catch(t){console.error("NCB Core: Hydration failed",t)}w(()=>Array.from(a.values()))}export{E as clearCache,c as setCache,_ as smartFetch};
1
+ var d=new Set,c={subscribe(e){return d.add(e),e(this.getValues()),()=>d.delete(e)},getValues(){return Array.from(a.values())},notify(e){let t=e||this.getValues();d.forEach(n=>n(t))}};var g=Symbol.for("ncb.cache"),y=Symbol.for("ncb.pending"),l=Symbol.for("ncb.inflight");globalThis[g]||(globalThis[g]=new Map);globalThis[y]||(globalThis[y]=new Map);globalThis[l]||(globalThis[l]=new Map);var a=globalThis[g],m=globalThis[y],p=globalThis[l];var x="ncb_cache_snapshot",v="ncb-devtools-update";var h=()=>{if(typeof window>"u")return;let e=c.getValues();try{localStorage.setItem(x,JSON.stringify(e))}catch(t){console.warn("NCB: Persist failed",t)}c.notify(e),window.dispatchEvent(new CustomEvent(v,{detail:e}))};var u="ncb_persistent_storage";function f(e,t,n){let r={key:e,data:t,expiry:Date.now()+n,ttl:n};if(a.set(e,r),typeof window<"u")try{let s=localStorage.getItem(u),o=s?JSON.parse(s):{};o[e]=r,localStorage.setItem(u,JSON.stringify(o))}catch(s){console.warn("NCB: Persistence failed",s)}return h(),r}async function E(e,t){let n=Date.now(),r=a.get(e);if(r&&r.expiry>n)return r.data;if(m.has(e))return m.get(e);let s=fetch(e).then(o=>o.json()).then(o=>(f(e,o,t),m.delete(e),o)).catch(o=>{throw m.delete(e),o});return m.set(e,s),s}async function b(e,t){try{let r=await(await fetch(e)).json();return f(e,r,t),r}catch(n){let r=a.get(e);if(r)return r.data;throw n}}async function C(e){return(await fetch(e)).json()}async function S(e,t){let n=a.get(e);if(n)return fetch(e).then(o=>o.json()).then(o=>f(e,o,t)).catch(o=>console.error("SWR Background Fetch Failed:",o)),n.data;let s=await(await fetch(e)).json();return f(e,s,t),s}async function T(e,t){let n=t?.ttl??1e4,r=t?.strategy??"cache-first",s=t?.dedupe??!0;if(s&&p.has(e))return p.get(e);let w=(async()=>{try{let i;switch(r){case"network-first":i=await b(e,n);break;case"swr":i=await S(e,n);break;case"no-cache":i=await C(e);break;default:i=await E(e,n);break}return h(),i}catch(i){throw console.error(`[NextCacheBrain] Fetch failed for ${e}:`,i),i}finally{s&&p.delete(e)}})();return s&&p.set(e,w),w}var _="ncb_persistent_storage";function O(e){if(e){if(a.delete(e),typeof window<"u"){let t=Array.from(a.values());localStorage.setItem(_,JSON.stringify(t))}}else a.clear(),typeof window<"u"&&localStorage.removeItem(_);h()}if(typeof window<"u"){try{let e=localStorage.getItem("ncb_persistent_storage");if(e){let t=JSON.parse(e);Object.values(t).forEach(n=>{n.expiry>Date.now()&&a.set(n.key,n)}),c.notify()}}catch(e){console.error("NCB Core: Hydration failed",e)}window.__NCB_DEVTOOLS__={getCache:()=>c.getValues(),subscribe:e=>c.subscribe(e)}}export{c as cacheEngine,O as clearCache,f as setCache,T as smartFetch};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pixel-pulse/next-cache-brain-core",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",