klaim 1.5.0 → 1.5.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antharuu/klaim",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Klaim is a lightweight TypeScript library designed to manage APIs and record requests, optimized for an optimal user experience.",
5
5
  "repository": {
6
6
  "type": "git",
package/dist/klaim.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var S=Object.defineProperty;var k=(s,t,e)=>t in s?S(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var c=(s,t,e)=>k(s,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function E(s){return s.trim().replace(/^\/|\/$/g,"")}function P(s){return s.replace(/([-_][a-z])/gi,t=>t.toUpperCase().replace("-","").replace("_","")).replace(/(^\w)/,t=>t.toLowerCase())}const o=class o{constructor(){c(this,"cache");this.cache=new Map}static get i(){return o._instance||(o._instance=new o),o._instance}set(t,e,r){const a=Date.now()+r;this.cache.set(t,{data:e,expiry:a})}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}};c(o,"_instance");let f=o;function O(s){let r=2166136261;for(let n=0;n<s.length;n++)r^=s.charCodeAt(n),r*=16777619;let a=(r>>>0).toString(16).padStart(8,"0");for(;a.length<32;)r^=a.charCodeAt(a.length%a.length),r*=16777619,a+=(r>>>0).toString(16).padStart(8,"0");return a.substring(0,32)}async function _(s,t,e){const r=`${s.toString()}${JSON.stringify(t)}`,a=O(r);if(f.i.has(a))return f.i.get(a);const i=await(await fetch(s,t)).json();return f.i.set(a,i,e),i}class y{static subscribe(t,e){this._callbacks.set(t,e)}static run(t){const e=this._callbacks.get(t);e&&e()}}c(y,"_callbacks",new Map);var $=(s=>(s.GET="GET",s.POST="POST",s.PUT="PUT",s.DELETE="DELETE",s.PATCH="PATCH",s.OPTIONS="OPTIONS",s))($||{});class T{constructor(t,e,r,a="GET"){c(this,"api","undefined");c(this,"name");c(this,"url");c(this,"method");c(this,"headers");c(this,"arguments",new Set);c(this,"callbacks",{before:null,after:null,call:null});c(this,"cache",!1);c(this,"retry",!1);this.name=P(t),this.name!==t&&console.warn(`Route name "${t}" has been camelCased to "${this.name}"`),this.url=E(e),this.headers=r||{},this.method=a,this.detectArguments()}static createRoute(t,e,r,a){const n=new T(t,e,r,a);return h.i.registerRoute(n),n}static get(t,e,r={}){return this.createRoute(t,e,r,"GET")}static post(t,e,r){return this.createRoute(t,e,r,"POST")}static put(t,e,r){return this.createRoute(t,e,r,"PUT")}static delete(t,e,r){return this.createRoute(t,e,r,"DELETE")}static patch(t,e,r){return this.createRoute(t,e,r,"PATCH")}static options(t,e,r){return this.createRoute(t,e,r,"OPTIONS")}before(t){return this.callbacks.before=t,this}after(t){return this.callbacks.after=t,this}onCall(t){return this.callbacks.call=t,this}detectArguments(){const t=this.url.match(/\[([^\]]+)]/g);t&&t.forEach(e=>{const r=e.replace("[","").replace("]","");this.arguments.add(r)})}withCache(t=20){return this.cache=t,this}withRetry(t=2){return this.retry=t,this}}const b={};async function I(s,t,e={},r={}){let a=U(`${s.url}/${t.url}`,t,e),n={};r&&t.method!==$.GET&&(n.body=JSON.stringify(r)),n.headers={"Content-Type":"application/json",...s.headers,...t.headers},n.method=t.method;const{beforeRoute:i,beforeApi:u,beforeUrl:w,beforeConfig:d}=H({route:t,api:s,url:a,config:n});a=w,n=d,s=h.updateApi(u),t=h.updateRoute(i);const p=await D(s,t,a,n),{afterRoute:g,afterApi:m,afterData:A}=N({route:t,api:s,response:p,data:p});return h.updateApi(m),h.updateRoute(g),y.run(`${s.name}.${t.name}`),A}async function R(s,t,e,r){return s?await _(t,e,r.cache):await(await fetch(t,e)).json()}async function D(s,t,e,r){var p,g,m;const a=s.cache||t.cache,n=t.retry||s.retry||0;let i,u=0,w=!1;const d=((p=t.callbacks)==null?void 0:p.call)!==null?(g=t.callbacks)==null?void 0:g.call:(m=s.callbacks)==null?void 0:m.call;for(;u<=n&&!w;){d&&d();try{i=await R(a,e,r,s),w=!0}catch(A){if(u++,u>n)throw A.message=`Failed to fetch ${e} after ${n} attempts`,A}}return i}function U(s,t,e){let r=s;return t.arguments.forEach(a=>{if(e[a]===void 0)throw new Error(`Argument ${a} is missing`);r=r.replace(`[${a}]`,e[a])}),r}function H({route:s,api:t,url:e,config:r}){var n,i;const a=(i=(n=s.callbacks).before)==null?void 0:i.call(n,{route:s,api:t,url:e,config:r});return{beforeRoute:(a==null?void 0:a.route)||s,beforeApi:(a==null?void 0:a.api)||t,beforeUrl:(a==null?void 0:a.url)||e,beforeConfig:(a==null?void 0:a.config)||r}}function N({route:s,api:t,response:e,data:r}){var n,i;const a=(i=(n=s.callbacks).after)==null?void 0:i.call(n,{route:s,api:t,response:e,data:r});return{afterRoute:(a==null?void 0:a.route)||s,afterApi:(a==null?void 0:a.api)||t,afterResponse:(a==null?void 0:a.response)||e,afterData:(a==null?void 0:a.data)||r}}const l=class l{constructor(){c(this,"_apis",new Map);c(this,"_currentApi",null)}static get i(){return l._instance||(l._instance=new l),l._instance}registerApi(t){this._apis.set(t.name,t),b[t.name]={}}setCurrent(t){const e=this._apis.get(t);if(!e)throw new Error(`API ${t} not found`);this._currentApi=e}clearCurrent(){this._currentApi=null}registerRoute(t){if(!this._currentApi)throw new Error("No current API set, use Route only inside Api.create callback");t.api=this._currentApi.name,this._currentApi.routes.set(t.name,t),this.addToKlaimRoute(t.api,t)}getApi(t){return this._apis.get(t)}getRoute(t,e){const r=this._apis.get(t);if(!r)throw new Error(`API ${t} not found`);return r.routes.get(e)}static updateApi(t){return l.i._apis.has(t.name)||l.i.registerApi(t),l.i._apis.set(t.name,t),t}static updateRoute(t){const e=l.i._apis.get(t.api);if(!e)throw new Error(`API ${t.api} not found`);return e.routes.set(t.name,t),t}addToKlaimRoute(t,e){b[t][e.name]=async(r={},a={})=>{const n=l.i._apis.get(t);if(!n)throw new Error(`API ${e.api} not found`);return I(n,e,r,a)}}};c(l,"_instance");let h=l;class C{constructor(t,e,r={}){c(this,"name");c(this,"url");c(this,"headers");c(this,"routes",new Map);c(this,"callbacks",{call:null});c(this,"cache",!1);c(this,"retry",!1);this.name=t,this.url=E(e),this.headers=r}static create(t,e,r,a={}){const n=P(t);n!==t&&console.warn(`API name "${t}" has been camelCased to "${n}"`);const i=new C(n,e,a);return h.i.registerApi(i),h.i.setCurrent(n),r(),h.i.clearCurrent(),i}onCall(t){return this.callbacks.call=t,this}withCache(t=20){return this.cache=t,this}withRetry(t=2){return this.retry=t,this}}exports.Api=C;exports.Hook=y;exports.Klaim=b;exports.Registry=h;exports.Route=T;
1
+ "use strict";var S=Object.defineProperty;var k=(n,t,a)=>t in n?S(n,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):n[t]=a;var i=(n,t,a)=>k(n,typeof t!="symbol"?t+"":t,a);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function C(n){return n.replace(/([-_][a-z])/gi,t=>t.toUpperCase().replace("-","").replace("_","")).replace(/(^\w)/,t=>t.toLowerCase())}function O(n){return n.trim().replace(/^\/|\/$/g,"")}class P{constructor(t,a,r={}){i(this,"name");i(this,"url");i(this,"headers");i(this,"callbacks",{before:null,after:null,call:null});i(this,"cache",!1);i(this,"retry",!1);this.name=C(t),this.name!==t&&console.warn(`Name "${t}" has been camelCased to "${this.name}"`),this.url=O(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}withCache(t=20){return this.cache=t,this}withRetry(t=2){return this.retry=t,this}}const u=class u{constructor(){i(this,"cache");this.cache=new Map}static get i(){return u._instance||(u._instance=new u),u._instance}set(t,a,r=0){const e=Date.now()+r;this.cache.set(t,{data:a,expiry:e})}has(t){const a=this.cache.get(t);return a?Date.now()>a.expiry?(this.cache.delete(t),!1):!0:!1}get(t){return this.has(t)?this.cache.get(t).data:null}};i(u,"_instance");let f=u;function _(n){let r=2166136261;for(let s=0;s<n.length;s++)r^=n.charCodeAt(s),r*=16777619;let e=(r>>>0).toString(16).padStart(8,"0");for(;e.length<32;)r^=e.charCodeAt(e.length%e.length),r*=16777619,e+=(r>>>0).toString(16).padStart(8,"0");return e.substring(0,32)}async function I(n,t,a){const r=`${n.toString()}${JSON.stringify(t)}`,e=_(r);if(f.i.has(e))return f.i.get(e);const c=await(await fetch(n,t)).json();return f.i.set(e,c,a),c}class E{static subscribe(t,a){this._callbacks.set(t,a)}static run(t){const a=this._callbacks.get(t);a&&a()}}i(E,"_callbacks",new Map);var $=(n=>(n.GET="GET",n.POST="POST",n.PUT="PUT",n.DELETE="DELETE",n.PATCH="PATCH",n.OPTIONS="OPTIONS",n))($||{});class T extends P{constructor(a,r,e,s="GET"){super(a,r,e);i(this,"api","undefined");i(this,"method");i(this,"arguments",new Set);this.method=s,this.detectArguments()}static createRoute(a,r,e,s){const c=new T(a,r,e,s);return o.i.registerRoute(c),c}static get(a,r,e={}){return this.createRoute(a,r,e,"GET")}static post(a,r,e){return this.createRoute(a,r,e,"POST")}static put(a,r,e){return this.createRoute(a,r,e,"PUT")}static delete(a,r,e){return this.createRoute(a,r,e,"DELETE")}static patch(a,r,e){return this.createRoute(a,r,e,"PATCH")}static options(a,r,e){return this.createRoute(a,r,e,"OPTIONS")}detectArguments(){const a=this.url.match(/\[([^\]]+)]/g);a&&a.forEach(r=>{const e=r.replace("[","").replace("]","");this.arguments.add(e)})}}const b={};async function D(n,t,a={},r={}){let e=N(`${n.url}/${t.url}`,t,a),s={};r&&t.method!==$.GET&&(s.body=JSON.stringify(r)),s.headers={"Content-Type":"application/json",...n.headers,...t.headers},s.method=t.method;const{beforeRoute:c,beforeApi:h,beforeUrl:d,beforeConfig:w}=R({route:t,api:n,url:e,config:s});e=d,s=w,n=o.updateApi(h),t=o.updateRoute(c);const p=await H(n,t,e,s),{afterRoute:g,afterApi:m,afterData:A}=x({route:t,api:n,response:p,data:p});return o.updateApi(m),o.updateRoute(g),E.run(`${n.name}.${t.name}`),A}async function U(n,t,a,r){return n?await I(t,a,r.cache):await(await fetch(t,a)).json()}async function H(n,t,a,r){var p,g,m;const e=n.cache||t.cache,s=t.retry||n.retry||0;let c,h=0,d=!1;const w=((p=t.callbacks)==null?void 0:p.call)!==null?(g=t.callbacks)==null?void 0:g.call:(m=n.callbacks)==null?void 0:m.call;for(;h<=s&&!d;){w&&w({});try{c=await U(!!e,a,r,n),d=!0}catch(A){if(h++,h>s)throw A.message=`Failed to fetch ${a} after ${s} attempts`,A}}return c}function N(n,t,a){let r=n;return t.arguments.forEach(e=>{if(a[e]===void 0)throw new Error(`Argument ${e} is missing`);r=r.replace(`[${e}]`,a[e])}),r}function R({route:n,api:t,url:a,config:r}){var s,c;const e=(c=(s=n.callbacks).before)==null?void 0:c.call(s,{route:n,api:t,url:a,config:r});return{beforeRoute:(e==null?void 0:e.route)||n,beforeApi:(e==null?void 0:e.api)||t,beforeUrl:(e==null?void 0:e.url)||a,beforeConfig:(e==null?void 0:e.config)||r}}function x({route:n,api:t,response:a,data:r}){var s,c;const e=(c=(s=n.callbacks).after)==null?void 0:c.call(s,{route:n,api:t,response:a,data:r});return{afterRoute:(e==null?void 0:e.route)||n,afterApi:(e==null?void 0:e.api)||t,afterResponse:(e==null?void 0:e.response)||a,afterData:(e==null?void 0:e.data)||r}}const l=class l{constructor(){i(this,"_apis",new Map);i(this,"_currentApi",null)}static get i(){return l._instance||(l._instance=new l),l._instance}registerApi(t){this._apis.set(t.name,t),b[t.name]={}}setCurrent(t){const a=this._apis.get(t);if(!a)throw new Error(`API ${t} not found`);this._currentApi=a}clearCurrent(){this._currentApi=null}registerRoute(t){if(!this._currentApi)throw new Error("No current API set, use Route only inside Api.create callback");t.api=this._currentApi.name,this._currentApi.routes.set(t.name,t),this.addToKlaimRoute(t.api,t)}getApi(t){return this._apis.get(t)}getRoute(t,a){const r=this._apis.get(t);if(!r)throw new Error(`API ${t} not found`);return r.routes.get(a)}static updateApi(t){return l.i._apis.has(t.name)||l.i.registerApi(t),l.i._apis.set(t.name,t),t}static updateRoute(t){const a=l.i._apis.get(t.api);if(!a)throw new Error(`API ${t.api} not found`);return a.routes.set(t.name,t),t}addToKlaimRoute(t,a){b[t][a.name]=async(r={},e={})=>{const s=l.i._apis.get(t);if(!s)throw new Error(`API ${a.api} not found`);return D(s,a,r,e)}}};i(l,"_instance");let o=l;class y extends P{constructor(){super(...arguments);i(this,"routes",new Map)}static create(a,r,e,s={}){const c=C(a);c!==a&&console.warn(`API name "${a}" has been camelCased to "${c}"`);const h=new y(c,r,s);return o.i.registerApi(h),o.i.setCurrent(c),e(),o.i.clearCurrent(),h}}exports.Api=y;exports.Hook=E;exports.Klaim=b;exports.Registry=o;exports.Route=T;