@veloxts/client 0.4.0 → 0.4.2
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 +22 -236
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,264 +1,50 @@
|
|
|
1
1
|
# @veloxts/client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Pre-Alpha Notice:** This framework is in early development (v0.4.x). APIs are subject to change. Not recommended for production use.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## What is this?
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Type-safe frontend API client for the VeloxTS Framework, with zero code generation and full type inference from backend procedures.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
- **Full type safety** - Autocomplete and compile-time type checking
|
|
11
|
-
- **Fetch-based** - Works in both browser and Node.js environments
|
|
12
|
-
- **Error handling** - Typed error classes with full response context
|
|
13
|
-
- **Interceptors** - Request, response, and error hooks
|
|
14
|
-
- **REST integration** - Maps to auto-generated REST endpoints
|
|
9
|
+
## Part of @veloxts/velox
|
|
15
10
|
|
|
16
|
-
|
|
11
|
+
This package is part of the VeloxTS Framework. For the complete framework experience, install:
|
|
17
12
|
|
|
18
13
|
```bash
|
|
19
|
-
npm install @veloxts/
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
### Basic Setup
|
|
25
|
-
|
|
26
|
-
```typescript
|
|
27
|
-
// Import your backend procedure types
|
|
28
|
-
import type { userProcedures, postProcedures } from '../server/procedures';
|
|
29
|
-
import { createClient } from '@veloxts/client';
|
|
30
|
-
|
|
31
|
-
// Create a typed client
|
|
32
|
-
const api = createClient<{
|
|
33
|
-
users: typeof userProcedures;
|
|
34
|
-
posts: typeof postProcedures;
|
|
35
|
-
}>({
|
|
36
|
-
baseUrl: '/api',
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
// Make fully typed API calls
|
|
40
|
-
const user = await api.users.getUser({ id: '123' });
|
|
41
|
-
// user is typed as User (inferred from backend output schema)
|
|
42
|
-
|
|
43
|
-
const users = await api.users.listUsers({ page: 1, limit: 10 });
|
|
44
|
-
// users is typed as User[] (or whatever your output schema defines)
|
|
45
|
-
|
|
46
|
-
const newUser = await api.users.createUser({
|
|
47
|
-
name: 'Alice',
|
|
48
|
-
email: 'alice@example.com'
|
|
49
|
-
});
|
|
50
|
-
// newUser is typed as User
|
|
14
|
+
npm install @veloxts/velox
|
|
51
15
|
```
|
|
52
16
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
```typescript
|
|
56
|
-
const api = createClient<Router>({
|
|
57
|
-
baseUrl: 'https://api.example.com/api',
|
|
58
|
-
|
|
59
|
-
// Add custom headers to all requests
|
|
60
|
-
headers: {
|
|
61
|
-
'Authorization': 'Bearer token123',
|
|
62
|
-
},
|
|
17
|
+
Visit [@veloxts/velox](https://www.npmjs.com/package/@veloxts/velox) for the complete framework documentation.
|
|
63
18
|
|
|
64
|
-
|
|
65
|
-
onRequest: async (url, options) => {
|
|
66
|
-
console.log(`${options.method} ${url}`);
|
|
67
|
-
},
|
|
19
|
+
## Standalone Installation
|
|
68
20
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
console.log(`Response: ${response.status}`);
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
// Error interceptor
|
|
75
|
-
onError: async (error) => {
|
|
76
|
-
console.error('API Error:', error.message);
|
|
77
|
-
// You can track errors, show notifications, etc.
|
|
78
|
-
},
|
|
79
|
-
});
|
|
21
|
+
```bash
|
|
22
|
+
npm install @veloxts/client
|
|
80
23
|
```
|
|
81
24
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
The client provides typed error classes for different error scenarios:
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
import {
|
|
88
|
-
isVeloxClientError,
|
|
89
|
-
isClientValidationError,
|
|
90
|
-
isClientNotFoundError,
|
|
91
|
-
isServerError,
|
|
92
|
-
isNetworkError,
|
|
93
|
-
} from '@veloxts/client';
|
|
25
|
+
## Documentation
|
|
94
26
|
|
|
95
|
-
|
|
96
|
-
const user = await api.users.getUser({ id: 'invalid' });
|
|
97
|
-
} catch (error) {
|
|
98
|
-
if (isClientValidationError(error)) {
|
|
99
|
-
// Handle validation errors (400)
|
|
100
|
-
console.log('Validation failed:', error.fields);
|
|
101
|
-
} else if (isClientNotFoundError(error)) {
|
|
102
|
-
// Handle not found errors (404)
|
|
103
|
-
console.log('Resource not found:', error.resource);
|
|
104
|
-
} else if (isServerError(error)) {
|
|
105
|
-
// Handle server errors (5xx)
|
|
106
|
-
console.log('Server error:', error.statusCode);
|
|
107
|
-
} else if (isNetworkError(error)) {
|
|
108
|
-
// Handle network errors (can't reach server)
|
|
109
|
-
console.log('Network error:', error.message);
|
|
110
|
-
} else if (isVeloxClientError(error)) {
|
|
111
|
-
// Generic client error
|
|
112
|
-
console.log('Error:', error.statusCode, error.message);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
```
|
|
27
|
+
For detailed documentation, usage examples, and API reference, see [GUIDE.md](./GUIDE.md).
|
|
116
28
|
|
|
117
|
-
|
|
29
|
+
## Quick Example
|
|
118
30
|
|
|
119
31
|
```typescript
|
|
120
|
-
// hooks/useApi.ts
|
|
121
32
|
import { createClient } from '@veloxts/client';
|
|
122
|
-
import type {
|
|
123
|
-
|
|
124
|
-
export const api = createClient<AppRouter>({
|
|
125
|
-
baseUrl: import.meta.env.VITE_API_URL || '/api',
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// In your component
|
|
129
|
-
import { api } from './hooks/useApi';
|
|
130
|
-
import { useState, useEffect } from 'react';
|
|
131
|
-
|
|
132
|
-
function UserProfile({ userId }: { userId: string }) {
|
|
133
|
-
const [user, setUser] = useState(null);
|
|
134
|
-
const [loading, setLoading] = useState(true);
|
|
135
|
-
const [error, setError] = useState(null);
|
|
136
|
-
|
|
137
|
-
useEffect(() => {
|
|
138
|
-
api.users.getUser({ id: userId })
|
|
139
|
-
.then(setUser)
|
|
140
|
-
.catch(setError)
|
|
141
|
-
.finally(() => setLoading(false));
|
|
142
|
-
}, [userId]);
|
|
143
|
-
|
|
144
|
-
if (loading) return <div>Loading...</div>;
|
|
145
|
-
if (error) return <div>Error: {error.message}</div>;
|
|
33
|
+
import type { userProcedures } from '../server/procedures';
|
|
146
34
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### Using with React Query (Recommended)
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
155
|
-
import { api } from './hooks/useApi';
|
|
156
|
-
|
|
157
|
-
// Query hook
|
|
158
|
-
function useUser(userId: string) {
|
|
159
|
-
return useQuery({
|
|
160
|
-
queryKey: ['users', userId],
|
|
161
|
-
queryFn: () => api.users.getUser({ id: userId }),
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Mutation hook
|
|
166
|
-
function useCreateUser() {
|
|
167
|
-
const queryClient = useQueryClient();
|
|
168
|
-
|
|
169
|
-
return useMutation({
|
|
170
|
-
mutationFn: (data) => api.users.createUser(data),
|
|
171
|
-
onSuccess: () => {
|
|
172
|
-
// Invalidate and refetch
|
|
173
|
-
queryClient.invalidateQueries({ queryKey: ['users'] });
|
|
174
|
-
},
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// In your component
|
|
179
|
-
function UserList() {
|
|
180
|
-
const { data: users, isLoading, error } = useQuery({
|
|
181
|
-
queryKey: ['users'],
|
|
182
|
-
queryFn: () => api.users.listUsers({}),
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
const createUser = useCreateUser();
|
|
186
|
-
|
|
187
|
-
const handleCreate = () => {
|
|
188
|
-
createUser.mutate({
|
|
189
|
-
name: 'New User',
|
|
190
|
-
email: 'user@example.com'
|
|
191
|
-
});
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
// ... rest of component
|
|
195
|
-
}
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
## Type Inference
|
|
199
|
-
|
|
200
|
-
The client uses TypeScript's type system to infer the complete API shape from your backend procedure definitions:
|
|
201
|
-
|
|
202
|
-
```typescript
|
|
203
|
-
// Backend (procedures.ts)
|
|
204
|
-
export const userProcedures = defineProcedures('users', {
|
|
205
|
-
getUser: procedure()
|
|
206
|
-
.input(z.object({ id: z.string().uuid() }))
|
|
207
|
-
.output(UserSchema)
|
|
208
|
-
.query(async ({ input, ctx }) => {
|
|
209
|
-
return ctx.db.user.findUnique({ where: { id: input.id } });
|
|
210
|
-
}),
|
|
35
|
+
const api = createClient<{ users: typeof userProcedures }>({
|
|
36
|
+
baseUrl: '/api',
|
|
211
37
|
});
|
|
212
38
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
// TypeScript knows:
|
|
217
|
-
// - api.users.getUser expects { id: string }
|
|
218
|
-
// - api.users.getUser returns Promise<User>
|
|
219
|
-
// - Invalid inputs will show compile-time errors
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
## REST Endpoint Mapping
|
|
223
|
-
|
|
224
|
-
The client automatically maps procedure calls to REST endpoints using the same conventions as the server:
|
|
225
|
-
|
|
226
|
-
| Procedure Name | HTTP Method | Path |
|
|
227
|
-
|---------------|-------------|------|
|
|
228
|
-
| `getUser` | GET | `/users/:id` |
|
|
229
|
-
| `listUsers` | GET | `/users` |
|
|
230
|
-
| `createUser` | POST | `/users` |
|
|
231
|
-
| `updateUser` | PUT | `/users/:id` |
|
|
232
|
-
| `deleteUser` | DELETE | `/users/:id` |
|
|
233
|
-
|
|
234
|
-
## Browser and Node.js Support
|
|
235
|
-
|
|
236
|
-
The client uses the native `fetch` API, which is available in:
|
|
237
|
-
- All modern browsers
|
|
238
|
-
- Node.js v20+ (native fetch)
|
|
239
|
-
- Earlier Node.js versions with a polyfill
|
|
240
|
-
|
|
241
|
-
For older Node.js versions, provide a custom fetch implementation:
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
import fetch from 'node-fetch';
|
|
245
|
-
|
|
246
|
-
const api = createClient<Router>({
|
|
247
|
-
baseUrl: 'https://api.example.com',
|
|
248
|
-
fetch: fetch as typeof globalThis.fetch,
|
|
249
|
-
});
|
|
39
|
+
const user = await api.users.getUser({ id: '123' });
|
|
40
|
+
// user is fully typed from backend schema
|
|
250
41
|
```
|
|
251
42
|
|
|
252
|
-
##
|
|
253
|
-
|
|
254
|
-
- [@veloxts/core](/packages/core) - Core framework with error classes
|
|
255
|
-
- [@veloxts/router](/packages/router) - Procedure definitions for backend
|
|
256
|
-
- [@veloxts/validation](/packages/validation) - Schema validation with Zod
|
|
257
|
-
- [create-velox-app](/packages/create) - Project scaffolder
|
|
258
|
-
|
|
259
|
-
## TypeScript Support
|
|
43
|
+
## Learn More
|
|
260
44
|
|
|
261
|
-
|
|
45
|
+
- [Full Documentation](./GUIDE.md)
|
|
46
|
+
- [VeloxTS Framework](https://www.npmjs.com/package/@veloxts/velox)
|
|
47
|
+
- [GitHub Repository](https://github.com/veloxts/velox-ts-framework)
|
|
262
48
|
|
|
263
49
|
## License
|
|
264
50
|
|