@giaeulate/baas-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/package.json +1 -1
- package/src/client.ts +2 -9
- package/src/http-client.ts +16 -29
- package/src/index.ts +0 -1
- package/src/modules/backups.ts +1 -2
- package/src/modules/realtime.ts +1 -1
- package/src/realtime.ts +4 -4
- package/src/modules/projects.ts +0 -25
package/package.json
CHANGED
package/src/client.ts
CHANGED
|
@@ -12,7 +12,6 @@ import { RealtimeAction, RealtimeCallback } from './types';
|
|
|
12
12
|
// Module imports
|
|
13
13
|
import { createAuthModule, type AuthModule } from './modules/auth';
|
|
14
14
|
import { createUsersModule, type UsersModule } from './modules/users';
|
|
15
|
-
import { createProjectsModule, type ProjectsModule } from './modules/projects';
|
|
16
15
|
import { createDatabaseModule, type DatabaseModule } from './modules/database';
|
|
17
16
|
import { createStorageModule, type StorageModule } from './modules/storage';
|
|
18
17
|
import { createBackupsModule, type BackupsModule } from './modules/backups';
|
|
@@ -52,7 +51,6 @@ export class BaasClient extends HttpClient {
|
|
|
52
51
|
// Feature modules
|
|
53
52
|
public readonly auth: AuthModule;
|
|
54
53
|
public readonly users: UsersModule;
|
|
55
|
-
public readonly projects: ProjectsModule;
|
|
56
54
|
public readonly database: DatabaseModule;
|
|
57
55
|
public readonly storage: StorageModule;
|
|
58
56
|
public readonly backups: BackupsModule;
|
|
@@ -71,13 +69,12 @@ export class BaasClient extends HttpClient {
|
|
|
71
69
|
public readonly realtime: RealtimeModule;
|
|
72
70
|
public readonly apiKeys: ApiKeysModule;
|
|
73
71
|
|
|
74
|
-
constructor(url?: string,
|
|
75
|
-
super(url,
|
|
72
|
+
constructor(url?: string, apiKey?: string) {
|
|
73
|
+
super(url, apiKey);
|
|
76
74
|
|
|
77
75
|
// Initialize all modules with this client instance
|
|
78
76
|
this.auth = createAuthModule(this);
|
|
79
77
|
this.users = createUsersModule(this);
|
|
80
|
-
this.projects = createProjectsModule(this);
|
|
81
78
|
this.database = createDatabaseModule(this);
|
|
82
79
|
this.storage = createStorageModule(this);
|
|
83
80
|
this.backups = createBackupsModule(this);
|
|
@@ -130,10 +127,6 @@ export class BaasClient extends HttpClient {
|
|
|
130
127
|
async updateUser(id: string, updates: any) { return this.users.update(id, updates); }
|
|
131
128
|
async deleteUser(id: string) { return this.users.delete(id); }
|
|
132
129
|
|
|
133
|
-
// Projects shortcuts
|
|
134
|
-
async listProjects() { return this.projects.list(); }
|
|
135
|
-
async createProject(name: string) { return this.projects.create(name); }
|
|
136
|
-
|
|
137
130
|
// Database shortcuts
|
|
138
131
|
async raw(query: string) { return this.database.raw(query); }
|
|
139
132
|
async getSchemas(options?: any) { return this.database.getSchemas(options); }
|
package/src/http-client.ts
CHANGED
|
@@ -7,20 +7,16 @@ import type { RequestOptions } from './types';
|
|
|
7
7
|
|
|
8
8
|
export class HttpClient {
|
|
9
9
|
public url: string;
|
|
10
|
-
public
|
|
10
|
+
public apiKey: string = '';
|
|
11
11
|
public token: string | null = null;
|
|
12
12
|
|
|
13
|
-
constructor(url?: string,
|
|
13
|
+
constructor(url?: string, apiKey?: string) {
|
|
14
14
|
let baseUrl = url || (typeof window !== 'undefined' ? window.location.origin : 'http://localhost:8080');
|
|
15
15
|
this.url = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
|
|
16
|
+
|
|
17
|
+
if (apiKey) this.apiKey = apiKey;
|
|
18
|
+
|
|
19
19
|
this.token = this.cleanValue(localStorage.getItem('baas_token'));
|
|
20
|
-
const storedProject = this.cleanValue(localStorage.getItem('baas_project_id'));
|
|
21
|
-
if (storedProject && !this.projectID) {
|
|
22
|
-
this.projectID = storedProject;
|
|
23
|
-
}
|
|
24
20
|
}
|
|
25
21
|
|
|
26
22
|
protected cleanValue(val: string | null): string | null {
|
|
@@ -32,13 +28,12 @@ export class HttpClient {
|
|
|
32
28
|
* Public header generator for adapters
|
|
33
29
|
*/
|
|
34
30
|
public getHeaders(contentType: string = 'application/json') {
|
|
35
|
-
const dynamicProject = this.getDynamicProjectID();
|
|
36
31
|
const dynamicToken = this.getDynamicToken();
|
|
37
|
-
|
|
32
|
+
|
|
38
33
|
const headers: Record<string, string> = {
|
|
39
|
-
'
|
|
34
|
+
'apikey': this.apiKey,
|
|
40
35
|
};
|
|
41
|
-
|
|
36
|
+
|
|
42
37
|
if (contentType) {
|
|
43
38
|
headers['Content-Type'] = contentType;
|
|
44
39
|
}
|
|
@@ -53,39 +48,31 @@ export class HttpClient {
|
|
|
53
48
|
return this.cleanValue(this.token || localStorage.getItem('baas_token'));
|
|
54
49
|
}
|
|
55
50
|
|
|
56
|
-
public getDynamicProjectID() {
|
|
57
|
-
const stored = this.cleanValue(localStorage.getItem('baas_project_id'));
|
|
58
|
-
if (this.projectID && this.projectID !== 'default' && this.projectID !== '') {
|
|
59
|
-
return this.projectID;
|
|
60
|
-
}
|
|
61
|
-
return stored || 'default';
|
|
62
|
-
}
|
|
63
|
-
|
|
64
51
|
/**
|
|
65
52
|
* Core HTTP request method - DRY principle
|
|
66
53
|
*/
|
|
67
54
|
protected async request<T = any>(endpoint: string, options: RequestOptions = {}): Promise<T> {
|
|
68
55
|
const { method = 'GET', body, headers: customHeaders, skipAuth = false } = options;
|
|
69
|
-
|
|
56
|
+
|
|
70
57
|
const headers = { ...this.getHeaders(), ...customHeaders };
|
|
71
|
-
|
|
58
|
+
|
|
72
59
|
const fetchOptions: RequestInit = {
|
|
73
60
|
method,
|
|
74
61
|
headers,
|
|
75
62
|
};
|
|
76
|
-
|
|
63
|
+
|
|
77
64
|
if (body !== undefined) {
|
|
78
65
|
fetchOptions.body = JSON.stringify(body);
|
|
79
66
|
}
|
|
80
|
-
|
|
67
|
+
|
|
81
68
|
const res = await fetch(`${this.url}${endpoint}`, fetchOptions);
|
|
82
|
-
|
|
69
|
+
|
|
83
70
|
if (res.status === 204) {
|
|
84
71
|
return { success: true } as T;
|
|
85
72
|
}
|
|
86
|
-
|
|
73
|
+
|
|
87
74
|
const data = await res.json().catch(() => null);
|
|
88
|
-
|
|
75
|
+
|
|
89
76
|
if (!res.ok) {
|
|
90
77
|
if (res.status === 401 && !skipAuth) {
|
|
91
78
|
this.forceLogout();
|
|
@@ -95,7 +82,7 @@ export class HttpClient {
|
|
|
95
82
|
}
|
|
96
83
|
return { error: data?.error || `Request failed: ${res.status}`, ...data } as T;
|
|
97
84
|
}
|
|
98
|
-
|
|
85
|
+
|
|
99
86
|
return data;
|
|
100
87
|
}
|
|
101
88
|
|
package/src/index.ts
CHANGED
|
@@ -23,7 +23,6 @@ export type { QueryFilter, HttpMethod, RequestOptions, PaginationOptions, ApiRes
|
|
|
23
23
|
// Module types (for advanced usage)
|
|
24
24
|
export type { AuthModule } from './modules/auth';
|
|
25
25
|
export type { UsersModule } from './modules/users';
|
|
26
|
-
export type { ProjectsModule } from './modules/projects';
|
|
27
26
|
export type { DatabaseModule } from './modules/database';
|
|
28
27
|
export type { StorageModule } from './modules/storage';
|
|
29
28
|
export type { BackupsModule } from './modules/backups';
|
package/src/modules/backups.ts
CHANGED
|
@@ -43,8 +43,7 @@ export function createBackupsModule(client: HttpClient): BackupsModule {
|
|
|
43
43
|
|
|
44
44
|
getDownloadUrl(backupId: string) {
|
|
45
45
|
const token = client.getDynamicToken();
|
|
46
|
-
|
|
47
|
-
return `${client.url}/api/backups/${backupId}/download?access_token=${token}&project_id=${projectId}`;
|
|
46
|
+
return `${client.url}/api/backups/${backupId}/download?access_token=${token}&apikey=${client.apiKey}`;
|
|
48
47
|
},
|
|
49
48
|
|
|
50
49
|
async listTables() {
|
package/src/modules/realtime.ts
CHANGED
|
@@ -17,7 +17,7 @@ export interface RealtimeModule {
|
|
|
17
17
|
export function createRealtimeModule(client: HttpClient): RealtimeModule {
|
|
18
18
|
const service = new RealtimeService(
|
|
19
19
|
client.url,
|
|
20
|
-
client.
|
|
20
|
+
client.apiKey,
|
|
21
21
|
() => client.getDynamicToken(),
|
|
22
22
|
typeof WebSocket !== 'undefined' ? WebSocket : undefined
|
|
23
23
|
);
|
package/src/realtime.ts
CHANGED
|
@@ -13,7 +13,7 @@ export class RealtimeService {
|
|
|
13
13
|
|
|
14
14
|
constructor(
|
|
15
15
|
private url: string,
|
|
16
|
-
private
|
|
16
|
+
private apiKey: string,
|
|
17
17
|
private tokenProvider: () => string | null,
|
|
18
18
|
private wsConstructor: any = typeof WebSocket !== 'undefined' ? WebSocket : null
|
|
19
19
|
) {}
|
|
@@ -26,14 +26,14 @@ export class RealtimeService {
|
|
|
26
26
|
|
|
27
27
|
return new Promise((resolve, reject) => {
|
|
28
28
|
const token = this.tokenProvider();
|
|
29
|
-
if (!this.
|
|
30
|
-
reject(new Error('Missing
|
|
29
|
+
if (!this.apiKey || !token) {
|
|
30
|
+
reject(new Error('Missing API key or authentication token for Realtime connection'));
|
|
31
31
|
return;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
// Convert http/https to ws/wss
|
|
35
35
|
const wsUrl = this.url.replace(/^http/, 'ws') + '/ws';
|
|
36
|
-
const fullUrl = `${wsUrl}?
|
|
36
|
+
const fullUrl = `${wsUrl}?apikey=${this.apiKey}&token=${token}`;
|
|
37
37
|
|
|
38
38
|
if (!this.wsConstructor) {
|
|
39
39
|
reject(new Error('WebSocket constructor not provided or available in this environment'));
|
package/src/modules/projects.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Projects Module - Project management operations
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { HttpClient } from '../http-client';
|
|
6
|
-
|
|
7
|
-
export interface ProjectsModule {
|
|
8
|
-
list(): Promise<any>;
|
|
9
|
-
create(name: string): Promise<any>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function createProjectsModule(client: HttpClient): ProjectsModule {
|
|
13
|
-
const get = (endpoint: string) => (client as any).get(endpoint);
|
|
14
|
-
const post = (endpoint: string, body?: unknown) => (client as any).post(endpoint, body);
|
|
15
|
-
|
|
16
|
-
return {
|
|
17
|
-
async list() {
|
|
18
|
-
return get('/api/projects');
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
async create(name: string) {
|
|
22
|
-
return post('/api/projects', { name });
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
}
|