swiftbase-admin-sdk 1.0.0 → 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/dist/index.cjs +193 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +50 -2
- package/dist/index.d.ts +50 -2
- package/dist/index.js +190 -28
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -129,6 +129,9 @@ var init_token = __esm({
|
|
|
129
129
|
* @returns {boolean} True if the API key is valid, false otherwise
|
|
130
130
|
*/
|
|
131
131
|
isValid() {
|
|
132
|
+
if (__privateGet(this, _expiration) === 0) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
132
135
|
return __privateGet(this, _expiration) > Date.now();
|
|
133
136
|
}
|
|
134
137
|
/**
|
|
@@ -217,7 +220,11 @@ var init_login = __esm({
|
|
|
217
220
|
body.append("client_id", serviceId);
|
|
218
221
|
body.append("client_secret", secretKey);
|
|
219
222
|
body.append("scope", "openid profile email");
|
|
220
|
-
|
|
223
|
+
let base = app.baseUrl || "https://api.swiftbase.io";
|
|
224
|
+
if (!base.includes("localhost") && !base.includes("127.0.0.1")) {
|
|
225
|
+
base = base.replace("api.swiftbase", "identity.swiftbase");
|
|
226
|
+
}
|
|
227
|
+
const url = `${base}/oauth2/token`;
|
|
221
228
|
const response = yield fetch(url, {
|
|
222
229
|
method: "POST",
|
|
223
230
|
headers: {
|
|
@@ -3779,11 +3786,13 @@ var index_exports = {};
|
|
|
3779
3786
|
__export(index_exports, {
|
|
3780
3787
|
PaginatedResponse: () => PaginatedResponse,
|
|
3781
3788
|
Storage: () => Storage,
|
|
3789
|
+
ai: () => ai,
|
|
3782
3790
|
app: () => app,
|
|
3783
3791
|
assignRole: () => assignRole,
|
|
3784
3792
|
changePassword: () => changePassword,
|
|
3785
3793
|
createRole: () => createRole,
|
|
3786
3794
|
createService: () => createService,
|
|
3795
|
+
createUser: () => createUser,
|
|
3787
3796
|
db: () => db,
|
|
3788
3797
|
deleteRole: () => deleteRole,
|
|
3789
3798
|
deleteService: () => deleteService,
|
|
@@ -3797,6 +3806,7 @@ __export(index_exports, {
|
|
|
3797
3806
|
isLoggedIn: () => isLoggedIn,
|
|
3798
3807
|
login: () => login,
|
|
3799
3808
|
logout: () => logout,
|
|
3809
|
+
search: () => search,
|
|
3800
3810
|
setAccessToken: () => setAccessToken,
|
|
3801
3811
|
unassignRole: () => unassignRole,
|
|
3802
3812
|
updateConfiguration: () => updateConfiguration,
|
|
@@ -3836,19 +3846,6 @@ var TooManyRequests = class _TooManyRequests extends Error {
|
|
|
3836
3846
|
Object.setPrototypeOf(this, _TooManyRequests.prototype);
|
|
3837
3847
|
}
|
|
3838
3848
|
};
|
|
3839
|
-
var handleRequestError = (status) => {
|
|
3840
|
-
if (status === 401) {
|
|
3841
|
-
throw new Unauthorized("Unauthorized");
|
|
3842
|
-
} else if (status === 403) {
|
|
3843
|
-
throw new Forbidden("Forbidden");
|
|
3844
|
-
} else if (status === 400) {
|
|
3845
|
-
throw new BadRequest("Bad Request");
|
|
3846
|
-
} else if (status === 429) {
|
|
3847
|
-
throw new TooManyRequests("Too Many Requests");
|
|
3848
|
-
} else {
|
|
3849
|
-
throw new Error("Unexpected error");
|
|
3850
|
-
}
|
|
3851
|
-
};
|
|
3852
3849
|
var _data, _meta;
|
|
3853
3850
|
var PaginatedResponse = class {
|
|
3854
3851
|
constructor(response) {
|
|
@@ -3903,7 +3900,17 @@ var makeRequest = (method, path, params, body) => __async(null, null, function*
|
|
|
3903
3900
|
} else if (app.accessToken && app.accessToken.isValid()) {
|
|
3904
3901
|
headers["Authorization"] = `Bearer ${app.accessToken.value()}`;
|
|
3905
3902
|
}
|
|
3906
|
-
let
|
|
3903
|
+
let base = app.baseUrl || "https://api.swiftbase.io";
|
|
3904
|
+
if (!base.includes("localhost") && !base.includes("127.0.0.1")) {
|
|
3905
|
+
if (path.includes("/db/") || path.includes("/database") || path.includes("/tables") || path.includes("/sql") || path.includes("/queries")) {
|
|
3906
|
+
base = base.replace("api.swiftbase", "database.swiftbase");
|
|
3907
|
+
} else if (path.includes("/oauth2") || path.includes("/login") || path.includes("/api/roles") || path.includes("/api/services") || path.includes("/api/users") || path.includes("/api/me")) {
|
|
3908
|
+
base = base.replace("api.swiftbase", "identity.swiftbase");
|
|
3909
|
+
} else {
|
|
3910
|
+
base = base.replace("api.swiftbase", "app.swiftbase");
|
|
3911
|
+
}
|
|
3912
|
+
}
|
|
3913
|
+
let url = path.startsWith("http") ? path : `${base}${!path.startsWith("/") ? "/" : ""}${path}`;
|
|
3907
3914
|
if (params) {
|
|
3908
3915
|
const query = new URLSearchParams();
|
|
3909
3916
|
for (const [key, value] of Object.entries(params)) {
|
|
@@ -3924,7 +3931,30 @@ var makeRequest = (method, path, params, body) => __async(null, null, function*
|
|
|
3924
3931
|
try {
|
|
3925
3932
|
const response = yield fetch(url, options);
|
|
3926
3933
|
if (!response.ok) {
|
|
3927
|
-
|
|
3934
|
+
let message = "";
|
|
3935
|
+
try {
|
|
3936
|
+
const bodyText = yield response.text();
|
|
3937
|
+
try {
|
|
3938
|
+
const parsed = JSON.parse(bodyText);
|
|
3939
|
+
message = parsed.message || parsed.error || bodyText;
|
|
3940
|
+
} catch (e) {
|
|
3941
|
+
message = bodyText;
|
|
3942
|
+
}
|
|
3943
|
+
} catch (_) {
|
|
3944
|
+
}
|
|
3945
|
+
const errorMsg = message ? `HTTP ${response.status}: ${message}` : `HTTP error ${response.status}`;
|
|
3946
|
+
console.error(errorMsg);
|
|
3947
|
+
if (response.status === 401) {
|
|
3948
|
+
throw new Unauthorized(errorMsg);
|
|
3949
|
+
} else if (response.status === 403) {
|
|
3950
|
+
throw new Forbidden(errorMsg);
|
|
3951
|
+
} else if (response.status === 400) {
|
|
3952
|
+
throw new BadRequest(errorMsg);
|
|
3953
|
+
} else if (response.status === 429) {
|
|
3954
|
+
throw new TooManyRequests(errorMsg);
|
|
3955
|
+
} else {
|
|
3956
|
+
throw new Error(errorMsg);
|
|
3957
|
+
}
|
|
3928
3958
|
}
|
|
3929
3959
|
const data = yield response.json();
|
|
3930
3960
|
if (data.data !== void 0 && data.meta !== void 0) {
|
|
@@ -3948,7 +3978,11 @@ var verifyToken = (token) => __async(null, null, function* () {
|
|
|
3948
3978
|
const headers = {
|
|
3949
3979
|
"Authorization": `Bearer ${token}`
|
|
3950
3980
|
};
|
|
3951
|
-
|
|
3981
|
+
let base = app.baseUrl || "https://api.swiftbase.io";
|
|
3982
|
+
if (!base.includes("localhost") && !base.includes("127.0.0.1")) {
|
|
3983
|
+
base = base.replace("api.swiftbase", "identity.swiftbase");
|
|
3984
|
+
}
|
|
3985
|
+
const response = yield fetch(`${base}/api/me`, { headers });
|
|
3952
3986
|
if (!response.ok) {
|
|
3953
3987
|
throw new Error(`Token verification failed: ${response.status}`);
|
|
3954
3988
|
}
|
|
@@ -3958,7 +3992,11 @@ var verifyToken = (token) => __async(null, null, function* () {
|
|
|
3958
3992
|
// src/auth/verifyservicetoken.ts
|
|
3959
3993
|
init_app();
|
|
3960
3994
|
var verifyServiceToken = (token, scope) => __async(null, null, function* () {
|
|
3961
|
-
|
|
3995
|
+
let base = app.baseUrl || "https://api.swiftbase.io";
|
|
3996
|
+
if (!base.includes("localhost") && !base.includes("127.0.0.1")) {
|
|
3997
|
+
base = base.replace("api.swiftbase", "identity.swiftbase");
|
|
3998
|
+
}
|
|
3999
|
+
const url = `${base}/api/verify-service-token`;
|
|
3962
4000
|
const response = yield fetch(url, {
|
|
3963
4001
|
method: "POST",
|
|
3964
4002
|
headers: {
|
|
@@ -4029,6 +4067,9 @@ var unassignRole = (userId, roleName) => __async(null, null, function* () {
|
|
|
4029
4067
|
const roles = (user.roles || []).filter((r) => r !== roleName);
|
|
4030
4068
|
return yield makeRequest("put" /* PUT */, `/api/users/${userId}`, void 0, { roles });
|
|
4031
4069
|
});
|
|
4070
|
+
var createUser = (input) => __async(null, null, function* () {
|
|
4071
|
+
return yield makeRequest("post" /* POST */, "/api/users", void 0, input);
|
|
4072
|
+
});
|
|
4032
4073
|
|
|
4033
4074
|
// src/profile/getprofile.ts
|
|
4034
4075
|
var getProfile = () => __async(null, null, function* () {
|
|
@@ -4076,7 +4117,7 @@ var _DatabaseSocketManager = class _DatabaseSocketManager {
|
|
|
4076
4117
|
return _DatabaseSocketManager.instance;
|
|
4077
4118
|
}
|
|
4078
4119
|
isWebSocketAvailable() {
|
|
4079
|
-
return this.socket !== null &&
|
|
4120
|
+
return this.socket !== null && this.socket.readyState === 1;
|
|
4080
4121
|
}
|
|
4081
4122
|
getWebSocketUrl(baseUrl, token) {
|
|
4082
4123
|
let wsUrl = baseUrl.replace(/^http/, "ws");
|
|
@@ -4337,14 +4378,6 @@ var QueryBuilder = class {
|
|
|
4337
4378
|
}
|
|
4338
4379
|
execute() {
|
|
4339
4380
|
return __async(this, null, function* () {
|
|
4340
|
-
const socketManager = DatabaseSocketManager.getInstance();
|
|
4341
|
-
if (socketManager.isWebSocketAvailable()) {
|
|
4342
|
-
try {
|
|
4343
|
-
return yield socketManager.executeQuery(this.payload);
|
|
4344
|
-
} catch (err) {
|
|
4345
|
-
console.warn("[QueryBuilder] WebSocket execution failed, falling back to REST:", err);
|
|
4346
|
-
}
|
|
4347
|
-
}
|
|
4348
4381
|
return yield makeRequest("post" /* POST */, "/api/db/query", void 0, this.payload);
|
|
4349
4382
|
});
|
|
4350
4383
|
}
|
|
@@ -4353,9 +4386,124 @@ var QueryBuilder = class {
|
|
|
4353
4386
|
}
|
|
4354
4387
|
};
|
|
4355
4388
|
function db(dbName) {
|
|
4356
|
-
|
|
4389
|
+
const dbClient = function(tableName) {
|
|
4357
4390
|
return new QueryBuilder(dbName, tableName);
|
|
4358
4391
|
};
|
|
4392
|
+
dbClient.initializeDatabase = function(tables) {
|
|
4393
|
+
return __async(this, null, function* () {
|
|
4394
|
+
const projectId = app.projectId;
|
|
4395
|
+
let dbs = [];
|
|
4396
|
+
try {
|
|
4397
|
+
dbs = yield makeRequest("get" /* GET */, "/api/databases", { projectId });
|
|
4398
|
+
} catch (err) {
|
|
4399
|
+
console.error("[DatabaseInit] Failed to list databases:", err.message);
|
|
4400
|
+
throw err;
|
|
4401
|
+
}
|
|
4402
|
+
let myDb = dbs.find((d) => d.name === dbName);
|
|
4403
|
+
if (!myDb) {
|
|
4404
|
+
console.log(`[DatabaseInit] Database '${dbName}' not found. Creating...`);
|
|
4405
|
+
try {
|
|
4406
|
+
myDb = yield makeRequest("post" /* POST */, "/api/databases", void 0, {
|
|
4407
|
+
name: dbName,
|
|
4408
|
+
projectId,
|
|
4409
|
+
engine: "oltp",
|
|
4410
|
+
enableGraphQL: true
|
|
4411
|
+
});
|
|
4412
|
+
} catch (err) {
|
|
4413
|
+
console.error("[DatabaseInit] Failed to create database:", err.message);
|
|
4414
|
+
throw err;
|
|
4415
|
+
}
|
|
4416
|
+
}
|
|
4417
|
+
const databaseId = myDb.id;
|
|
4418
|
+
const schemaName = dbName.toLowerCase().replace(/[^a-z0-9_]/g, "_");
|
|
4419
|
+
try {
|
|
4420
|
+
yield makeRequest("post" /* POST */, "/api/sql", void 0, {
|
|
4421
|
+
databaseId,
|
|
4422
|
+
sql: `CREATE SCHEMA IF NOT EXISTS "${schemaName}"`
|
|
4423
|
+
});
|
|
4424
|
+
} catch (err) {
|
|
4425
|
+
console.error("[DatabaseInit] Failed to create schema:", err.message);
|
|
4426
|
+
throw err;
|
|
4427
|
+
}
|
|
4428
|
+
let existingTables = [];
|
|
4429
|
+
try {
|
|
4430
|
+
existingTables = yield makeRequest("get" /* GET */, "/api/tables", { databaseId });
|
|
4431
|
+
} catch (err) {
|
|
4432
|
+
console.error("[DatabaseInit] Failed to list tables:", err.message);
|
|
4433
|
+
throw err;
|
|
4434
|
+
}
|
|
4435
|
+
for (const t of tables) {
|
|
4436
|
+
try {
|
|
4437
|
+
yield makeRequest("post" /* POST */, "/api/sql", void 0, {
|
|
4438
|
+
databaseId,
|
|
4439
|
+
sql: t.ddl
|
|
4440
|
+
});
|
|
4441
|
+
} catch (err) {
|
|
4442
|
+
console.error(`[DatabaseInit] Failed to execute DDL for table ${t.dbname}:`, err.message);
|
|
4443
|
+
throw err;
|
|
4444
|
+
}
|
|
4445
|
+
const metaExists = existingTables.some((meta) => meta.dbname === t.dbname);
|
|
4446
|
+
if (!metaExists) {
|
|
4447
|
+
try {
|
|
4448
|
+
yield makeRequest("post" /* POST */, "/api/tables", void 0, {
|
|
4449
|
+
databaseId,
|
|
4450
|
+
dbname: t.dbname,
|
|
4451
|
+
name: t.name,
|
|
4452
|
+
docName: t.dbname,
|
|
4453
|
+
rules: "true"
|
|
4454
|
+
});
|
|
4455
|
+
} catch (err) {
|
|
4456
|
+
console.error(`[DatabaseInit] Failed to register metadata for table ${t.dbname}:`, err.message);
|
|
4457
|
+
throw err;
|
|
4458
|
+
}
|
|
4459
|
+
}
|
|
4460
|
+
}
|
|
4461
|
+
});
|
|
4462
|
+
};
|
|
4463
|
+
dbClient.executeSQL = function(sql) {
|
|
4464
|
+
return __async(this, null, function* () {
|
|
4465
|
+
const projectId = app.projectId;
|
|
4466
|
+
let dbs = [];
|
|
4467
|
+
try {
|
|
4468
|
+
dbs = yield makeRequest("get" /* GET */, "/api/databases", { projectId });
|
|
4469
|
+
} catch (err) {
|
|
4470
|
+
console.error("[DatabaseSQL] Failed to list databases:", err.message);
|
|
4471
|
+
throw err;
|
|
4472
|
+
}
|
|
4473
|
+
const myDb = dbs.find((d) => d.name === dbName);
|
|
4474
|
+
if (!myDb) throw new Error(`Database ${dbName} not found`);
|
|
4475
|
+
return yield makeRequest("post" /* POST */, "/api/sql", void 0, {
|
|
4476
|
+
databaseId: myDb.id,
|
|
4477
|
+
sql
|
|
4478
|
+
});
|
|
4479
|
+
});
|
|
4480
|
+
};
|
|
4481
|
+
return dbClient;
|
|
4482
|
+
}
|
|
4483
|
+
|
|
4484
|
+
// src/database/search.ts
|
|
4485
|
+
init_app();
|
|
4486
|
+
function search(indexId, query) {
|
|
4487
|
+
return __async(this, null, function* () {
|
|
4488
|
+
if (!indexId || !query) {
|
|
4489
|
+
throw new Error("indexId and query are required to perform a search");
|
|
4490
|
+
}
|
|
4491
|
+
const baseUrl = app.baseUrl || "https://api.swiftbase.io";
|
|
4492
|
+
const token = app.accessToken || "";
|
|
4493
|
+
const res = yield fetch(`${baseUrl}/api/search/query`, {
|
|
4494
|
+
method: "POST",
|
|
4495
|
+
headers: {
|
|
4496
|
+
"Content-Type": "application/json",
|
|
4497
|
+
"Authorization": token ? `Bearer ${token}` : ""
|
|
4498
|
+
},
|
|
4499
|
+
body: JSON.stringify({ indexId, query })
|
|
4500
|
+
});
|
|
4501
|
+
if (!res.ok) {
|
|
4502
|
+
throw new Error("Failed to execute search query against Swiftbase indexes");
|
|
4503
|
+
}
|
|
4504
|
+
const data = yield res.json();
|
|
4505
|
+
return data || [];
|
|
4506
|
+
});
|
|
4359
4507
|
}
|
|
4360
4508
|
|
|
4361
4509
|
// src/storage/storage.ts
|
|
@@ -4683,15 +4831,31 @@ var Storage = class {
|
|
|
4683
4831
|
});
|
|
4684
4832
|
}
|
|
4685
4833
|
};
|
|
4834
|
+
|
|
4835
|
+
// src/ai/index.ts
|
|
4836
|
+
var ai = {
|
|
4837
|
+
chat: {
|
|
4838
|
+
completions: {
|
|
4839
|
+
create: (options) => __async(null, null, function* () {
|
|
4840
|
+
return yield makeRequest("post" /* POST */, "/api/ai/chat/completions", void 0, {
|
|
4841
|
+
model: options.model || "gemini-3.5-flash",
|
|
4842
|
+
messages: options.messages
|
|
4843
|
+
});
|
|
4844
|
+
})
|
|
4845
|
+
}
|
|
4846
|
+
}
|
|
4847
|
+
};
|
|
4686
4848
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4687
4849
|
0 && (module.exports = {
|
|
4688
4850
|
PaginatedResponse,
|
|
4689
4851
|
Storage,
|
|
4852
|
+
ai,
|
|
4690
4853
|
app,
|
|
4691
4854
|
assignRole,
|
|
4692
4855
|
changePassword,
|
|
4693
4856
|
createRole,
|
|
4694
4857
|
createService,
|
|
4858
|
+
createUser,
|
|
4695
4859
|
db,
|
|
4696
4860
|
deleteRole,
|
|
4697
4861
|
deleteService,
|
|
@@ -4705,6 +4869,7 @@ var Storage = class {
|
|
|
4705
4869
|
isLoggedIn,
|
|
4706
4870
|
login,
|
|
4707
4871
|
logout,
|
|
4872
|
+
search,
|
|
4708
4873
|
setAccessToken,
|
|
4709
4874
|
unassignRole,
|
|
4710
4875
|
updateConfiguration,
|