swiftbase-admin-sdk 1.0.0 → 1.0.2
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 +184 -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 +181 -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,115 @@ 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
|
+
let existingTables = [];
|
|
4420
|
+
try {
|
|
4421
|
+
existingTables = yield makeRequest("get" /* GET */, "/api/tables", { databaseId });
|
|
4422
|
+
} catch (err) {
|
|
4423
|
+
console.error("[DatabaseInit] Failed to list tables:", err.message);
|
|
4424
|
+
throw err;
|
|
4425
|
+
}
|
|
4426
|
+
for (const t of tables) {
|
|
4427
|
+
try {
|
|
4428
|
+
yield makeRequest("post" /* POST */, "/api/sql", void 0, {
|
|
4429
|
+
databaseId,
|
|
4430
|
+
sql: t.ddl
|
|
4431
|
+
});
|
|
4432
|
+
} catch (err) {
|
|
4433
|
+
console.error(`[DatabaseInit] Failed to execute DDL for table ${t.dbname}:`, err.message);
|
|
4434
|
+
throw err;
|
|
4435
|
+
}
|
|
4436
|
+
const metaExists = existingTables.some((meta) => meta.dbname === t.dbname);
|
|
4437
|
+
if (!metaExists) {
|
|
4438
|
+
try {
|
|
4439
|
+
yield makeRequest("post" /* POST */, "/api/tables", void 0, {
|
|
4440
|
+
databaseId,
|
|
4441
|
+
dbname: t.dbname,
|
|
4442
|
+
name: t.name,
|
|
4443
|
+
docName: t.dbname,
|
|
4444
|
+
rules: "true"
|
|
4445
|
+
});
|
|
4446
|
+
} catch (err) {
|
|
4447
|
+
console.error(`[DatabaseInit] Failed to register metadata for table ${t.dbname}:`, err.message);
|
|
4448
|
+
throw err;
|
|
4449
|
+
}
|
|
4450
|
+
}
|
|
4451
|
+
}
|
|
4452
|
+
});
|
|
4453
|
+
};
|
|
4454
|
+
dbClient.executeSQL = function(sql) {
|
|
4455
|
+
return __async(this, null, function* () {
|
|
4456
|
+
const projectId = app.projectId;
|
|
4457
|
+
let dbs = [];
|
|
4458
|
+
try {
|
|
4459
|
+
dbs = yield makeRequest("get" /* GET */, "/api/databases", { projectId });
|
|
4460
|
+
} catch (err) {
|
|
4461
|
+
console.error("[DatabaseSQL] Failed to list databases:", err.message);
|
|
4462
|
+
throw err;
|
|
4463
|
+
}
|
|
4464
|
+
const myDb = dbs.find((d) => d.name === dbName);
|
|
4465
|
+
if (!myDb) throw new Error(`Database ${dbName} not found`);
|
|
4466
|
+
return yield makeRequest("post" /* POST */, "/api/sql", void 0, {
|
|
4467
|
+
databaseId: myDb.id,
|
|
4468
|
+
sql
|
|
4469
|
+
});
|
|
4470
|
+
});
|
|
4471
|
+
};
|
|
4472
|
+
return dbClient;
|
|
4473
|
+
}
|
|
4474
|
+
|
|
4475
|
+
// src/database/search.ts
|
|
4476
|
+
init_app();
|
|
4477
|
+
function search(indexId, query) {
|
|
4478
|
+
return __async(this, null, function* () {
|
|
4479
|
+
if (!indexId || !query) {
|
|
4480
|
+
throw new Error("indexId and query are required to perform a search");
|
|
4481
|
+
}
|
|
4482
|
+
const baseUrl = app.baseUrl || "https://api.swiftbase.io";
|
|
4483
|
+
const token = app.accessToken || "";
|
|
4484
|
+
const res = yield fetch(`${baseUrl}/api/search/query`, {
|
|
4485
|
+
method: "POST",
|
|
4486
|
+
headers: {
|
|
4487
|
+
"Content-Type": "application/json",
|
|
4488
|
+
"Authorization": token ? `Bearer ${token}` : ""
|
|
4489
|
+
},
|
|
4490
|
+
body: JSON.stringify({ indexId, query })
|
|
4491
|
+
});
|
|
4492
|
+
if (!res.ok) {
|
|
4493
|
+
throw new Error("Failed to execute search query against Swiftbase indexes");
|
|
4494
|
+
}
|
|
4495
|
+
const data = yield res.json();
|
|
4496
|
+
return data || [];
|
|
4497
|
+
});
|
|
4359
4498
|
}
|
|
4360
4499
|
|
|
4361
4500
|
// src/storage/storage.ts
|
|
@@ -4683,15 +4822,31 @@ var Storage = class {
|
|
|
4683
4822
|
});
|
|
4684
4823
|
}
|
|
4685
4824
|
};
|
|
4825
|
+
|
|
4826
|
+
// src/ai/index.ts
|
|
4827
|
+
var ai = {
|
|
4828
|
+
chat: {
|
|
4829
|
+
completions: {
|
|
4830
|
+
create: (options) => __async(null, null, function* () {
|
|
4831
|
+
return yield makeRequest("post" /* POST */, "/api/ai/chat/completions", void 0, {
|
|
4832
|
+
model: options.model || "gemini-3.5-flash",
|
|
4833
|
+
messages: options.messages
|
|
4834
|
+
});
|
|
4835
|
+
})
|
|
4836
|
+
}
|
|
4837
|
+
}
|
|
4838
|
+
};
|
|
4686
4839
|
// Annotate the CommonJS export names for ESM import in node:
|
|
4687
4840
|
0 && (module.exports = {
|
|
4688
4841
|
PaginatedResponse,
|
|
4689
4842
|
Storage,
|
|
4843
|
+
ai,
|
|
4690
4844
|
app,
|
|
4691
4845
|
assignRole,
|
|
4692
4846
|
changePassword,
|
|
4693
4847
|
createRole,
|
|
4694
4848
|
createService,
|
|
4849
|
+
createUser,
|
|
4695
4850
|
db,
|
|
4696
4851
|
deleteRole,
|
|
4697
4852
|
deleteService,
|
|
@@ -4705,6 +4860,7 @@ var Storage = class {
|
|
|
4705
4860
|
isLoggedIn,
|
|
4706
4861
|
login,
|
|
4707
4862
|
logout,
|
|
4863
|
+
search,
|
|
4708
4864
|
setAccessToken,
|
|
4709
4865
|
unassignRole,
|
|
4710
4866
|
updateConfiguration,
|