@pixel-pulse/next-cache-brain-core 0.1.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/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright © 2026 Lin Htet Aung (Liam)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ type Strategy = "cache-first" | "network-first" | "swr" | "no-cache";
2
+ type SmartFetchOptions = {
3
+ strategy?: Strategy;
4
+ ttl?: number;
5
+ dedupe?: boolean;
6
+ };
7
+
8
+ declare function smartFetch(url: string, options?: SmartFetchOptions): Promise<any>;
9
+
10
+ declare function clearCache(url?: string): void;
11
+
12
+ declare global {
13
+ interface Window {
14
+ __NCB_DEVTOOLS__?: {
15
+ getCache: () => any[];
16
+ subscribe: (cb: () => void) => () => void;
17
+ };
18
+ }
19
+ }
20
+ declare const emitUpdate: () => void;
21
+
22
+ export { type SmartFetchOptions, type Strategy, clearCache, emitUpdate, smartFetch };
@@ -0,0 +1,22 @@
1
+ type Strategy = "cache-first" | "network-first" | "swr" | "no-cache";
2
+ type SmartFetchOptions = {
3
+ strategy?: Strategy;
4
+ ttl?: number;
5
+ dedupe?: boolean;
6
+ };
7
+
8
+ declare function smartFetch(url: string, options?: SmartFetchOptions): Promise<any>;
9
+
10
+ declare function clearCache(url?: string): void;
11
+
12
+ declare global {
13
+ interface Window {
14
+ __NCB_DEVTOOLS__?: {
15
+ getCache: () => any[];
16
+ subscribe: (cb: () => void) => () => void;
17
+ };
18
+ }
19
+ }
20
+ declare const emitUpdate: () => void;
21
+
22
+ export { type SmartFetchOptions, type Strategy, clearCache, emitUpdate, smartFetch };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var f=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var E=(t,e)=>{for(var o in e)f(t,o,{get:e[o],enumerable:!0})},S=(t,e,o,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of D(e))!T.call(t,a)&&a!==o&&f(t,a,{get:()=>e[a],enumerable:!(n=C(e,a))||n.enumerable});return t};var O=t=>S(f({},"__esModule",{value:!0}),t);var M={};E(M,{clearCache:()=>_,emitUpdate:()=>i,smartFetch:()=>F});module.exports=O(M);var h=Symbol.for("ncb.cache"),y=Symbol.for("ncb.pending"),d=Symbol.for("ncb.inflight");globalThis[h]||(globalThis[h]=new Map);globalThis[y]||(globalThis[y]=new Map);globalThis[d]||(globalThis[d]=new Map);var r=globalThis[h],p=globalThis[y],m=globalThis[d];var g=new Set,i=()=>{g.forEach(t=>t())},w=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:t,subscribe(e){return g.add(e),()=>g.delete(e)}})};async function b(t,e){let o=Date.now(),n=r.get(t);if(n&&n.expiry>o)return n.data;if(p.has(t))return p.get(t);let a=fetch(t).then(s=>s.json()).then(s=>(r.set(t,{data:s,expiry:Date.now()+e,ttl:e}),p.delete(t),i(),s)).catch(s=>{throw p.delete(t),s});return p.set(t,a),i(),a}async function l(t,e){try{let n=await(await fetch(t)).json();return r.set(t,{data:n,expiry:Date.now()+e,ttl:e}),i(),n}catch(o){let n=r.get(t);if(n)return n.data;throw o}}async function u(t){return(await fetch(t)).json()}async function x(t,e){let o=Date.now(),n=r.get(t);if(n)return fetch(t).then(c=>c.json()).then(c=>{r.set(t,{data:c,expiry:Date.now()+e,ttl:e}),i()}).catch(c=>console.error("SWR Background Fetch Failed:",c)),n.data;let s=await(await fetch(t)).json();return r.set(t,{data:s,expiry:Date.now()+e,ttl:e}),i(),s}async function F(t,e){let o=e?.ttl??1e4,n=e?.strategy??"cache-first",a=e?.dedupe??!1;if(a&&m.has(t))return m.get(t);let c=(async()=>{try{switch(n){case"network-first":return await l(t,o);case"swr":return await x(t,o);case"no-cache":return await u(t);default:return await b(t,o)}}finally{a&&m.delete(t)}})();return a&&m.set(t,c),c}function _(t){t?r.delete(t):r.clear()}typeof window<"u"&&w(()=>Array.from(r.entries()).map(([t,e])=>({key:t,...e})));0&&(module.exports={clearCache,emitUpdate,smartFetch});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ var f=Symbol.for("ncb.cache"),h=Symbol.for("ncb.pending"),y=Symbol.for("ncb.inflight");globalThis[f]||(globalThis[f]=new Map);globalThis[h]||(globalThis[h]=new Map);globalThis[y]||(globalThis[y]=new Map);var n=globalThis[f],p=globalThis[h],m=globalThis[y];var d=new Set,s=()=>{d.forEach(t=>t())},g=t=>{typeof window>"u"||(window.__NCB_DEVTOOLS__={getCache:t,subscribe(e){return d.add(e),()=>d.delete(e)}})};async function w(t,e){let a=Date.now(),r=n.get(t);if(r&&r.expiry>a)return r.data;if(p.has(t))return p.get(t);let c=fetch(t).then(o=>o.json()).then(o=>(n.set(t,{data:o,expiry:Date.now()+e,ttl:e}),p.delete(t),s(),o)).catch(o=>{throw p.delete(t),o});return p.set(t,c),s(),c}async function b(t,e){try{let r=await(await fetch(t)).json();return n.set(t,{data:r,expiry:Date.now()+e,ttl:e}),s(),r}catch(a){let r=n.get(t);if(r)return r.data;throw a}}async function l(t){return(await fetch(t)).json()}async function u(t,e){let a=Date.now(),r=n.get(t);if(r)return fetch(t).then(i=>i.json()).then(i=>{n.set(t,{data:i,expiry:Date.now()+e,ttl:e}),s()}).catch(i=>console.error("SWR Background Fetch Failed:",i)),r.data;let o=await(await fetch(t)).json();return n.set(t,{data:o,expiry:Date.now()+e,ttl:e}),s(),o}async function x(t,e){let a=e?.ttl??1e4,r=e?.strategy??"cache-first",c=e?.dedupe??!1;if(c&&m.has(t))return m.get(t);let i=(async()=>{try{switch(r){case"network-first":return await b(t,a);case"swr":return await u(t,a);case"no-cache":return await l(t);default:return await w(t,a)}}finally{c&&m.delete(t)}})();return c&&m.set(t,i),i}function F(t){t?n.delete(t):n.clear()}typeof window<"u"&&g(()=>Array.from(n.entries()).map(([t,e])=>({key:t,...e})));export{F as clearCache,s as emitUpdate,x as smartFetch};
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@pixel-pulse/next-cache-brain-core",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.js",
5
+ "module": "dist/index.mjs",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "keywords": [
11
+ "nextjs",
12
+ "cache",
13
+ "fetch"
14
+ ],
15
+ "author": "Lin Htet Aung (Liam) (https://github.com/LinnHtetAungSE)",
16
+ "license": "MIT",
17
+ "description": "",
18
+ "dependencies": {
19
+ "tsup": "^8.5.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^25.5.0"
23
+ },
24
+ "scripts": {
25
+ "build": "tsup",
26
+ "dev": "tsup --watch"
27
+ }
28
+ }