@spoosh/core 0.5.0 → 0.7.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 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
- $get: Endpoint<{ data: User[] }>;
25
- $post: Endpoint<{ data: User; body: { name: string; email: string } }>;
26
- _: {
27
- $get: Endpoint<{ data: User }>;
28
- $put: Endpoint<{ data: User; body: Partial<User> }>;
29
- $delete: void;
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
- $get: Endpoint<{ data: User[]; query: { q: string; page?: number } }>;
31
+ GET: { data: User[]; query: { q: string; page?: number } };
34
32
  };
35
33
  upload: {
36
- $post: Endpoint<{ data: { url: string }; formData: { file: File } }>;
34
+ POST: { data: { url: string }; body: { file: File } };
37
35
  };
38
36
  payments: {
39
- $post: Endpoint<{
37
+ POST: {
40
38
  data: { id: string };
41
- urlEncoded: { amount: number; currency: string };
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.users.$get();
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.search.$get({ query: { q: "john", page: 1 } });
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.users.$post({
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 (direct usage - simplest)
72
- const { data: user } = await api.users(123).$get();
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 (with variable)
72
+ // PUT /api/users/123
75
73
  const userId = 123;
76
- const { data: updated } = await api.users(userId).$put({
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.users(123).$delete();
80
+ await api("users/:id").DELETE({ params: { id: 123 } });
82
81
 
83
- // Typed params (advanced - when you need explicit param names)
84
- const { data } = await api.users(":userId").$get({
85
- params: { userId: 123 },
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 FormData
89
- const { data: uploaded } = await api.upload.$post({
90
- formData: { file: myFile },
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.posts.$get();
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.users(123).posts.$get();
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
- | Type | Description | Example |
180
- | -------------------------------- | ---------------------------- | ------------------------------------------------------------------- |
181
- | `Endpoint<{ data }>` | Endpoint with data only | `$get: Endpoint<{ data: User[] }>` |
182
- | `Endpoint<{ data; body }>` | Endpoint with JSON body | `$post: Endpoint<{ data: User; body: CreateUserBody }>` |
183
- | `Endpoint<{ data; query }>` | Endpoint with query params | `$get: Endpoint<{ data: User[]; query: { page: number } }>` |
184
- | `Endpoint<{ data; formData }>` | Endpoint with multipart form | `$post: Endpoint<{ data: Result; formData: { file: File } }>` |
185
- | `Endpoint<{ data; urlEncoded }>` | Endpoint with URL-encoded | `$post: Endpoint<{ data: Result; urlEncoded: { amount: number } }>` |
186
- | `Endpoint<{ data; error }>` | Endpoint with typed error | `$get: Endpoint<{ data: User; error: ApiError }>` |
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.users.$get();
210
+ const { data } = await api("users").GET();
217
211
  ```
218
212
 
219
213
  **Constructor Parameters:**