@mitralab.io/platform-sdk 1.0.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 +147 -0
- package/dist/index.d.mts +1140 -0
- package/dist/index.d.ts +1140 -0
- package/dist/index.js +763 -0
- package/dist/index.mjs +735 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# Mitra Platform SDK
|
|
2
|
+
|
|
3
|
+
The Mitra Platform SDK provides a JavaScript/TypeScript interface for building apps on the Mitra Platform. When Mitra generates your app, the generated code uses this SDK to authenticate users, manage your app's data, execute serverless functions, and more. You can use the same SDK to modify and extend your app.
|
|
4
|
+
|
|
5
|
+
**Zero runtime dependencies.** Uses only standard Web APIs (`fetch`, `localStorage`, `URL`, `Proxy`).
|
|
6
|
+
|
|
7
|
+
## Modules
|
|
8
|
+
|
|
9
|
+
- **`auth`**: User authentication, registration, and session handling.
|
|
10
|
+
- **`entities`**: Database CRUD operations.
|
|
11
|
+
- **`functions`**: Serverless function execution.
|
|
12
|
+
- **`integration`**: Proxy HTTP requests to external APIs with automatic credential injection.
|
|
13
|
+
- **`queries`**: Execute reusable named queries.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install mitra-platform-sdk
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```typescript
|
|
24
|
+
import { createClient } from 'mitra-platform-sdk';
|
|
25
|
+
|
|
26
|
+
const mitra = createClient({
|
|
27
|
+
appId: 'your-app-id',
|
|
28
|
+
apiUrl: 'https://api.mitra.io',
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
await mitra.init();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
| Parameter | Type | Required | Description |
|
|
35
|
+
|-----------|------|----------|-------------|
|
|
36
|
+
| `appId` | `string` | Yes | Your app's unique identifier |
|
|
37
|
+
| `apiUrl` | `string` | Yes | Base URL of the Mitra API |
|
|
38
|
+
| `onError` | `(error) => void` | No | Global error handler for all API requests |
|
|
39
|
+
|
|
40
|
+
`init()` must be called before using the client. Safe to call multiple times.
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
All modules and methods are fully typed — explore the full API through your editor's autocomplete or read the JSDoc in the source code.
|
|
45
|
+
|
|
46
|
+
### Authentication
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Sign up (auto-signs in after registration)
|
|
50
|
+
const user = await mitra.auth.signUp({
|
|
51
|
+
email: 'user@example.com',
|
|
52
|
+
password: 'password123',
|
|
53
|
+
name: 'Jane Doe',
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Sign in
|
|
57
|
+
await mitra.auth.signIn({ email: 'user@example.com', password: 'password123' });
|
|
58
|
+
|
|
59
|
+
// Check state
|
|
60
|
+
console.log(mitra.auth.isAuthenticated, mitra.auth.currentUser);
|
|
61
|
+
|
|
62
|
+
// Listen for auth changes (fires immediately with current state)
|
|
63
|
+
const unsubscribe = mitra.auth.onAuthStateChange((user) => {
|
|
64
|
+
console.log(user ? 'Logged in' : 'Logged out');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// Sign out
|
|
68
|
+
mitra.auth.signOut();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Entities
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
const tasks = await mitra.entities.Task.list('-created_at', 10);
|
|
75
|
+
|
|
76
|
+
const task = await mitra.entities.Task.create({
|
|
77
|
+
title: 'New task',
|
|
78
|
+
status: 'pending',
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
await mitra.entities.Task.update(task.id, { status: 'done' });
|
|
82
|
+
|
|
83
|
+
await mitra.entities.Task.delete(task.id);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Functions
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
const execution = await mitra.functions.execute('function-id', {
|
|
90
|
+
to: 'user@example.com',
|
|
91
|
+
subject: 'Welcome',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
console.log(execution.status, execution.output);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### Queries
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
const result = await mitra.queries.execute('query-id', { status: 'active' });
|
|
101
|
+
console.log(result.rows);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Integration
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
const result = await mitra.integration.execute('config-id', {
|
|
108
|
+
method: 'GET',
|
|
109
|
+
endpoint: '/users',
|
|
110
|
+
});
|
|
111
|
+
console.log(result.status, result.body);
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Error Handling
|
|
115
|
+
|
|
116
|
+
All API errors throw `MitraApiError`:
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { MitraApiError } from 'mitra-platform-sdk';
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
await mitra.entities.Task.get('non-existent-id');
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (error instanceof MitraApiError) {
|
|
125
|
+
console.error(error.status, error.code, error.message);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Development
|
|
131
|
+
|
|
132
|
+
### Build the SDK
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npm install
|
|
136
|
+
npm run build
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Run tests
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
npm test
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|