@urbackend/sdk 0.2.0 → 0.2.1
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 +67 -44
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# urbackend-sdk
|
|
2
2
|
|
|
3
|
-
Official TypeScript SDK for [urBackend](https://urbackend.bitbros.in) — the instant Backend-as-a-Service for
|
|
3
|
+
Official TypeScript SDK for [urBackend](https://urbackend.bitbros.in) — the instant Backend-as-a-Service for MongoDB.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
|
+
```bash
|
|
6
7
|
npm install @urbackend/sdk
|
|
8
|
+
```
|
|
7
9
|
|
|
8
10
|
## Quick Start
|
|
9
11
|
```javascript
|
|
@@ -12,49 +14,80 @@ import urBackend from '@urbackend/sdk';
|
|
|
12
14
|
const client = urBackend({ apiKey: 'YOUR_API_KEY' });
|
|
13
15
|
|
|
14
16
|
// Auth
|
|
15
|
-
await client.auth.
|
|
16
|
-
await client.auth.
|
|
17
|
-
await client.auth.me();
|
|
17
|
+
const { accessToken } = await client.auth.login({ email, password });
|
|
18
|
+
const user = await client.auth.me();
|
|
18
19
|
|
|
19
|
-
// Database — collections are
|
|
20
|
-
await client.db.insert('
|
|
21
|
-
await client.db.getAll('
|
|
22
|
-
await client.db.getOne('products', id);
|
|
23
|
-
await client.db.update('products', id, { price: 79 });
|
|
24
|
-
await client.db.delete('products', id);
|
|
20
|
+
// Database — collections are managed via the urBackend Dashboard
|
|
21
|
+
await client.db.insert('posts', { title: 'Hello World' }, client.auth.getToken());
|
|
22
|
+
await client.db.getAll('posts', { sort: 'createdAt:desc', limit: 10 });
|
|
25
23
|
|
|
26
24
|
// Storage
|
|
27
|
-
await client.storage.upload(file);
|
|
28
|
-
|
|
25
|
+
const { url } = await client.storage.upload(file);
|
|
26
|
+
|
|
27
|
+
// Mail (Requires Secret Key)
|
|
28
|
+
await client.mail.send({
|
|
29
|
+
to: 'user@example.com',
|
|
30
|
+
subject: 'Welcome!',
|
|
31
|
+
text: 'Glad to have you.'
|
|
32
|
+
});
|
|
29
33
|
```
|
|
30
34
|
|
|
31
35
|
## API Reference
|
|
32
36
|
|
|
33
|
-
### Client
|
|
37
|
+
### Client Initialization
|
|
34
38
|
`urBackend({ apiKey: string, baseUrl?: string })`
|
|
35
39
|
|
|
36
|
-
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
### Auth (`client.auth`)
|
|
37
43
|
| Method | Params | Returns |
|
|
38
44
|
|--------|--------|---------|
|
|
39
|
-
| signUp | { email, password, name
|
|
40
|
-
| login
|
|
41
|
-
| me
|
|
42
|
-
| logout |
|
|
45
|
+
| `signUp` | `{ email, password, username?, name?, ... }` | `AuthUser` |
|
|
46
|
+
| `login` | `{ email, password }` | `AuthResponse` |
|
|
47
|
+
| `me` | `token?` | `AuthUser` |
|
|
48
|
+
| `logout` | `token?` | `{ success: boolean }` |
|
|
49
|
+
| `refreshToken` | `refreshToken?` | `AuthResponse` |
|
|
50
|
+
| `updateProfile`| `payload, token?` | `{ message: string }` |
|
|
51
|
+
| `changePassword`| `payload, token?` | `{ message: string }` |
|
|
52
|
+
| `verifyEmail` | `{ email, otp }` | `{ message: string }` |
|
|
53
|
+
| `publicProfile`| `username` | `AuthUser` |
|
|
54
|
+
| `socialStart` | `provider ('github'\|'google')` | `string` (Redirect URL) |
|
|
55
|
+
| `socialExchange`| `{ token, rtCode }` | `SocialExchangeResponse` |
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
### Database (`client.db`)
|
|
60
|
+
Support for **Row-Level Security (RLS)** is built-in. Pass the user's `accessToken` as the final parameter to write routes if RLS is enabled for the collection.
|
|
43
61
|
|
|
44
|
-
### Database
|
|
45
62
|
| Method | Params | Returns |
|
|
46
63
|
|--------|--------|---------|
|
|
47
|
-
| getAll<T
|
|
48
|
-
| getOne<T
|
|
49
|
-
| insert<T
|
|
50
|
-
| update<T
|
|
51
|
-
|
|
|
52
|
-
|
|
53
|
-
|
|
64
|
+
| `getAll<T>` | `collection, params?` | `T[]` |
|
|
65
|
+
| `getOne<T>` | `collection, id, options?` | `T` |
|
|
66
|
+
| `insert<T>` | `collection, data, token?` | `T` |
|
|
67
|
+
| `update<T>` | `collection, id, data, token?` | `T` (Full Replace) |
|
|
68
|
+
| `patch<T>` | `collection, id, data, token?` | `T` (Partial Update) |
|
|
69
|
+
| `delete` | `collection, id, token?` | `{ deleted: boolean }` |
|
|
70
|
+
|
|
71
|
+
**Query Parameters (`params`):**
|
|
72
|
+
- `filter`: `{ price_gt: 100 }`
|
|
73
|
+
- `sort`: `"createdAt:desc"`
|
|
74
|
+
- `limit`: `50`
|
|
75
|
+
- `page`: `1`
|
|
76
|
+
- `populate`: `"author"` (Expand Reference fields)
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
### Storage (`client.storage`)
|
|
54
81
|
| Method | Params | Returns |
|
|
55
82
|
|--------|--------|---------|
|
|
56
|
-
| upload | file, filename
|
|
57
|
-
| deleteFile | path | { deleted: boolean } |
|
|
83
|
+
| `upload` | `file (Buffer\|Blob), filename?` | `{ url, path, provider }` |
|
|
84
|
+
| `deleteFile` | `path` | `{ deleted: boolean }` |
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
### New Modules
|
|
89
|
+
- **`client.schema`**: Use `getSchema(collection)` to fetch visual field definitions.
|
|
90
|
+
- **`client.mail`**: Use `send(payload)` to send emails via Resend (Requires Secret Key).
|
|
58
91
|
|
|
59
92
|
## Error Handling
|
|
60
93
|
```javascript
|
|
@@ -63,23 +96,13 @@ import { AuthError, NotFoundError, RateLimitError } from '@urbackend/sdk';
|
|
|
63
96
|
try {
|
|
64
97
|
await client.db.getOne('products', id);
|
|
65
98
|
} catch (e) {
|
|
66
|
-
if (e instanceof
|
|
67
|
-
if (e instanceof
|
|
99
|
+
if (e instanceof AuthError) console.error('Invalid token or insufficient RLS permissions');
|
|
100
|
+
if (e instanceof NotFoundError) console.error('Resource not found');
|
|
101
|
+
if (e instanceof RateLimitError) console.error('Too many requests');
|
|
68
102
|
}
|
|
69
103
|
```
|
|
70
104
|
|
|
71
|
-
## TypeScript Support
|
|
72
|
-
```typescript
|
|
73
|
-
interface Product { _id: string; name: string; price: number; }
|
|
74
|
-
const products = await client.db.getAll<Product>('products');
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
## Limits
|
|
78
|
-
- Rate limit: 100 requests / 15 mins per IP
|
|
79
|
-
- Database: 50 MB per project
|
|
80
|
-
- Storage: 100 MB per project
|
|
81
|
-
- File upload: 5 MB per file
|
|
82
|
-
|
|
83
105
|
## Security
|
|
84
|
-
|
|
85
|
-
Use
|
|
106
|
+
- **Never expose your Secret Key (`sk_live_...`)** in frontend/browser code.
|
|
107
|
+
- Use **Publishable Keys (`pk_live_...`)** for client-side applications.
|
|
108
|
+
- Enable **RLS** in the urBackend Dashboard to allow secure writes from the frontend using the user's access token.
|