klaim 1.6.0 → 1.6.2
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/Makefile +39 -14
- package/README.md +262 -262
- package/deno.json +38 -38
- package/package.json +10 -10
- package/src/core/Cache.ts +72 -72
- package/src/core/Element.ts +150 -150
- package/src/core/Klaim.ts +222 -222
- package/src/core/Route.ts +165 -165
- package/src/tools/fetchWithCache.ts +30 -30
- package/tests/01.api.test.ts +60 -60
- package/tests/02.route.test.ts +186 -186
- package/tests/03.hook.test.ts +34 -34
- package/tests/04.klaim.test.ts +96 -96
- package/tests/05.cache.test.ts +46 -46
- package/tests/06.retry.test.ts +54 -54
- package/tests/07.validate.test.ts +61 -61
package/Makefile
CHANGED
|
@@ -1,14 +1,39 @@
|
|
|
1
|
-
dev:
|
|
2
|
-
nr dev
|
|
3
|
-
|
|
4
|
-
dev-test:
|
|
5
|
-
cd ../klaim-test && nr dev
|
|
6
|
-
|
|
7
|
-
update-version-deno:
|
|
8
|
-
@echo "Extracting version from package.json..."
|
|
9
|
-
@VERSION=$(shell grep -oP '"version":\s*"\K[^"]+' package.json) && \
|
|
10
|
-
echo "Found version: $$VERSION" && \
|
|
11
|
-
echo "Updating deno.json..." && \
|
|
12
|
-
sed -i.bak 's/"version": "[^"]*"/"version": "'$$VERSION'"/' deno.json && \
|
|
13
|
-
rm deno.json.bak && \
|
|
14
|
-
echo "Update completed successfully!"
|
|
1
|
+
dev:
|
|
2
|
+
nr dev
|
|
3
|
+
|
|
4
|
+
dev-test:
|
|
5
|
+
cd ../klaim-test && nr dev
|
|
6
|
+
|
|
7
|
+
update-version-deno:
|
|
8
|
+
@echo "Extracting version from package.json..."
|
|
9
|
+
@VERSION=$(shell grep -oP '"version":\s*"\K[^"]+' package.json) && \
|
|
10
|
+
echo "Found version: $$VERSION" && \
|
|
11
|
+
echo "Updating deno.json..." && \
|
|
12
|
+
sed -i.bak 's/"version": "[^"]*"/"version": "'$$VERSION'"/' deno.json && \
|
|
13
|
+
rm deno.json.bak && \
|
|
14
|
+
echo "Update completed successfully!"
|
|
15
|
+
|
|
16
|
+
.PHONY: release release-minor release-major
|
|
17
|
+
|
|
18
|
+
release:
|
|
19
|
+
( \
|
|
20
|
+
eval `ssh-agent -s`; \
|
|
21
|
+
ssh-add ~/.ssh/id_rsa; \
|
|
22
|
+
nr release; \
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
release-minor:
|
|
26
|
+
( \
|
|
27
|
+
eval `ssh-agent -s`; \
|
|
28
|
+
ssh-add ~/.ssh/id_rsa; \
|
|
29
|
+
nr release:minor; \
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
release-major:
|
|
33
|
+
( \
|
|
34
|
+
eval `ssh-agent -s`; \
|
|
35
|
+
ssh-add ~/.ssh/id_rsa; \
|
|
36
|
+
nr release:major; \
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
package/README.md
CHANGED
|
@@ -1,262 +1,262 @@
|
|
|
1
|
-
## 📚 Table of Contents
|
|
2
|
-
|
|
3
|
-
- [Features](#-features)
|
|
4
|
-
- [Installation](#-installation)
|
|
5
|
-
- [Usage](#-usage)
|
|
6
|
-
- [Basic API Configuration](#basic-api-configuration)
|
|
7
|
-
- [Route Definition](#route-definition)
|
|
8
|
-
- [Request Handling](#request-handling)
|
|
9
|
-
- [Middleware Usage](#middleware-usage)
|
|
10
|
-
- [Hook Subscription](#hook-subscription)
|
|
11
|
-
- [Caching Requests](#caching-requests)
|
|
12
|
-
- [Retry Mechanism](#retry-mechanism)
|
|
13
|
-
- [Response Validation](#response-validation)
|
|
14
|
-
- [Links](#-links)
|
|
15
|
-
- [Contributing](#-contributing)
|
|
16
|
-
- [License](#-license)
|
|
17
|
-
|
|
18
|
-
## 🚀 Features
|
|
19
|
-
|
|
20
|
-
- **Efficient API Management**: Easily manage multiple APIs with streamlined integration and interaction capabilities.
|
|
21
|
-
- **Request Recording**: Seamlessly track requests for debugging and monitoring.
|
|
22
|
-
- **User Experience Optimization**: Focused on performance and usability for a smooth user experience.
|
|
23
|
-
- **Lightweight**: Minimal footprint for fast load times and minimal performance impact.
|
|
24
|
-
- **Middleware Support**: Easily add middleware to modify requests and responses (`before` and `after`).
|
|
25
|
-
- **Hook System**: Subscribe to hooks to monitor and react to specific events.
|
|
26
|
-
- **Caching**: Enable caching on requests to reduce network load and improve performance.
|
|
27
|
-
- **Retry Mechanism**: Automatically retry failed requests to enhance reliability.
|
|
28
|
-
- **TypeScript Support**: Fully typed for enhanced code quality and developer experience.
|
|
29
|
-
- **Response Validation**: Validate responses using schemas for increased reliability and consistency.
|
|
30
|
-
|
|
31
|
-
## 📥 Installation
|
|
32
|
-
|
|
33
|
-
Install Klaim via npm:
|
|
34
|
-
|
|
35
|
-
```sh
|
|
36
|
-
// Using npm
|
|
37
|
-
npm install klaim
|
|
38
|
-
|
|
39
|
-
// Using bun
|
|
40
|
-
bun add klaim
|
|
41
|
-
|
|
42
|
-
// Using deno
|
|
43
|
-
deno add @antharuu/klaim
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## 🛠 Usage
|
|
47
|
-
|
|
48
|
-
### Basic API Configuration
|
|
49
|
-
|
|
50
|
-
First, set up the API configuration. Define the API and its base URL.
|
|
51
|
-
|
|
52
|
-
```typescript
|
|
53
|
-
import {Api, Route} from 'klaim';
|
|
54
|
-
// For deno: import { Api, Route } from "@antharuu/klaim";
|
|
55
|
-
|
|
56
|
-
// Your simple Todo type
|
|
57
|
-
type Todo = {
|
|
58
|
-
userId: number;
|
|
59
|
-
id: number;
|
|
60
|
-
title: string;
|
|
61
|
-
completed: boolean;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
// Create a new API with the name "hello" and the base URL "https://jsonplaceholder.typicode.com/"
|
|
65
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
66
|
-
// Define routes for the API
|
|
67
|
-
Route.get<Todo[]>("listTodos", "todos");
|
|
68
|
-
Route.get<Todo>("getTodo", "todos/[id]");
|
|
69
|
-
Route.post<Todo>("addTodo", "todos");
|
|
70
|
-
});
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
### Route Definition
|
|
74
|
-
|
|
75
|
-
Define various routes within the API callback:
|
|
76
|
-
|
|
77
|
-
```typescript
|
|
78
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
79
|
-
// Get a list of todos
|
|
80
|
-
Route.get<Todo[]>("listTodos", "todos");
|
|
81
|
-
|
|
82
|
-
// Get a specific todo by id
|
|
83
|
-
Route.get<Todo>("getTodo", "todos/[id]");
|
|
84
|
-
|
|
85
|
-
// Add a new todo
|
|
86
|
-
Route.post<Todo>("addTodo", "todos");
|
|
87
|
-
});
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
### Request Handling
|
|
91
|
-
|
|
92
|
-
Handle requests using the defined routes:
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
import {Klaim} from 'klaim';
|
|
96
|
-
// For deno: import { Klaim } from "@antharuu/klaim";
|
|
97
|
-
|
|
98
|
-
// Make a request to the "listTodos" route
|
|
99
|
-
const listOfTodos = await Klaim.hello.listTodos<Todo[]>();
|
|
100
|
-
|
|
101
|
-
// Make a request to the "getTodo" route with the parameter "id"
|
|
102
|
-
const todo = await Klaim.hello.getTodo<Todo>({id: 1});
|
|
103
|
-
|
|
104
|
-
// Make a request to the "addTodo" route
|
|
105
|
-
const newTodo = await Klaim.hello.addTodo<Todo>({}, {title: "New Todo", completed: false, userId: 1});
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
### Middleware Usage
|
|
109
|
-
|
|
110
|
-
Add middleware to modify requests and responses. Use `before` middleware to alter requests before they are sent
|
|
111
|
-
and `after` middleware to process responses:
|
|
112
|
-
|
|
113
|
-
```typescript
|
|
114
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
115
|
-
// With before middleware
|
|
116
|
-
Route.get<Todo>("getRandomTodo", "todos")
|
|
117
|
-
.before(({url}) => {
|
|
118
|
-
const random = Math.floor(Math.random() * 10) + 1;
|
|
119
|
-
return {url: `${url}/${random}`};
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// With after middleware
|
|
123
|
-
Route.get<Todo>("getFirstTodo", "todos")
|
|
124
|
-
.after(({data: [first]}) => ({data: first}));
|
|
125
|
-
});
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
### Hook Subscription
|
|
129
|
-
|
|
130
|
-
Subscribe to hooks to monitor specific events:
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
import {Hook} from 'klaim';
|
|
134
|
-
// For deno: import { Hook } from "@antharuu/klaim";
|
|
135
|
-
|
|
136
|
-
// Subscribe to the "hello.getFirstTodo" hook
|
|
137
|
-
Hook.subscribe("hello.getFirstTodo", ({url}) => {
|
|
138
|
-
console.log(`Requesting ${url}`);
|
|
139
|
-
});
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
### Caching Requests
|
|
143
|
-
|
|
144
|
-
Enable caching on requests to reduce network load and improve performance. By default, the cache duration is 20 seconds,
|
|
145
|
-
but you can specify a custom duration in seconds.
|
|
146
|
-
|
|
147
|
-
#### Caching Individual Routes
|
|
148
|
-
|
|
149
|
-
You can enable caching on individual routes:
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
153
|
-
// Get a list of todos with default cache duration (20 seconds)
|
|
154
|
-
Route.get<Todo[]>("listTodos", "todos").withCache();
|
|
155
|
-
|
|
156
|
-
// Get a specific todo by id with custom cache duration (300 seconds)
|
|
157
|
-
Route.get<Todo>("getTodo", "todos/[id]").withCache(300);
|
|
158
|
-
|
|
159
|
-
// Add a new todo (no cache)
|
|
160
|
-
Route.post<Todo>("addTodo", "todos");
|
|
161
|
-
});
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
Now, when making requests, the caching feature will be applied.
|
|
165
|
-
|
|
166
|
-
#### Caching the Entire API
|
|
167
|
-
|
|
168
|
-
You can also enable caching for all routes defined within an API:
|
|
169
|
-
|
|
170
|
-
```typescript
|
|
171
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
172
|
-
// Define routes for the API
|
|
173
|
-
Route.get<Todo[]>("listTodos", "todos");
|
|
174
|
-
Route.get<Todo>("getTodo", "todos/[id]");
|
|
175
|
-
Route.post<Todo>("addTodo", "todos");
|
|
176
|
-
}).withCache(); // Enable default cache duration (20 seconds) for all routes
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
### Retry Mechanism
|
|
180
|
-
|
|
181
|
-
Automatically retry failed requests to enhance reliability. You can specify the number of retry attempts for individual
|
|
182
|
-
routes or for the entire API.
|
|
183
|
-
|
|
184
|
-
#### Retry on Individual Routes
|
|
185
|
-
|
|
186
|
-
Enable retry on individual routes:
|
|
187
|
-
|
|
188
|
-
```typescript
|
|
189
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
190
|
-
// Get a list of todos with retry mechanism (default: 2)
|
|
191
|
-
Route.get<Todo[]>("listTodos", "todos").withRetry();
|
|
192
|
-
|
|
193
|
-
// Get a specific todo by id with retry mechanism (specified to 5)
|
|
194
|
-
Route.get<Todo>("getTodo", "todos/[id]").withRetry(5);
|
|
195
|
-
|
|
196
|
-
// Add a new todo (no retry)
|
|
197
|
-
Route.post<Todo>("addTodo", "todos");
|
|
198
|
-
});
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
#### Retry the Entire API
|
|
202
|
-
|
|
203
|
-
Enable retry for all routes defined within an API:
|
|
204
|
-
|
|
205
|
-
```typescript
|
|
206
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
207
|
-
// Define routes for the API
|
|
208
|
-
Route.get<Todo[]>("listTodos", "todos");
|
|
209
|
-
Route.get<Todo>("getTodo", "todos/[id]");
|
|
210
|
-
Route.post<Todo>("addTodo", "todos");
|
|
211
|
-
}).withRetry();
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
Now, when a request fails, it will be retried the specified number of times before ultimately failing.
|
|
215
|
-
|
|
216
|
-
### Response Validation
|
|
217
|
-
|
|
218
|
-
You can use [Yup](https://www.npmjs.com/package/yup) to validate the response schema for increased reliability and consistency. You can specify a schema for
|
|
219
|
-
individual routes to ensure the response data conforms to the expected structure.
|
|
220
|
-
|
|
221
|
-
⚠️ **Note**: This feature requires the `yup` package to be installed.
|
|
222
|
-
|
|
223
|
-
#### Adding Validation to Individual Routes
|
|
224
|
-
|
|
225
|
-
Enable validation on individual routes:
|
|
226
|
-
|
|
227
|
-
```typescript
|
|
228
|
-
import * as yup from 'yup';
|
|
229
|
-
|
|
230
|
-
// Define the schema using Yup
|
|
231
|
-
const todoSchema = yup.object().shape({
|
|
232
|
-
userId: yup.number().required(),
|
|
233
|
-
id: yup.number().min(1).max(10).required(),
|
|
234
|
-
title: yup.string().required(),
|
|
235
|
-
completed: yup.boolean().required()
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
239
|
-
// Get a specific todo by id with validation
|
|
240
|
-
Route.get<Todo>("getTodo", "todos/[id]").validate(todoSchema);
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// This request will fail because the id is out of range
|
|
244
|
-
const todoFail = await Klaim.hello.getTodo<Todo>({id: 15});
|
|
245
|
-
|
|
246
|
-
// This request will succeed
|
|
247
|
-
const todo = await Klaim.hello.getTodo<Todo>({id: 1});
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
## 🔗 Links
|
|
251
|
-
|
|
252
|
-
- [NPM](https://www.npmjs.com/package/klaim)
|
|
253
|
-
- [JSR](https://jsr.io/@antharuu/klaim)
|
|
254
|
-
- [GitHub](https://github.com/antharuu/klaim)
|
|
255
|
-
|
|
256
|
-
## 🤝 Contributing
|
|
257
|
-
|
|
258
|
-
Contributions are welcome! Please see the [Contributing Guide](CONTRIBUTING.md) for more details.
|
|
259
|
-
|
|
260
|
-
## 📜 License
|
|
261
|
-
|
|
262
|
-
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
1
|
+
## 📚 Table of Contents
|
|
2
|
+
|
|
3
|
+
- [Features](#-features)
|
|
4
|
+
- [Installation](#-installation)
|
|
5
|
+
- [Usage](#-usage)
|
|
6
|
+
- [Basic API Configuration](#basic-api-configuration)
|
|
7
|
+
- [Route Definition](#route-definition)
|
|
8
|
+
- [Request Handling](#request-handling)
|
|
9
|
+
- [Middleware Usage](#middleware-usage)
|
|
10
|
+
- [Hook Subscription](#hook-subscription)
|
|
11
|
+
- [Caching Requests](#caching-requests)
|
|
12
|
+
- [Retry Mechanism](#retry-mechanism)
|
|
13
|
+
- [Response Validation](#response-validation)
|
|
14
|
+
- [Links](#-links)
|
|
15
|
+
- [Contributing](#-contributing)
|
|
16
|
+
- [License](#-license)
|
|
17
|
+
|
|
18
|
+
## 🚀 Features
|
|
19
|
+
|
|
20
|
+
- **Efficient API Management**: Easily manage multiple APIs with streamlined integration and interaction capabilities.
|
|
21
|
+
- **Request Recording**: Seamlessly track requests for debugging and monitoring.
|
|
22
|
+
- **User Experience Optimization**: Focused on performance and usability for a smooth user experience.
|
|
23
|
+
- **Lightweight**: Minimal footprint for fast load times and minimal performance impact.
|
|
24
|
+
- **Middleware Support**: Easily add middleware to modify requests and responses (`before` and `after`).
|
|
25
|
+
- **Hook System**: Subscribe to hooks to monitor and react to specific events.
|
|
26
|
+
- **Caching**: Enable caching on requests to reduce network load and improve performance.
|
|
27
|
+
- **Retry Mechanism**: Automatically retry failed requests to enhance reliability.
|
|
28
|
+
- **TypeScript Support**: Fully typed for enhanced code quality and developer experience.
|
|
29
|
+
- **Response Validation**: Validate responses using schemas for increased reliability and consistency.
|
|
30
|
+
|
|
31
|
+
## 📥 Installation
|
|
32
|
+
|
|
33
|
+
Install Klaim via npm:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
// Using npm
|
|
37
|
+
npm install klaim
|
|
38
|
+
|
|
39
|
+
// Using bun
|
|
40
|
+
bun add klaim
|
|
41
|
+
|
|
42
|
+
// Using deno
|
|
43
|
+
deno add @antharuu/klaim
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## 🛠 Usage
|
|
47
|
+
|
|
48
|
+
### Basic API Configuration
|
|
49
|
+
|
|
50
|
+
First, set up the API configuration. Define the API and its base URL.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import {Api, Route} from 'klaim';
|
|
54
|
+
// For deno: import { Api, Route } from "@antharuu/klaim";
|
|
55
|
+
|
|
56
|
+
// Your simple Todo type
|
|
57
|
+
type Todo = {
|
|
58
|
+
userId: number;
|
|
59
|
+
id: number;
|
|
60
|
+
title: string;
|
|
61
|
+
completed: boolean;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Create a new API with the name "hello" and the base URL "https://jsonplaceholder.typicode.com/"
|
|
65
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
66
|
+
// Define routes for the API
|
|
67
|
+
Route.get<Todo[]>("listTodos", "todos");
|
|
68
|
+
Route.get<Todo>("getTodo", "todos/[id]");
|
|
69
|
+
Route.post<Todo>("addTodo", "todos");
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Route Definition
|
|
74
|
+
|
|
75
|
+
Define various routes within the API callback:
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
79
|
+
// Get a list of todos
|
|
80
|
+
Route.get<Todo[]>("listTodos", "todos");
|
|
81
|
+
|
|
82
|
+
// Get a specific todo by id
|
|
83
|
+
Route.get<Todo>("getTodo", "todos/[id]");
|
|
84
|
+
|
|
85
|
+
// Add a new todo
|
|
86
|
+
Route.post<Todo>("addTodo", "todos");
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Request Handling
|
|
91
|
+
|
|
92
|
+
Handle requests using the defined routes:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import {Klaim} from 'klaim';
|
|
96
|
+
// For deno: import { Klaim } from "@antharuu/klaim";
|
|
97
|
+
|
|
98
|
+
// Make a request to the "listTodos" route
|
|
99
|
+
const listOfTodos = await Klaim.hello.listTodos<Todo[]>();
|
|
100
|
+
|
|
101
|
+
// Make a request to the "getTodo" route with the parameter "id"
|
|
102
|
+
const todo = await Klaim.hello.getTodo<Todo>({id: 1});
|
|
103
|
+
|
|
104
|
+
// Make a request to the "addTodo" route
|
|
105
|
+
const newTodo = await Klaim.hello.addTodo<Todo>({}, {title: "New Todo", completed: false, userId: 1});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Middleware Usage
|
|
109
|
+
|
|
110
|
+
Add middleware to modify requests and responses. Use `before` middleware to alter requests before they are sent
|
|
111
|
+
and `after` middleware to process responses:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
115
|
+
// With before middleware
|
|
116
|
+
Route.get<Todo>("getRandomTodo", "todos")
|
|
117
|
+
.before(({url}) => {
|
|
118
|
+
const random = Math.floor(Math.random() * 10) + 1;
|
|
119
|
+
return {url: `${url}/${random}`};
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// With after middleware
|
|
123
|
+
Route.get<Todo>("getFirstTodo", "todos")
|
|
124
|
+
.after(({data: [first]}) => ({data: first}));
|
|
125
|
+
});
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Hook Subscription
|
|
129
|
+
|
|
130
|
+
Subscribe to hooks to monitor specific events:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import {Hook} from 'klaim';
|
|
134
|
+
// For deno: import { Hook } from "@antharuu/klaim";
|
|
135
|
+
|
|
136
|
+
// Subscribe to the "hello.getFirstTodo" hook
|
|
137
|
+
Hook.subscribe("hello.getFirstTodo", ({url}) => {
|
|
138
|
+
console.log(`Requesting ${url}`);
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Caching Requests
|
|
143
|
+
|
|
144
|
+
Enable caching on requests to reduce network load and improve performance. By default, the cache duration is 20 seconds,
|
|
145
|
+
but you can specify a custom duration in seconds.
|
|
146
|
+
|
|
147
|
+
#### Caching Individual Routes
|
|
148
|
+
|
|
149
|
+
You can enable caching on individual routes:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
153
|
+
// Get a list of todos with default cache duration (20 seconds)
|
|
154
|
+
Route.get<Todo[]>("listTodos", "todos").withCache();
|
|
155
|
+
|
|
156
|
+
// Get a specific todo by id with custom cache duration (300 seconds)
|
|
157
|
+
Route.get<Todo>("getTodo", "todos/[id]").withCache(300);
|
|
158
|
+
|
|
159
|
+
// Add a new todo (no cache)
|
|
160
|
+
Route.post<Todo>("addTodo", "todos");
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Now, when making requests, the caching feature will be applied.
|
|
165
|
+
|
|
166
|
+
#### Caching the Entire API
|
|
167
|
+
|
|
168
|
+
You can also enable caching for all routes defined within an API:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
172
|
+
// Define routes for the API
|
|
173
|
+
Route.get<Todo[]>("listTodos", "todos");
|
|
174
|
+
Route.get<Todo>("getTodo", "todos/[id]");
|
|
175
|
+
Route.post<Todo>("addTodo", "todos");
|
|
176
|
+
}).withCache(); // Enable default cache duration (20 seconds) for all routes
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Retry Mechanism
|
|
180
|
+
|
|
181
|
+
Automatically retry failed requests to enhance reliability. You can specify the number of retry attempts for individual
|
|
182
|
+
routes or for the entire API.
|
|
183
|
+
|
|
184
|
+
#### Retry on Individual Routes
|
|
185
|
+
|
|
186
|
+
Enable retry on individual routes:
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
190
|
+
// Get a list of todos with retry mechanism (default: 2)
|
|
191
|
+
Route.get<Todo[]>("listTodos", "todos").withRetry();
|
|
192
|
+
|
|
193
|
+
// Get a specific todo by id with retry mechanism (specified to 5)
|
|
194
|
+
Route.get<Todo>("getTodo", "todos/[id]").withRetry(5);
|
|
195
|
+
|
|
196
|
+
// Add a new todo (no retry)
|
|
197
|
+
Route.post<Todo>("addTodo", "todos");
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
#### Retry the Entire API
|
|
202
|
+
|
|
203
|
+
Enable retry for all routes defined within an API:
|
|
204
|
+
|
|
205
|
+
```typescript
|
|
206
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
207
|
+
// Define routes for the API
|
|
208
|
+
Route.get<Todo[]>("listTodos", "todos");
|
|
209
|
+
Route.get<Todo>("getTodo", "todos/[id]");
|
|
210
|
+
Route.post<Todo>("addTodo", "todos");
|
|
211
|
+
}).withRetry();
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Now, when a request fails, it will be retried the specified number of times before ultimately failing.
|
|
215
|
+
|
|
216
|
+
### Response Validation
|
|
217
|
+
|
|
218
|
+
You can use [Yup](https://www.npmjs.com/package/yup) to validate the response schema for increased reliability and consistency. You can specify a schema for
|
|
219
|
+
individual routes to ensure the response data conforms to the expected structure.
|
|
220
|
+
|
|
221
|
+
⚠️ **Note**: This feature requires the `yup` package to be installed.
|
|
222
|
+
|
|
223
|
+
#### Adding Validation to Individual Routes
|
|
224
|
+
|
|
225
|
+
Enable validation on individual routes:
|
|
226
|
+
|
|
227
|
+
```typescript
|
|
228
|
+
import * as yup from 'yup';
|
|
229
|
+
|
|
230
|
+
// Define the schema using Yup
|
|
231
|
+
const todoSchema = yup.object().shape({
|
|
232
|
+
userId: yup.number().required(),
|
|
233
|
+
id: yup.number().min(1).max(10).required(),
|
|
234
|
+
title: yup.string().required(),
|
|
235
|
+
completed: yup.boolean().required()
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
239
|
+
// Get a specific todo by id with validation
|
|
240
|
+
Route.get<Todo>("getTodo", "todos/[id]").validate(todoSchema);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// This request will fail because the id is out of range
|
|
244
|
+
const todoFail = await Klaim.hello.getTodo<Todo>({id: 15});
|
|
245
|
+
|
|
246
|
+
// This request will succeed
|
|
247
|
+
const todo = await Klaim.hello.getTodo<Todo>({id: 1});
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## 🔗 Links
|
|
251
|
+
|
|
252
|
+
- [NPM](https://www.npmjs.com/package/klaim)
|
|
253
|
+
- [JSR](https://jsr.io/@antharuu/klaim)
|
|
254
|
+
- [GitHub](https://github.com/antharuu/klaim)
|
|
255
|
+
|
|
256
|
+
## 🤝 Contributing
|
|
257
|
+
|
|
258
|
+
Contributions are welcome! Please see the [Contributing Guide](CONTRIBUTING.md) for more details.
|
|
259
|
+
|
|
260
|
+
## 📜 License
|
|
261
|
+
|
|
262
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
package/deno.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@antharuu/klaim",
|
|
3
|
-
"version": "1.6.
|
|
4
|
-
"description": "Klaim is a lightweight TypeScript library designed to manage APIs and record requests, optimized for an optimal user experience.",
|
|
5
|
-
"repository": {
|
|
6
|
-
"type": "git",
|
|
7
|
-
"url": "https://github.com/antharuu/klaim.git"
|
|
8
|
-
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
"typescript",
|
|
11
|
-
"api",
|
|
12
|
-
"request",
|
|
13
|
-
"user experience",
|
|
14
|
-
"optimization",
|
|
15
|
-
"lightweight"
|
|
16
|
-
],
|
|
17
|
-
"author": "antharuu",
|
|
18
|
-
"license": "MIT",
|
|
19
|
-
"bugs": {
|
|
20
|
-
"url": "https://github.com/antharuu/klaim/issues"
|
|
21
|
-
},
|
|
22
|
-
"homepage": "https://github.com/antharuu/klaim#readme",
|
|
23
|
-
"main": "dist/klaim.cjs.js",
|
|
24
|
-
"module": "dist/klaim.es.js",
|
|
25
|
-
"types": "dist/index.d.ts",
|
|
26
|
-
"exports": "./mod.ts",
|
|
27
|
-
"scripts": {
|
|
28
|
-
"build": "vite build",
|
|
29
|
-
"dev": "vite build --watch",
|
|
30
|
-
"test": "vitest",
|
|
31
|
-
"lint": "eslint src",
|
|
32
|
-
"lint:fix": "eslint src **/*.ts --fix",
|
|
33
|
-
"release": "dotenv release-it --"
|
|
34
|
-
},
|
|
35
|
-
"peerDependencies": {
|
|
36
|
-
"typescript": "^5.5.3"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@antharuu/klaim",
|
|
3
|
+
"version": "1.6.2",
|
|
4
|
+
"description": "Klaim is a lightweight TypeScript library designed to manage APIs and record requests, optimized for an optimal user experience.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/antharuu/klaim.git"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"typescript",
|
|
11
|
+
"api",
|
|
12
|
+
"request",
|
|
13
|
+
"user experience",
|
|
14
|
+
"optimization",
|
|
15
|
+
"lightweight"
|
|
16
|
+
],
|
|
17
|
+
"author": "antharuu",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/antharuu/klaim/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/antharuu/klaim#readme",
|
|
23
|
+
"main": "dist/klaim.cjs.js",
|
|
24
|
+
"module": "dist/klaim.es.js",
|
|
25
|
+
"types": "dist/index.d.ts",
|
|
26
|
+
"exports": "./mod.ts",
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "vite build",
|
|
29
|
+
"dev": "vite build --watch",
|
|
30
|
+
"test": "vitest",
|
|
31
|
+
"lint": "eslint src",
|
|
32
|
+
"lint:fix": "eslint src **/*.ts --fix",
|
|
33
|
+
"release": "dotenv release-it --"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"typescript": "^5.5.3"
|
|
37
|
+
}
|
|
38
|
+
}
|