@mitralab.io/platform-sdk 1.0.7 → 1.0.9
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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +110 -85
- package/dist/{index.mjs → index.cjs} +188 -201
- package/dist/{index.d.mts → index.d.cts} +37 -513
- package/dist/index.d.ts +37 -513
- package/dist/index.js +167 -228
- package/package.json +45 -17
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file.
|
|
4
|
+
|
|
5
|
+
## Unreleased
|
|
6
|
+
|
|
7
|
+
- Make the SonarCloud job wait for the Quality Gate result.
|
|
8
|
+
- Align the public package metadata and ESM, CommonJS, and TypeScript artifacts.
|
|
9
|
+
- Add package shape checks and public tarball smoke coverage.
|
|
10
|
+
- Correct public imports and required configuration in documentation examples.
|
|
11
|
+
- Add the MIT license.
|
|
12
|
+
|
|
13
|
+
## 1.0.8
|
|
14
|
+
|
|
15
|
+
- Share environment-neutral API contracts through `@mitralab.io/sdk-core`.
|
|
16
|
+
- Preserve the Platform SDK 1.x browser authentication and entity facade.
|
|
17
|
+
- Validate redirects, API errors, sensitive-value redaction, and package consumers.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mitra Platform
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -3,148 +3,173 @@
|
|
|
3
3
|
[](https://sonarcloud.io/summary/new_code?id=mitra-platform-sdk)
|
|
4
4
|
[](https://sonarcloud.io/summary/new_code?id=mitra-platform-sdk)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
JavaScript and TypeScript SDK for browser applications built on the Mitra Platform. Applications generated by Code Studio use this package to authenticate users, access Data Manager entities, execute Server Functions and custom queries, and call integrations.
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
## Modules
|
|
11
|
-
|
|
12
|
-
- **`auth`**: User authentication, registration, and session handling.
|
|
13
|
-
- **`entities`**: Database CRUD operations.
|
|
14
|
-
- **`functions`**: Serverless function execution.
|
|
15
|
-
- **`integration`**: Proxy HTTP requests to external APIs with automatic credential injection.
|
|
16
|
-
- **`queries`**: Execute reusable named queries.
|
|
8
|
+
The browser transport uses standard Web APIs only: `fetch`, `localStorage`, `URL`, and `Proxy`. Shared contracts and API modules come from `@mitralab.io/sdk-core`, without bringing browser authentication into the Core package.
|
|
17
9
|
|
|
18
10
|
## Installation
|
|
19
11
|
|
|
20
12
|
```bash
|
|
21
|
-
npm install
|
|
13
|
+
npm install @mitralab.io/platform-sdk
|
|
22
14
|
```
|
|
23
15
|
|
|
24
|
-
|
|
16
|
+
Node.js 18 or newer is required for development and server-side tooling. The runtime application must provide the browser Web APIs used by the SDK.
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
25
19
|
|
|
26
20
|
```typescript
|
|
27
|
-
import { createClient } from
|
|
21
|
+
import { createClient } from "@mitralab.io/platform-sdk"
|
|
28
22
|
|
|
29
|
-
const mitra = createClient({
|
|
30
|
-
appId:
|
|
31
|
-
apiUrl:
|
|
32
|
-
|
|
23
|
+
export const mitra = createClient({
|
|
24
|
+
appId: import.meta.env.VITE_MITRA_APP_ID,
|
|
25
|
+
apiUrl: import.meta.env.VITE_MITRA_API_URL,
|
|
26
|
+
onError: (error) => console.error(error.status, error.code, error.message),
|
|
27
|
+
})
|
|
33
28
|
|
|
34
|
-
await mitra.init()
|
|
29
|
+
await mitra.init()
|
|
35
30
|
```
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
|
32
|
+
`init()` resolves the application's public Code Studio configuration, including `dataSourceId` and `allowSignup`. Call it during application startup before using entities, custom queries, or sign-up.
|
|
33
|
+
|
|
34
|
+
## Configuration
|
|
35
|
+
|
|
36
|
+
| Field | Required | Description |
|
|
37
|
+
|---|---|---|
|
|
38
|
+
| `appId` | yes | ID of the published Code Studio application. |
|
|
39
|
+
| `apiUrl` | yes | Base URL of the Mitra API gateway. |
|
|
40
|
+
| `onError` | no | Global callback for API errors. |
|
|
41
|
+
|
|
42
|
+
The client derives service endpoints from `apiUrl`: `/iam`, `/data-manager`, `/functions`, `/integration`, and `/code-studio`.
|
|
43
|
+
|
|
44
|
+
## Boundary
|
|
42
45
|
|
|
43
|
-
|
|
46
|
+
The Platform SDK owns:
|
|
44
47
|
|
|
45
|
-
|
|
48
|
+
- browser login, sign-up, logout, and session refresh
|
|
49
|
+
- session persistence in `localStorage`
|
|
50
|
+
- auth-state listeners
|
|
51
|
+
- one retry after a successful token refresh on a `401` response
|
|
52
|
+
- browser HTTP transport and public application initialization
|
|
46
53
|
|
|
47
|
-
|
|
54
|
+
`@mitralab.io/sdk-core` owns the shared entities, custom queries, Functions, integrations, `auth.me`, safe paths, and structural response validation. Server Function code should use `@mitralab.io/functions-sdk` instead of this browser SDK.
|
|
48
55
|
|
|
49
|
-
|
|
56
|
+
## Authentication
|
|
50
57
|
|
|
51
58
|
```typescript
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
console.log(mitra.auth.isAuthenticated, mitra.auth.currentUser);
|
|
64
|
-
|
|
65
|
-
// Listen for auth changes (fires immediately with current state)
|
|
66
|
-
const unsubscribe = mitra.auth.onAuthStateChange((user) => {
|
|
67
|
-
console.log(user ? 'Logged in' : 'Logged out');
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
// Sign out
|
|
71
|
-
mitra.auth.signOut();
|
|
59
|
+
const user = await mitra.auth.signIn({
|
|
60
|
+
email: "user@example.com",
|
|
61
|
+
password: "password123",
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
const unsubscribe = mitra.auth.onAuthStateChange((currentUser) => {
|
|
65
|
+
console.log(currentUser?.email)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
mitra.auth.signOut("/login")
|
|
69
|
+
unsubscribe()
|
|
72
70
|
```
|
|
73
71
|
|
|
74
|
-
|
|
72
|
+
Authentication state is stored under `mitra_auth_{appId}`. When an API request returns `401`, the SDK attempts `refreshSession()` once and repeats the request only when refresh succeeds.
|
|
73
|
+
|
|
74
|
+
## Entities
|
|
75
75
|
|
|
76
76
|
```typescript
|
|
77
|
-
|
|
77
|
+
type Task = {
|
|
78
|
+
id: string
|
|
79
|
+
title: string
|
|
80
|
+
status: "pending" | "done"
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const tasks = await mitra.entities.getTable<Task>("Task").list({
|
|
84
|
+
sort: "-created_at",
|
|
85
|
+
limit: 10,
|
|
86
|
+
fields: ["id", "title", "status"],
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
const pending = await mitra.entities.Task.filter({ status: "pending" })
|
|
90
|
+
const created = await mitra.entities.Task.create({ title: "New task" })
|
|
91
|
+
await mitra.entities.Task.update(created.id, { status: "done" })
|
|
92
|
+
await mitra.entities.Task.delete(created.id)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Table names are case-sensitive and must match the Data Manager table name. Record operations use `/api/v1/tables/{table}/records`. Application and tenant scope come from the authenticated context, not from a data source in the path.
|
|
78
96
|
|
|
79
|
-
|
|
80
|
-
title: 'New task',
|
|
81
|
-
status: 'pending',
|
|
82
|
-
});
|
|
97
|
+
## Server Functions
|
|
83
98
|
|
|
84
|
-
|
|
99
|
+
```typescript
|
|
100
|
+
const execution = await mitra.functions.execute("function-id", {
|
|
101
|
+
orderId: "order-123",
|
|
102
|
+
})
|
|
85
103
|
|
|
86
|
-
|
|
104
|
+
console.log(execution.id, execution.status)
|
|
87
105
|
```
|
|
88
106
|
|
|
89
|
-
|
|
107
|
+
The Platform SDK 1.x `execute` method keeps the existing asynchronous API behavior. It does not send `X-Invocation-Type`, so the Functions service applies its default and returns the created execution, normally with `PENDING` status.
|
|
108
|
+
|
|
109
|
+
## Custom queries
|
|
90
110
|
|
|
91
111
|
```typescript
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
});
|
|
112
|
+
const result = await mitra.queries.execute("query-id", {
|
|
113
|
+
status: "active",
|
|
114
|
+
})
|
|
96
115
|
|
|
97
|
-
console.log(
|
|
116
|
+
console.log(result.rows, result.affectedRows)
|
|
98
117
|
```
|
|
99
118
|
|
|
100
|
-
|
|
119
|
+
## Integrations
|
|
120
|
+
|
|
121
|
+
Execute a predefined resource:
|
|
101
122
|
|
|
102
123
|
```typescript
|
|
103
|
-
const result = await mitra.
|
|
104
|
-
|
|
124
|
+
const result = await mitra.integration.executeResource("resource-id", {
|
|
125
|
+
description: "Notebook",
|
|
126
|
+
limit: 10,
|
|
127
|
+
})
|
|
105
128
|
```
|
|
106
129
|
|
|
107
|
-
|
|
130
|
+
Or execute an integration config directly:
|
|
108
131
|
|
|
109
132
|
```typescript
|
|
110
|
-
const result = await mitra.integration.execute(
|
|
111
|
-
method:
|
|
112
|
-
endpoint:
|
|
113
|
-
}
|
|
114
|
-
|
|
133
|
+
const result = await mitra.integration.execute("config-id", {
|
|
134
|
+
method: "GET",
|
|
135
|
+
endpoint: "/users",
|
|
136
|
+
queryParams: { limit: "10" },
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
console.log(result.status, result.body)
|
|
115
140
|
```
|
|
116
141
|
|
|
117
|
-
|
|
142
|
+
Integration credentials are injected by the Integration service. Do not pass provider credentials through browser input.
|
|
143
|
+
|
|
144
|
+
## Errors and request behavior
|
|
118
145
|
|
|
119
|
-
|
|
146
|
+
API failures throw `MitraApiError`:
|
|
120
147
|
|
|
121
148
|
```typescript
|
|
122
|
-
import { MitraApiError } from
|
|
149
|
+
import { MitraApiError } from "@mitralab.io/platform-sdk"
|
|
123
150
|
|
|
124
151
|
try {
|
|
125
|
-
await mitra.entities.Task.get(
|
|
152
|
+
await mitra.entities.Task.get("missing-id")
|
|
126
153
|
} catch (error) {
|
|
127
154
|
if (error instanceof MitraApiError) {
|
|
128
|
-
console.error(error.status, error.code, error.message)
|
|
155
|
+
console.error(error.status, error.code, error.message)
|
|
129
156
|
}
|
|
130
157
|
}
|
|
131
158
|
```
|
|
132
159
|
|
|
133
|
-
|
|
160
|
+
The transport refuses HTTP redirects. Statuses `307` and `308`, opaque redirects, and responses already marked as redirected fail without replay. The only automatic replay is the single request attempted after a successful session refresh on `401`.
|
|
161
|
+
|
|
162
|
+
Before constructing `MitraApiError`, the SDK recursively redacts the token used by the request and credentials in `Bearer` format from the error message, code, details, arrays, values, and object keys.
|
|
134
163
|
|
|
135
|
-
|
|
164
|
+
## Development
|
|
136
165
|
|
|
137
166
|
```bash
|
|
138
167
|
npm install
|
|
139
|
-
npm run
|
|
168
|
+
npm run check
|
|
140
169
|
```
|
|
141
170
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
```bash
|
|
145
|
-
npm test
|
|
146
|
-
```
|
|
171
|
+
`@mitralab.io/sdk-core@0.1.0` is resolved from the public npm registry and locked by integrity in `package-lock.json`. Do not replace it with a `file:` dependency or a local tarball.
|
|
147
172
|
|
|
148
|
-
|
|
173
|
+
The build produces ESM, CommonJS, `.d.ts`, and `.d.cts` artifacts. Package checks inspect the public tarball with Are The Types Wrong, install it into an isolated consumer, and validate ESM, CommonJS, and TypeScript resolution.
|
|
149
174
|
|
|
150
|
-
|
|
175
|
+
See [CHANGELOG.md](CHANGELOG.md) for release history and [LICENSE](LICENSE) for license terms.
|