@giaeulate/baas-sdk 1.0.8 → 1.0.10
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 +107 -15
- package/dist/index.d.cts +62 -3
- package/dist/index.d.ts +62 -3
- package/dist/index.js +107 -15
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -32,12 +32,32 @@ var HttpClient = class {
|
|
|
32
32
|
apiKey = "";
|
|
33
33
|
token = null;
|
|
34
34
|
environment = "prod";
|
|
35
|
+
_onForceLogout = null;
|
|
35
36
|
constructor(url, apiKey) {
|
|
36
37
|
let baseUrl = url || (typeof window !== "undefined" ? window.location.origin : "http://localhost:8080");
|
|
37
38
|
this.url = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
38
39
|
if (apiKey) this.apiKey = apiKey;
|
|
39
40
|
this.token = this.cleanValue(localStorage.getItem("baas_token"));
|
|
40
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Set the auth token manually (e.g. restoring from SecureStore in React Native).
|
|
44
|
+
* Persists to both the in-memory property and localStorage so getDynamicToken() works.
|
|
45
|
+
*/
|
|
46
|
+
setToken(token) {
|
|
47
|
+
this.token = token;
|
|
48
|
+
if (token) {
|
|
49
|
+
localStorage.setItem("baas_token", token);
|
|
50
|
+
} else {
|
|
51
|
+
localStorage.removeItem("baas_token");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Register a callback invoked on forced logout (401).
|
|
56
|
+
* Use this in React Native instead of the default window.location redirect.
|
|
57
|
+
*/
|
|
58
|
+
onForceLogout(callback) {
|
|
59
|
+
this._onForceLogout = callback;
|
|
60
|
+
}
|
|
41
61
|
cleanValue(val) {
|
|
42
62
|
if (!val || val === "null" || val === "undefined") return null;
|
|
43
63
|
return val.trim();
|
|
@@ -120,9 +140,13 @@ var HttpClient = class {
|
|
|
120
140
|
}
|
|
121
141
|
forceLogout() {
|
|
122
142
|
this.logout();
|
|
123
|
-
if (
|
|
143
|
+
if (this._onForceLogout) {
|
|
144
|
+
this._onForceLogout();
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (typeof window !== "undefined" && window.location && window.location.search !== void 0) {
|
|
124
148
|
if (!window.location.search.includes("expired")) {
|
|
125
|
-
window.location.href = "/
|
|
149
|
+
window.location.href = "/authentication/login?reason=expired";
|
|
126
150
|
}
|
|
127
151
|
}
|
|
128
152
|
}
|
|
@@ -490,18 +514,62 @@ function createDatabaseModule(client) {
|
|
|
490
514
|
|
|
491
515
|
// src/modules/storage.ts
|
|
492
516
|
function createStorageModule(client) {
|
|
517
|
+
const get = (endpoint) => client.get(endpoint);
|
|
518
|
+
const post = (endpoint, body) => client.post(endpoint, body);
|
|
519
|
+
const del = (endpoint) => client.delete(endpoint);
|
|
520
|
+
async function uploadFile(fileOrTable, fileOrBucketId) {
|
|
521
|
+
let file;
|
|
522
|
+
let bucketId;
|
|
523
|
+
if (fileOrTable instanceof File) {
|
|
524
|
+
file = fileOrTable;
|
|
525
|
+
bucketId = typeof fileOrBucketId === "string" ? fileOrBucketId : void 0;
|
|
526
|
+
} else {
|
|
527
|
+
file = fileOrBucketId;
|
|
528
|
+
bucketId = void 0;
|
|
529
|
+
}
|
|
530
|
+
const formData = new FormData();
|
|
531
|
+
formData.append("file", file);
|
|
532
|
+
if (bucketId) {
|
|
533
|
+
formData.append("bucket_id", bucketId);
|
|
534
|
+
}
|
|
535
|
+
const headers = client.getHeaders("");
|
|
536
|
+
const res = await fetch(`${client.url}/api/storage/upload`, {
|
|
537
|
+
method: "POST",
|
|
538
|
+
headers,
|
|
539
|
+
body: formData
|
|
540
|
+
});
|
|
541
|
+
if (!res.ok) {
|
|
542
|
+
if (res.status === 401) client.forceLogout();
|
|
543
|
+
const err = await res.json().catch(() => ({ error: `Upload failed: ${res.status}` }));
|
|
544
|
+
throw new Error(err.error ?? `Upload failed: ${res.status}`);
|
|
545
|
+
}
|
|
546
|
+
return res.json();
|
|
547
|
+
}
|
|
493
548
|
return {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
549
|
+
upload: uploadFile,
|
|
550
|
+
async listFiles() {
|
|
551
|
+
return get("/api/storage/files");
|
|
552
|
+
},
|
|
553
|
+
async getFile(fileId) {
|
|
554
|
+
return get(`/api/storage/files/${fileId}`);
|
|
555
|
+
},
|
|
556
|
+
async deleteFile(fileId) {
|
|
557
|
+
return del(`/api/storage/files/${fileId}`);
|
|
558
|
+
},
|
|
559
|
+
async createBucket(name, isPublic = false) {
|
|
560
|
+
return post("/api/storage/buckets", { name, is_public: isPublic });
|
|
561
|
+
},
|
|
562
|
+
async listBuckets() {
|
|
563
|
+
return get("/api/storage/buckets");
|
|
564
|
+
},
|
|
565
|
+
async getBucket(bucketId) {
|
|
566
|
+
return get(`/api/storage/buckets/${bucketId}`);
|
|
567
|
+
},
|
|
568
|
+
async deleteBucket(bucketId) {
|
|
569
|
+
return del(`/api/storage/buckets/${bucketId}`);
|
|
570
|
+
},
|
|
571
|
+
async listBucketFiles(bucketId) {
|
|
572
|
+
return get(`/api/storage/buckets/${bucketId}/files`);
|
|
505
573
|
}
|
|
506
574
|
};
|
|
507
575
|
}
|
|
@@ -1264,8 +1332,32 @@ var BaasClient = class extends HttpClient {
|
|
|
1264
1332
|
return this.database.downloadExport(tableName, format, filters);
|
|
1265
1333
|
}
|
|
1266
1334
|
// Storage shortcuts
|
|
1267
|
-
async upload(
|
|
1268
|
-
return this.storage.upload(
|
|
1335
|
+
async upload(file, bucketId) {
|
|
1336
|
+
return this.storage.upload(file, bucketId);
|
|
1337
|
+
}
|
|
1338
|
+
async listStorageFiles() {
|
|
1339
|
+
return this.storage.listFiles();
|
|
1340
|
+
}
|
|
1341
|
+
async getStorageFile(fileId) {
|
|
1342
|
+
return this.storage.getFile(fileId);
|
|
1343
|
+
}
|
|
1344
|
+
async deleteStorageFile(fileId) {
|
|
1345
|
+
return this.storage.deleteFile(fileId);
|
|
1346
|
+
}
|
|
1347
|
+
async createStorageBucket(name, isPublic) {
|
|
1348
|
+
return this.storage.createBucket(name, isPublic);
|
|
1349
|
+
}
|
|
1350
|
+
async listStorageBuckets() {
|
|
1351
|
+
return this.storage.listBuckets();
|
|
1352
|
+
}
|
|
1353
|
+
async getStorageBucket(bucketId) {
|
|
1354
|
+
return this.storage.getBucket(bucketId);
|
|
1355
|
+
}
|
|
1356
|
+
async deleteStorageBucket(bucketId) {
|
|
1357
|
+
return this.storage.deleteBucket(bucketId);
|
|
1358
|
+
}
|
|
1359
|
+
async listStorageBucketFiles(bucketId) {
|
|
1360
|
+
return this.storage.listBucketFiles(bucketId);
|
|
1269
1361
|
}
|
|
1270
1362
|
// Functions shortcuts
|
|
1271
1363
|
async invokeFunction(id, data) {
|
package/dist/index.d.cts
CHANGED
|
@@ -50,7 +50,18 @@ declare class HttpClient {
|
|
|
50
50
|
apiKey: string;
|
|
51
51
|
token: string | null;
|
|
52
52
|
private environment;
|
|
53
|
+
private _onForceLogout;
|
|
53
54
|
constructor(url?: string, apiKey?: string);
|
|
55
|
+
/**
|
|
56
|
+
* Set the auth token manually (e.g. restoring from SecureStore in React Native).
|
|
57
|
+
* Persists to both the in-memory property and localStorage so getDynamicToken() works.
|
|
58
|
+
*/
|
|
59
|
+
setToken(token: string | null): void;
|
|
60
|
+
/**
|
|
61
|
+
* Register a callback invoked on forced logout (401).
|
|
62
|
+
* Use this in React Native instead of the default window.location redirect.
|
|
63
|
+
*/
|
|
64
|
+
onForceLogout(callback: () => void): void;
|
|
54
65
|
protected cleanValue(val: string | null): string | null;
|
|
55
66
|
/**
|
|
56
67
|
* Public header generator for adapters
|
|
@@ -265,10 +276,50 @@ interface DatabaseModule {
|
|
|
265
276
|
}
|
|
266
277
|
|
|
267
278
|
/**
|
|
268
|
-
* Storage Module - File
|
|
279
|
+
* Storage Module - File & bucket operations (MinIO-backed)
|
|
280
|
+
*
|
|
281
|
+
* Files: upload, list, get (metadata + presigned URL), delete
|
|
282
|
+
* Buckets: create, list, get, delete, listFiles
|
|
269
283
|
*/
|
|
270
284
|
|
|
285
|
+
interface StorageFile {
|
|
286
|
+
id: string;
|
|
287
|
+
bucket_id: string;
|
|
288
|
+
name: string;
|
|
289
|
+
mime_type: string;
|
|
290
|
+
size: number;
|
|
291
|
+
created_at: string;
|
|
292
|
+
}
|
|
293
|
+
interface StorageFileWithUrl {
|
|
294
|
+
metadata: StorageFile;
|
|
295
|
+
url: string;
|
|
296
|
+
}
|
|
297
|
+
interface StorageBucket {
|
|
298
|
+
id: string;
|
|
299
|
+
name: string;
|
|
300
|
+
is_public: boolean;
|
|
301
|
+
created_at: string;
|
|
302
|
+
}
|
|
271
303
|
interface StorageModule {
|
|
304
|
+
/** Upload a file. Optionally target a specific bucket by ID. */
|
|
305
|
+
upload(file: File, bucketId?: string): Promise<StorageFile>;
|
|
306
|
+
/** List all files across buckets. */
|
|
307
|
+
listFiles(): Promise<StorageFile[]>;
|
|
308
|
+
/** Get file metadata + a 15-min presigned download URL. */
|
|
309
|
+
getFile(fileId: string): Promise<StorageFileWithUrl>;
|
|
310
|
+
/** Delete a file by ID. */
|
|
311
|
+
deleteFile(fileId: string): Promise<void>;
|
|
312
|
+
/** Create a new bucket. */
|
|
313
|
+
createBucket(name: string, isPublic?: boolean): Promise<StorageBucket>;
|
|
314
|
+
/** List all buckets. */
|
|
315
|
+
listBuckets(): Promise<StorageBucket[]>;
|
|
316
|
+
/** Get bucket info by ID. */
|
|
317
|
+
getBucket(bucketId: string): Promise<StorageBucket>;
|
|
318
|
+
/** Delete an empty bucket. */
|
|
319
|
+
deleteBucket(bucketId: string): Promise<void>;
|
|
320
|
+
/** List files inside a specific bucket. */
|
|
321
|
+
listBucketFiles(bucketId: string): Promise<StorageFile[]>;
|
|
322
|
+
/** @deprecated Use upload(file, bucketId?) instead. */
|
|
272
323
|
upload(table: string, file: File): Promise<any>;
|
|
273
324
|
}
|
|
274
325
|
|
|
@@ -680,7 +731,15 @@ declare class BaasClient extends HttpClient {
|
|
|
680
731
|
dropConstraint(tableName: string, constraintName: string): Promise<any>;
|
|
681
732
|
queryData(tableName: string, options: any): Promise<any>;
|
|
682
733
|
downloadExport(tableName: string, format: 'sql' | 'csv' | 'backup', filters?: any[]): Promise<any>;
|
|
683
|
-
upload(
|
|
734
|
+
upload(file: File, bucketId?: string): Promise<StorageFile>;
|
|
735
|
+
listStorageFiles(): Promise<StorageFile[]>;
|
|
736
|
+
getStorageFile(fileId: string): Promise<StorageFileWithUrl>;
|
|
737
|
+
deleteStorageFile(fileId: string): Promise<void>;
|
|
738
|
+
createStorageBucket(name: string, isPublic?: boolean): Promise<StorageBucket>;
|
|
739
|
+
listStorageBuckets(): Promise<StorageBucket[]>;
|
|
740
|
+
getStorageBucket(bucketId: string): Promise<StorageBucket>;
|
|
741
|
+
deleteStorageBucket(bucketId: string): Promise<void>;
|
|
742
|
+
listStorageBucketFiles(bucketId: string): Promise<StorageFile[]>;
|
|
684
743
|
invokeFunction(id: string, data?: any): Promise<any>;
|
|
685
744
|
createApiKey(name: string, permissions?: string[], expiresAt?: string): Promise<any>;
|
|
686
745
|
listApiKeys(): Promise<any>;
|
|
@@ -764,4 +823,4 @@ declare class BaasClient extends HttpClient {
|
|
|
764
823
|
deleteCorsOrigin(id: string): Promise<any>;
|
|
765
824
|
}
|
|
766
825
|
|
|
767
|
-
export { type ApiKeysModule, type ApiResponse, type ApplicationLogsOptions, type AuditLogsOptions, type AuditModule, type AuthModule, BaasClient, type BackupsModule, type BranchesModule, type CorsOriginsModule, type DatabaseModule, type EmailConfig, type EmailModule, type EmailTemplate, type EnvVarsModule, type FunctionsModule, type GraphQLModule, HttpClient, type HttpMethod, type JobInput, type JobUpdateInput, type JobsModule, type LogDrainInput, type LogDrainUpdateInput, type LogDrainsModule, type MetricsModule, type MigrationsModule, type PaginationOptions, QueryBuilder, type QueryFilter, type RealtimeModule, type RequestLogsOptions, type RequestOptions, type SearchModule, type SearchOptions, type SendEmailInput, type StorageModule, type Subscription, type TimeseriesOptions, type UsersModule, type WebhookInput, type WebhookUpdateInput, type WebhooksModule };
|
|
826
|
+
export { type ApiKeysModule, type ApiResponse, type ApplicationLogsOptions, type AuditLogsOptions, type AuditModule, type AuthModule, BaasClient, type BackupsModule, type BranchesModule, type CorsOriginsModule, type DatabaseModule, type EmailConfig, type EmailModule, type EmailTemplate, type EnvVarsModule, type FunctionsModule, type GraphQLModule, HttpClient, type HttpMethod, type JobInput, type JobUpdateInput, type JobsModule, type LogDrainInput, type LogDrainUpdateInput, type LogDrainsModule, type MetricsModule, type MigrationsModule, type PaginationOptions, QueryBuilder, type QueryFilter, type RealtimeModule, type RequestLogsOptions, type RequestOptions, type SearchModule, type SearchOptions, type SendEmailInput, type StorageBucket, type StorageFile, type StorageFileWithUrl, type StorageModule, type Subscription, type TimeseriesOptions, type UsersModule, type WebhookInput, type WebhookUpdateInput, type WebhooksModule };
|
package/dist/index.d.ts
CHANGED
|
@@ -50,7 +50,18 @@ declare class HttpClient {
|
|
|
50
50
|
apiKey: string;
|
|
51
51
|
token: string | null;
|
|
52
52
|
private environment;
|
|
53
|
+
private _onForceLogout;
|
|
53
54
|
constructor(url?: string, apiKey?: string);
|
|
55
|
+
/**
|
|
56
|
+
* Set the auth token manually (e.g. restoring from SecureStore in React Native).
|
|
57
|
+
* Persists to both the in-memory property and localStorage so getDynamicToken() works.
|
|
58
|
+
*/
|
|
59
|
+
setToken(token: string | null): void;
|
|
60
|
+
/**
|
|
61
|
+
* Register a callback invoked on forced logout (401).
|
|
62
|
+
* Use this in React Native instead of the default window.location redirect.
|
|
63
|
+
*/
|
|
64
|
+
onForceLogout(callback: () => void): void;
|
|
54
65
|
protected cleanValue(val: string | null): string | null;
|
|
55
66
|
/**
|
|
56
67
|
* Public header generator for adapters
|
|
@@ -265,10 +276,50 @@ interface DatabaseModule {
|
|
|
265
276
|
}
|
|
266
277
|
|
|
267
278
|
/**
|
|
268
|
-
* Storage Module - File
|
|
279
|
+
* Storage Module - File & bucket operations (MinIO-backed)
|
|
280
|
+
*
|
|
281
|
+
* Files: upload, list, get (metadata + presigned URL), delete
|
|
282
|
+
* Buckets: create, list, get, delete, listFiles
|
|
269
283
|
*/
|
|
270
284
|
|
|
285
|
+
interface StorageFile {
|
|
286
|
+
id: string;
|
|
287
|
+
bucket_id: string;
|
|
288
|
+
name: string;
|
|
289
|
+
mime_type: string;
|
|
290
|
+
size: number;
|
|
291
|
+
created_at: string;
|
|
292
|
+
}
|
|
293
|
+
interface StorageFileWithUrl {
|
|
294
|
+
metadata: StorageFile;
|
|
295
|
+
url: string;
|
|
296
|
+
}
|
|
297
|
+
interface StorageBucket {
|
|
298
|
+
id: string;
|
|
299
|
+
name: string;
|
|
300
|
+
is_public: boolean;
|
|
301
|
+
created_at: string;
|
|
302
|
+
}
|
|
271
303
|
interface StorageModule {
|
|
304
|
+
/** Upload a file. Optionally target a specific bucket by ID. */
|
|
305
|
+
upload(file: File, bucketId?: string): Promise<StorageFile>;
|
|
306
|
+
/** List all files across buckets. */
|
|
307
|
+
listFiles(): Promise<StorageFile[]>;
|
|
308
|
+
/** Get file metadata + a 15-min presigned download URL. */
|
|
309
|
+
getFile(fileId: string): Promise<StorageFileWithUrl>;
|
|
310
|
+
/** Delete a file by ID. */
|
|
311
|
+
deleteFile(fileId: string): Promise<void>;
|
|
312
|
+
/** Create a new bucket. */
|
|
313
|
+
createBucket(name: string, isPublic?: boolean): Promise<StorageBucket>;
|
|
314
|
+
/** List all buckets. */
|
|
315
|
+
listBuckets(): Promise<StorageBucket[]>;
|
|
316
|
+
/** Get bucket info by ID. */
|
|
317
|
+
getBucket(bucketId: string): Promise<StorageBucket>;
|
|
318
|
+
/** Delete an empty bucket. */
|
|
319
|
+
deleteBucket(bucketId: string): Promise<void>;
|
|
320
|
+
/** List files inside a specific bucket. */
|
|
321
|
+
listBucketFiles(bucketId: string): Promise<StorageFile[]>;
|
|
322
|
+
/** @deprecated Use upload(file, bucketId?) instead. */
|
|
272
323
|
upload(table: string, file: File): Promise<any>;
|
|
273
324
|
}
|
|
274
325
|
|
|
@@ -680,7 +731,15 @@ declare class BaasClient extends HttpClient {
|
|
|
680
731
|
dropConstraint(tableName: string, constraintName: string): Promise<any>;
|
|
681
732
|
queryData(tableName: string, options: any): Promise<any>;
|
|
682
733
|
downloadExport(tableName: string, format: 'sql' | 'csv' | 'backup', filters?: any[]): Promise<any>;
|
|
683
|
-
upload(
|
|
734
|
+
upload(file: File, bucketId?: string): Promise<StorageFile>;
|
|
735
|
+
listStorageFiles(): Promise<StorageFile[]>;
|
|
736
|
+
getStorageFile(fileId: string): Promise<StorageFileWithUrl>;
|
|
737
|
+
deleteStorageFile(fileId: string): Promise<void>;
|
|
738
|
+
createStorageBucket(name: string, isPublic?: boolean): Promise<StorageBucket>;
|
|
739
|
+
listStorageBuckets(): Promise<StorageBucket[]>;
|
|
740
|
+
getStorageBucket(bucketId: string): Promise<StorageBucket>;
|
|
741
|
+
deleteStorageBucket(bucketId: string): Promise<void>;
|
|
742
|
+
listStorageBucketFiles(bucketId: string): Promise<StorageFile[]>;
|
|
684
743
|
invokeFunction(id: string, data?: any): Promise<any>;
|
|
685
744
|
createApiKey(name: string, permissions?: string[], expiresAt?: string): Promise<any>;
|
|
686
745
|
listApiKeys(): Promise<any>;
|
|
@@ -764,4 +823,4 @@ declare class BaasClient extends HttpClient {
|
|
|
764
823
|
deleteCorsOrigin(id: string): Promise<any>;
|
|
765
824
|
}
|
|
766
825
|
|
|
767
|
-
export { type ApiKeysModule, type ApiResponse, type ApplicationLogsOptions, type AuditLogsOptions, type AuditModule, type AuthModule, BaasClient, type BackupsModule, type BranchesModule, type CorsOriginsModule, type DatabaseModule, type EmailConfig, type EmailModule, type EmailTemplate, type EnvVarsModule, type FunctionsModule, type GraphQLModule, HttpClient, type HttpMethod, type JobInput, type JobUpdateInput, type JobsModule, type LogDrainInput, type LogDrainUpdateInput, type LogDrainsModule, type MetricsModule, type MigrationsModule, type PaginationOptions, QueryBuilder, type QueryFilter, type RealtimeModule, type RequestLogsOptions, type RequestOptions, type SearchModule, type SearchOptions, type SendEmailInput, type StorageModule, type Subscription, type TimeseriesOptions, type UsersModule, type WebhookInput, type WebhookUpdateInput, type WebhooksModule };
|
|
826
|
+
export { type ApiKeysModule, type ApiResponse, type ApplicationLogsOptions, type AuditLogsOptions, type AuditModule, type AuthModule, BaasClient, type BackupsModule, type BranchesModule, type CorsOriginsModule, type DatabaseModule, type EmailConfig, type EmailModule, type EmailTemplate, type EnvVarsModule, type FunctionsModule, type GraphQLModule, HttpClient, type HttpMethod, type JobInput, type JobUpdateInput, type JobsModule, type LogDrainInput, type LogDrainUpdateInput, type LogDrainsModule, type MetricsModule, type MigrationsModule, type PaginationOptions, QueryBuilder, type QueryFilter, type RealtimeModule, type RequestLogsOptions, type RequestOptions, type SearchModule, type SearchOptions, type SendEmailInput, type StorageBucket, type StorageFile, type StorageFileWithUrl, type StorageModule, type Subscription, type TimeseriesOptions, type UsersModule, type WebhookInput, type WebhookUpdateInput, type WebhooksModule };
|
package/dist/index.js
CHANGED
|
@@ -4,12 +4,32 @@ var HttpClient = class {
|
|
|
4
4
|
apiKey = "";
|
|
5
5
|
token = null;
|
|
6
6
|
environment = "prod";
|
|
7
|
+
_onForceLogout = null;
|
|
7
8
|
constructor(url, apiKey) {
|
|
8
9
|
let baseUrl = url || (typeof window !== "undefined" ? window.location.origin : "http://localhost:8080");
|
|
9
10
|
this.url = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
10
11
|
if (apiKey) this.apiKey = apiKey;
|
|
11
12
|
this.token = this.cleanValue(localStorage.getItem("baas_token"));
|
|
12
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Set the auth token manually (e.g. restoring from SecureStore in React Native).
|
|
16
|
+
* Persists to both the in-memory property and localStorage so getDynamicToken() works.
|
|
17
|
+
*/
|
|
18
|
+
setToken(token) {
|
|
19
|
+
this.token = token;
|
|
20
|
+
if (token) {
|
|
21
|
+
localStorage.setItem("baas_token", token);
|
|
22
|
+
} else {
|
|
23
|
+
localStorage.removeItem("baas_token");
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Register a callback invoked on forced logout (401).
|
|
28
|
+
* Use this in React Native instead of the default window.location redirect.
|
|
29
|
+
*/
|
|
30
|
+
onForceLogout(callback) {
|
|
31
|
+
this._onForceLogout = callback;
|
|
32
|
+
}
|
|
13
33
|
cleanValue(val) {
|
|
14
34
|
if (!val || val === "null" || val === "undefined") return null;
|
|
15
35
|
return val.trim();
|
|
@@ -92,9 +112,13 @@ var HttpClient = class {
|
|
|
92
112
|
}
|
|
93
113
|
forceLogout() {
|
|
94
114
|
this.logout();
|
|
95
|
-
if (
|
|
115
|
+
if (this._onForceLogout) {
|
|
116
|
+
this._onForceLogout();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
if (typeof window !== "undefined" && window.location && window.location.search !== void 0) {
|
|
96
120
|
if (!window.location.search.includes("expired")) {
|
|
97
|
-
window.location.href = "/
|
|
121
|
+
window.location.href = "/authentication/login?reason=expired";
|
|
98
122
|
}
|
|
99
123
|
}
|
|
100
124
|
}
|
|
@@ -462,18 +486,62 @@ function createDatabaseModule(client) {
|
|
|
462
486
|
|
|
463
487
|
// src/modules/storage.ts
|
|
464
488
|
function createStorageModule(client) {
|
|
489
|
+
const get = (endpoint) => client.get(endpoint);
|
|
490
|
+
const post = (endpoint, body) => client.post(endpoint, body);
|
|
491
|
+
const del = (endpoint) => client.delete(endpoint);
|
|
492
|
+
async function uploadFile(fileOrTable, fileOrBucketId) {
|
|
493
|
+
let file;
|
|
494
|
+
let bucketId;
|
|
495
|
+
if (fileOrTable instanceof File) {
|
|
496
|
+
file = fileOrTable;
|
|
497
|
+
bucketId = typeof fileOrBucketId === "string" ? fileOrBucketId : void 0;
|
|
498
|
+
} else {
|
|
499
|
+
file = fileOrBucketId;
|
|
500
|
+
bucketId = void 0;
|
|
501
|
+
}
|
|
502
|
+
const formData = new FormData();
|
|
503
|
+
formData.append("file", file);
|
|
504
|
+
if (bucketId) {
|
|
505
|
+
formData.append("bucket_id", bucketId);
|
|
506
|
+
}
|
|
507
|
+
const headers = client.getHeaders("");
|
|
508
|
+
const res = await fetch(`${client.url}/api/storage/upload`, {
|
|
509
|
+
method: "POST",
|
|
510
|
+
headers,
|
|
511
|
+
body: formData
|
|
512
|
+
});
|
|
513
|
+
if (!res.ok) {
|
|
514
|
+
if (res.status === 401) client.forceLogout();
|
|
515
|
+
const err = await res.json().catch(() => ({ error: `Upload failed: ${res.status}` }));
|
|
516
|
+
throw new Error(err.error ?? `Upload failed: ${res.status}`);
|
|
517
|
+
}
|
|
518
|
+
return res.json();
|
|
519
|
+
}
|
|
465
520
|
return {
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
521
|
+
upload: uploadFile,
|
|
522
|
+
async listFiles() {
|
|
523
|
+
return get("/api/storage/files");
|
|
524
|
+
},
|
|
525
|
+
async getFile(fileId) {
|
|
526
|
+
return get(`/api/storage/files/${fileId}`);
|
|
527
|
+
},
|
|
528
|
+
async deleteFile(fileId) {
|
|
529
|
+
return del(`/api/storage/files/${fileId}`);
|
|
530
|
+
},
|
|
531
|
+
async createBucket(name, isPublic = false) {
|
|
532
|
+
return post("/api/storage/buckets", { name, is_public: isPublic });
|
|
533
|
+
},
|
|
534
|
+
async listBuckets() {
|
|
535
|
+
return get("/api/storage/buckets");
|
|
536
|
+
},
|
|
537
|
+
async getBucket(bucketId) {
|
|
538
|
+
return get(`/api/storage/buckets/${bucketId}`);
|
|
539
|
+
},
|
|
540
|
+
async deleteBucket(bucketId) {
|
|
541
|
+
return del(`/api/storage/buckets/${bucketId}`);
|
|
542
|
+
},
|
|
543
|
+
async listBucketFiles(bucketId) {
|
|
544
|
+
return get(`/api/storage/buckets/${bucketId}/files`);
|
|
477
545
|
}
|
|
478
546
|
};
|
|
479
547
|
}
|
|
@@ -1236,8 +1304,32 @@ var BaasClient = class extends HttpClient {
|
|
|
1236
1304
|
return this.database.downloadExport(tableName, format, filters);
|
|
1237
1305
|
}
|
|
1238
1306
|
// Storage shortcuts
|
|
1239
|
-
async upload(
|
|
1240
|
-
return this.storage.upload(
|
|
1307
|
+
async upload(file, bucketId) {
|
|
1308
|
+
return this.storage.upload(file, bucketId);
|
|
1309
|
+
}
|
|
1310
|
+
async listStorageFiles() {
|
|
1311
|
+
return this.storage.listFiles();
|
|
1312
|
+
}
|
|
1313
|
+
async getStorageFile(fileId) {
|
|
1314
|
+
return this.storage.getFile(fileId);
|
|
1315
|
+
}
|
|
1316
|
+
async deleteStorageFile(fileId) {
|
|
1317
|
+
return this.storage.deleteFile(fileId);
|
|
1318
|
+
}
|
|
1319
|
+
async createStorageBucket(name, isPublic) {
|
|
1320
|
+
return this.storage.createBucket(name, isPublic);
|
|
1321
|
+
}
|
|
1322
|
+
async listStorageBuckets() {
|
|
1323
|
+
return this.storage.listBuckets();
|
|
1324
|
+
}
|
|
1325
|
+
async getStorageBucket(bucketId) {
|
|
1326
|
+
return this.storage.getBucket(bucketId);
|
|
1327
|
+
}
|
|
1328
|
+
async deleteStorageBucket(bucketId) {
|
|
1329
|
+
return this.storage.deleteBucket(bucketId);
|
|
1330
|
+
}
|
|
1331
|
+
async listStorageBucketFiles(bucketId) {
|
|
1332
|
+
return this.storage.listBucketFiles(bucketId);
|
|
1241
1333
|
}
|
|
1242
1334
|
// Functions shortcuts
|
|
1243
1335
|
async invokeFunction(id, data) {
|