klaim 1.2.31 → 1.2.33
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 +51 -24
- package/deno.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Klaim 📦
|
|
2
2
|
|
|
3
|
+
[](https://jsr.io/@antharuu/klaim)
|
|
4
|
+
|
|
3
5
|
Klaim is a lightweight TypeScript library designed to manage APIs and record requests, optimized for an optimal user
|
|
4
6
|
experience.
|
|
5
7
|
|
|
@@ -11,6 +13,7 @@ experience.
|
|
|
11
13
|
- [Basic API Configuration](#basic-api-configuration)
|
|
12
14
|
- [Route Definition](#route-definition)
|
|
13
15
|
- [Request Handling](#request-handling)
|
|
16
|
+
- [Middleware Usage](#middleware-usage)
|
|
14
17
|
- [Hook Subscription](#hook-subscription)
|
|
15
18
|
- [Links](#-links)
|
|
16
19
|
- [Contributing](#-contributing)
|
|
@@ -45,9 +48,11 @@ deno add @antharuu/klaim
|
|
|
45
48
|
|
|
46
49
|
### Basic API Configuration
|
|
47
50
|
|
|
51
|
+
First, set up the API configuration. Define the API and its base URL.
|
|
52
|
+
|
|
48
53
|
```typescript
|
|
49
|
-
import {Api,
|
|
50
|
-
// For deno: import { Api,
|
|
54
|
+
import {Api, Route} from 'klaim';
|
|
55
|
+
// For deno: import { Api, Route } from "@antharuu/klaim";
|
|
51
56
|
|
|
52
57
|
// Your simple Todo type
|
|
53
58
|
type Todo = {
|
|
@@ -55,45 +60,44 @@ type Todo = {
|
|
|
55
60
|
id: number;
|
|
56
61
|
title: string;
|
|
57
62
|
completed: boolean;
|
|
58
|
-
}
|
|
63
|
+
};
|
|
59
64
|
|
|
60
65
|
// Create a new API with the name "hello" and the base URL "https://jsonplaceholder.typicode.com/"
|
|
61
|
-
Api.create("hello", "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
|
+
});
|
|
62
72
|
```
|
|
63
73
|
|
|
64
74
|
### Route Definition
|
|
65
75
|
|
|
66
|
-
Define routes
|
|
76
|
+
Define various routes within the API callback:
|
|
67
77
|
|
|
68
78
|
```typescript
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
// Define a route to get a todo by id
|
|
73
|
-
Route.get<Todo>("getTodo", "todos/[id]");
|
|
79
|
+
Api.create("hello", "https://jsonplaceholder.typicode.com/", () => {
|
|
80
|
+
// Get a list of todos
|
|
81
|
+
Route.get<Todo[]>("listTodos", "todos");
|
|
74
82
|
|
|
75
|
-
//
|
|
76
|
-
Route.
|
|
83
|
+
// Get a specific todo by id
|
|
84
|
+
Route.get<Todo>("getTodo", "todos/[id]");
|
|
77
85
|
|
|
78
|
-
//
|
|
79
|
-
Route.
|
|
80
|
-
|
|
81
|
-
const random = Math.floor(Math.random() * 10) + 1;
|
|
82
|
-
return ({url: `${url}/${random}`});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// Define a route with after middleware
|
|
86
|
-
Route.get<Todo>("getFirstTodo", "todos")
|
|
87
|
-
.after(({data: [first]}) => ({data: first}));
|
|
86
|
+
// Add a new todo
|
|
87
|
+
Route.post<Todo>("addTodo", "todos");
|
|
88
|
+
});
|
|
88
89
|
```
|
|
89
90
|
|
|
90
91
|
### Request Handling
|
|
91
92
|
|
|
92
|
-
|
|
93
|
+
Handle requests using the defined routes:
|
|
93
94
|
|
|
94
95
|
```typescript
|
|
96
|
+
import {Klaim} from 'klaim';
|
|
97
|
+
// For deno: import { Klaim } from "@antharuu/klaim";
|
|
98
|
+
|
|
95
99
|
// Make a request to the "listTodos" route
|
|
96
|
-
const listOfTodos = await Klaim.hello.
|
|
100
|
+
const listOfTodos = await Klaim.hello.listTodos<Todo[]>();
|
|
97
101
|
|
|
98
102
|
// Make a request to the "getTodo" route with the parameter "id"
|
|
99
103
|
const todo = await Klaim.hello.getTodo<Todo>({id: 1});
|
|
@@ -102,11 +106,34 @@ const todo = await Klaim.hello.getTodo<Todo>({id: 1});
|
|
|
102
106
|
const newTodo = await Klaim.hello.addTodo<Todo>({}, {title: "New Todo", completed: false, userId: 1});
|
|
103
107
|
```
|
|
104
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
|
+
|
|
105
129
|
### Hook Subscription
|
|
106
130
|
|
|
107
131
|
Subscribe to hooks to monitor specific events:
|
|
108
132
|
|
|
109
133
|
```typescript
|
|
134
|
+
import {Hook} from 'klaim';
|
|
135
|
+
// For deno: import { Hook } from "@antharuu/klaim";
|
|
136
|
+
|
|
110
137
|
// Subscribe to the "hello.getFirstTodo" hook
|
|
111
138
|
Hook.subscribe("hello.getFirstTodo", ({url}) => {
|
|
112
139
|
console.log(`Requesting ${url}`);
|
package/deno.json
CHANGED