@vaiftech/client 1.0.1 → 1.0.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 +257 -8
- package/dist/index.d.mts +626 -1
- package/dist/index.d.ts +626 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,9 +23,34 @@ const vaif = createVaifClient({
|
|
|
23
23
|
});
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
##
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
## Modules
|
|
27
|
+
|
|
28
|
+
The client provides access to the following modules:
|
|
29
|
+
|
|
30
|
+
| Module | Description |
|
|
31
|
+
|--------|-------------|
|
|
32
|
+
| `vaif.auth` | Authentication (email, OAuth, MFA, magic link) |
|
|
33
|
+
| `vaif.from()` | Database operations with query builder |
|
|
34
|
+
| `vaif.mongodb` | MongoDB document database |
|
|
35
|
+
| `vaif.realtime()` | Real-time subscriptions and presence |
|
|
36
|
+
| `vaif.storage` | File storage and CDN |
|
|
37
|
+
| `vaif.functions` | Edge functions |
|
|
38
|
+
| `vaif.projects` | Project management |
|
|
39
|
+
| `vaif.orgs` | Organization management |
|
|
40
|
+
| `vaif.schema` | Database schema migrations |
|
|
41
|
+
| `vaif.secrets` | Secret management |
|
|
42
|
+
| `vaif.deployments` | Deployment management |
|
|
43
|
+
| `vaif.integrations` | Webhooks and events |
|
|
44
|
+
| `vaif.billing` | Billing and subscriptions |
|
|
45
|
+
| `vaif.flags` | Feature flags |
|
|
46
|
+
| `vaif.security` | Security and environment variables |
|
|
47
|
+
| `vaif.ai` | AI-powered code generation |
|
|
48
|
+
| `vaif.templates` | Project templates |
|
|
49
|
+
| `vaif.oauth` | OAuth provider configuration |
|
|
50
|
+
| `vaif.docs` | Documentation management |
|
|
51
|
+
| `vaif.admin` | Admin operations |
|
|
52
|
+
|
|
53
|
+
## Authentication
|
|
29
54
|
|
|
30
55
|
```typescript
|
|
31
56
|
// Sign up
|
|
@@ -46,6 +71,13 @@ const { url } = await vaif.auth.signInWithOAuth({
|
|
|
46
71
|
redirectTo: 'https://myapp.com/callback',
|
|
47
72
|
});
|
|
48
73
|
|
|
74
|
+
// Magic Link
|
|
75
|
+
await vaif.auth.requestMagicLink({ email: 'user@example.com' });
|
|
76
|
+
|
|
77
|
+
// MFA
|
|
78
|
+
const { qrCode, secret } = await vaif.auth.setupMFA({ method: 'totp' });
|
|
79
|
+
await vaif.auth.verifyMFA({ code: '123456' });
|
|
80
|
+
|
|
49
81
|
// Get current user
|
|
50
82
|
const user = await vaif.auth.getUser();
|
|
51
83
|
|
|
@@ -53,7 +85,31 @@ const user = await vaif.auth.getUser();
|
|
|
53
85
|
await vaif.auth.signOut();
|
|
54
86
|
```
|
|
55
87
|
|
|
56
|
-
###
|
|
88
|
+
### Standalone Auth Client
|
|
89
|
+
|
|
90
|
+
For auth-only applications, use the standalone auth package:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { createAuthClient } from '@vaiftech/client';
|
|
94
|
+
// or import directly: import { createAuthClient } from '@vaiftech/auth';
|
|
95
|
+
|
|
96
|
+
const auth = createAuthClient({
|
|
97
|
+
url: 'https://api.vaif.studio',
|
|
98
|
+
apiKey: 'your-project-key',
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Full auth functionality
|
|
102
|
+
await auth.signInWithPassword({ email, password });
|
|
103
|
+
await auth.signInWithOAuth({ provider: 'google' });
|
|
104
|
+
await auth.signInWithMagicLink({ email });
|
|
105
|
+
|
|
106
|
+
// Session management
|
|
107
|
+
auth.onAuthStateChange((event) => {
|
|
108
|
+
console.log('Auth event:', event.event);
|
|
109
|
+
});
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Database Operations
|
|
57
113
|
|
|
58
114
|
```typescript
|
|
59
115
|
// Define your types
|
|
@@ -93,12 +149,126 @@ await vaif.from<User>('users')
|
|
|
93
149
|
.eq('id', 'user-123')
|
|
94
150
|
.delete();
|
|
95
151
|
|
|
152
|
+
// Batch operations
|
|
153
|
+
await vaif.from<User>('users').batchCreate([
|
|
154
|
+
{ email: 'user1@example.com', name: 'User 1' },
|
|
155
|
+
{ email: 'user2@example.com', name: 'User 2' },
|
|
156
|
+
]);
|
|
157
|
+
|
|
96
158
|
// Pagination
|
|
97
159
|
const { data, total, page, pageSize } = await vaif.from<User>('users')
|
|
98
160
|
.paginate({ page: 1, pageSize: 20 });
|
|
161
|
+
|
|
162
|
+
// Aggregations
|
|
163
|
+
const stats = await vaif.from<User>('users')
|
|
164
|
+
.aggregate({ count: '*', avg: 'age' });
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## MongoDB Operations
|
|
168
|
+
|
|
169
|
+
Full MongoDB support for document-based data:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
// Get a collection reference
|
|
173
|
+
const users = vaif.mongodb.collection<User>('users');
|
|
174
|
+
|
|
175
|
+
// Find documents
|
|
176
|
+
const activeUsers = await users.find({
|
|
177
|
+
filter: { status: 'active' },
|
|
178
|
+
sort: { createdAt: -1 },
|
|
179
|
+
limit: 10,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Find one document
|
|
183
|
+
const user = await users.findOne({ email: 'user@example.com' });
|
|
184
|
+
|
|
185
|
+
// Find by ID
|
|
186
|
+
const userById = await users.findById('507f1f77bcf86cd799439011');
|
|
187
|
+
|
|
188
|
+
// Insert documents
|
|
189
|
+
const newUser = await users.insertOne({
|
|
190
|
+
email: 'new@example.com',
|
|
191
|
+
name: 'New User',
|
|
192
|
+
createdAt: new Date(),
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const manyUsers = await users.insertMany([
|
|
196
|
+
{ email: 'user1@example.com', name: 'User 1' },
|
|
197
|
+
{ email: 'user2@example.com', name: 'User 2' },
|
|
198
|
+
]);
|
|
199
|
+
|
|
200
|
+
// Update documents
|
|
201
|
+
await users.updateOne(
|
|
202
|
+
{ email: 'user@example.com' },
|
|
203
|
+
{ $set: { name: 'Updated Name' } }
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
await users.updateMany(
|
|
207
|
+
{ status: 'pending' },
|
|
208
|
+
{ $set: { status: 'active' } }
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
// Atomic operations
|
|
212
|
+
const updated = await users.findOneAndUpdate(
|
|
213
|
+
{ email: 'user@example.com' },
|
|
214
|
+
{ $inc: { loginCount: 1 } },
|
|
215
|
+
{ returnDocument: 'after' }
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
// Delete documents
|
|
219
|
+
await users.deleteOne({ email: 'old@example.com' });
|
|
220
|
+
await users.deleteMany({ status: 'inactive' });
|
|
221
|
+
|
|
222
|
+
// Aggregation pipeline
|
|
223
|
+
const stats = await users.aggregate([
|
|
224
|
+
{ $match: { status: 'active' } },
|
|
225
|
+
{ $group: { _id: '$country', count: { $sum: 1 } } },
|
|
226
|
+
{ $sort: { count: -1 } },
|
|
227
|
+
]);
|
|
228
|
+
|
|
229
|
+
// Count and distinct
|
|
230
|
+
const totalUsers = await users.count({ status: 'active' });
|
|
231
|
+
const countries = await users.distinct('country', { status: 'active' });
|
|
232
|
+
|
|
233
|
+
// Index management
|
|
234
|
+
const indexes = await users.listIndexes();
|
|
235
|
+
await users.createIndex({ email: 1 }, { unique: true });
|
|
236
|
+
await users.dropIndex('old_index');
|
|
237
|
+
|
|
238
|
+
// Bulk operations
|
|
239
|
+
await users.bulkWrite([
|
|
240
|
+
{ insertOne: { document: { email: 'bulk@example.com' } } },
|
|
241
|
+
{ updateOne: { filter: { email: 'old@example.com' }, update: { $set: { migrated: true } } } },
|
|
242
|
+
{ deleteOne: { filter: { email: 'remove@example.com' } } },
|
|
243
|
+
]);
|
|
244
|
+
|
|
245
|
+
// Collection management
|
|
246
|
+
const collections = await vaif.mongodb.listCollections();
|
|
247
|
+
await vaif.mongodb.createCollection('logs');
|
|
99
248
|
```
|
|
100
249
|
|
|
101
|
-
###
|
|
250
|
+
### Cursor Support
|
|
251
|
+
|
|
252
|
+
For large datasets, use cursors to paginate through results:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
const cursor = await users.findWithCursor({
|
|
256
|
+
filter: { status: 'active' },
|
|
257
|
+
batchSize: 100,
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
let batch = await cursor.next();
|
|
261
|
+
while (batch.length > 0) {
|
|
262
|
+
for (const user of batch) {
|
|
263
|
+
console.log(user.email);
|
|
264
|
+
}
|
|
265
|
+
batch = await cursor.next();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
await cursor.close();
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## Realtime Subscriptions
|
|
102
272
|
|
|
103
273
|
```typescript
|
|
104
274
|
const realtime = vaif.realtime();
|
|
@@ -132,7 +302,7 @@ realtime.unsubscribe({ table: 'messages' });
|
|
|
132
302
|
await realtime.disconnect();
|
|
133
303
|
```
|
|
134
304
|
|
|
135
|
-
|
|
305
|
+
## Storage
|
|
136
306
|
|
|
137
307
|
```typescript
|
|
138
308
|
// Upload file
|
|
@@ -157,9 +327,15 @@ const { files } = await vaif.storage.list({ prefix: 'avatars/' });
|
|
|
157
327
|
|
|
158
328
|
// Delete file
|
|
159
329
|
await vaif.storage.delete('avatars/old-avatar.jpg');
|
|
330
|
+
|
|
331
|
+
// Multipart uploads for large files
|
|
332
|
+
const upload = await vaif.storage.createMultipartUpload('large-file.zip');
|
|
333
|
+
await vaif.storage.uploadPart(upload.uploadId, 1, chunk1);
|
|
334
|
+
await vaif.storage.uploadPart(upload.uploadId, 2, chunk2);
|
|
335
|
+
await vaif.storage.completeMultipartUpload(upload.uploadId, parts);
|
|
160
336
|
```
|
|
161
337
|
|
|
162
|
-
|
|
338
|
+
## Edge Functions
|
|
163
339
|
|
|
164
340
|
```typescript
|
|
165
341
|
// Invoke a function
|
|
@@ -169,13 +345,57 @@ const result = await vaif.functions.invoke('send-email', {
|
|
|
169
345
|
body: 'Welcome to VAIF!',
|
|
170
346
|
});
|
|
171
347
|
|
|
348
|
+
// Invoke with options
|
|
349
|
+
const result = await vaif.functions.invoke('process-data', payload, {
|
|
350
|
+
timeout: 30000,
|
|
351
|
+
retry: { attempts: 3, backoff: 'exponential' },
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
// Batch invoke
|
|
355
|
+
const results = await vaif.functions.batchInvoke([
|
|
356
|
+
{ name: 'func1', payload: { data: 1 } },
|
|
357
|
+
{ name: 'func2', payload: { data: 2 } },
|
|
358
|
+
]);
|
|
359
|
+
|
|
172
360
|
// List functions
|
|
173
361
|
const functions = await vaif.functions.list({ projectId: 'proj-123' });
|
|
362
|
+
|
|
363
|
+
// Deploy a function
|
|
364
|
+
await vaif.functions.deploy('my-function', {
|
|
365
|
+
code: functionCode,
|
|
366
|
+
runtime: 'node20',
|
|
367
|
+
});
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
## Integrations (Webhooks & Events)
|
|
371
|
+
|
|
372
|
+
```typescript
|
|
373
|
+
// Create a webhook subscription
|
|
374
|
+
const subscription = await vaif.integrations.createSubscription({
|
|
375
|
+
type: 'webhook',
|
|
376
|
+
url: 'https://myapp.com/webhooks',
|
|
377
|
+
events: ['user.created', 'user.updated'],
|
|
378
|
+
secret: 'whsec_xxx',
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
// Publish custom events
|
|
382
|
+
await vaif.integrations.publish({
|
|
383
|
+
name: 'order.completed',
|
|
384
|
+
data: { orderId: '123', amount: 99.99 },
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// List webhook deliveries
|
|
388
|
+
const deliveries = await vaif.integrations.listDeliveries({
|
|
389
|
+
subscriptionId: 'sub-123',
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// Retry failed delivery
|
|
393
|
+
await vaif.integrations.retryDelivery('delivery-123');
|
|
174
394
|
```
|
|
175
395
|
|
|
176
396
|
## TypeScript Support
|
|
177
397
|
|
|
178
|
-
The SDK is fully typed. Use generics for type-safe
|
|
398
|
+
The SDK is fully typed. Use generics for type-safe operations:
|
|
179
399
|
|
|
180
400
|
```typescript
|
|
181
401
|
interface Post {
|
|
@@ -189,10 +409,39 @@ interface Post {
|
|
|
189
409
|
// Full type inference
|
|
190
410
|
const posts = await vaif.from<Post>('posts').list();
|
|
191
411
|
// posts is Post[]
|
|
412
|
+
|
|
413
|
+
// MongoDB with types
|
|
414
|
+
const users = vaif.mongodb.collection<User>('users');
|
|
415
|
+
const user = await users.findOne({ email: 'test@example.com' });
|
|
416
|
+
// user is User | null
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## Error Handling
|
|
420
|
+
|
|
421
|
+
```typescript
|
|
422
|
+
import {
|
|
423
|
+
VaifError,
|
|
424
|
+
VaifAuthError,
|
|
425
|
+
VaifNotFoundError,
|
|
426
|
+
isVaifError
|
|
427
|
+
} from '@vaiftech/client';
|
|
428
|
+
|
|
429
|
+
try {
|
|
430
|
+
await vaif.from('users').eq('id', 'invalid').single();
|
|
431
|
+
} catch (error) {
|
|
432
|
+
if (error instanceof VaifNotFoundError) {
|
|
433
|
+
console.log('User not found');
|
|
434
|
+
} else if (error instanceof VaifAuthError) {
|
|
435
|
+
console.log('Authentication failed');
|
|
436
|
+
} else if (isVaifError(error)) {
|
|
437
|
+
console.log('VAIF error:', error.message);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
192
440
|
```
|
|
193
441
|
|
|
194
442
|
## Related Packages
|
|
195
443
|
|
|
444
|
+
- [@vaiftech/auth](https://www.npmjs.com/package/@vaiftech/auth) - Standalone authentication client
|
|
196
445
|
- [@vaiftech/react](https://www.npmjs.com/package/@vaiftech/react) - React hooks
|
|
197
446
|
- [@vaiftech/sdk-expo](https://www.npmjs.com/package/@vaiftech/sdk-expo) - React Native/Expo SDK
|
|
198
447
|
- [@vaiftech/cli](https://www.npmjs.com/package/@vaiftech/cli) - CLI tools
|