swiftbase-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/LICENSE +19 -0
- package/README.md +215 -0
- package/dist/index.cjs +4778 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +296 -0
- package/dist/index.d.ts +296 -0
- package/dist/index.js +4744 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 Swiftbase
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# Swiftbase TypeScript SDK
|
|
2
|
+
|
|
3
|
+
TypeScript SDK for integrating customer applications with the Swiftbase platform. Supports SDK initialization, authentication, user profiles, identity management, database queries (with real-time WebSocket subscriptions), and S3-compatible cloud storage.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package via npm:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install swiftbase-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Getting Started
|
|
16
|
+
|
|
17
|
+
### Initialize the SDK
|
|
18
|
+
|
|
19
|
+
Initialize the SDK at the entry point of your application:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { initializeSdk } from "swiftbase-sdk";
|
|
23
|
+
|
|
24
|
+
initializeSdk("your-project-id");
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Authentication
|
|
30
|
+
|
|
31
|
+
The SDK provides several authentication mechanisms, including credentials, redirect-based OAuth2/OIDC, and service logins.
|
|
32
|
+
|
|
33
|
+
### User Login (Email / Password)
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { loginWithPassword } from "swiftbase-sdk";
|
|
37
|
+
|
|
38
|
+
const userProfile = await loginWithPassword({
|
|
39
|
+
projectId: "your-project-id",
|
|
40
|
+
email: "user@example.com",
|
|
41
|
+
password: "password123",
|
|
42
|
+
});
|
|
43
|
+
console.log(`Welcome back, ${userProfile.firstName}!`);
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Password-less / Redirect OIDC Login (Browser Environment)
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { loginWithRedirect, handleRedirectCallback } from "swiftbase-sdk";
|
|
50
|
+
|
|
51
|
+
// Trigger OIDC redirect flow
|
|
52
|
+
await loginWithRedirect({
|
|
53
|
+
redirectUri: window.location.origin + "/callback",
|
|
54
|
+
state: "secure-random-state",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// On the callback route:
|
|
58
|
+
const profile = await handleRedirectCallback();
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Service Login (Client Credentials)
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { loginService } from "swiftbase-sdk";
|
|
65
|
+
|
|
66
|
+
const tokenResponse = await loginService(
|
|
67
|
+
"your-project-id",
|
|
68
|
+
"your-service-id",
|
|
69
|
+
"your-service-secret",
|
|
70
|
+
);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Manage Session
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { isLoggedIn, logout, getAccessToken } from "swiftbase-sdk";
|
|
77
|
+
|
|
78
|
+
if (isLoggedIn()) {
|
|
79
|
+
const token = await getAccessToken();
|
|
80
|
+
console.log("Logged in token:", token);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
await logout();
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## User Profiles
|
|
89
|
+
|
|
90
|
+
Retrieve and manage user profile details:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { getProfile, updateProfile, changePassword } from "swiftbase-sdk";
|
|
94
|
+
|
|
95
|
+
// Get current profile
|
|
96
|
+
const profile = await getProfile();
|
|
97
|
+
|
|
98
|
+
// Update profile details
|
|
99
|
+
const updatedProfile = await updateProfile({
|
|
100
|
+
firstName: "Jane",
|
|
101
|
+
lastName: "Doe",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Change Password
|
|
105
|
+
await changePassword({
|
|
106
|
+
currentPassword: "old-password",
|
|
107
|
+
newPassword: "new-password",
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## Database (Real-time & REST Queries)
|
|
114
|
+
|
|
115
|
+
Query your Swiftbase databases using a fluent, type-safe builder. Query execution automatically utilizes WebSockets if available, otherwise it transparently falls back to REST.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
import { db } from "swiftbase-sdk";
|
|
119
|
+
|
|
120
|
+
// Initialize builder
|
|
121
|
+
const usersDb = db("my_database")("users");
|
|
122
|
+
|
|
123
|
+
// Fetch multiple rows
|
|
124
|
+
const results = await usersDb
|
|
125
|
+
.select("id", "email", "firstName")
|
|
126
|
+
.where("roles", "admin")
|
|
127
|
+
.limit(10)
|
|
128
|
+
.offset(0);
|
|
129
|
+
|
|
130
|
+
// Insert a row
|
|
131
|
+
await db("my_database")("users")
|
|
132
|
+
.insert({ firstName: "Alice", email: "alice@example.com" })
|
|
133
|
+
.execute();
|
|
134
|
+
|
|
135
|
+
// Update rows
|
|
136
|
+
await db("my_database")("users").where("id", "user-id").update({ firstName: "Bob" }).execute();
|
|
137
|
+
|
|
138
|
+
// Delete rows
|
|
139
|
+
await db("my_database")("users").where("id", "user-id").delete().execute();
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Real-time Subscriptions
|
|
143
|
+
|
|
144
|
+
Subscribe to database changes using WebSockets:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const unsubscribe = db("my_database")("posts")
|
|
148
|
+
.where("status", "published")
|
|
149
|
+
.listen((change) => {
|
|
150
|
+
console.log("Database event:", change.event); // 'insert' | 'update' | 'delete'
|
|
151
|
+
console.log("Record data:", change.data);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// To stop listening later:
|
|
155
|
+
unsubscribe();
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## Object Storage (S3-Compatible)
|
|
161
|
+
|
|
162
|
+
Interface with your storage buckets using the S3-compatible `Storage` module.
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { Storage } from "swiftbase-sdk";
|
|
166
|
+
|
|
167
|
+
const storage = new Storage({
|
|
168
|
+
bucket: "my-bucket",
|
|
169
|
+
// region: "us-east-1", (Optional, default is us-east-1)
|
|
170
|
+
// accessKeyId: "key", (Optional: signs requests with SigV4; defaults to bearer token auth)
|
|
171
|
+
// secretAccessKey: "secret"
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// List files
|
|
175
|
+
const { contents } = await storage.listObjects({ prefix: "uploads/" });
|
|
176
|
+
|
|
177
|
+
// Upload a file
|
|
178
|
+
await storage.putObject("uploads/hello.txt", "Hello World!", {
|
|
179
|
+
contentType: "text/plain",
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Get a file
|
|
183
|
+
const text = await storage.getObjectAsText("uploads/hello.txt");
|
|
184
|
+
const json = await storage.getObjectAsJson("uploads/data.json");
|
|
185
|
+
|
|
186
|
+
// Delete a file
|
|
187
|
+
await storage.deleteObject("uploads/hello.txt");
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
## Identity & Role Management (Admin only)
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
import { getUsers, assignRole, unassignRole } from "swiftbase-sdk";
|
|
196
|
+
|
|
197
|
+
// List all users in a project
|
|
198
|
+
const allUsers = await getUsers("your-project-id");
|
|
199
|
+
|
|
200
|
+
// Assign a role
|
|
201
|
+
await assignRole("user-id", "admin");
|
|
202
|
+
|
|
203
|
+
// Revoke a role
|
|
204
|
+
await unassignRole("user-id", "admin");
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Running Tests
|
|
210
|
+
|
|
211
|
+
Run the test suite using Vitest:
|
|
212
|
+
|
|
213
|
+
```bash
|
|
214
|
+
npm run test
|
|
215
|
+
```
|