@baclet/sdk 0.2.3 → 0.2.4

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 CHANGED
@@ -27,6 +27,7 @@ Open the dashboard, choose a workspace, then go to **Projects → your project
27
27
  - **API URL** → use as `apiUrl`
28
28
  - **Publishable key** → use as `publishableKey`; safe for client code
29
29
  - **Secret key** → server-only; shown in full only when created or rotated
30
+ - **Storage bucket** → copy from **Storage** when the project has an active storage connection; use as `storageBucket`
30
31
 
31
32
  ---
32
33
 
@@ -45,7 +46,7 @@ export const baclet = initializeBaclet({
45
46
  export const auth = getAuth(baclet);
46
47
  ```
47
48
 
48
- `initializeBaclet()` is idempotent. Calling it again with the same name and credentials returns the existing client. Calling it with different credentials throws a `BacletError` with status `409`.
49
+ `initializeBaclet()` is idempotent. Calling it again with the same name, credentials, and storage bucket returns the existing client. Calling it with different settings throws a `BacletError` with status `409`.
49
50
 
50
51
  The client derives your project ID from the publishable key (`pb_<uuid>_...`), so there is no separate project ID to configure.
51
52
 
@@ -108,6 +109,7 @@ const baclet = createBacletClient({
108
109
  | --- | --- | --- | --- |
109
110
  | `apiUrl` | `string` | required | Base BACLET API URL. `https://` is added when omitted; a trailing `/v1` is removed automatically. |
110
111
  | `publishableKey` | `string` | required | Your project publishable key. Safe to use in browser bundles. |
112
+ | `storageBucket` | `string` | `undefined` | Default bucket for storage methods. Required unless a `bucket` is passed per call. |
111
113
  | `secretKey` | `string` | `undefined` | Optional server-side key. Validated when provided; omit it from browser code. |
112
114
  | `storage` | `StorageLike \| null` | `localStorage`, or memory | Custom key/value store. Pass `null` to force in-memory storage. |
113
115
  | `autoRefresh` | `boolean` | `true` | Refresh the session automatically when `getSession()` sees an expired access token. |
@@ -188,28 +190,37 @@ Pass `redirect: false` to `signInWithGoogle()` to receive the URL without naviga
188
190
 
189
191
  ## Storage
190
192
 
191
- Storage uses the same `BacletClient` instance. The project must have an active storage connection and a bucket configured in the dashboard.
193
+ Firebase shows uploaded objects under **Storage → Files**. BACLET uses the same model: the bucket is the container created during provisioning, and object keys are paths inside that bucket. The bucket is not part of the key.
194
+
195
+ Initialize the client with the bucket shown in the BACLET dashboard:
196
+
197
+ ```ts
198
+ import { initializeBaclet } from "@baclet/sdk";
199
+
200
+ const client = initializeBaclet({
201
+ apiUrl: import.meta.env.VITE_BACLET_API_URL,
202
+ publishableKey: import.meta.env.VITE_BACLET_PUBLISHABLE_KEY,
203
+ storageBucket: import.meta.env.VITE_BACLET_STORAGE_BUCKET,
204
+ });
205
+ ```
206
+
207
+ Then pass only object keys and prefixes to storage methods:
192
208
 
193
209
  ```ts
194
210
  const object = await client.storage.upload({
195
- bucket: "app-bucket",
196
211
  key: `users/${user.id}/avatar.png`,
197
212
  file: avatarFile,
198
213
  contentType: "image/png",
199
214
  visibility: "private",
200
215
  });
201
216
 
202
- const { url } = await client.storage.download({
203
- bucket: "app-bucket",
204
- key: object.key,
205
- expiresIn: 900,
206
- });
217
+ const { url } = await client.storage.download({ key: object.key, expiresIn: 900 });
207
218
 
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 });
219
+ const objects = await client.storage.list({ prefix: `users/${user.id}/` });
220
+ await client.storage.remove({ key: object.key });
210
221
  ```
211
222
 
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.
223
+ `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. You can pass `bucket` to an individual storage call to override the configured bucket.
213
224
 
214
225
  Available methods: `upload()`, `download()`, `signedURL()`, `info()`, `list()`, `remove()`, `copy()`, and `move()`.
215
226
 
package/dist/index.d.ts CHANGED
@@ -43,6 +43,7 @@ export interface StorageLike {
43
43
  export interface BacletConfig {
44
44
  apiUrl: string;
45
45
  publishableKey: string;
46
+ storageBucket?: string;
46
47
  secretKey?: string;
47
48
  }
48
49
  export interface BacletClientOptions extends BacletConfig {
@@ -91,7 +92,7 @@ export interface StorageDownload {
91
92
  expires_at: string;
92
93
  }
93
94
  export interface StorageUploadInput {
94
- bucket: string;
95
+ bucket?: string;
95
96
  key: string;
96
97
  file: Blob;
97
98
  fileName?: string;
@@ -100,16 +101,16 @@ export interface StorageUploadInput {
100
101
  sizeBytes?: number;
101
102
  }
102
103
  export interface StorageObjectInput {
103
- bucket: string;
104
+ bucket?: string;
104
105
  key: string;
105
106
  }
106
107
  export interface StorageListInput {
107
- bucket: string;
108
+ bucket?: string;
108
109
  prefix?: string;
109
110
  limit?: number;
110
111
  }
111
112
  export interface StorageMutationInput {
112
- bucket: string;
113
+ bucket?: string;
113
114
  from: string;
114
115
  to: string;
115
116
  }
@@ -165,6 +166,7 @@ export declare class BacletError extends Error {
165
166
  export declare class BacletClient {
166
167
  readonly apiUrl: string;
167
168
  readonly publishableKey: string;
169
+ readonly storageBucket?: string;
168
170
  readonly secretKey?: string;
169
171
  readonly projectID: string;
170
172
  private readonly tokenStorage;
package/dist/index.js CHANGED
@@ -100,6 +100,7 @@ export class BacletError extends Error {
100
100
  export class BacletClient {
101
101
  apiUrl;
102
102
  publishableKey;
103
+ storageBucket;
103
104
  secretKey;
104
105
  projectID;
105
106
  tokenStorage;
@@ -117,6 +118,7 @@ export class BacletClient {
117
118
  validateSecretKey(options.secretKey);
118
119
  this.apiUrl = normalizeAPIURL(options.apiUrl);
119
120
  this.publishableKey = options.publishableKey;
121
+ this.storageBucket = options.storageBucket?.trim() || undefined;
120
122
  this.secretKey = options.secretKey;
121
123
  this.projectID = projectIDFromKey(options.publishableKey);
122
124
  this.tokenStorage = resolveStorage(options.storage);
@@ -160,8 +162,15 @@ export class BacletClient {
160
162
  }
161
163
  createStorageService() {
162
164
  const path = (suffix) => `/v1/projects/${this.projectID}/storage${suffix}`;
165
+ const bucketFor = (bucket) => {
166
+ const resolved = bucket?.trim() || this.storageBucket;
167
+ if (!resolved) {
168
+ throw new BacletError("A storage bucket is required. Set storageBucket when initializing BACLET or pass bucket to the storage call.", 400);
169
+ }
170
+ return resolved;
171
+ };
163
172
  const objectQuery = (input) => {
164
- const query = new URLSearchParams({ bucket: input.bucket, key: input.key });
173
+ const query = new URLSearchParams({ bucket: bucketFor(input.bucket), key: input.key });
165
174
  if (input.expiresIn)
166
175
  query.set("expires_in", String(input.expiresIn));
167
176
  return `?${query.toString()}`;
@@ -171,7 +180,7 @@ export class BacletClient {
171
180
  const upload = await this.storageRequest(path("/uploads"), {
172
181
  method: "POST",
173
182
  body: {
174
- bucket: input.bucket,
183
+ bucket: bucketFor(input.bucket),
175
184
  key: input.key,
176
185
  size_bytes: input.sizeBytes ?? input.file.size,
177
186
  content_type: input.contentType ?? input.file.type,
@@ -209,7 +218,7 @@ export class BacletClient {
209
218
  },
210
219
  info: (input) => this.storageRequest(path(`/objects/info${objectQuery(input)}`)),
211
220
  list: async (input) => {
212
- const query = new URLSearchParams({ bucket: input.bucket });
221
+ const query = new URLSearchParams({ bucket: bucketFor(input.bucket) });
213
222
  if (input.prefix)
214
223
  query.set("prefix", input.prefix);
215
224
  if (input.limit)
@@ -222,11 +231,11 @@ export class BacletClient {
222
231
  },
223
232
  copy: (input) => this.storageRequest(path("/objects/copy"), {
224
233
  method: "POST",
225
- body: { bucket: input.bucket, from: input.from, to: input.to },
234
+ body: { bucket: bucketFor(input.bucket), from: input.from, to: input.to },
226
235
  }),
227
236
  move: (input) => this.storageRequest(path("/objects/move"), {
228
237
  method: "POST",
229
- body: { bucket: input.bucket, from: input.from, to: input.to },
238
+ body: { bucket: bucketFor(input.bucket), from: input.from, to: input.to },
230
239
  }),
231
240
  };
232
241
  }
@@ -496,6 +505,7 @@ export function initializeBaclet(config, name = "DEFAULT") {
496
505
  if (existing) {
497
506
  if (existing.apiUrl !== normalizeAPIURL(config.apiUrl) ||
498
507
  existing.publishableKey !== config.publishableKey ||
508
+ existing.storageBucket !== config.storageBucket?.trim() ||
499
509
  existing.secretKey !== config.secretKey) {
500
510
  throw new BacletError(`BACLET app "${name}" already exists with different credentials.`, 409);
501
511
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baclet/sdk",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "BACLET users database SDK for browsers and servers",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",