klaim 1.8.34 → 1.8.35
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/deno.json +1 -1
- package/dist/klaim.cjs +1 -1
- package/dist/klaim.es.js +22 -22
- package/dist/klaim.umd.js +1 -1
- package/package.json +12 -12
package/deno.json
CHANGED
package/dist/klaim.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var O=Object.defineProperty;var U=(n,t,e)=>t in n?O(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var o=(n,t,e)=>U(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function E(n){return n.replace(/([-_][a-z])/gi,t=>t.toUpperCase().replace("-","").replace("_","")).replace(/(^\w)/,t=>t.toLowerCase())}function D(n){return n.trim().replace(/^\/|\/$/g,"")}const j={page:1,pageParam:"page",limit:10,limitParam:"limit"};class ${constructor(t,e,a,r={}){o(this,"type");o(this,"name");o(this,"url");o(this,"headers");o(this,"parent");o(this,"method");o(this,"arguments",new Set);o(this,"schema");o(this,"pagination");o(this,"callbacks",{before:null,after:null,call:null});o(this,"cache",!1);o(this,"retry",!1);o(this,"withCache",(t=20)=>(this.cache=t,this));o(this,"withRetry",(t=2)=>(this.retry=t,this));this.type=t,this.name=E(e),this.name!==e&&console.warn(`Name "${e}" has been camelCased to "${this.name}"`),this.url=D(a),this.headers=r||{}}before(t){return this.callbacks.before=t,this}after(t){return this.callbacks.after=t,this}onCall(t){return this.callbacks.call=t,this}withPagination(t={}){return this.pagination={...j,...t},this}}const p=class p{constructor(){o(this,"cache");this.cache=new Map}static get i(){return p._instance||(p._instance=new p),p._instance}set(t,e,a=0){const r=Date.now()+a;this.cache.set(t,{data:e,expiry:r})}has(t){const e=this.cache.get(t);return e?Date.now()>e.expiry?(this.cache.delete(t),!1):!0:!1}get(t){return this.has(t)?this.cache.get(t).data:null}};o(p,"_instance");let d=p;function x(n){let a=2166136261;for(let i=0;i<n.length;i++)a^=n.charCodeAt(i),a*=16777619;let r=(a>>>0).toString(16).padStart(8,"0");for(;r.length<32;)a^=r.charCodeAt(r.length%r.length),a*=16777619,r+=(a>>>0).toString(16).padStart(8,"0");return r.substring(0,32)}async function H(n,t,e){const a=`${n.toString()}${JSON.stringify(t)}`,r=x(a);if(d.i.has(r))return d.i.get(r);const c=await(await fetch(n,t)).json();return d.i.set(r,c,e),c}class A{static subscribe(t,e){this._callbacks.set(t,e)}static run(t){const e=this._callbacks.get(t);e&&e()}}o(A,"_callbacks",new Map);const C={};function I(n,t){return async(...e)=>{if(t.pagination){const[i=0,c={},l={}]=e;return F(n,t,i,c,l)}const[a={},r={}]=e;return F(n,t,void 0,a,r)}}async function F(n,t,e,a={},r={}){const i=n.split(".");let c;for(let g=0;g<i.length;g++){const y=i[g];if(c=s.i.getApi(y),c)break}if(!t||!c||t.type!=="route"||c.type!=="api")throw new Error(`Invalid path: ${n}.${t.name}`);let l=W(`${c.url}/${t.url}`,t,a);if(t.pagination&&typeof e<"u"){const{pageParam:g="page",limit:y=10,limitParam:K="limit"}=t.pagination,b=new URLSearchParams;b.append(g,String(e)),b.append(K,String(y));const G=l.includes("?")?"&":"?";l=`${l}${G}${b.toString()}`}let u={};r&&t.method!=="GET"&&(u.body=JSON.stringify(r)),u.headers={"Content-Type":"application/json",...c.headers,...t.headers},u.method=t.method;const{beforeRoute:f,beforeApi:P,beforeUrl:m,beforeConfig:
|
|
1
|
+
"use strict";var O=Object.defineProperty;var U=(n,t,e)=>t in n?O(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var o=(n,t,e)=>U(n,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function E(n){return n.replace(/([-_][a-z])/gi,t=>t.toUpperCase().replace("-","").replace("_","")).replace(/(^\w)/,t=>t.toLowerCase())}function D(n){return n.trim().replace(/^\/|\/$/g,"")}const j={page:1,pageParam:"page",limit:10,limitParam:"limit"};class ${constructor(t,e,a,r={}){o(this,"type");o(this,"name");o(this,"url");o(this,"headers");o(this,"parent");o(this,"method");o(this,"arguments",new Set);o(this,"schema");o(this,"pagination");o(this,"callbacks",{before:null,after:null,call:null});o(this,"cache",!1);o(this,"retry",!1);o(this,"withCache",(t=20)=>(this.cache=t,this));o(this,"withRetry",(t=2)=>(this.retry=t,this));this.type=t,this.name=E(e),this.name!==e&&console.warn(`Name "${e}" has been camelCased to "${this.name}"`),this.url=D(a),this.headers=r||{}}before(t){return this.callbacks.before=t,this}after(t){return this.callbacks.after=t,this}onCall(t){return this.callbacks.call=t,this}withPagination(t={}){return this.pagination={...j,...t},this}}const p=class p{constructor(){o(this,"cache");this.cache=new Map}static get i(){return p._instance||(p._instance=new p),p._instance}set(t,e,a=0){const r=Date.now()+a;this.cache.set(t,{data:e,expiry:r})}has(t){const e=this.cache.get(t);return e?Date.now()>e.expiry?(this.cache.delete(t),!1):!0:!1}get(t){return this.has(t)?this.cache.get(t).data:null}};o(p,"_instance");let d=p;function x(n){let a=2166136261;for(let i=0;i<n.length;i++)a^=n.charCodeAt(i),a*=16777619;let r=(a>>>0).toString(16).padStart(8,"0");for(;r.length<32;)a^=r.charCodeAt(r.length%r.length),a*=16777619,r+=(a>>>0).toString(16).padStart(8,"0");return r.substring(0,32)}async function H(n,t,e){const a=`${n.toString()}${JSON.stringify(t)}`,r=x(a);if(d.i.has(r))return d.i.get(r);const c=await(await fetch(n,t)).json();return d.i.set(r,c,e),c}class A{static subscribe(t,e){this._callbacks.set(t,e)}static run(t){const e=this._callbacks.get(t);e&&e()}}o(A,"_callbacks",new Map);const C={};function I(n,t){return async(...e)=>{if(t.pagination){const[i=0,c={},l={}]=e;return F(n,t,i,c,l)}const[a={},r={}]=e;return F(n,t,void 0,a,r)}}async function F(n,t,e,a={},r={}){const i=n.split(".");let c;for(let g=0;g<i.length;g++){const y=i[g];if(c=s.i.getApi(y),c)break}if(!t||!c||t.type!=="route"||c.type!=="api")throw new Error(`Invalid path: ${n}.${t.name}`);let l=W(`${c.url}/${t.url}`,t,a);if(t.pagination&&typeof e<"u"){const{pageParam:g="page",limit:y=10,limitParam:K="limit"}=t.pagination,b=new URLSearchParams;b.append(g,String(e)),b.append(K,String(y));const G=l.includes("?")?"&":"?";l=`${l}${G}${b.toString()}`}let u={};r&&t.method!=="GET"&&(u.body=JSON.stringify(r)),u.headers={"Content-Type":"application/json",...c.headers,...t.headers},u.method=t.method;const{beforeRoute:f,beforeApi:P,beforeUrl:m,beforeConfig:v}=J({route:t,api:c,url:l,config:u});l=m,u=v,s.updateElement(P),s.updateElement(f);let w=await M(c,t,l,u);t.schema&&"validate"in t.schema&&(w=await t.schema.validate(w));const{afterRoute:T,afterApi:N,afterData:R}=z({route:t,api:c,response:w,data:w});return s.updateElement(N),s.updateElement(T),A.run(`${c.name}.${t.name}`),R}async function L(n,t,e,a){return n?await H(t,e,a.cache):await(await fetch(t,e)).json()}async function M(n,t,e,a){var f,P;const r=n.cache||t.cache,i=t.retry||n.retry||0;let c,l=!1,u=0;for(;u<=i&&!l;)try{(f=t.callbacks)!=null&&f.call?t.callbacks.call({}):(P=n.callbacks)!=null&&P.call&&n.callbacks.call({}),c=await L(!!r,e,a,n),l=!0}catch(m){if(u++,u>i)throw m.message=`Failed to fetch ${e} after ${i} attempts`,m}return c}function W(n,t,e){let a=n;return t.arguments.forEach(r=>{if(e[r]===void 0)throw new Error(`Argument ${r} is missing`);a=a.replace(`[${r}]`,e[r])}),a}function J({route:n,api:t,url:e,config:a}){var i,c;const r=(c=(i=n.callbacks).before)==null?void 0:c.call(i,{route:n,api:t,url:e,config:a});return{beforeRoute:(r==null?void 0:r.route)||n,beforeApi:(r==null?void 0:r.api)||t,beforeUrl:(r==null?void 0:r.url)||e,beforeConfig:(r==null?void 0:r.config)||a}}function z({route:n,api:t,response:e,data:a}){var i,c;const r=(c=(i=n.callbacks).after)==null?void 0:c.call(i,{route:n,api:t,response:e,data:a});return{afterRoute:(r==null?void 0:r.route)||n,afterApi:(r==null?void 0:r.api)||t,afterResponse:(r==null?void 0:r.response)||e,afterData:(r==null?void 0:r.data)||a}}const h=class h{constructor(){o(this,"_elements",new Map);o(this,"_currentParent",null)}static get i(){return h._instance||(h._instance=new h),h._instance}registerElement(t){const e=this._currentParent;e&&(t.parent=this.getFullPath(e));const a=this.getElementKey(t);if(this._elements.set(a,t),t.type==="api"||t.type==="group"){let r=C;if(e){const i=this.getFullPath(e).split(".");for(const c of i)r[c]||(r[c]={}),r=r[c]}r[t.name]||(r[t.name]={})}}getCurrentParent(){return this._currentParent}setCurrentParent(t){const e=this._elements.get(t);if(!e||e.type!=="api"&&e.type!=="group")throw new Error(`Element ${t} not found or not a valid parent type`);this._currentParent=e}clearCurrentParent(){this._currentParent=null}registerRoute(t){if(!this._currentParent)throw new Error("No current parent set, use Route only inside Api or Group create callback");t.parent=this.getFullPath(this._currentParent);const e=this.getElementKey(t);this._elements.set(e,t),this.addToKlaimRoute(t)}addToKlaimRoute(t){if(!t.parent)return;let e=C;const a=t.parent.split(".");for(const r of a)e[r]||(e[r]={}),e=e[r];e[t.name]=I(t.parent,t)}getElementKey(t){return t?t.parent?`${t.parent}.${t.name}`:t.name:""}getFullPath(t){if(!t)return"";if(!t.parent)return t.name;const e=[t.name];let a=t;for(;a.parent;){const r=this._elements.get(a.parent);if(!r)break;e.unshift(r.name),a=r}return e.join(".")}getRoute(t,e){return this._elements.get(`${t}.${e}`)}getChildren(t){const e=[];return this._elements.forEach(a=>{a.parent===t&&e.push(a)}),e}static updateElement(t){return h.i._elements.get(h.i.getElementKey(t))||t}getApi(t){const e=this._elements.get(t);if(!e){for(const[a,r]of this._elements.entries())if(r.type==="api"&&a.endsWith(`.${t}`))return r;return}return e.type==="api"?e:this.findApi(e)}findApi(t){if(!t||!t.parent)return;const e=t.parent.split(".");for(let a=e.length;a>=0;a--){const r=e.slice(0,a).join("."),i=this._elements.get(r);if((i==null?void 0:i.type)==="api")return i}}};o(h,"_instance");let s=h;class k extends ${static create(t,e,a,r={}){const i=E(t);i!==t&&console.warn(`API name "${t}" has been camelCased to "${i}"`);const c=new k(i,e,r),l=s.i.getCurrentParent();s.i.registerElement(c);const u=l?s.i.getFullPath(l):"",f=u?`${u}.${i}`:i;return s.i.setCurrentParent(f),a(),l?s.i.setCurrentParent(s.i.getFullPath(l)):s.i.clearCurrentParent(),c}constructor(t,e,a={}){super("api",t,e,a)}}class S extends ${static create(t,e){const a=E(t),r=s.i.getCurrentParent(),i=r?s.i.getFullPath(r):"",c=i?`${i}.${a}`:a,l=new S(a,"");a!==t&&console.warn(`Group name "${t}" has been camelCased to "${a}"`),s.i.registerElement(l);const u=s.i.getCurrentParent();return s.i.setCurrentParent(c),e(),u?s.i.setCurrentParent(s.i.getFullPath(u)):s.i.clearCurrentParent(),l}constructor(t,e,a={}){super("group",t,e,a)}withCache(t=20){return super.withCache(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.cache||(e.cache=t)}),this}withRetry(t=2){return super.withRetry(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.retry||(e.retry=t)}),this}before(t){return super.before(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.callbacks.before||(e.callbacks.before=t)}),this}after(t){return super.after(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.callbacks.after||(e.callbacks.after=t)}),this}onCall(t){return super.onCall(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.callbacks.call||(e.callbacks.call=t)}),this}}class _ extends ${constructor(t,e,a={},r="GET"){super("route",t,e,a),this.method=r,this.detectArguments()}static createRoute(t,e,a={},r){const i=new _(t,e,a,r);return s.i.registerRoute(i),i}static get(t,e,a={}){return this.createRoute(t,e,a,"GET")}static post(t,e,a={}){return this.createRoute(t,e,a,"POST")}static put(t,e,a={}){return this.createRoute(t,e,a,"PUT")}static delete(t,e,a={}){return this.createRoute(t,e,a,"DELETE")}static patch(t,e,a={}){return this.createRoute(t,e,a,"PATCH")}static options(t,e,a={}){return this.createRoute(t,e,a,"OPTIONS")}detectArguments(){const t=this.url.match(/\[([^\]]+)]/g);t&&t.forEach(e=>{const a=e.replace("[","").replace("]","");this.arguments.add(a)})}validate(t){return this.schema=t,this}}exports.Api=k;exports.Group=S;exports.Hook=A;exports.Klaim=C;exports.Registry=s;exports.Route=_;
|
package/dist/klaim.es.js
CHANGED
|
@@ -276,27 +276,27 @@ async function A(n, t, e, a = {}, r = {}) {
|
|
|
276
276
|
const U = l.includes("?") ? "&" : "?";
|
|
277
277
|
l = `${l}${U}${C.toString()}`;
|
|
278
278
|
}
|
|
279
|
-
let
|
|
280
|
-
r && t.method !== "GET" && (
|
|
279
|
+
let h = {};
|
|
280
|
+
r && t.method !== "GET" && (h.body = JSON.stringify(r)), h.headers = {
|
|
281
281
|
"Content-Type": "application/json",
|
|
282
282
|
...s.headers,
|
|
283
283
|
...t.headers
|
|
284
|
-
},
|
|
284
|
+
}, h.method = t.method;
|
|
285
285
|
const {
|
|
286
286
|
beforeRoute: f,
|
|
287
287
|
beforeApi: P,
|
|
288
288
|
beforeUrl: m,
|
|
289
|
-
beforeConfig:
|
|
290
|
-
} = J({ route: t, api: s, url: l, config:
|
|
291
|
-
l = m,
|
|
292
|
-
let w = await M(s, t, l,
|
|
289
|
+
beforeConfig: v
|
|
290
|
+
} = J({ route: t, api: s, url: l, config: h });
|
|
291
|
+
l = m, h = v, c.updateElement(P), c.updateElement(f);
|
|
292
|
+
let w = await M(s, t, l, h);
|
|
293
293
|
t.schema && "validate" in t.schema && (w = await t.schema.validate(w));
|
|
294
294
|
const {
|
|
295
|
-
afterRoute:
|
|
296
|
-
afterApi:
|
|
297
|
-
afterData:
|
|
295
|
+
afterRoute: T,
|
|
296
|
+
afterApi: N,
|
|
297
|
+
afterData: R
|
|
298
298
|
} = z({ route: t, api: s, response: w, data: w });
|
|
299
|
-
return c.updateElement(
|
|
299
|
+
return c.updateElement(N), c.updateElement(T), k.run(`${s.name}.${t.name}`), R;
|
|
300
300
|
}
|
|
301
301
|
async function L(n, t, e, a) {
|
|
302
302
|
return n ? await j(t, e, a.cache) : await (await fetch(t, e)).json();
|
|
@@ -304,12 +304,12 @@ async function L(n, t, e, a) {
|
|
|
304
304
|
async function M(n, t, e, a) {
|
|
305
305
|
var f, P;
|
|
306
306
|
const r = n.cache || t.cache, i = t.retry || n.retry || 0;
|
|
307
|
-
let s, l = !1,
|
|
308
|
-
for (;
|
|
307
|
+
let s, l = !1, h = 0;
|
|
308
|
+
for (; h <= i && !l; )
|
|
309
309
|
try {
|
|
310
310
|
(f = t.callbacks) != null && f.call ? t.callbacks.call({}) : (P = n.callbacks) != null && P.call && n.callbacks.call({}), s = await L(!!r, e, a, n), l = !0;
|
|
311
311
|
} catch (m) {
|
|
312
|
-
if (
|
|
312
|
+
if (h++, h > i)
|
|
313
313
|
throw m.message = `Failed to fetch ${e} after ${i} attempts`, m;
|
|
314
314
|
}
|
|
315
315
|
return s;
|
|
@@ -342,7 +342,7 @@ function z({ route: n, api: t, response: e, data: a }) {
|
|
|
342
342
|
afterData: (r == null ? void 0 : r.data) || a
|
|
343
343
|
};
|
|
344
344
|
}
|
|
345
|
-
const
|
|
345
|
+
const u = class u {
|
|
346
346
|
constructor() {
|
|
347
347
|
/**
|
|
348
348
|
* Map storing all registered elements with their full paths as keys
|
|
@@ -360,7 +360,7 @@ const h = class h {
|
|
|
360
360
|
* @returns The singleton Registry instance
|
|
361
361
|
*/
|
|
362
362
|
static get i() {
|
|
363
|
-
return
|
|
363
|
+
return u._instance || (u._instance = new u()), u._instance;
|
|
364
364
|
}
|
|
365
365
|
/**
|
|
366
366
|
* Registers a new element in the registry and updates the Klaim object structure
|
|
@@ -494,7 +494,7 @@ const h = class h {
|
|
|
494
494
|
* @returns The updated element
|
|
495
495
|
*/
|
|
496
496
|
static updateElement(t) {
|
|
497
|
-
return
|
|
497
|
+
return u.i._elements.get(u.i.getElementKey(t)) || t;
|
|
498
498
|
}
|
|
499
499
|
/**
|
|
500
500
|
* Retrieves an API element by name, searching through the entire registry if necessary
|
|
@@ -528,8 +528,8 @@ const h = class h {
|
|
|
528
528
|
}
|
|
529
529
|
}
|
|
530
530
|
};
|
|
531
|
-
o(
|
|
532
|
-
let c =
|
|
531
|
+
o(u, "_instance");
|
|
532
|
+
let c = u;
|
|
533
533
|
class _ extends E {
|
|
534
534
|
/**
|
|
535
535
|
* Creates and registers a new API instance with the given configuration
|
|
@@ -554,7 +554,7 @@ class _ extends E {
|
|
|
554
554
|
i !== t && console.warn(`API name "${t}" has been camelCased to "${i}"`);
|
|
555
555
|
const s = new _(i, e, r), l = c.i.getCurrentParent();
|
|
556
556
|
c.i.registerElement(s);
|
|
557
|
-
const
|
|
557
|
+
const h = l ? c.i.getFullPath(l) : "", f = h ? `${h}.${i}` : i;
|
|
558
558
|
return c.i.setCurrentParent(f), a(), l ? c.i.setCurrentParent(c.i.getFullPath(l)) : c.i.clearCurrentParent(), s;
|
|
559
559
|
}
|
|
560
560
|
/**
|
|
@@ -598,8 +598,8 @@ class F extends E {
|
|
|
598
598
|
static create(t, e) {
|
|
599
599
|
const a = b(t), r = c.i.getCurrentParent(), i = r ? c.i.getFullPath(r) : "", s = i ? `${i}.${a}` : a, l = new F(a, "");
|
|
600
600
|
a !== t && console.warn(`Group name "${t}" has been camelCased to "${a}"`), c.i.registerElement(l);
|
|
601
|
-
const
|
|
602
|
-
return c.i.setCurrentParent(s), e(),
|
|
601
|
+
const h = c.i.getCurrentParent();
|
|
602
|
+
return c.i.setCurrentParent(s), e(), h ? c.i.setCurrentParent(c.i.getFullPath(h)) : c.i.clearCurrentParent(), l;
|
|
603
603
|
}
|
|
604
604
|
/**
|
|
605
605
|
* Creates a new Group instance.
|
package/dist/klaim.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(u,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(u=typeof globalThis<"u"?globalThis:u||self,p(u.klaim={}))})(this,function(u){"use strict";var z=Object.defineProperty;var B=(u,p,d)=>p in u?z(u,p,{enumerable:!0,configurable:!0,writable:!0,value:d}):u[p]=d;var o=(u,p,d)=>B(u,typeof p!="symbol"?p+"":p,d);function p(i){return i.replace(/([-_][a-z])/gi,t=>t.toUpperCase().replace("-","").replace("_","")).replace(/(^\w)/,t=>t.toLowerCase())}function d(i){return i.trim().replace(/^\/|\/$/g,"")}const
|
|
1
|
+
(function(u,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(u=typeof globalThis<"u"?globalThis:u||self,p(u.klaim={}))})(this,function(u){"use strict";var z=Object.defineProperty;var B=(u,p,d)=>p in u?z(u,p,{enumerable:!0,configurable:!0,writable:!0,value:d}):u[p]=d;var o=(u,p,d)=>B(u,typeof p!="symbol"?p+"":p,d);function p(i){return i.replace(/([-_][a-z])/gi,t=>t.toUpperCase().replace("-","").replace("_","")).replace(/(^\w)/,t=>t.toLowerCase())}function d(i){return i.trim().replace(/^\/|\/$/g,"")}const N={page:1,pageParam:"page",limit:10,limitParam:"limit"};class E{constructor(t,e,a,r={}){o(this,"type");o(this,"name");o(this,"url");o(this,"headers");o(this,"parent");o(this,"method");o(this,"arguments",new Set);o(this,"schema");o(this,"pagination");o(this,"callbacks",{before:null,after:null,call:null});o(this,"cache",!1);o(this,"retry",!1);o(this,"withCache",(t=20)=>(this.cache=t,this));o(this,"withRetry",(t=2)=>(this.retry=t,this));this.type=t,this.name=p(e),this.name!==e&&console.warn(`Name "${e}" has been camelCased to "${this.name}"`),this.url=d(a),this.headers=r||{}}before(t){return this.callbacks.before=t,this}after(t){return this.callbacks.after=t,this}onCall(t){return this.callbacks.call=t,this}withPagination(t={}){return this.pagination={...N,...t},this}}const g=class g{constructor(){o(this,"cache");this.cache=new Map}static get i(){return g._instance||(g._instance=new g),g._instance}set(t,e,a=0){const r=Date.now()+a;this.cache.set(t,{data:e,expiry:r})}has(t){const e=this.cache.get(t);return e?Date.now()>e.expiry?(this.cache.delete(t),!1):!0:!1}get(t){return this.has(t)?this.cache.get(t).data:null}};o(g,"_instance");let P=g;function R(i){let a=2166136261;for(let n=0;n<i.length;n++)a^=i.charCodeAt(n),a*=16777619;let r=(a>>>0).toString(16).padStart(8,"0");for(;r.length<32;)a^=r.charCodeAt(r.length%r.length),a*=16777619,r+=(a>>>0).toString(16).padStart(8,"0");return r.substring(0,32)}async function K(i,t,e){const a=`${i.toString()}${JSON.stringify(t)}`,r=R(a);if(P.i.has(r))return P.i.get(r);const c=await(await fetch(i,t)).json();return P.i.set(r,c,e),c}class ${static subscribe(t,e){this._callbacks.set(t,e)}static run(t){const e=this._callbacks.get(t);e&&e()}}o($,"_callbacks",new Map);const A={};function G(i,t){return async(...e)=>{if(t.pagination){const[n=0,c={},l={}]=e;return v(i,t,n,c,l)}const[a={},r={}]=e;return v(i,t,void 0,a,r)}}async function v(i,t,e,a={},r={}){const n=i.split(".");let c;for(let y=0;y<n.length;y++){const F=n[y];if(c=s.i.getApi(F),c)break}if(!t||!c||t.type!=="route"||c.type!=="api")throw new Error(`Invalid path: ${i}.${t.name}`);let l=j(`${c.url}/${t.url}`,t,a);if(t.pagination&&typeof e<"u"){const{pageParam:y="page",limit:F=10,limitParam:W="limit"}=t.pagination,T=new URLSearchParams;T.append(y,String(e)),T.append(W,String(F));const J=l.includes("?")?"&":"?";l=`${l}${J}${T.toString()}`}let h={};r&&t.method!=="GET"&&(h.body=JSON.stringify(r)),h.headers={"Content-Type":"application/json",...c.headers,...t.headers},h.method=t.method;const{beforeRoute:m,beforeApi:w,beforeUrl:b,beforeConfig:I}=D({route:t,api:c,url:l,config:h});l=b,h=I,s.updateElement(w),s.updateElement(m);let C=await U(c,t,l,h);t.schema&&"validate"in t.schema&&(C=await t.schema.validate(C));const{afterRoute:L,afterApi:M,afterData:x}=H({route:t,api:c,response:C,data:C});return s.updateElement(M),s.updateElement(L),$.run(`${c.name}.${t.name}`),x}async function O(i,t,e,a){return i?await K(t,e,a.cache):await(await fetch(t,e)).json()}async function U(i,t,e,a){var m,w;const r=i.cache||t.cache,n=t.retry||i.retry||0;let c,l=!1,h=0;for(;h<=n&&!l;)try{(m=t.callbacks)!=null&&m.call?t.callbacks.call({}):(w=i.callbacks)!=null&&w.call&&i.callbacks.call({}),c=await O(!!r,e,a,i),l=!0}catch(b){if(h++,h>n)throw b.message=`Failed to fetch ${e} after ${n} attempts`,b}return c}function j(i,t,e){let a=i;return t.arguments.forEach(r=>{if(e[r]===void 0)throw new Error(`Argument ${r} is missing`);a=a.replace(`[${r}]`,e[r])}),a}function D({route:i,api:t,url:e,config:a}){var n,c;const r=(c=(n=i.callbacks).before)==null?void 0:c.call(n,{route:i,api:t,url:e,config:a});return{beforeRoute:(r==null?void 0:r.route)||i,beforeApi:(r==null?void 0:r.api)||t,beforeUrl:(r==null?void 0:r.url)||e,beforeConfig:(r==null?void 0:r.config)||a}}function H({route:i,api:t,response:e,data:a}){var n,c;const r=(c=(n=i.callbacks).after)==null?void 0:c.call(n,{route:i,api:t,response:e,data:a});return{afterRoute:(r==null?void 0:r.route)||i,afterApi:(r==null?void 0:r.api)||t,afterResponse:(r==null?void 0:r.response)||e,afterData:(r==null?void 0:r.data)||a}}const f=class f{constructor(){o(this,"_elements",new Map);o(this,"_currentParent",null)}static get i(){return f._instance||(f._instance=new f),f._instance}registerElement(t){const e=this._currentParent;e&&(t.parent=this.getFullPath(e));const a=this.getElementKey(t);if(this._elements.set(a,t),t.type==="api"||t.type==="group"){let r=A;if(e){const n=this.getFullPath(e).split(".");for(const c of n)r[c]||(r[c]={}),r=r[c]}r[t.name]||(r[t.name]={})}}getCurrentParent(){return this._currentParent}setCurrentParent(t){const e=this._elements.get(t);if(!e||e.type!=="api"&&e.type!=="group")throw new Error(`Element ${t} not found or not a valid parent type`);this._currentParent=e}clearCurrentParent(){this._currentParent=null}registerRoute(t){if(!this._currentParent)throw new Error("No current parent set, use Route only inside Api or Group create callback");t.parent=this.getFullPath(this._currentParent);const e=this.getElementKey(t);this._elements.set(e,t),this.addToKlaimRoute(t)}addToKlaimRoute(t){if(!t.parent)return;let e=A;const a=t.parent.split(".");for(const r of a)e[r]||(e[r]={}),e=e[r];e[t.name]=G(t.parent,t)}getElementKey(t){return t?t.parent?`${t.parent}.${t.name}`:t.name:""}getFullPath(t){if(!t)return"";if(!t.parent)return t.name;const e=[t.name];let a=t;for(;a.parent;){const r=this._elements.get(a.parent);if(!r)break;e.unshift(r.name),a=r}return e.join(".")}getRoute(t,e){return this._elements.get(`${t}.${e}`)}getChildren(t){const e=[];return this._elements.forEach(a=>{a.parent===t&&e.push(a)}),e}static updateElement(t){return f.i._elements.get(f.i.getElementKey(t))||t}getApi(t){const e=this._elements.get(t);if(!e){for(const[a,r]of this._elements.entries())if(r.type==="api"&&a.endsWith(`.${t}`))return r;return}return e.type==="api"?e:this.findApi(e)}findApi(t){if(!t||!t.parent)return;const e=t.parent.split(".");for(let a=e.length;a>=0;a--){const r=e.slice(0,a).join("."),n=this._elements.get(r);if((n==null?void 0:n.type)==="api")return n}}};o(f,"_instance");let s=f;class k extends E{static create(t,e,a,r={}){const n=p(t);n!==t&&console.warn(`API name "${t}" has been camelCased to "${n}"`);const c=new k(n,e,r),l=s.i.getCurrentParent();s.i.registerElement(c);const h=l?s.i.getFullPath(l):"",m=h?`${h}.${n}`:n;return s.i.setCurrentParent(m),a(),l?s.i.setCurrentParent(s.i.getFullPath(l)):s.i.clearCurrentParent(),c}constructor(t,e,a={}){super("api",t,e,a)}}class S extends E{static create(t,e){const a=p(t),r=s.i.getCurrentParent(),n=r?s.i.getFullPath(r):"",c=n?`${n}.${a}`:a,l=new S(a,"");a!==t&&console.warn(`Group name "${t}" has been camelCased to "${a}"`),s.i.registerElement(l);const h=s.i.getCurrentParent();return s.i.setCurrentParent(c),e(),h?s.i.setCurrentParent(s.i.getFullPath(h)):s.i.clearCurrentParent(),l}constructor(t,e,a={}){super("group",t,e,a)}withCache(t=20){return super.withCache(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.cache||(e.cache=t)}),this}withRetry(t=2){return super.withRetry(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.retry||(e.retry=t)}),this}before(t){return super.before(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.callbacks.before||(e.callbacks.before=t)}),this}after(t){return super.after(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.callbacks.after||(e.callbacks.after=t)}),this}onCall(t){return super.onCall(t),s.i.getChildren(s.i.getFullPath(this)).forEach(e=>{e.callbacks.call||(e.callbacks.call=t)}),this}}class _ extends E{constructor(t,e,a={},r="GET"){super("route",t,e,a),this.method=r,this.detectArguments()}static createRoute(t,e,a={},r){const n=new _(t,e,a,r);return s.i.registerRoute(n),n}static get(t,e,a={}){return this.createRoute(t,e,a,"GET")}static post(t,e,a={}){return this.createRoute(t,e,a,"POST")}static put(t,e,a={}){return this.createRoute(t,e,a,"PUT")}static delete(t,e,a={}){return this.createRoute(t,e,a,"DELETE")}static patch(t,e,a={}){return this.createRoute(t,e,a,"PATCH")}static options(t,e,a={}){return this.createRoute(t,e,a,"OPTIONS")}detectArguments(){const t=this.url.match(/\[([^\]]+)]/g);t&&t.forEach(e=>{const a=e.replace("[","").replace("]","");this.arguments.add(a)})}validate(t){return this.schema=t,this}}u.Api=k,u.Group=S,u.Hook=$,u.Klaim=A,u.Registry=s,u.Route=_,Object.defineProperty(u,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "klaim",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.35",
|
|
4
4
|
"author": "antharuu",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,23 +9,23 @@
|
|
|
9
9
|
"main": "dist/klaim.cjs.js",
|
|
10
10
|
"module": "dist/klaim.es.js",
|
|
11
11
|
"devDependencies": {
|
|
12
|
-
"@eslint/js": "9.
|
|
13
|
-
"@stylistic/eslint-plugin": "4.
|
|
12
|
+
"@eslint/js": "9.22.0",
|
|
13
|
+
"@stylistic/eslint-plugin": "4.2.0",
|
|
14
14
|
"@types/bun": "latest",
|
|
15
15
|
"@types/eslint__js": "9.14.0",
|
|
16
|
-
"@types/node": "^22.13.
|
|
17
|
-
"@vitest/coverage-v8": "^3.0.
|
|
18
|
-
"@vitest/ui": "^3.0.
|
|
16
|
+
"@types/node": "^22.13.10",
|
|
17
|
+
"@vitest/coverage-v8": "^3.0.8",
|
|
18
|
+
"@vitest/ui": "^3.0.8",
|
|
19
19
|
"dotenv-cli": "^8.0.0",
|
|
20
|
-
"eslint": "9.
|
|
21
|
-
"eslint-plugin-jsdoc": "50.6.
|
|
20
|
+
"eslint": "9.22.0",
|
|
21
|
+
"eslint-plugin-jsdoc": "50.6.4",
|
|
22
22
|
"eslint-plugin-simple-import-sort": "12.1.1",
|
|
23
23
|
"jsdom": "^26.0.0",
|
|
24
24
|
"release-it": "^18.1.2",
|
|
25
|
-
"typescript": "
|
|
26
|
-
"typescript-eslint": "8.
|
|
27
|
-
"vite": "^6.2.
|
|
28
|
-
"vitest": "^3.0.
|
|
25
|
+
"typescript": "5.8",
|
|
26
|
+
"typescript-eslint": "8.26",
|
|
27
|
+
"vite": "^6.2.1",
|
|
28
|
+
"vitest": "^3.0.8",
|
|
29
29
|
"yup": "^1.6.1"
|
|
30
30
|
},
|
|
31
31
|
"bugs": {
|