klaim 1.3.0 → 1.5.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/README.md CHANGED
@@ -1,155 +1,233 @@
1
- # Klaim 📦
2
-
3
- [![JSR Score](https://jsr.io/badges/@antharuu/klaim/score)](https://jsr.io/@antharuu/klaim)
4
-
5
- Klaim is a lightweight TypeScript library designed to manage APIs and record requests, optimized for an optimal user
6
- experience.
7
-
8
- ## 📚 Table of Contents
9
-
10
- - [Features](#-features)
11
- - [Installation](#-installation)
12
- - [Usage](#-usage)
13
- - [Basic API Configuration](#basic-api-configuration)
14
- - [Route Definition](#route-definition)
15
- - [Request Handling](#request-handling)
16
- - [Middleware Usage](#middleware-usage)
17
- - [Hook Subscription](#hook-subscription)
18
- - [Links](#-links)
19
- - [Contributing](#-contributing)
20
- - [License](#-license)
21
-
22
- ## 🚀 Features
23
-
24
- - **Efficient API Management**: Easily manage multiple APIs with streamlined integration and interaction capabilities.
25
- - **Request Recording**: Seamlessly track requests for debugging and monitoring.
26
- - **User Experience Optimization**: Focused on performance and usability for a smooth user experience.
27
- - **Lightweight**: Minimal footprint for fast load times and minimal performance impact.
28
- - **Middleware Support**: Easily add middleware to modify requests and responses (`before` and `after`).
29
- - **Hook System**: Subscribe to hooks to monitor and react to specific events.
30
- - **TypeScript Support**: Fully typed for enhanced code quality and developer experience.
31
-
32
- ## 📥 Installation
33
-
34
- Install Klaim via npm:
35
-
36
- ```sh
37
- // Using npm
38
- npm install klaim
39
-
40
- // Using bun
41
- bun add klaim
42
-
43
- // Using deno
44
- deno add @antharuu/klaim
45
- ```
46
-
47
- ## 🛠 Usage
48
-
49
- ### Basic API Configuration
50
-
51
- First, set up the API configuration. Define the API and its base URL.
52
-
53
- ```typescript
54
- import {Api, Route} from 'klaim';
55
- // For deno: import { Api, Route } from "@antharuu/klaim";
56
-
57
- // Your simple Todo type
58
- type Todo = {
59
- userId: number;
60
- id: number;
61
- title: string;
62
- completed: boolean;
63
- };
64
-
65
- // Create a new API with the name "hello" and the base URL "https://jsonplaceholder.typicode.com/"
66
- Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
67
- // Define routes for the API
68
- Route.get<Todo[]>("listTodos", "todos");
69
- Route.get<Todo>("getTodo", "todos/[id]");
70
- Route.post<Todo>("addTodo", "todos");
71
- });
72
- ```
73
-
74
- ### Route Definition
75
-
76
- Define various routes within the API callback:
77
-
78
- ```typescript
79
- Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
80
- // Get a list of todos
81
- Route.get<Todo[]>("listTodos", "todos");
82
-
83
- // Get a specific todo by id
84
- Route.get<Todo>("getTodo", "todos/[id]");
85
-
86
- // Add a new todo
87
- Route.post<Todo>("addTodo", "todos");
88
- });
89
- ```
90
-
91
- ### Request Handling
92
-
93
- Handle requests using the defined routes:
94
-
95
- ```typescript
96
- import {Klaim} from 'klaim';
97
- // For deno: import { Klaim } from "@antharuu/klaim";
98
-
99
- // Make a request to the "listTodos" route
100
- const listOfTodos = await Klaim.hello.listTodos<Todo[]>();
101
-
102
- // Make a request to the "getTodo" route with the parameter "id"
103
- const todo = await Klaim.hello.getTodo<Todo>({id: 1});
104
-
105
- // Make a request to the "addTodo" route
106
- const newTodo = await Klaim.hello.addTodo<Todo>({}, {title: "New Todo", completed: false, userId: 1});
107
- ```
108
-
109
- ### Middleware Usage
110
-
111
- Add middleware to modify requests and responses. Use `before` middleware to alter requests before they are sent
112
- and `after` middleware to process responses:
113
-
114
- ```typescript
115
- Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
116
- // With before middleware
117
- Route.get<Todo>("getRandomTodo", "todos")
118
- .before(({url}) => {
119
- const random = Math.floor(Math.random() * 10) + 1;
120
- return {url: `${url}/${random}`};
121
- });
122
-
123
- // With after middleware
124
- Route.get<Todo>("getFirstTodo", "todos")
125
- .after(({data: [first]}) => ({data: first}));
126
- });
127
- ```
128
-
129
- ### Hook Subscription
130
-
131
- Subscribe to hooks to monitor specific events:
132
-
133
- ```typescript
134
- import {Hook} from 'klaim';
135
- // For deno: import { Hook } from "@antharuu/klaim";
136
-
137
- // Subscribe to the "hello.getFirstTodo" hook
138
- Hook.subscribe("hello.getFirstTodo", ({url}) => {
139
- console.log(`Requesting ${url}`);
140
- });
141
- ```
142
-
143
- ## 🔗 Links
144
-
145
- - [NPM](https://www.npmjs.com/package/klaim)
146
- - [JSR](https://jsr.io/@antharuu/klaim)
147
- - [GitHub](https://github.com/antharuu/klaim)
148
-
149
- ## 🤝 Contributing
150
-
151
- Contributions are welcome! Please see the [Contributing Guide](CONTRIBUTING.md) for more details.
152
-
153
- ## 📜 License
154
-
155
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
1
+ # Klaim 📦
2
+
3
+ [![JSR Score](https://jsr.io/badges/@antharuu/klaim/score)](https://jsr.io/@antharuu/klaim)
4
+
5
+ Klaim is a lightweight TypeScript library designed to manage APIs and record requests, optimized for an optimal user
6
+ experience.
7
+
8
+ ## 📚 Table of Contents
9
+
10
+ - [Features](#-features)
11
+ - [Installation](#-installation)
12
+ - [Usage](#-usage)
13
+ - [Basic API Configuration](#basic-api-configuration)
14
+ - [Route Definition](#route-definition)
15
+ - [Request Handling](#request-handling)
16
+ - [Middleware Usage](#middleware-usage)
17
+ - [Hook Subscription](#hook-subscription)
18
+ - [Caching Requests](#caching-requests)
19
+ - [Retry Mechanism](#retry-mechanism)
20
+ - [Links](#-links)
21
+ - [Contributing](#-contributing)
22
+ - [License](#-license)
23
+
24
+ ## 🚀 Features
25
+
26
+ - **Efficient API Management**: Easily manage multiple APIs with streamlined integration and interaction capabilities.
27
+ - **Request Recording**: Seamlessly track requests for debugging and monitoring.
28
+ - **User Experience Optimization**: Focused on performance and usability for a smooth user experience.
29
+ - **Lightweight**: Minimal footprint for fast load times and minimal performance impact.
30
+ - **Middleware Support**: Easily add middleware to modify requests and responses (`before` and `after`).
31
+ - **Hook System**: Subscribe to hooks to monitor and react to specific events.
32
+ - **Caching**: Enable caching on requests to reduce network load and improve performance.
33
+ - **Retry Mechanism**: Automatically retry failed requests to enhance reliability.
34
+ - **TypeScript Support**: Fully typed for enhanced code quality and developer experience.
35
+
36
+ ## 📥 Installation
37
+
38
+ Install Klaim via npm:
39
+
40
+ ```sh
41
+ // Using npm
42
+ npm install klaim
43
+
44
+ // Using bun
45
+ bun add klaim
46
+
47
+ // Using deno
48
+ deno add @antharuu/klaim
49
+ ```
50
+
51
+ ## 🛠 Usage
52
+
53
+ ### Basic API Configuration
54
+
55
+ First, set up the API configuration. Define the API and its base URL.
56
+
57
+ ```typescript
58
+ import {Api, Route} from 'klaim';
59
+ // For deno: import { Api, Route } from "@antharuu/klaim";
60
+
61
+ // Your simple Todo type
62
+ type Todo = {
63
+ userId: number;
64
+ id: number;
65
+ title: string;
66
+ completed: boolean;
67
+ };
68
+
69
+ // Create a new API with the name "hello" and the base URL "https://jsonplaceholder.typicode.com/"
70
+ Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
71
+ // Define routes for the API
72
+ Route.get<Todo[]>("listTodos", "todos");
73
+ Route.get<Todo>("getTodo", "todos/[id]");
74
+ Route.post<Todo>("addTodo", "todos");
75
+ });
76
+ ```
77
+
78
+ ### Route Definition
79
+
80
+ Define various routes within the API callback:
81
+
82
+ ```typescript
83
+ Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
84
+ // Get a list of todos
85
+ Route.get<Todo[]>("listTodos", "todos");
86
+
87
+ // Get a specific todo by id
88
+ Route.get<Todo>("getTodo", "todos/[id]");
89
+
90
+ // Add a new todo
91
+ Route.post<Todo>("addTodo", "todos");
92
+ });
93
+ ```
94
+
95
+ ### Request Handling
96
+
97
+ Handle requests using the defined routes:
98
+
99
+ ```typescript
100
+ import {Klaim} from 'klaim';
101
+ // For deno: import { Klaim } from "@antharuu/klaim";
102
+
103
+ // Make a request to the "listTodos" route
104
+ const listOfTodos = await Klaim.hello.listTodos<Todo[]>();
105
+
106
+ // Make a request to the "getTodo" route with the parameter "id"
107
+ const todo = await Klaim.hello.getTodo<Todo>({id: 1});
108
+
109
+ // Make a request to the "addTodo" route
110
+ const newTodo = await Klaim.hello.addTodo<Todo>({}, {title: "New Todo", completed: false, userId: 1});
111
+ ```
112
+
113
+ ### Middleware Usage
114
+
115
+ Add middleware to modify requests and responses. Use `before` middleware to alter requests before they are sent
116
+ and `after` middleware to process responses:
117
+
118
+ ```typescript
119
+ Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
120
+ // With before middleware
121
+ Route.get<Todo>("getRandomTodo", "todos")
122
+ .before(({url}) => {
123
+ const random = Math.floor(Math.random() * 10) + 1;
124
+ return {url: `${url}/${random}`};
125
+ });
126
+
127
+ // With after middleware
128
+ Route.get<Todo>("getFirstTodo", "todos")
129
+ .after(({data: [first]}) => ({data: first}));
130
+ });
131
+ ```
132
+
133
+ ### Hook Subscription
134
+
135
+ Subscribe to hooks to monitor specific events:
136
+
137
+ ```typescript
138
+ import {Hook} from 'klaim';
139
+ // For deno: import { Hook } from "@antharuu/klaim";
140
+
141
+ // Subscribe to the "hello.getFirstTodo" hook
142
+ Hook.subscribe("hello.getFirstTodo", ({url}) => {
143
+ console.log(`Requesting ${url}`);
144
+ });
145
+ ```
146
+
147
+ ### Caching Requests
148
+
149
+ Enable caching on requests to reduce network load and improve performance. By default, the cache duration is 20 seconds,
150
+ but you can specify a custom duration in seconds.
151
+
152
+ #### Caching Individual Routes
153
+
154
+ You can enable caching on individual routes:
155
+
156
+ ```typescript
157
+ Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
158
+ // Get a list of todos with default cache duration (20 seconds)
159
+ Route.get<Todo[]>("listTodos", "todos").withCache();
160
+
161
+ // Get a specific todo by id with custom cache duration (300 seconds)
162
+ Route.get<Todo>("getTodo", "todos/[id]").withCache(300);
163
+
164
+ // Add a new todo (no cache)
165
+ Route.post<Todo>("addTodo", "todos");
166
+ });
167
+ ```
168
+
169
+ Now, when making requests, the caching feature will be applied.
170
+
171
+ #### Caching the Entire API
172
+
173
+ You can also enable caching for all routes defined within an API:
174
+
175
+ ```typescript
176
+ Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
177
+ // Define routes for the API
178
+ Route.get<Todo[]>("listTodos", "todos");
179
+ Route.get<Todo>("getTodo", "todos/[id]");
180
+ Route.post<Todo>("addTodo", "todos");
181
+ }).withCache(); // Enable default cache duration (20 seconds) for all routes
182
+ ```
183
+
184
+ ### Retry Mechanism
185
+
186
+ Automatically retry failed requests to enhance reliability. You can specify the number of retry attempts for individual
187
+ routes or for the entire API.
188
+
189
+ #### Retry on Individual Routes
190
+
191
+ Enable retry on individual routes:
192
+
193
+ ```typescript
194
+ Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
195
+ // Get a list of todos with retry mechanism (default: 2)
196
+ Route.get<Todo[]>("listTodos", "todos").withRetry();
197
+
198
+ // Get a specific todo by id with retry mechanism (specified to 5)
199
+ Route.get<Todo>("getTodo", "todos/[id]").withRetry(5);
200
+
201
+ // Add a new todo (no retry)
202
+ Route.post<Todo>("addTodo", "todos");
203
+ });
204
+ ```
205
+
206
+ #### Retry the Entire API
207
+
208
+ Enable retry for all routes defined within an API:
209
+
210
+ ```typescript
211
+ Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
212
+ // Define routes for the API
213
+ Route.get<Todo[]>("listTodos", "todos");
214
+ Route.get<Todo>("getTodo", "todos/[id]");
215
+ Route.post<Todo>("addTodo", "todos");
216
+ }).withRetry();
217
+ ```
218
+
219
+ Now, when a request fails, it will be retried the specified number of times before ultimately failing.
220
+
221
+ ## 🔗 Links
222
+
223
+ - [NPM](https://www.npmjs.com/package/klaim)
224
+ - [JSR](https://jsr.io/@antharuu/klaim)
225
+ - [GitHub](https://github.com/antharuu/klaim)
226
+
227
+ ## 🤝 Contributing
228
+
229
+ Contributions are welcome! Please see the [Contributing Guide](CONTRIBUTING.md) for more details.
230
+
231
+ ## 📜 License
232
+
233
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
package/deno.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antharuu/klaim",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
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 $=Object.defineProperty;var k=(a,t,e)=>t in a?$(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e;var i=(a,t,e)=>k(a,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function d(a){return a.trim().replace(/^\/|\/$/g,"")}function A(a){return a.replace(/([-_][a-z])/gi,t=>t.toUpperCase().replace("-","").replace("_","")).replace(/(^\w)/,t=>t.toLowerCase())}class p{static subscribe(t,e){this._callbacks.set(t,e)}static run(t){const e=this._callbacks.get(t);e&&e()}}i(p,"_callbacks",new Map);var g=(a=>(a.GET="GET",a.POST="POST",a.PUT="PUT",a.DELETE="DELETE",a.PATCH="PATCH",a.OPTIONS="OPTIONS",a))(g||{});class h{constructor(t,e,n,r="GET"){i(this,"api","undefined");i(this,"name");i(this,"url");i(this,"method");i(this,"headers");i(this,"arguments",new Set);i(this,"callbacks",{before:null,after:null});this.name=A(t),this.name!==t&&console.warn(`Route name "${t}" has been camelCased to "${this.name}"`),this.url=d(e),this.headers=n||{},this.method=r,this.detectArguments()}static createRoute(t,e,n,r){const s=new h(t,e,n,r);return o.i.registerRoute(s),s}static get(t,e,n={}){return this.createRoute(t,e,n,"GET")}static post(t,e,n){return this.createRoute(t,e,n,"POST")}static put(t,e,n){return this.createRoute(t,e,n,"PUT")}static delete(t,e,n){return this.createRoute(t,e,n,"DELETE")}static patch(t,e,n){return this.createRoute(t,e,n,"PATCH")}static options(t,e,n){return this.createRoute(t,e,n,"OPTIONS")}before(t){return this.callbacks.before=t,this}after(t){return this.callbacks.after=t,this}detectArguments(){const t=this.url.match(/\[([^\]]+)]/g);t&&t.forEach(e=>{const n=e.replace(/\[|]/g,"");this.arguments.add(n)})}}const l={};async function O(a,t,e={},n={}){let r=_(`${a.url}/${t.url}`,t,e),s={};n&&t.method!==g.GET&&(s.body=JSON.stringify(n)),s.headers={"Content-Type":"application/json",...a.headers,...t.headers},s.method=t.method;const{beforeRoute:u,beforeApi:w,beforeUrl:T,beforeConfig:b}=y({route:t,api:a,url:r,config:s});r=T,s=b,a=o.updateApi(w),t=o.updateRoute(u);const m=await fetch(r,s),{afterRoute:E,afterApi:P,afterData:C}=S({route:t,api:a,response:m,data:await m.json()});return o.updateApi(P),o.updateRoute(E),p.run(`${a.name}.${t.name}`),C}function _(a,t,e){let n=a;return t.arguments.forEach(r=>{if(e[r]===void 0)throw new Error(`Argument ${r} is missing`);n=n.replace(`[${r}]`,e[r])}),n}function y({route:a,api:t,url:e,config:n}){var s,u;const r=(u=(s=a.callbacks).before)==null?void 0:u.call(s,{route:a,api:t,url:e,config:n});return{beforeRoute:(r==null?void 0:r.route)||a,beforeApi:(r==null?void 0:r.api)||t,beforeUrl:(r==null?void 0:r.url)||e,beforeConfig:(r==null?void 0:r.config)||n}}function S({route:a,api:t,response:e,data:n}){var s,u;const r=(u=(s=a.callbacks).after)==null?void 0:u.call(s,{route:a,api:t,response:e,data:n});return{afterRoute:(r==null?void 0:r.route)||a,afterApi:(r==null?void 0:r.api)||t,afterResponse:(r==null?void 0:r.response)||e,afterData:(r==null?void 0:r.data)||n}}const c=class c{constructor(){i(this,"_apis",new Map);i(this,"_currentApi",null)}static get i(){return c._instance||(c._instance=new c),c._instance}registerApi(t){this._apis.set(t.name,t),l[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 n=this._apis.get(t);if(!n)throw new Error(`API ${t} not found`);return n.routes.get(e)}static updateApi(t){return c.i._apis.has(t.name)||c.i.registerApi(t),c.i._apis.set(t.name,t),t}static updateRoute(t){const e=c.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){l[t][e.name]=async(n={},r={})=>{const s=c.i._apis.get(t);if(!s)throw new Error(`API ${e.api} not found`);return O(s,e,n,r)}}};i(c,"_instance");let o=c;class f{constructor(t,e,n={}){i(this,"name");i(this,"url");i(this,"headers");i(this,"routes",new Map);this.name=t,this.url=d(e),this.headers=n}static create(t,e,n,r={}){const s=A(t);s!==t&&console.warn(`API name "${t}" has been camelCased to "${s}"`);const u=new f(s,e,r);return o.i.registerApi(u),o.i.setCurrent(s),n(),o.i.clearCurrent(),u}}exports.Api=f;exports.Hook=p;exports.Klaim=l;exports.Registry=o;exports.Route=h;
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;