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