@baclet/sdk 0.2.1 → 0.2.3
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/README.md +42 -0
- package/dist/index.d.ts +65 -1
- package/dist/index.js +91 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,6 +20,14 @@ npm install @baclet/sdk
|
|
|
20
20
|
|
|
21
21
|
Requires Node.js 18+ or a modern browser with `fetch` support.
|
|
22
22
|
|
|
23
|
+
### Where to get your keys
|
|
24
|
+
|
|
25
|
+
Open the dashboard, choose a workspace, then go to **Projects → your project → API Keys**. You can also use **API Keys** in the workspace sidebar and select **Manage keys** beside the project.
|
|
26
|
+
|
|
27
|
+
- **API URL** → use as `apiUrl`
|
|
28
|
+
- **Publishable key** → use as `publishableKey`; safe for client code
|
|
29
|
+
- **Secret key** → server-only; shown in full only when created or rotated
|
|
30
|
+
|
|
23
31
|
---
|
|
24
32
|
|
|
25
33
|
## Quickstart
|
|
@@ -178,6 +186,35 @@ Pass `redirect: false` to `signInWithGoogle()` to receive the URL without naviga
|
|
|
178
186
|
|
|
179
187
|
---
|
|
180
188
|
|
|
189
|
+
## Storage
|
|
190
|
+
|
|
191
|
+
Storage uses the same `BacletClient` instance. The project must have an active storage connection and a bucket configured in the dashboard.
|
|
192
|
+
|
|
193
|
+
```ts
|
|
194
|
+
const object = await client.storage.upload({
|
|
195
|
+
bucket: "app-bucket",
|
|
196
|
+
key: `users/${user.id}/avatar.png`,
|
|
197
|
+
file: avatarFile,
|
|
198
|
+
contentType: "image/png",
|
|
199
|
+
visibility: "private",
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const { url } = await client.storage.download({
|
|
203
|
+
bucket: "app-bucket",
|
|
204
|
+
key: object.key,
|
|
205
|
+
expiresIn: 900,
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const objects = await client.storage.list({ bucket: "app-bucket", prefix: `users/${user.id}/` });
|
|
209
|
+
await client.storage.remove({ bucket: "app-bucket", key: object.key });
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
`upload()` requests a short-lived provider URL from BACLET, sends the file bytes directly to the provider, then records the object metadata. File bytes never pass through the BACLET API.
|
|
213
|
+
|
|
214
|
+
Available methods: `upload()`, `download()`, `signedURL()`, `info()`, `list()`, `remove()`, `copy()`, and `move()`.
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
181
218
|
## Sessions and state
|
|
182
219
|
|
|
183
220
|
### Session methods
|
|
@@ -278,10 +315,15 @@ import type {
|
|
|
278
315
|
BacletSession,
|
|
279
316
|
BacletTokens,
|
|
280
317
|
BacletUser,
|
|
318
|
+
BacletStorage,
|
|
281
319
|
GoogleSignInOptions,
|
|
282
320
|
SignInInput,
|
|
283
321
|
SignUpInput,
|
|
322
|
+
StorageDownload,
|
|
284
323
|
StorageLike,
|
|
324
|
+
StorageObject,
|
|
325
|
+
StorageObjectInput,
|
|
326
|
+
StorageUploadInput,
|
|
285
327
|
} from "@baclet/sdk";
|
|
286
328
|
```
|
|
287
329
|
|
package/dist/index.d.ts
CHANGED
|
@@ -67,6 +67,66 @@ export type AuthStateListener = (session: BacletSession | null) => void;
|
|
|
67
67
|
type MessageResponse = {
|
|
68
68
|
message: string;
|
|
69
69
|
};
|
|
70
|
+
export interface StorageObject {
|
|
71
|
+
key: string;
|
|
72
|
+
size_bytes: number;
|
|
73
|
+
content_type?: string;
|
|
74
|
+
etag?: string;
|
|
75
|
+
visibility: "public" | "private";
|
|
76
|
+
owner_user_id?: string;
|
|
77
|
+
}
|
|
78
|
+
export interface StorageUploadResult extends StorageObject {
|
|
79
|
+
upload_url: string;
|
|
80
|
+
method: string;
|
|
81
|
+
headers?: Record<string, string>;
|
|
82
|
+
fields?: Record<string, string>;
|
|
83
|
+
token: string;
|
|
84
|
+
expires_at: string;
|
|
85
|
+
bucket: string;
|
|
86
|
+
}
|
|
87
|
+
export interface StorageDownload {
|
|
88
|
+
url: string;
|
|
89
|
+
method: string;
|
|
90
|
+
headers?: Record<string, string>;
|
|
91
|
+
expires_at: string;
|
|
92
|
+
}
|
|
93
|
+
export interface StorageUploadInput {
|
|
94
|
+
bucket: string;
|
|
95
|
+
key: string;
|
|
96
|
+
file: Blob;
|
|
97
|
+
fileName?: string;
|
|
98
|
+
contentType?: string;
|
|
99
|
+
visibility?: "public" | "private";
|
|
100
|
+
sizeBytes?: number;
|
|
101
|
+
}
|
|
102
|
+
export interface StorageObjectInput {
|
|
103
|
+
bucket: string;
|
|
104
|
+
key: string;
|
|
105
|
+
}
|
|
106
|
+
export interface StorageListInput {
|
|
107
|
+
bucket: string;
|
|
108
|
+
prefix?: string;
|
|
109
|
+
limit?: number;
|
|
110
|
+
}
|
|
111
|
+
export interface StorageMutationInput {
|
|
112
|
+
bucket: string;
|
|
113
|
+
from: string;
|
|
114
|
+
to: string;
|
|
115
|
+
}
|
|
116
|
+
export interface BacletStorage {
|
|
117
|
+
upload(input: StorageUploadInput): Promise<StorageObject>;
|
|
118
|
+
download(input: StorageObjectInput & {
|
|
119
|
+
expiresIn?: number;
|
|
120
|
+
}): Promise<StorageDownload>;
|
|
121
|
+
signedURL(input: StorageObjectInput & {
|
|
122
|
+
expiresIn?: number;
|
|
123
|
+
}): Promise<string>;
|
|
124
|
+
info(input: StorageObjectInput): Promise<StorageObject>;
|
|
125
|
+
list(input: StorageListInput): Promise<StorageObject[]>;
|
|
126
|
+
remove(input: StorageObjectInput): Promise<void>;
|
|
127
|
+
copy(input: StorageMutationInput): Promise<StorageObject>;
|
|
128
|
+
move(input: StorageMutationInput): Promise<StorageObject>;
|
|
129
|
+
}
|
|
70
130
|
export interface BacletAuth {
|
|
71
131
|
capabilities(): Promise<AuthCapabilities>;
|
|
72
132
|
signUp(input: SignUpInput): Promise<BacletSession>;
|
|
@@ -107,16 +167,19 @@ export declare class BacletClient {
|
|
|
107
167
|
readonly publishableKey: string;
|
|
108
168
|
readonly secretKey?: string;
|
|
109
169
|
readonly projectID: string;
|
|
110
|
-
private readonly
|
|
170
|
+
private readonly tokenStorage;
|
|
111
171
|
private readonly fetchImpl;
|
|
112
172
|
private readonly autoRefresh;
|
|
113
173
|
private readonly tokensKey;
|
|
114
174
|
private readonly userKey;
|
|
115
175
|
private readonly listeners;
|
|
116
176
|
private readonly authService;
|
|
177
|
+
private readonly storageService;
|
|
117
178
|
constructor(options: BacletClientOptions);
|
|
118
179
|
get auth(): BacletAuth;
|
|
180
|
+
get storage(): BacletStorage;
|
|
119
181
|
private createAuthService;
|
|
182
|
+
private createStorageService;
|
|
120
183
|
capabilities(): Promise<AuthCapabilities>;
|
|
121
184
|
signUp(input: SignUpInput): Promise<BacletSession>;
|
|
122
185
|
signIn(input: SignInInput): Promise<BacletSession>;
|
|
@@ -137,6 +200,7 @@ export declare class BacletClient {
|
|
|
137
200
|
onAuthStateChanged(listener: AuthStateListener): () => void;
|
|
138
201
|
private authPath;
|
|
139
202
|
private currentUser;
|
|
203
|
+
private storageRequest;
|
|
140
204
|
private request;
|
|
141
205
|
private throwResponseError;
|
|
142
206
|
private readTokens;
|
package/dist/index.js
CHANGED
|
@@ -102,13 +102,14 @@ export class BacletClient {
|
|
|
102
102
|
publishableKey;
|
|
103
103
|
secretKey;
|
|
104
104
|
projectID;
|
|
105
|
-
|
|
105
|
+
tokenStorage;
|
|
106
106
|
fetchImpl;
|
|
107
107
|
autoRefresh;
|
|
108
108
|
tokensKey;
|
|
109
109
|
userKey;
|
|
110
110
|
listeners = new Set();
|
|
111
111
|
authService;
|
|
112
|
+
storageService;
|
|
112
113
|
constructor(options) {
|
|
113
114
|
if (!options.publishableKey) {
|
|
114
115
|
throw new BacletError("A BACLET publishable key is required.", 400);
|
|
@@ -118,8 +119,8 @@ export class BacletClient {
|
|
|
118
119
|
this.publishableKey = options.publishableKey;
|
|
119
120
|
this.secretKey = options.secretKey;
|
|
120
121
|
this.projectID = projectIDFromKey(options.publishableKey);
|
|
121
|
-
this.
|
|
122
|
-
this.fetchImpl = options.fetch ?? globalThis.fetch;
|
|
122
|
+
this.tokenStorage = resolveStorage(options.storage);
|
|
123
|
+
this.fetchImpl = (options.fetch ?? globalThis.fetch).bind(globalThis);
|
|
123
124
|
if (typeof this.fetchImpl !== "function") {
|
|
124
125
|
throw new BacletError("This runtime does not provide fetch; pass options.fetch.", 500);
|
|
125
126
|
}
|
|
@@ -127,10 +128,14 @@ export class BacletClient {
|
|
|
127
128
|
this.tokensKey = `baclet:${this.projectID}:tokens`;
|
|
128
129
|
this.userKey = `baclet:${this.projectID}:user`;
|
|
129
130
|
this.authService = this.createAuthService();
|
|
131
|
+
this.storageService = this.createStorageService();
|
|
130
132
|
}
|
|
131
133
|
get auth() {
|
|
132
134
|
return this.authService;
|
|
133
135
|
}
|
|
136
|
+
get storage() {
|
|
137
|
+
return this.storageService;
|
|
138
|
+
}
|
|
134
139
|
createAuthService() {
|
|
135
140
|
return {
|
|
136
141
|
capabilities: () => this.capabilities(),
|
|
@@ -153,6 +158,78 @@ export class BacletClient {
|
|
|
153
158
|
onAuthStateChanged: (listener) => this.onAuthStateChanged(listener),
|
|
154
159
|
};
|
|
155
160
|
}
|
|
161
|
+
createStorageService() {
|
|
162
|
+
const path = (suffix) => `/v1/projects/${this.projectID}/storage${suffix}`;
|
|
163
|
+
const objectQuery = (input) => {
|
|
164
|
+
const query = new URLSearchParams({ bucket: input.bucket, key: input.key });
|
|
165
|
+
if (input.expiresIn)
|
|
166
|
+
query.set("expires_in", String(input.expiresIn));
|
|
167
|
+
return `?${query.toString()}`;
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
upload: async (input) => {
|
|
171
|
+
const upload = await this.storageRequest(path("/uploads"), {
|
|
172
|
+
method: "POST",
|
|
173
|
+
body: {
|
|
174
|
+
bucket: input.bucket,
|
|
175
|
+
key: input.key,
|
|
176
|
+
size_bytes: input.sizeBytes ?? input.file.size,
|
|
177
|
+
content_type: input.contentType ?? input.file.type,
|
|
178
|
+
visibility: input.visibility ?? "private",
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
let response;
|
|
182
|
+
if (upload.fields) {
|
|
183
|
+
const form = new FormData();
|
|
184
|
+
for (const [key, value] of Object.entries(upload.fields))
|
|
185
|
+
form.append(key, value);
|
|
186
|
+
form.append("file", input.file, input.fileName ?? "upload.bin");
|
|
187
|
+
response = await this.fetchImpl(upload.upload_url, { method: upload.method, body: form });
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
const headers = { ...(upload.headers ?? {}) };
|
|
191
|
+
response = await this.fetchImpl(upload.upload_url, {
|
|
192
|
+
method: upload.method,
|
|
193
|
+
headers,
|
|
194
|
+
body: input.file,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
if (!response.ok)
|
|
198
|
+
await this.throwResponseError(response);
|
|
199
|
+
const object = await this.storageRequest(path("/uploads/complete"), {
|
|
200
|
+
method: "POST",
|
|
201
|
+
body: { token: upload.token },
|
|
202
|
+
});
|
|
203
|
+
return object;
|
|
204
|
+
},
|
|
205
|
+
download: (input) => this.storageRequest(path(`/objects${objectQuery(input)}`)),
|
|
206
|
+
signedURL: async (input) => {
|
|
207
|
+
const result = await this.storageRequest(path(`/objects${objectQuery(input)}`));
|
|
208
|
+
return result.url;
|
|
209
|
+
},
|
|
210
|
+
info: (input) => this.storageRequest(path(`/objects/info${objectQuery(input)}`)),
|
|
211
|
+
list: async (input) => {
|
|
212
|
+
const query = new URLSearchParams({ bucket: input.bucket });
|
|
213
|
+
if (input.prefix)
|
|
214
|
+
query.set("prefix", input.prefix);
|
|
215
|
+
if (input.limit)
|
|
216
|
+
query.set("limit", String(input.limit));
|
|
217
|
+
const result = await this.storageRequest(path(`/objects/list?${query.toString()}`));
|
|
218
|
+
return result.objects;
|
|
219
|
+
},
|
|
220
|
+
remove: async (input) => {
|
|
221
|
+
await this.storageRequest(path(`/objects${objectQuery(input)}`), { method: "DELETE" });
|
|
222
|
+
},
|
|
223
|
+
copy: (input) => this.storageRequest(path("/objects/copy"), {
|
|
224
|
+
method: "POST",
|
|
225
|
+
body: { bucket: input.bucket, from: input.from, to: input.to },
|
|
226
|
+
}),
|
|
227
|
+
move: (input) => this.storageRequest(path("/objects/move"), {
|
|
228
|
+
method: "POST",
|
|
229
|
+
body: { bucket: input.bucket, from: input.from, to: input.to },
|
|
230
|
+
}),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
156
233
|
capabilities() {
|
|
157
234
|
return this.request(`/v1/projects/${this.projectID}/auth/capabilities`);
|
|
158
235
|
}
|
|
@@ -219,7 +296,7 @@ export class BacletClient {
|
|
|
219
296
|
this.clearSession();
|
|
220
297
|
throw new BacletError("OAuth response did not include tokens.", 400);
|
|
221
298
|
}
|
|
222
|
-
this.
|
|
299
|
+
this.tokenStorage.setItem(this.tokensKey, JSON.stringify(tokens));
|
|
223
300
|
const session = await this.getSession();
|
|
224
301
|
if (!session) {
|
|
225
302
|
throw new BacletError("OAuth sign-in could not load the user.", 401);
|
|
@@ -333,6 +410,10 @@ export class BacletClient {
|
|
|
333
410
|
accessToken,
|
|
334
411
|
}).then((result) => result.user);
|
|
335
412
|
}
|
|
413
|
+
storageRequest(path, options = {}) {
|
|
414
|
+
const accessToken = options.accessToken ?? this.readTokens()?.access_token;
|
|
415
|
+
return this.request(path, { ...options, accessToken });
|
|
416
|
+
}
|
|
336
417
|
async request(path, options = {}) {
|
|
337
418
|
const headers = {
|
|
338
419
|
Accept: "application/json",
|
|
@@ -367,22 +448,22 @@ export class BacletClient {
|
|
|
367
448
|
throw new BacletError(message, response.status, payload);
|
|
368
449
|
}
|
|
369
450
|
readTokens() {
|
|
370
|
-
const tokens = parseTokens(this.
|
|
451
|
+
const tokens = parseTokens(this.tokenStorage.getItem(this.tokensKey));
|
|
371
452
|
if (!tokens)
|
|
372
|
-
this.
|
|
453
|
+
this.tokenStorage.removeItem(this.tokensKey);
|
|
373
454
|
return tokens;
|
|
374
455
|
}
|
|
375
456
|
writeSession(session) {
|
|
376
|
-
this.
|
|
377
|
-
this.
|
|
457
|
+
this.tokenStorage.setItem(this.tokensKey, JSON.stringify(session.tokens));
|
|
458
|
+
this.tokenStorage.setItem(this.userKey, JSON.stringify(session.user));
|
|
378
459
|
this.emit(session);
|
|
379
460
|
}
|
|
380
461
|
applySession(session) {
|
|
381
462
|
this.writeSession(session);
|
|
382
463
|
}
|
|
383
464
|
clearSession() {
|
|
384
|
-
this.
|
|
385
|
-
this.
|
|
465
|
+
this.tokenStorage.removeItem(this.tokensKey);
|
|
466
|
+
this.tokenStorage.removeItem(this.userKey);
|
|
386
467
|
this.emit(null);
|
|
387
468
|
}
|
|
388
469
|
emit(session) {
|