@gigadrive/sdk 0.7.28 → 0.8.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/dist/index.js CHANGED
@@ -43,6 +43,7 @@ __export(src_exports, {
43
43
  ApplicationStorageResource: () => ApplicationStorageResource,
44
44
  ApplicationsResource: () => ApplicationsResource,
45
45
  AuthenticationError: () => AuthenticationError,
46
+ ConfigurationError: () => ConfigurationError,
46
47
  DeploymentsResource: () => DeploymentsResource,
47
48
  GigadriveClient: () => GigadriveClient,
48
49
  GigadriveError: () => GigadriveError,
@@ -55,6 +56,7 @@ __export(src_exports, {
55
56
  StickySessionsResource: () => StickySessionsResource,
56
57
  StorageBucketsResource: () => StorageBucketsResource,
57
58
  StorageObjectsResource: () => StorageObjectsResource,
59
+ StorageTrashResource: () => StorageTrashResource,
58
60
  StorageUploadSessionsResource: () => StorageUploadSessionsResource,
59
61
  UploadError: () => UploadError,
60
62
  UploadSessionExpiredError: () => UploadSessionExpiredError,
@@ -77,6 +79,12 @@ var AuthenticationError = class extends GigadriveError {
77
79
  this.name = "AuthenticationError";
78
80
  }
79
81
  };
82
+ var ConfigurationError = class extends GigadriveError {
83
+ constructor(message) {
84
+ super(message);
85
+ this.name = "ConfigurationError";
86
+ }
87
+ };
80
88
  var ApiError = class extends GigadriveError {
81
89
  /** The HTTP status code of the failed response (e.g. 404, 500). */
82
90
  status;
@@ -1078,11 +1086,15 @@ var sizeOf = (data) => {
1078
1086
  if (data instanceof ArrayBuffer) return data.byteLength;
1079
1087
  throw new Error("Cannot determine upload size. Pass a File, Blob, Buffer, Uint8Array, or ArrayBuffer.");
1080
1088
  };
1081
- var toTusFile = (data) => {
1082
- if (data instanceof Uint8Array) return isNode() ? data : new Blob([data]);
1083
- if (typeof Blob !== "undefined" && data instanceof Blob) return data;
1084
- const bytes = new Uint8Array(data);
1085
- return isNode() ? bytes : new Blob([bytes]);
1089
+ var toTusFile = async (data) => {
1090
+ if (!isNode()) {
1091
+ if (typeof Blob !== "undefined" && data instanceof Blob) return data;
1092
+ const bytes2 = data instanceof Uint8Array ? data : new Uint8Array(data);
1093
+ return new Blob([bytes2]);
1094
+ }
1095
+ const { Buffer: Buffer2 } = await import("buffer");
1096
+ const bytes = typeof Blob !== "undefined" && data instanceof Blob ? new Uint8Array(await data.arrayBuffer()) : data instanceof Uint8Array ? data : new Uint8Array(data);
1097
+ return Buffer2.isBuffer(data) ? data : Buffer2.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1086
1098
  };
1087
1099
  var buildChecksums = (input, sha256) => {
1088
1100
  const checksums = { sha256 };
@@ -1103,7 +1115,7 @@ var resolveUploadSource = async (input, options = {}) => {
1103
1115
  const size = input.contentLength ?? sizeOf(input.data);
1104
1116
  const sha256 = input.checksumSha256 ?? (hash ? (await computeChecksums(await toBytes(input.data))).sha256 : "");
1105
1117
  return {
1106
- tusFile: toTusFile(input.data),
1118
+ tusFile: await toTusFile(input.data),
1107
1119
  size,
1108
1120
  contentType,
1109
1121
  checksums: buildChecksums(input, sha256),
@@ -1205,230 +1217,283 @@ var toUploadError = (error) => {
1205
1217
  throw new UploadError(error instanceof Error ? error.message : "The upload failed.", error);
1206
1218
  };
1207
1219
 
1220
+ // src/resources/storage-context.ts
1221
+ var resolveStorageApplicationId = (defaultApplicationId, explicitApplicationId) => {
1222
+ const applicationId = explicitApplicationId ?? defaultApplicationId;
1223
+ if (applicationId) return applicationId;
1224
+ throw new ConfigurationError(
1225
+ "No application context is configured. Pass an application ID to this call, set GigadriveClientConfig.applicationId, or run inside a Gigadrive deployment with GIGADRIVE_APPLICATION_ID."
1226
+ );
1227
+ };
1228
+ var resolveStorageBucketReference = (input) => {
1229
+ const hasBucket = typeof input.bucket === "string";
1230
+ const hasBucketId = typeof input.bucketId === "string";
1231
+ if (hasBucket === hasBucketId) {
1232
+ throw new ConfigurationError(
1233
+ "Provide exactly one storage bucket reference using `bucket` or deprecated `bucketId`."
1234
+ );
1235
+ }
1236
+ return hasBucket ? input.bucket : input.bucketId;
1237
+ };
1238
+ var encodeStorageBucketReference = (bucketRef) => encodeURIComponent(bucketRef);
1239
+
1208
1240
  // src/resources/storage-buckets.ts
1209
1241
  var StorageBucketsResource = class extends BaseResource {
1210
- /**
1211
- * List all storage buckets for an application.
1212
- *
1213
- * @param applicationId - The application ID (UUID).
1214
- * @param query - Optional pagination parameters.
1215
- * @returns A paginated list of storage buckets.
1216
- *
1217
- * @example
1218
- * ```ts
1219
- * const { items, total } = await client.applications.storage.buckets.list('app-id');
1220
- * console.log(`${total} buckets found`);
1221
- * ```
1222
- */
1223
- async list(applicationId, query) {
1242
+ constructor(httpClient, defaultApplicationId) {
1243
+ super(httpClient);
1244
+ this.defaultApplicationId = defaultApplicationId;
1245
+ }
1246
+ defaultApplicationId;
1247
+ async list(applicationIdOrQuery, legacyQuery) {
1248
+ const applicationId = resolveStorageApplicationId(
1249
+ this.defaultApplicationId,
1250
+ typeof applicationIdOrQuery === "string" ? applicationIdOrQuery : void 0
1251
+ );
1252
+ const query = typeof applicationIdOrQuery === "string" ? legacyQuery : applicationIdOrQuery;
1224
1253
  return this.httpClient.get(`/applications/${applicationId}/storage/buckets`, {
1225
1254
  query
1226
1255
  });
1227
1256
  }
1228
- /**
1229
- * Create a new storage bucket.
1230
- *
1231
- * @param applicationId - The application ID (UUID).
1232
- * @param data - Bucket name, environment, and optional slug/visibility.
1233
- * @returns The newly created bucket.
1234
- *
1235
- * @example
1236
- * ```ts
1237
- * const bucket = await client.applications.storage.buckets.create('app-id', {
1238
- * name: 'Assets',
1239
- * environmentId: 'env-id',
1240
- * visibility: 'public',
1241
- * });
1242
- * console.log(`Bucket CDN: https://${bucket.cdnHostname}`);
1243
- * ```
1244
- */
1245
- async create(applicationId, data) {
1257
+ async create(applicationIdOrData, legacyData) {
1258
+ const applicationId = resolveStorageApplicationId(
1259
+ this.defaultApplicationId,
1260
+ typeof applicationIdOrData === "string" ? applicationIdOrData : void 0
1261
+ );
1262
+ const data = typeof applicationIdOrData === "string" ? legacyData : applicationIdOrData;
1246
1263
  return this.httpClient.post(`/applications/${applicationId}/storage/buckets`, data);
1247
1264
  }
1248
- /**
1249
- * Get a storage bucket by ID.
1250
- *
1251
- * @param applicationId - The application ID (UUID).
1252
- * @param bucketId - The bucket ID (UUID).
1253
- * @returns The bucket details.
1254
- *
1255
- * @example
1256
- * ```ts
1257
- * const bucket = await client.applications.storage.buckets.get('app-id', 'bucket-id');
1258
- * console.log(`${bucket.name} (${bucket.visibility})`);
1259
- * ```
1260
- */
1261
- async get(applicationId, bucketId) {
1262
- return this.httpClient.get(`/applications/${applicationId}/storage/buckets/${bucketId}`);
1265
+ async get(applicationIdOrBucketRef, bucketRefOrOptions, legacyOptions) {
1266
+ const explicitApplication = typeof bucketRefOrOptions === "string";
1267
+ const applicationId = resolveStorageApplicationId(
1268
+ this.defaultApplicationId,
1269
+ explicitApplication ? applicationIdOrBucketRef : void 0
1270
+ );
1271
+ const bucketRef = explicitApplication ? bucketRefOrOptions : applicationIdOrBucketRef;
1272
+ const options = explicitApplication ? legacyOptions : bucketRefOrOptions;
1273
+ return this.httpClient.get(
1274
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}`,
1275
+ {
1276
+ query: { environment: options?.environment }
1277
+ }
1278
+ );
1263
1279
  }
1264
- /**
1265
- * Permanently delete a storage bucket and all its objects.
1266
- *
1267
- * @param applicationId - The application ID (UUID).
1268
- * @param bucketId - The bucket ID (UUID).
1269
- *
1270
- * @example
1271
- * ```ts
1272
- * await client.applications.storage.buckets.delete('app-id', 'bucket-id');
1273
- * ```
1274
- */
1275
- async delete(applicationId, bucketId) {
1276
- return this.httpClient.delete(`/applications/${applicationId}/storage/buckets/${bucketId}`);
1280
+ async delete(applicationIdOrBucketRef, bucketRefOrOptions, legacyOptions) {
1281
+ const explicitApplication = typeof bucketRefOrOptions === "string";
1282
+ const applicationId = resolveStorageApplicationId(
1283
+ this.defaultApplicationId,
1284
+ explicitApplication ? applicationIdOrBucketRef : void 0
1285
+ );
1286
+ const bucketRef = explicitApplication ? bucketRefOrOptions : applicationIdOrBucketRef;
1287
+ const options = explicitApplication ? legacyOptions : bucketRefOrOptions;
1288
+ return this.httpClient.delete(
1289
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}`,
1290
+ { query: { environment: options?.environment } }
1291
+ );
1277
1292
  }
1278
1293
  };
1279
1294
 
1280
1295
  // src/resources/storage-objects.ts
1281
1296
  var StorageObjectsResource = class extends BaseResource {
1282
- /**
1283
- * List objects in a storage bucket, optionally filtered by key prefix and
1284
- * grouped into virtual folders by a delimiter.
1285
- *
1286
- * @param applicationId - The application ID (UUID).
1287
- * @param bucketId - The bucket ID (UUID).
1288
- * @param query - Optional prefix/delimiter/cursor/limit parameters.
1289
- * @returns A page of objects plus any common (folder) prefixes and a `nextCursor`.
1290
- *
1291
- * @example
1292
- * ```ts
1293
- * // List the "images/" folder, one level deep
1294
- * const { items, commonPrefixes, nextCursor } = await client.applications.storage.objects.list(
1295
- * 'app-id', 'bucket-id', { prefix: 'images/', limit: 100 },
1296
- * );
1297
- * ```
1298
- */
1299
- async list(applicationId, bucketId, query) {
1300
- return this.httpClient.get(`/applications/${applicationId}/storage/buckets/${bucketId}/objects`, {
1301
- query
1302
- });
1297
+ constructor(httpClient, defaultApplicationId) {
1298
+ super(httpClient);
1299
+ this.defaultApplicationId = defaultApplicationId;
1300
+ }
1301
+ defaultApplicationId;
1302
+ async list(applicationIdOrBucketRef, bucketRefOrQuery, legacyQuery) {
1303
+ const explicitApplication = typeof bucketRefOrQuery === "string";
1304
+ const applicationId = resolveStorageApplicationId(
1305
+ this.defaultApplicationId,
1306
+ explicitApplication ? applicationIdOrBucketRef : void 0
1307
+ );
1308
+ const bucketRef = explicitApplication ? bucketRefOrQuery : applicationIdOrBucketRef;
1309
+ const query = explicitApplication ? legacyQuery : bucketRefOrQuery;
1310
+ return this.httpClient.get(
1311
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/objects`,
1312
+ {
1313
+ query
1314
+ }
1315
+ );
1303
1316
  }
1304
- /**
1305
- * Get metadata for a specific object by its object ID.
1306
- *
1307
- * @param applicationId - The application ID (UUID).
1308
- * @param bucketId - The bucket ID (UUID).
1309
- * @param objectId - The object ID (UUID) — not the object key. Use {@link getByKey} to resolve a key.
1310
- * @returns The object metadata.
1311
- *
1312
- * @example
1313
- * ```ts
1314
- * const obj = await client.applications.storage.objects.get('app-id', 'bucket-id', 'object-id');
1315
- * console.log(`${obj.key}: ${obj.contentLength} bytes, type: ${obj.contentType}`);
1316
- * ```
1317
- */
1318
- async get(applicationId, bucketId, objectId) {
1319
- return this.httpClient.get(`/applications/${applicationId}/storage/buckets/${bucketId}/objects/${objectId}`);
1317
+ async get(applicationIdOrBucketRef, bucketRefOrObjectId, objectIdOrOptions, legacyOptions) {
1318
+ const explicitApplication = typeof objectIdOrOptions === "string";
1319
+ const applicationId = resolveStorageApplicationId(
1320
+ this.defaultApplicationId,
1321
+ explicitApplication ? applicationIdOrBucketRef : void 0
1322
+ );
1323
+ const bucketRef = explicitApplication ? bucketRefOrObjectId : applicationIdOrBucketRef;
1324
+ const objectId = explicitApplication ? objectIdOrOptions : bucketRefOrObjectId;
1325
+ const options = explicitApplication ? legacyOptions : objectIdOrOptions;
1326
+ return this.httpClient.get(
1327
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/objects/${objectId}`,
1328
+ { query: { environment: options?.environment } }
1329
+ );
1320
1330
  }
1321
- /**
1322
- * Resolve an object by its key (path) instead of its ID. Convenience over
1323
- * {@link list} — lists with the key as the prefix and returns the exact match,
1324
- * paging through results until found, or `null` if no object with that key
1325
- * exists. (The API has no get-by-key endpoint.)
1326
- *
1327
- * @param applicationId - The application ID (UUID).
1328
- * @param bucketId - The bucket ID (UUID).
1329
- * @param key - The object key/path (e.g. `"images/photo.jpg"`).
1330
- * @returns The matching object, or `null` if not found.
1331
- */
1332
- async getByKey(applicationId, bucketId, key) {
1331
+ async getByKey(applicationIdOrBucketRef, bucketRefOrKey, keyOrOptions, legacyOptions) {
1332
+ const explicitApplication = typeof keyOrOptions === "string";
1333
+ const applicationId = resolveStorageApplicationId(
1334
+ this.defaultApplicationId,
1335
+ explicitApplication ? applicationIdOrBucketRef : void 0
1336
+ );
1337
+ const bucketRef = explicitApplication ? bucketRefOrKey : applicationIdOrBucketRef;
1338
+ const key = explicitApplication ? keyOrOptions : bucketRefOrKey;
1339
+ const options = explicitApplication ? legacyOptions : keyOrOptions;
1333
1340
  let cursor;
1334
1341
  do {
1335
- const page = await this.list(applicationId, bucketId, { prefix: key, delimiter: "", cursor });
1342
+ const page = await this.list(applicationId, bucketRef, {
1343
+ environment: options?.environment,
1344
+ prefix: key,
1345
+ cursor
1346
+ });
1336
1347
  const match = page.items.find((object) => object.key === key);
1337
1348
  if (match) return match;
1338
1349
  cursor = page.nextCursor;
1339
1350
  } while (cursor);
1340
1351
  return null;
1341
1352
  }
1342
- /**
1343
- * Permanently delete an object from a storage bucket.
1344
- *
1345
- * @param applicationId - The application ID (UUID).
1346
- * @param bucketId - The bucket ID (UUID).
1347
- * @param objectId - The object ID (UUID) — not the object key.
1348
- *
1349
- * @example
1350
- * ```ts
1351
- * await client.applications.storage.objects.delete('app-id', 'bucket-id', 'object-id');
1352
- * ```
1353
- */
1354
- async delete(applicationId, bucketId, objectId) {
1355
- return this.httpClient.delete(`/applications/${applicationId}/storage/buckets/${bucketId}/objects/${objectId}`);
1353
+ async delete(applicationIdOrBucketRef, bucketRefOrObjectId, objectIdOrOptions, legacyOptions) {
1354
+ const explicitApplication = typeof objectIdOrOptions === "string";
1355
+ const applicationId = resolveStorageApplicationId(
1356
+ this.defaultApplicationId,
1357
+ explicitApplication ? applicationIdOrBucketRef : void 0
1358
+ );
1359
+ const bucketRef = explicitApplication ? bucketRefOrObjectId : applicationIdOrBucketRef;
1360
+ const objectId = explicitApplication ? objectIdOrOptions : bucketRefOrObjectId;
1361
+ const options = explicitApplication ? legacyOptions : objectIdOrOptions;
1362
+ return this.httpClient.delete(
1363
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/objects/${objectId}`,
1364
+ { query: { environment: options?.environment } }
1365
+ );
1356
1366
  }
1357
- /**
1358
- * Get an access URL for a storage object. For objects in public buckets,
1359
- * returns a stable CDN URL. For private buckets, returns a time-limited
1360
- * signed URL.
1361
- *
1362
- * @param applicationId - The application ID (UUID).
1363
- * @param bucketId - The bucket ID (UUID).
1364
- * @param objectId - The object ID (UUID) — not the object key.
1365
- * @param options - Optional `expiresInSeconds` for signed URLs (60–86400).
1366
- * @returns The access URL and its type/expiry.
1367
- *
1368
- * @example
1369
- * ```ts
1370
- * const access = await client.applications.storage.objects.getAccessUrl('app-id', 'bucket-id', 'object-id', {
1371
- * expiresInSeconds: 3600,
1372
- * });
1373
- * if (access.accessType === 'signed') {
1374
- * console.log(`Signed URL expires at ${access.expiresAt}`);
1375
- * }
1376
- * console.log(`Download: ${access.url}`);
1377
- * ```
1378
- */
1379
- async getAccessUrl(applicationId, bucketId, objectId, options) {
1367
+ async getAccessUrl(applicationIdOrBucketRef, bucketRefOrObjectId, objectIdOrOptions, legacyOptions) {
1368
+ const explicitApplication = typeof objectIdOrOptions === "string";
1369
+ const applicationId = resolveStorageApplicationId(
1370
+ this.defaultApplicationId,
1371
+ explicitApplication ? applicationIdOrBucketRef : void 0
1372
+ );
1373
+ const bucketRef = explicitApplication ? bucketRefOrObjectId : applicationIdOrBucketRef;
1374
+ const objectId = explicitApplication ? objectIdOrOptions : bucketRefOrObjectId;
1375
+ const options = explicitApplication ? legacyOptions : objectIdOrOptions;
1380
1376
  return this.httpClient.get(
1381
- `/applications/${applicationId}/storage/buckets/${bucketId}/objects/${objectId}/access-url`,
1382
- { query: { expiresInSeconds: options?.expiresInSeconds } }
1377
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/objects/${objectId}/access-url`,
1378
+ { query: { environment: options?.environment, expiresInSeconds: options?.expiresInSeconds } }
1379
+ );
1380
+ }
1381
+ };
1382
+
1383
+ // src/resources/storage-trash.ts
1384
+ var StorageTrashResource = class extends BaseResource {
1385
+ constructor(httpClient, defaultApplicationId) {
1386
+ super(httpClient);
1387
+ this.defaultApplicationId = defaultApplicationId;
1388
+ }
1389
+ defaultApplicationId;
1390
+ async list(applicationIdOrBucketRef, bucketRefOrQuery, legacyQuery) {
1391
+ const explicitApplication = typeof bucketRefOrQuery === "string";
1392
+ const applicationId = resolveStorageApplicationId(
1393
+ this.defaultApplicationId,
1394
+ explicitApplication ? applicationIdOrBucketRef : void 0
1395
+ );
1396
+ const bucketRef = explicitApplication ? bucketRefOrQuery : applicationIdOrBucketRef;
1397
+ const query = explicitApplication ? legacyQuery : bucketRefOrQuery;
1398
+ return this.httpClient.get(
1399
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/trash`,
1400
+ { query }
1401
+ );
1402
+ }
1403
+ async restore(applicationIdOrBucketRef, bucketRefOrObjectId, objectIdOrOptions, legacyOptions) {
1404
+ const explicitApplication = typeof objectIdOrOptions === "string";
1405
+ const applicationId = resolveStorageApplicationId(
1406
+ this.defaultApplicationId,
1407
+ explicitApplication ? applicationIdOrBucketRef : void 0
1408
+ );
1409
+ const bucketRef = explicitApplication ? bucketRefOrObjectId : applicationIdOrBucketRef;
1410
+ const objectId = explicitApplication ? objectIdOrOptions : bucketRefOrObjectId;
1411
+ const options = explicitApplication ? legacyOptions : objectIdOrOptions;
1412
+ return this.httpClient.post(
1413
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/trash/${objectId}/restore`,
1414
+ void 0,
1415
+ { query: { environment: options?.environment } }
1416
+ );
1417
+ }
1418
+ async purge(applicationIdOrBucketRef, bucketRefOrObjectId, objectIdOrOptions, legacyOptions) {
1419
+ const explicitApplication = typeof objectIdOrOptions === "string";
1420
+ const applicationId = resolveStorageApplicationId(
1421
+ this.defaultApplicationId,
1422
+ explicitApplication ? applicationIdOrBucketRef : void 0
1423
+ );
1424
+ const bucketRef = explicitApplication ? bucketRefOrObjectId : applicationIdOrBucketRef;
1425
+ const objectId = explicitApplication ? objectIdOrOptions : bucketRefOrObjectId;
1426
+ const options = explicitApplication ? legacyOptions : objectIdOrOptions;
1427
+ return this.httpClient.delete(
1428
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/trash/${objectId}`,
1429
+ { query: { environment: options?.environment } }
1430
+ );
1431
+ }
1432
+ async empty(applicationIdOrBucketRef, bucketRefOrOptions, legacyOptions) {
1433
+ const explicitApplication = typeof bucketRefOrOptions === "string";
1434
+ const applicationId = resolveStorageApplicationId(
1435
+ this.defaultApplicationId,
1436
+ explicitApplication ? applicationIdOrBucketRef : void 0
1437
+ );
1438
+ const bucketRef = explicitApplication ? bucketRefOrOptions : applicationIdOrBucketRef;
1439
+ const options = explicitApplication ? legacyOptions : bucketRefOrOptions;
1440
+ return this.httpClient.delete(
1441
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/trash`,
1442
+ { query: { environment: options?.environment } }
1383
1443
  );
1384
1444
  }
1385
1445
  };
1386
1446
 
1387
1447
  // src/resources/storage-upload-sessions.ts
1388
1448
  var StorageUploadSessionsResource = class extends BaseResource {
1389
- constructor(httpClient, transport = tusUploadTransport) {
1449
+ constructor(httpClient, transport = tusUploadTransport, defaultApplicationId) {
1390
1450
  super(httpClient);
1391
1451
  this.transport = transport;
1452
+ this.defaultApplicationId = defaultApplicationId;
1392
1453
  }
1393
1454
  transport;
1394
- /**
1395
- * List upload sessions for a bucket.
1396
- *
1397
- * @param applicationId - The application ID (UUID).
1398
- * @param bucketId - The bucket ID (UUID).
1399
- * @param query - Optional pagination parameters.
1400
- * @returns A paginated list of upload sessions.
1401
- */
1402
- async list(applicationId, bucketId, query) {
1403
- return this.httpClient.get(`/applications/${applicationId}/storage/buckets/${bucketId}/uploads`, {
1404
- query
1405
- });
1455
+ defaultApplicationId;
1456
+ async list(applicationIdOrBucketRef, bucketRefOrQuery, legacyQuery) {
1457
+ const explicitApplication = typeof bucketRefOrQuery === "string";
1458
+ const applicationId = resolveStorageApplicationId(
1459
+ this.defaultApplicationId,
1460
+ explicitApplication ? applicationIdOrBucketRef : void 0
1461
+ );
1462
+ const bucketRef = explicitApplication ? bucketRefOrQuery : applicationIdOrBucketRef;
1463
+ const query = explicitApplication ? legacyQuery : bucketRefOrQuery;
1464
+ return this.httpClient.get(
1465
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/uploads`,
1466
+ { query }
1467
+ );
1406
1468
  }
1407
- /**
1408
- * Create an upload session. Returns the session metadata and a signed upload
1409
- * URL for sending file data. This is the low-level method — for most use
1410
- * cases prefer `client.applications.storage.upload()` which also computes the
1411
- * required SHA-256 checksum for you.
1412
- *
1413
- * @param applicationId - The application ID (UUID).
1414
- * @param bucketId - The bucket ID (UUID).
1415
- * @param data - Object key, content length, SHA-256 checksum, and optional content type / extra checksums.
1416
- * @returns The session and resumable upload instructions (URL, method, headers).
1417
- */
1418
- async create(applicationId, bucketId, data) {
1419
- return this.httpClient.post(`/applications/${applicationId}/storage/buckets/${bucketId}/uploads`, data);
1469
+ async create(applicationIdOrBucketRef, bucketRefOrData, dataOrOptions, legacyOptions) {
1470
+ const explicitApplication = typeof bucketRefOrData === "string";
1471
+ const applicationId = resolveStorageApplicationId(
1472
+ this.defaultApplicationId,
1473
+ explicitApplication ? applicationIdOrBucketRef : void 0
1474
+ );
1475
+ const bucketRef = explicitApplication ? bucketRefOrData : applicationIdOrBucketRef;
1476
+ const data = explicitApplication ? dataOrOptions : bucketRefOrData;
1477
+ const options = explicitApplication ? legacyOptions : dataOrOptions;
1478
+ return this.httpClient.post(
1479
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/uploads`,
1480
+ data,
1481
+ { query: { environment: options?.environment } }
1482
+ );
1420
1483
  }
1421
- /**
1422
- * Get an upload session by ID. Use this to track server-side processing after
1423
- * an upload (poll until `state === 'completed'`).
1424
- *
1425
- * @param applicationId - The application ID (UUID).
1426
- * @param bucketId - The bucket ID (UUID).
1427
- * @param sessionId - The upload session ID (UUID).
1428
- * @returns The current session state.
1429
- */
1430
- async get(applicationId, bucketId, sessionId) {
1431
- return this.httpClient.get(`/applications/${applicationId}/storage/buckets/${bucketId}/uploads/${sessionId}`);
1484
+ async get(applicationIdOrBucketRef, bucketRefOrSessionId, sessionIdOrOptions, legacyOptions) {
1485
+ const explicitApplication = typeof sessionIdOrOptions === "string";
1486
+ const applicationId = resolveStorageApplicationId(
1487
+ this.defaultApplicationId,
1488
+ explicitApplication ? applicationIdOrBucketRef : void 0
1489
+ );
1490
+ const bucketRef = explicitApplication ? bucketRefOrSessionId : applicationIdOrBucketRef;
1491
+ const sessionId = explicitApplication ? sessionIdOrOptions : bucketRefOrSessionId;
1492
+ const options = explicitApplication ? legacyOptions : sessionIdOrOptions;
1493
+ return this.httpClient.get(
1494
+ `/applications/${applicationId}/storage/buckets/${encodeStorageBucketReference(bucketRef)}/uploads/${sessionId}`,
1495
+ { query: { environment: options?.environment } }
1496
+ );
1432
1497
  }
1433
1498
  /**
1434
1499
  * Upload bytes directly to a pre-existing signed upload URL — for example, a
@@ -1462,19 +1527,24 @@ var StorageUploadSessionsResource = class extends BaseResource {
1462
1527
  // src/resources/application-storage.ts
1463
1528
  var delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
1464
1529
  var ApplicationStorageResource = class {
1530
+ constructor(httpClient, transport = tusUploadTransport, defaultApplicationId) {
1531
+ this.defaultApplicationId = defaultApplicationId;
1532
+ this.transport = transport;
1533
+ this.buckets = new StorageBucketsResource(httpClient, defaultApplicationId);
1534
+ this.objects = new StorageObjectsResource(httpClient, defaultApplicationId);
1535
+ this.uploadSessions = new StorageUploadSessionsResource(httpClient, transport, defaultApplicationId);
1536
+ this.trash = new StorageTrashResource(httpClient, defaultApplicationId);
1537
+ }
1538
+ defaultApplicationId;
1465
1539
  /** Create, list, get, and delete storage buckets. */
1466
1540
  buckets;
1467
1541
  /** List, get, delete objects, and generate access URLs. */
1468
1542
  objects;
1469
1543
  /** Low-level upload sessions and direct-to-URL byte uploads. */
1470
1544
  uploadSessions;
1545
+ /** Lists, restores, and permanently purges soft-deleted objects. */
1546
+ trash;
1471
1547
  transport;
1472
- constructor(httpClient, transport = tusUploadTransport) {
1473
- this.transport = transport;
1474
- this.buckets = new StorageBucketsResource(httpClient);
1475
- this.objects = new StorageObjectsResource(httpClient);
1476
- this.uploadSessions = new StorageUploadSessionsResource(httpClient, transport);
1477
- }
1478
1548
  /**
1479
1549
  * Upload a file to a storage bucket. Handles the full flow: computes the
1480
1550
  * required SHA-256 checksum, creates an upload session, uploads the bytes with
@@ -1492,14 +1562,18 @@ var ApplicationStorageResource = class {
1492
1562
  *
1493
1563
  * @example
1494
1564
  * ```ts
1495
- * const { session, url, object } = await client.applications.storage.upload({
1496
- * applicationId, bucketId, key: 'reports/q1.pdf', path: './q1.pdf',
1565
+ * const { session, url, object } = await client.storage.upload({
1566
+ * bucket: 'reports', key: 'q1.pdf', path: './q1.pdf',
1497
1567
  * waitForCompletion: true,
1498
1568
  * });
1499
1569
  * console.log(object?.contentLength, 'bytes available at', url);
1500
1570
  * ```
1501
1571
  */
1502
1572
  async upload(input) {
1573
+ const applicationId = resolveStorageApplicationId(this.defaultApplicationId, input.applicationId);
1574
+ const bucketRef = resolveStorageBucketReference(input);
1575
+ const completionOptions = typeof input.waitForCompletion === "object" ? input.waitForCompletion : void 0;
1576
+ const environment = input.environment ?? completionOptions?.environment;
1503
1577
  const resolved = await resolveUploadSource({
1504
1578
  key: input.key,
1505
1579
  data: input.data,
@@ -1511,14 +1585,19 @@ var ApplicationStorageResource = class {
1511
1585
  checksumSha1: input.checksumSha1,
1512
1586
  checksumMd5: input.checksumMd5
1513
1587
  });
1514
- const { session, upload } = await this.uploadSessions.create(input.applicationId, input.bucketId, {
1515
- key: input.key,
1516
- contentLength: resolved.size,
1517
- checksumSha256: resolved.checksums.sha256,
1518
- contentType: resolved.contentType,
1519
- checksumSha1: resolved.checksums.sha1,
1520
- checksumMd5: resolved.checksums.md5
1521
- });
1588
+ const { session, upload } = await this.uploadSessions.create(
1589
+ applicationId,
1590
+ bucketRef,
1591
+ {
1592
+ key: input.key,
1593
+ contentLength: resolved.size,
1594
+ checksumSha256: resolved.checksums.sha256,
1595
+ contentType: resolved.contentType,
1596
+ checksumSha1: resolved.checksums.sha1,
1597
+ checksumMd5: resolved.checksums.md5
1598
+ },
1599
+ { environment }
1600
+ );
1522
1601
  await runResolvedUpload(
1523
1602
  this.transport,
1524
1603
  upload.url,
@@ -1535,9 +1614,12 @@ var ApplicationStorageResource = class {
1535
1614
  { "Tus-Resumable": "1.0.0", ...upload.headers }
1536
1615
  ).catch(toUploadError);
1537
1616
  if (input.waitForCompletion) {
1538
- const options = typeof input.waitForCompletion === "object" ? input.waitForCompletion : void 0;
1539
- const completed = await this.waitForCompletion(input.applicationId, input.bucketId, session.id, options);
1540
- const object = await this.objects.getByKey(input.applicationId, input.bucketId, input.key) ?? void 0;
1617
+ const options = completionOptions ?? {};
1618
+ const completed = await this.waitForCompletion(applicationId, bucketRef, session.id, {
1619
+ ...options,
1620
+ environment
1621
+ });
1622
+ const object = await this.objects.getByKey(applicationId, bucketRef, input.key, { environment }) ?? void 0;
1541
1623
  return { session: completed, url: upload.publicObjectUrl, object };
1542
1624
  }
1543
1625
  return { session, url: upload.publicObjectUrl };
@@ -1553,8 +1635,8 @@ var ApplicationStorageResource = class {
1553
1635
  *
1554
1636
  * @example
1555
1637
  * ```ts
1556
- * const results = await client.applications.storage.uploadBatch(
1557
- * files.map((f) => ({ applicationId, bucketId, key: `uploads/${f.name}`, data: f })),
1638
+ * const results = await client.storage.uploadBatch(
1639
+ * files.map((f) => ({ bucket: 'uploads', key: f.name, data: f })),
1558
1640
  * { concurrency: 6, onProgress: (done, total) => console.log(`${done}/${total}`) },
1559
1641
  * );
1560
1642
  * const failed = results.filter((r) => r.error);
@@ -1581,22 +1663,22 @@ var ApplicationStorageResource = class {
1581
1663
  await Promise.all(Array.from({ length: Math.min(concurrency, inputs.length) }, () => worker()));
1582
1664
  return results;
1583
1665
  }
1584
- /**
1585
- * Poll an upload session until the object is finalized server-side.
1586
- *
1587
- * @param applicationId - The application ID (UUID).
1588
- * @param bucketId - The bucket ID (UUID).
1589
- * @param sessionId - The upload session ID (UUID).
1590
- * @param options - Timeout and polling interval.
1591
- * @returns The completed session.
1592
- * @throws {@link UploadError} if the session fails, expires, or the timeout elapses.
1593
- */
1594
- async waitForCompletion(applicationId, bucketId, sessionId, options) {
1666
+ async waitForCompletion(applicationIdOrBucketRef, bucketRefOrSessionId, sessionIdOrOptions, legacyOptions) {
1667
+ const explicitApplication = typeof sessionIdOrOptions === "string";
1668
+ const applicationId = resolveStorageApplicationId(
1669
+ this.defaultApplicationId,
1670
+ explicitApplication ? applicationIdOrBucketRef : void 0
1671
+ );
1672
+ const bucketRef = explicitApplication ? bucketRefOrSessionId : applicationIdOrBucketRef;
1673
+ const sessionId = explicitApplication ? sessionIdOrOptions : bucketRefOrSessionId;
1674
+ const options = explicitApplication ? legacyOptions : sessionIdOrOptions;
1595
1675
  const timeoutMs = options?.timeoutMs ?? 6e4;
1596
1676
  const pollIntervalMs = options?.pollIntervalMs ?? 1e3;
1597
1677
  const deadline = Date.now() + timeoutMs;
1598
1678
  for (; ; ) {
1599
- const session = await this.uploadSessions.get(applicationId, bucketId, sessionId);
1679
+ const session = await this.uploadSessions.get(applicationId, bucketRef, sessionId, {
1680
+ environment: options?.environment
1681
+ });
1600
1682
  if (session.state === "completed") return session;
1601
1683
  if (session.state === "failed" || session.state === "expired") {
1602
1684
  throw new UploadError(`Upload session ${session.state}.`);
@@ -1630,15 +1712,14 @@ var ApplicationsResource = class extends BaseResource {
1630
1712
  * @example
1631
1713
  * ```ts
1632
1714
  * // Upload a file
1633
- * const { url } = await client.applications.storage.upload({
1634
- * applicationId: 'app-id',
1635
- * bucketId: 'bucket-id',
1715
+ * const { url } = await client.storage.upload({
1716
+ * bucket: 'assets',
1636
1717
  * key: 'images/logo.png',
1637
1718
  * data: fileData,
1638
1719
  * });
1639
1720
  *
1640
1721
  * // List objects in a bucket
1641
- * const { items } = await client.applications.storage.objects.list('app-id', 'bucket-id');
1722
+ * const { items } = await client.storage.objects.list('assets');
1642
1723
  * ```
1643
1724
  */
1644
1725
  storage;
@@ -1651,10 +1732,10 @@ var ApplicationsResource = class extends BaseResource {
1651
1732
  * ```
1652
1733
  */
1653
1734
  requests;
1654
- constructor(...args) {
1655
- super(...args);
1735
+ constructor(httpClient, defaultApplicationId) {
1736
+ super(httpClient);
1656
1737
  this.envVars = new ApplicationEnvVarsResource(this.httpClient);
1657
- this.storage = new ApplicationStorageResource(this.httpClient);
1738
+ this.storage = new ApplicationStorageResource(this.httpClient, void 0, defaultApplicationId);
1658
1739
  this.requests = new ApplicationRequestsResource(this.httpClient);
1659
1740
  }
1660
1741
  /**
@@ -2364,6 +2445,15 @@ var GigadriveClient = class {
2364
2445
  organizations;
2365
2446
  /** Application management (list, environment variables, storage). */
2366
2447
  applications;
2448
+ /**
2449
+ * Context-bound file storage operations.
2450
+ *
2451
+ * Deployed workloads receive their application context through
2452
+ * `GIGADRIVE_APPLICATION_ID`; management clients can set
2453
+ * {@link GigadriveClientConfig.applicationId}. This is the same resource as
2454
+ * {@link applications}.storage.
2455
+ */
2456
+ storage;
2367
2457
  /** Deployment lifecycle (create, upload, status, logs). */
2368
2458
  deployments;
2369
2459
  /** AI Gateway — OpenAI-compatible chat completions and model listing. */
@@ -2376,12 +2466,14 @@ var GigadriveClient = class {
2376
2466
  imageOptimization;
2377
2467
  constructor(config = {}) {
2378
2468
  const baseUrl = config.baseUrl ?? readEnv("GIGADRIVE_API_BASE_URL") ?? DEFAULT_BASE_URL2;
2469
+ const applicationId = config.applicationId ?? readEnv("GIGADRIVE_APPLICATION_ID");
2379
2470
  const fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
2380
2471
  const credentialProvider = resolveCredentialProvider({ ...config, baseUrl, fetch: fetchFn });
2381
2472
  const tokenManager = new TokenManager(credentialProvider);
2382
2473
  const httpClient = new HttpClient(baseUrl, tokenManager, fetchFn);
2383
2474
  this.organizations = new OrganizationsResource(httpClient);
2384
- this.applications = new ApplicationsResource(httpClient);
2475
+ this.applications = new ApplicationsResource(httpClient, applicationId);
2476
+ this.storage = this.applications.storage;
2385
2477
  this.deployments = new DeploymentsResource(httpClient);
2386
2478
  this.aiGateway = new AiGatewayResource(httpClient);
2387
2479
  this.apiKeys = new ApiKeysResource(httpClient);
@@ -2416,6 +2508,7 @@ async function* paginate(fetchPage) {
2416
2508
  ApplicationStorageResource,
2417
2509
  ApplicationsResource,
2418
2510
  AuthenticationError,
2511
+ ConfigurationError,
2419
2512
  DeploymentsResource,
2420
2513
  GigadriveClient,
2421
2514
  GigadriveError,
@@ -2428,6 +2521,7 @@ async function* paginate(fetchPage) {
2428
2521
  StickySessionsResource,
2429
2522
  StorageBucketsResource,
2430
2523
  StorageObjectsResource,
2524
+ StorageTrashResource,
2431
2525
  StorageUploadSessionsResource,
2432
2526
  UploadError,
2433
2527
  UploadSessionExpiredError,