enlace 0.0.1-beta.2 → 0.0.1-beta.3
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 +44 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,11 +15,11 @@ import { createEnlaceHook, Endpoint } from "enlace";
|
|
|
15
15
|
|
|
16
16
|
type ApiSchema = {
|
|
17
17
|
posts: {
|
|
18
|
-
$get: Endpoint<Post[]>;
|
|
18
|
+
$get: Endpoint<Post[], ApiError>;
|
|
19
19
|
$post: Endpoint<Post, ApiError, CreatePost>;
|
|
20
20
|
_: {
|
|
21
|
-
$get: Endpoint<Post>;
|
|
22
|
-
$delete: Endpoint<void>;
|
|
21
|
+
$get: Endpoint<Post, ApiError>;
|
|
22
|
+
$delete: Endpoint<void, ApiError>;
|
|
23
23
|
};
|
|
24
24
|
};
|
|
25
25
|
};
|
|
@@ -52,14 +52,14 @@ import { Endpoint } from "enlace";
|
|
|
52
52
|
|
|
53
53
|
type ApiSchema = {
|
|
54
54
|
users: {
|
|
55
|
-
$get: Endpoint<User[]>; // GET /users
|
|
56
|
-
$post: Endpoint<User>; // POST /users
|
|
57
|
-
_: {
|
|
58
|
-
$get: Endpoint<User>; // GET /users/:id
|
|
59
|
-
$put: Endpoint<User>; // PUT /users/:id
|
|
60
|
-
$delete: Endpoint<void>; // DELETE /users/:id
|
|
55
|
+
$get: Endpoint<User[], ApiError>; // GET /users
|
|
56
|
+
$post: Endpoint<User, ApiError>; // POST /users
|
|
57
|
+
_: { // /users/:id
|
|
58
|
+
$get: Endpoint<User, ApiError>; // GET /users/:id
|
|
59
|
+
$put: Endpoint<User, ApiError>; // PUT /users/:id
|
|
60
|
+
$delete: Endpoint<void, ApiError>; // DELETE /users/:id
|
|
61
61
|
profile: {
|
|
62
|
-
$get: Endpoint<Profile>; // GET /users/:id/profile
|
|
62
|
+
$get: Endpoint<Profile, ApiError>; // GET /users/:id/profile
|
|
63
63
|
};
|
|
64
64
|
};
|
|
65
65
|
};
|
|
@@ -74,16 +74,16 @@ api.users[123].profile.get(); // GET /users/123/profile
|
|
|
74
74
|
### Endpoint Type
|
|
75
75
|
|
|
76
76
|
```typescript
|
|
77
|
-
type Endpoint<TData, TError
|
|
77
|
+
type Endpoint<TData, TError, TBody = never> = {
|
|
78
78
|
data: TData; // Response data type
|
|
79
|
-
error: TError; // Error response type
|
|
80
|
-
body: TBody; // Request body type
|
|
79
|
+
error: TError; // Error response type (required)
|
|
80
|
+
body: TBody; // Request body type (optional)
|
|
81
81
|
};
|
|
82
82
|
|
|
83
83
|
// Examples
|
|
84
|
-
type GetUsers = Endpoint<User[]>;
|
|
84
|
+
type GetUsers = Endpoint<User[], ApiError>; // GET, no body
|
|
85
85
|
type CreateUser = Endpoint<User, ApiError, CreateUserInput>; // POST with body
|
|
86
|
-
type DeleteUser = Endpoint<void, NotFoundError>;
|
|
86
|
+
type DeleteUser = Endpoint<void, NotFoundError>; // DELETE, no response data
|
|
87
87
|
```
|
|
88
88
|
|
|
89
89
|
## React Hooks
|
|
@@ -115,6 +115,7 @@ function Posts({ page, limit }: { page: number; limit: number }) {
|
|
|
115
115
|
- Auto-fetches on mount
|
|
116
116
|
- Re-fetches when dependencies change (no deps array needed!)
|
|
117
117
|
- Returns cached data while revalidating
|
|
118
|
+
- **Request deduplication** — identical requests from multiple components trigger only one fetch
|
|
118
119
|
|
|
119
120
|
```typescript
|
|
120
121
|
function Post({ id }: { id: number }) {
|
|
@@ -124,6 +125,34 @@ function Post({ id }: { id: number }) {
|
|
|
124
125
|
}
|
|
125
126
|
```
|
|
126
127
|
|
|
128
|
+
### Request Deduplication
|
|
129
|
+
|
|
130
|
+
Multiple components requesting the same data will share a single network request:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
// Both components render at the same time
|
|
134
|
+
function PostTitle({ id }: { id: number }) {
|
|
135
|
+
const { data } = useAPI((api) => api.posts[id].get());
|
|
136
|
+
return <h1>{data?.title}</h1>;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function PostBody({ id }: { id: number }) {
|
|
140
|
+
const { data } = useAPI((api) => api.posts[id].get());
|
|
141
|
+
return <p>{data?.body}</p>;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Only ONE fetch request is made to GET /posts/123
|
|
145
|
+
// Both components share the same cached result
|
|
146
|
+
function PostPage() {
|
|
147
|
+
return (
|
|
148
|
+
<>
|
|
149
|
+
<PostTitle id={123} />
|
|
150
|
+
<PostBody id={123} />
|
|
151
|
+
</>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
127
156
|
### Selector Mode (Manual Trigger)
|
|
128
157
|
|
|
129
158
|
For mutations or lazy-loaded requests:
|