@spoosh/core 0.5.0 → 0.6.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 +41 -47
- package/dist/index.d.mts +387 -253
- package/dist/index.d.ts +387 -253
- package/dist/index.js +87 -111
- package/dist/index.mjs +87 -111
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -15,31 +15,29 @@ npm install @spoosh/core
|
|
|
15
15
|
### Define API Schema
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
import type { Endpoint } from "@spoosh/core";
|
|
19
|
-
|
|
20
18
|
type User = { id: number; name: string; email: string };
|
|
21
19
|
|
|
22
20
|
type ApiSchema = {
|
|
23
21
|
users: {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
};
|
|
22
|
+
GET: { data: User[] };
|
|
23
|
+
POST: { data: User; body: { name: string; email: string } };
|
|
24
|
+
};
|
|
25
|
+
"users/:id": {
|
|
26
|
+
GET: { data: User };
|
|
27
|
+
PUT: { data: User; body: Partial<User> };
|
|
28
|
+
DELETE: { data: void };
|
|
31
29
|
};
|
|
32
30
|
search: {
|
|
33
|
-
|
|
31
|
+
GET: { data: User[]; query: { q: string; page?: number } };
|
|
34
32
|
};
|
|
35
33
|
upload: {
|
|
36
|
-
|
|
34
|
+
POST: { data: { url: string }; body: { file: File } };
|
|
37
35
|
};
|
|
38
36
|
payments: {
|
|
39
|
-
|
|
37
|
+
POST: {
|
|
40
38
|
data: { id: string };
|
|
41
|
-
|
|
42
|
-
}
|
|
39
|
+
body: { amount: number; currency: string };
|
|
40
|
+
};
|
|
43
41
|
};
|
|
44
42
|
};
|
|
45
43
|
```
|
|
@@ -58,41 +56,37 @@ const api = createClient<ApiSchema>({
|
|
|
58
56
|
|
|
59
57
|
```typescript
|
|
60
58
|
// GET /api/users
|
|
61
|
-
const { data, error, status } = await api.
|
|
59
|
+
const { data, error, status } = await api("users").GET();
|
|
62
60
|
|
|
63
61
|
// GET /api/search?q=john&page=1
|
|
64
|
-
const { data } = await api.
|
|
62
|
+
const { data } = await api("search").GET({ query: { q: "john", page: 1 } });
|
|
65
63
|
|
|
66
64
|
// POST /api/users with JSON body
|
|
67
|
-
const { data: newUser } = await api.
|
|
65
|
+
const { data: newUser } = await api("users").POST({
|
|
68
66
|
body: { name: "John", email: "john@example.com" },
|
|
69
67
|
});
|
|
70
68
|
|
|
71
|
-
// GET /api/users/123 (
|
|
72
|
-
const { data: user } = await api.
|
|
69
|
+
// GET /api/users/123 (with params)
|
|
70
|
+
const { data: user } = await api("users/:id").GET({ params: { id: 123 } });
|
|
73
71
|
|
|
74
|
-
// PUT /api/users/123
|
|
72
|
+
// PUT /api/users/123
|
|
75
73
|
const userId = 123;
|
|
76
|
-
const { data: updated } = await api
|
|
74
|
+
const { data: updated } = await api("users/:id").PUT({
|
|
75
|
+
params: { id: userId },
|
|
77
76
|
body: { name: "John Updated" },
|
|
78
77
|
});
|
|
79
78
|
|
|
80
79
|
// DELETE /api/users/123
|
|
81
|
-
await api.
|
|
80
|
+
await api("users/:id").DELETE({ params: { id: 123 } });
|
|
82
81
|
|
|
83
|
-
//
|
|
84
|
-
const { data } = await api
|
|
85
|
-
|
|
82
|
+
// POST with file upload (auto FormData when File detected)
|
|
83
|
+
const { data: uploaded } = await api("upload").POST({
|
|
84
|
+
body: { file: myFile },
|
|
86
85
|
});
|
|
87
86
|
|
|
88
|
-
// POST with
|
|
89
|
-
const { data:
|
|
90
|
-
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// POST with URL-encoded body (auto Content-Type: application/x-www-form-urlencoded)
|
|
94
|
-
const { data: payment } = await api.payments.$post({
|
|
95
|
-
urlEncoded: { amount: 1000, currency: "usd" },
|
|
87
|
+
// POST with form data
|
|
88
|
+
const { data: payment } = await api("payments").POST({
|
|
89
|
+
body: { amount: 1000, currency: "usd" },
|
|
96
90
|
});
|
|
97
91
|
```
|
|
98
92
|
|
|
@@ -121,10 +115,12 @@ import { createClient } from "@spoosh/core";
|
|
|
121
115
|
const api = createClient<ApiSchema>({ baseUrl: process.env.API_URL! });
|
|
122
116
|
|
|
123
117
|
// Auto-generates next: { tags: ['posts'] }
|
|
124
|
-
const { data: posts } = await api.
|
|
118
|
+
const { data: posts } = await api("posts").GET();
|
|
125
119
|
|
|
126
120
|
// Auto-generates next: { tags: ['users', 'users/123', 'users/123/posts'] }
|
|
127
|
-
const { data: userPosts } = await api
|
|
121
|
+
const { data: userPosts } = await api("users/:id/posts").GET({
|
|
122
|
+
params: { id: 123 },
|
|
123
|
+
});
|
|
128
124
|
```
|
|
129
125
|
|
|
130
126
|
This enables automatic cache invalidation with `revalidateTag()` in Next.js.
|
|
@@ -176,16 +172,14 @@ const updatedContext = await applyMiddlewares(context, middlewares, "before");
|
|
|
176
172
|
|
|
177
173
|
## Schema Types
|
|
178
174
|
|
|
179
|
-
|
|
|
180
|
-
|
|
|
181
|
-
| `
|
|
182
|
-
| `
|
|
183
|
-
| `
|
|
184
|
-
| `
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
| `void` | No response body | `$delete: void` |
|
|
188
|
-
| `_` | Dynamic path segment | `users: { _: { $get: Endpoint<{ data: User }> } }` |
|
|
175
|
+
| Field | Description | Example |
|
|
176
|
+
| ------- | --------------------------------------- | ------------------------------------------------ |
|
|
177
|
+
| `data` | Response data type | `GET: { data: User[] }` |
|
|
178
|
+
| `body` | Request body type (files auto-detected) | `POST: { data: User; body: CreateUserBody }` |
|
|
179
|
+
| `query` | Query parameters type | `GET: { data: User[]; query: { page: number } }` |
|
|
180
|
+
| `error` | Typed error type | `GET: { data: User; error: ApiError }` |
|
|
181
|
+
|
|
182
|
+
Path parameters are defined using `:param` syntax in the path key (e.g., `"users/:id"`).
|
|
189
183
|
|
|
190
184
|
## API Reference
|
|
191
185
|
|
|
@@ -201,7 +195,7 @@ Creates a lightweight type-safe API client.
|
|
|
201
195
|
|
|
202
196
|
### Spoosh (class)
|
|
203
197
|
|
|
204
|
-
Creates a full-featured client with plugin system using a clean class-based API. Use this with `@spoosh/react`.
|
|
198
|
+
Creates a full-featured client with plugin system using a clean class-based API. Use this with `@spoosh/react` or `@spoosh/angular`.
|
|
205
199
|
|
|
206
200
|
```typescript
|
|
207
201
|
import { Spoosh } from "@spoosh/core";
|
|
@@ -213,7 +207,7 @@ const client = new Spoosh<ApiSchema, Error>("/api", {
|
|
|
213
207
|
}).use([cachePlugin({ staleTime: 5000 }), retryPlugin({ retries: 3 })]);
|
|
214
208
|
|
|
215
209
|
const { api } = client;
|
|
216
|
-
const { data } = await api.
|
|
210
|
+
const { data } = await api("users").GET();
|
|
217
211
|
```
|
|
218
212
|
|
|
219
213
|
**Constructor Parameters:**
|