@tinycloud/sdk-services 3.0.0-beta.8 → 3.1.0-beta.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.
@@ -919,6 +919,14 @@ declare class KVService extends BaseService implements IKVService {
919
919
  */
920
920
  private parseResponse;
921
921
  private createSignedReadUrlError;
922
+ /**
923
+ * AUTH_UNAUTHORIZED for a 401/403 response. The message keeps the HTTP
924
+ * status and the server text (`<context>: <status> - <text>`, the same
925
+ * shape as every other KV failure) so callers that rethrow only the message
926
+ * still see the status; `meta.status` carries it for typed callers.
927
+ * Resource/ability hints are parsed from the raw body, never the message.
928
+ */
929
+ private authorizationFailure;
922
930
  private normalizeSignedReadUrlResponse;
923
931
  /**
924
932
  * Get a value by key.
@@ -919,6 +919,14 @@ declare class KVService extends BaseService implements IKVService {
919
919
  */
920
920
  private parseResponse;
921
921
  private createSignedReadUrlError;
922
+ /**
923
+ * AUTH_UNAUTHORIZED for a 401/403 response. The message keeps the HTTP
924
+ * status and the server text (`<context>: <status> - <text>`, the same
925
+ * shape as every other KV failure) so callers that rethrow only the message
926
+ * still see the status; `meta.status` carries it for typed callers.
927
+ * Resource/ability hints are parsed from the raw body, never the message.
928
+ */
929
+ private authorizationFailure;
922
930
  private normalizeSignedReadUrlResponse;
923
931
  /**
924
932
  * Get a value by key.
package/dist/kv/index.js CHANGED
@@ -563,7 +563,7 @@ var KVService = class extends BaseService {
563
563
  return err(
564
564
  storageQuotaExceededError(
565
565
  "kv",
566
- `Storage quota exceeded for key "${key}": ${errorText}`,
566
+ `Storage quota exceeded for key ${JSON.stringify(key)}: ${errorText}`,
567
567
  {
568
568
  status: response.status,
569
569
  ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
@@ -576,7 +576,7 @@ var KVService = class extends BaseService {
576
576
  return err(
577
577
  storageLimitReachedError(
578
578
  "kv",
579
- `Storage limit reached for key "${key}": ${errorText}`,
579
+ `Storage limit reached for key ${JSON.stringify(key)}: ${errorText}`,
580
580
  {
581
581
  status: response.status,
582
582
  ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
@@ -620,7 +620,7 @@ var KVService = class extends BaseService {
620
620
  */
621
621
  getFullPath(key, prefixOverride) {
622
622
  const prefix = prefixOverride ?? this._config.prefix ?? "";
623
- return prefix ? `${prefix}/${key}` : key;
623
+ return prefix ? key === "" ? prefix : `${prefix}/${key}` : key;
624
624
  }
625
625
  /**
626
626
  * Get the host URL.
@@ -862,12 +862,11 @@ var KVService = class extends BaseService {
862
862
  if (!response.ok) {
863
863
  const errorText = await response.text();
864
864
  if (response.status === 401 || response.status === 403) {
865
- const { resource, action: requiredAction } = parseAuthError(errorText);
866
- return err(authUnauthorizedError("kv", errorText, {
867
- status: response.status,
868
- ...requiredAction && { requiredAction },
869
- ...resource && { resource }
870
- }));
865
+ return this.authorizationFailure(
866
+ `Failed to batch read ${keys.length} key(s)`,
867
+ response,
868
+ errorText
869
+ );
871
870
  }
872
871
  if (response.status === 413) {
873
872
  return err(serviceError(
@@ -966,23 +965,39 @@ var KVService = class extends BaseService {
966
965
  } catch {
967
966
  }
968
967
  if (response.status === 401 || response.status === 403) {
969
- const { resource, action } = parseAuthError(errorText);
970
- return err(authUnauthorizedError("kv", errorText, {
971
- status: response.status,
972
- ...action && { requiredAction: action },
973
- ...resource && { resource }
974
- }));
968
+ return this.authorizationFailure(
969
+ `Failed to create signed read URL for key ${JSON.stringify(key)}`,
970
+ response,
971
+ errorText
972
+ );
975
973
  }
976
974
  const code = response.status === 400 ? ErrorCodes.INVALID_INPUT : ErrorCodes.NETWORK_ERROR;
977
975
  return err(
978
976
  serviceError(
979
977
  code,
980
- `Failed to create signed read URL for key "${key}": ${response.status} - ${errorText}`,
978
+ `Failed to create signed read URL for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
981
979
  "kv",
982
980
  { meta: { status: response.status, statusText: response.statusText } }
983
981
  )
984
982
  );
985
983
  }
984
+ /**
985
+ * AUTH_UNAUTHORIZED for a 401/403 response. The message keeps the HTTP
986
+ * status and the server text (`<context>: <status> - <text>`, the same
987
+ * shape as every other KV failure) so callers that rethrow only the message
988
+ * still see the status; `meta.status` carries it for typed callers.
989
+ * Resource/ability hints are parsed from the raw body, never the message.
990
+ */
991
+ authorizationFailure(context, response, errorText, extraMeta = {}) {
992
+ const { resource, action } = parseAuthError(errorText);
993
+ const detail = errorText.trim().length > 0 ? errorText : response.statusText || "authorization failed";
994
+ return err(authUnauthorizedError("kv", `${context}: ${response.status} - ${detail}`, {
995
+ status: response.status,
996
+ ...action && { requiredAction: action },
997
+ ...resource && { resource },
998
+ ...extraMeta
999
+ }));
1000
+ }
986
1001
  normalizeSignedReadUrlResponse(data) {
987
1002
  if (!data || typeof data !== "object") {
988
1003
  return void 0;
@@ -1025,14 +1040,13 @@ var KVService = class extends BaseService {
1025
1040
  if (!response.ok) {
1026
1041
  if (response.status === 401 || response.status === 403) {
1027
1042
  const errorText2 = await response.text();
1028
- const { resource, action } = parseAuthError(errorText2);
1029
1043
  const permissionHint = parsePermissionHintFromErrorText(errorText2);
1030
- return err(authUnauthorizedError("kv", errorText2, {
1031
- status: response.status,
1032
- ...action && { requiredAction: action },
1033
- ...resource && { resource },
1034
- ...permissionHint === void 0 ? {} : { permissionHint }
1035
- }));
1044
+ return this.authorizationFailure(
1045
+ `Failed to get key ${JSON.stringify(key)}`,
1046
+ response,
1047
+ errorText2,
1048
+ permissionHint === void 0 ? {} : { permissionHint }
1049
+ );
1036
1050
  }
1037
1051
  if (response.status === 404) {
1038
1052
  return this.classifyNotFound(response, key);
@@ -1041,7 +1055,7 @@ var KVService = class extends BaseService {
1041
1055
  if (response.status === 413) {
1042
1056
  return err(serviceError(
1043
1057
  ErrorCodes.KV_RESPONSE_TOO_LARGE,
1044
- `KV value at key "${key}" exceeds the requested response limit`,
1058
+ `KV value at key ${JSON.stringify(key)} exceeds the requested response limit`,
1045
1059
  "kv",
1046
1060
  { meta: { status: response.status, statusText: response.statusText } }
1047
1061
  ));
@@ -1049,7 +1063,7 @@ var KVService = class extends BaseService {
1049
1063
  return err(
1050
1064
  serviceError(
1051
1065
  ErrorCodes.NETWORK_ERROR,
1052
- `Failed to get key "${key}": ${response.status} - ${errorText}`,
1066
+ `Failed to get key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1053
1067
  "kv",
1054
1068
  { meta: { status: response.status, statusText: response.statusText } }
1055
1069
  )
@@ -1104,21 +1118,19 @@ var KVService = class extends BaseService {
1104
1118
  ...options?.ifNoneMatch === void 0 ? {} : { "if-none-match": options.ifNoneMatch }
1105
1119
  }
1106
1120
  );
1121
+ if (response.status === 401 || response.status === 403) {
1122
+ return this.authorizationFailure(
1123
+ `Failed to put key ${JSON.stringify(key)}`,
1124
+ response,
1125
+ await response.text()
1126
+ );
1127
+ }
1107
1128
  if (!response.ok) {
1108
- if (response.status === 401) {
1109
- const errorText2 = await response.text();
1110
- const { resource, action } = parseAuthError(errorText2);
1111
- return err(authUnauthorizedError("kv", errorText2, {
1112
- status: response.status,
1113
- ...action && { requiredAction: action },
1114
- ...resource && { resource }
1115
- }));
1116
- }
1117
1129
  const errorText = await response.text();
1118
1130
  if (response.status === 412) {
1119
1131
  return err(serviceError(
1120
1132
  ErrorCodes.KV_PRECONDITION_FAILED,
1121
- `KV precondition failed for key "${key}"`,
1133
+ `KV precondition failed for key ${JSON.stringify(key)}`,
1122
1134
  "kv",
1123
1135
  { meta: { status: response.status, statusText: response.statusText } }
1124
1136
  ));
@@ -1126,7 +1138,7 @@ var KVService = class extends BaseService {
1126
1138
  if (response.status === 503 && (options?.ifMatch !== void 0 || options?.ifNoneMatch !== void 0)) {
1127
1139
  return err(serviceError(
1128
1140
  ErrorCodes.KV_CONFLICT,
1129
- `Concurrent KV update conflicted for key "${key}"`,
1141
+ `Concurrent KV update conflicted for key ${JSON.stringify(key)}`,
1130
1142
  "kv",
1131
1143
  { meta: { status: response.status, statusText: response.statusText } }
1132
1144
  ));
@@ -1142,7 +1154,7 @@ var KVService = class extends BaseService {
1142
1154
  return err(
1143
1155
  serviceError(
1144
1156
  ErrorCodes.KV_WRITE_FAILED,
1145
- `Failed to put key "${key}": ${response.status} - ${errorText}`,
1157
+ `Failed to put key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1146
1158
  "kv",
1147
1159
  { meta: { status: response.status, statusText: response.statusText } }
1148
1160
  )
@@ -1236,12 +1248,11 @@ var KVService = class extends BaseService {
1236
1248
  );
1237
1249
  }
1238
1250
  if (response.status === 401 || response.status === 403) {
1239
- const { resource, action } = parseAuthError(errorText);
1240
- return err(authUnauthorizedError("kv", errorText, {
1241
- status: response.status,
1242
- ...action && { requiredAction: action },
1243
- ...resource && { resource }
1244
- }));
1251
+ return this.authorizationFailure(
1252
+ `Failed to batch put ${items.length} key(s)`,
1253
+ response,
1254
+ errorText
1255
+ );
1245
1256
  }
1246
1257
  const quotaError = this.handleQuotaErrorResponse(
1247
1258
  response,
@@ -1362,13 +1373,11 @@ var KVService = class extends BaseService {
1362
1373
  );
1363
1374
  if (!response.ok) {
1364
1375
  if (response.status === 401) {
1365
- const errorText2 = await response.text();
1366
- const { resource, action } = parseAuthError(errorText2);
1367
- return err(authUnauthorizedError("kv", errorText2, {
1368
- status: response.status,
1369
- ...action && { requiredAction: action },
1370
- ...resource && { resource }
1371
- }));
1376
+ return this.authorizationFailure(
1377
+ "Failed to list keys",
1378
+ response,
1379
+ await response.text()
1380
+ );
1372
1381
  }
1373
1382
  const errorText = await response.text();
1374
1383
  return err(
@@ -1417,13 +1426,11 @@ var KVService = class extends BaseService {
1417
1426
  );
1418
1427
  if (!response.ok) {
1419
1428
  if (response.status === 401) {
1420
- const errorText2 = await response.text();
1421
- const { resource, action } = parseAuthError(errorText2);
1422
- return err(authUnauthorizedError("kv", errorText2, {
1423
- status: response.status,
1424
- ...action && { requiredAction: action },
1425
- ...resource && { resource }
1426
- }));
1429
+ return this.authorizationFailure(
1430
+ `Failed to delete key ${JSON.stringify(key)}`,
1431
+ response,
1432
+ await response.text()
1433
+ );
1427
1434
  }
1428
1435
  if (response.status === 404) {
1429
1436
  return this.classifyNotFound(response, key);
@@ -1432,7 +1439,7 @@ var KVService = class extends BaseService {
1432
1439
  if (response.status === 412) {
1433
1440
  return err(serviceError(
1434
1441
  ErrorCodes.KV_PRECONDITION_FAILED,
1435
- `KV precondition failed for key "${key}"`,
1442
+ `KV precondition failed for key ${JSON.stringify(key)}`,
1436
1443
  "kv",
1437
1444
  { meta: { status: response.status, statusText: response.statusText } }
1438
1445
  ));
@@ -1440,7 +1447,7 @@ var KVService = class extends BaseService {
1440
1447
  if (response.status === 503 && options?.ifMatch !== void 0) {
1441
1448
  return err(serviceError(
1442
1449
  ErrorCodes.KV_CONFLICT,
1443
- `Concurrent KV delete conflicted for key "${key}"`,
1450
+ `Concurrent KV delete conflicted for key ${JSON.stringify(key)}`,
1444
1451
  "kv",
1445
1452
  { meta: { status: response.status, statusText: response.statusText } }
1446
1453
  ));
@@ -1448,7 +1455,7 @@ var KVService = class extends BaseService {
1448
1455
  return err(
1449
1456
  serviceError(
1450
1457
  ErrorCodes.NETWORK_ERROR,
1451
- `Failed to delete key "${key}": ${response.status} - ${errorText}`,
1458
+ `Failed to delete key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1452
1459
  "kv",
1453
1460
  { meta: { status: response.status, statusText: response.statusText } }
1454
1461
  )
@@ -1481,13 +1488,11 @@ var KVService = class extends BaseService {
1481
1488
  );
1482
1489
  if (!response.ok) {
1483
1490
  if (response.status === 401) {
1484
- const errorText2 = await response.text();
1485
- const { resource, action } = parseAuthError(errorText2);
1486
- return err(authUnauthorizedError("kv", errorText2, {
1487
- status: response.status,
1488
- ...action && { requiredAction: action },
1489
- ...resource && { resource }
1490
- }));
1491
+ return this.authorizationFailure(
1492
+ `Failed to get metadata for key ${JSON.stringify(key)}`,
1493
+ response,
1494
+ await response.text()
1495
+ );
1491
1496
  }
1492
1497
  if (response.status === 404) {
1493
1498
  return this.classifyNotFound(response, key);
@@ -1496,7 +1501,7 @@ var KVService = class extends BaseService {
1496
1501
  return err(
1497
1502
  serviceError(
1498
1503
  ErrorCodes.NETWORK_ERROR,
1499
- `Failed to get metadata for key "${key}": ${response.status} - ${errorText}`,
1504
+ `Failed to get metadata for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1500
1505
  "kv",
1501
1506
  { meta: { status: response.status, statusText: response.statusText } }
1502
1507
  )