@tinycloud/sdk-services 3.0.0 → 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.
package/dist/kv/index.cjs CHANGED
@@ -592,7 +592,7 @@ var KVService = class extends BaseService {
592
592
  return err(
593
593
  storageQuotaExceededError(
594
594
  "kv",
595
- `Storage quota exceeded for key "${key}": ${errorText}`,
595
+ `Storage quota exceeded for key ${JSON.stringify(key)}: ${errorText}`,
596
596
  {
597
597
  status: response.status,
598
598
  ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
@@ -605,7 +605,7 @@ var KVService = class extends BaseService {
605
605
  return err(
606
606
  storageLimitReachedError(
607
607
  "kv",
608
- `Storage limit reached for key "${key}": ${errorText}`,
608
+ `Storage limit reached for key ${JSON.stringify(key)}: ${errorText}`,
609
609
  {
610
610
  status: response.status,
611
611
  ...quotaInfo ? { usedBytes: quotaInfo.usedBytes, limitBytes: quotaInfo.limitBytes } : {}
@@ -891,12 +891,11 @@ var KVService = class extends BaseService {
891
891
  if (!response.ok) {
892
892
  const errorText = await response.text();
893
893
  if (response.status === 401 || response.status === 403) {
894
- const { resource, action: requiredAction } = parseAuthError(errorText);
895
- return err(authUnauthorizedError("kv", errorText, {
896
- status: response.status,
897
- ...requiredAction && { requiredAction },
898
- ...resource && { resource }
899
- }));
894
+ return this.authorizationFailure(
895
+ `Failed to batch read ${keys.length} key(s)`,
896
+ response,
897
+ errorText
898
+ );
900
899
  }
901
900
  if (response.status === 413) {
902
901
  return err(serviceError(
@@ -995,23 +994,39 @@ var KVService = class extends BaseService {
995
994
  } catch {
996
995
  }
997
996
  if (response.status === 401 || response.status === 403) {
998
- const { resource, action } = parseAuthError(errorText);
999
- return err(authUnauthorizedError("kv", errorText, {
1000
- status: response.status,
1001
- ...action && { requiredAction: action },
1002
- ...resource && { resource }
1003
- }));
997
+ return this.authorizationFailure(
998
+ `Failed to create signed read URL for key ${JSON.stringify(key)}`,
999
+ response,
1000
+ errorText
1001
+ );
1004
1002
  }
1005
1003
  const code = response.status === 400 ? ErrorCodes.INVALID_INPUT : ErrorCodes.NETWORK_ERROR;
1006
1004
  return err(
1007
1005
  serviceError(
1008
1006
  code,
1009
- `Failed to create signed read URL for key "${key}": ${response.status} - ${errorText}`,
1007
+ `Failed to create signed read URL for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1010
1008
  "kv",
1011
1009
  { meta: { status: response.status, statusText: response.statusText } }
1012
1010
  )
1013
1011
  );
1014
1012
  }
1013
+ /**
1014
+ * AUTH_UNAUTHORIZED for a 401/403 response. The message keeps the HTTP
1015
+ * status and the server text (`<context>: <status> - <text>`, the same
1016
+ * shape as every other KV failure) so callers that rethrow only the message
1017
+ * still see the status; `meta.status` carries it for typed callers.
1018
+ * Resource/ability hints are parsed from the raw body, never the message.
1019
+ */
1020
+ authorizationFailure(context, response, errorText, extraMeta = {}) {
1021
+ const { resource, action } = parseAuthError(errorText);
1022
+ const detail = errorText.trim().length > 0 ? errorText : response.statusText || "authorization failed";
1023
+ return err(authUnauthorizedError("kv", `${context}: ${response.status} - ${detail}`, {
1024
+ status: response.status,
1025
+ ...action && { requiredAction: action },
1026
+ ...resource && { resource },
1027
+ ...extraMeta
1028
+ }));
1029
+ }
1015
1030
  normalizeSignedReadUrlResponse(data) {
1016
1031
  if (!data || typeof data !== "object") {
1017
1032
  return void 0;
@@ -1054,14 +1069,13 @@ var KVService = class extends BaseService {
1054
1069
  if (!response.ok) {
1055
1070
  if (response.status === 401 || response.status === 403) {
1056
1071
  const errorText2 = await response.text();
1057
- const { resource, action } = parseAuthError(errorText2);
1058
1072
  const permissionHint = parsePermissionHintFromErrorText(errorText2);
1059
- return err(authUnauthorizedError("kv", errorText2, {
1060
- status: response.status,
1061
- ...action && { requiredAction: action },
1062
- ...resource && { resource },
1063
- ...permissionHint === void 0 ? {} : { permissionHint }
1064
- }));
1073
+ return this.authorizationFailure(
1074
+ `Failed to get key ${JSON.stringify(key)}`,
1075
+ response,
1076
+ errorText2,
1077
+ permissionHint === void 0 ? {} : { permissionHint }
1078
+ );
1065
1079
  }
1066
1080
  if (response.status === 404) {
1067
1081
  return this.classifyNotFound(response, key);
@@ -1070,7 +1084,7 @@ var KVService = class extends BaseService {
1070
1084
  if (response.status === 413) {
1071
1085
  return err(serviceError(
1072
1086
  ErrorCodes.KV_RESPONSE_TOO_LARGE,
1073
- `KV value at key "${key}" exceeds the requested response limit`,
1087
+ `KV value at key ${JSON.stringify(key)} exceeds the requested response limit`,
1074
1088
  "kv",
1075
1089
  { meta: { status: response.status, statusText: response.statusText } }
1076
1090
  ));
@@ -1078,7 +1092,7 @@ var KVService = class extends BaseService {
1078
1092
  return err(
1079
1093
  serviceError(
1080
1094
  ErrorCodes.NETWORK_ERROR,
1081
- `Failed to get key "${key}": ${response.status} - ${errorText}`,
1095
+ `Failed to get key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1082
1096
  "kv",
1083
1097
  { meta: { status: response.status, statusText: response.statusText } }
1084
1098
  )
@@ -1134,21 +1148,18 @@ var KVService = class extends BaseService {
1134
1148
  }
1135
1149
  );
1136
1150
  if (response.status === 401 || response.status === 403) {
1137
- const errorText = await response.text();
1138
- const message = errorText.trim().length > 0 ? errorText : `Failed to put key "${key}": ${response.status} - ${response.statusText || "authorization failed"}`;
1139
- const { resource, action: requiredAction } = parseAuthError(errorText);
1140
- return err(authUnauthorizedError("kv", message, {
1141
- status: response.status,
1142
- ...requiredAction && { requiredAction },
1143
- ...resource && { resource }
1144
- }));
1151
+ return this.authorizationFailure(
1152
+ `Failed to put key ${JSON.stringify(key)}`,
1153
+ response,
1154
+ await response.text()
1155
+ );
1145
1156
  }
1146
1157
  if (!response.ok) {
1147
1158
  const errorText = await response.text();
1148
1159
  if (response.status === 412) {
1149
1160
  return err(serviceError(
1150
1161
  ErrorCodes.KV_PRECONDITION_FAILED,
1151
- `KV precondition failed for key "${key}"`,
1162
+ `KV precondition failed for key ${JSON.stringify(key)}`,
1152
1163
  "kv",
1153
1164
  { meta: { status: response.status, statusText: response.statusText } }
1154
1165
  ));
@@ -1156,7 +1167,7 @@ var KVService = class extends BaseService {
1156
1167
  if (response.status === 503 && (options?.ifMatch !== void 0 || options?.ifNoneMatch !== void 0)) {
1157
1168
  return err(serviceError(
1158
1169
  ErrorCodes.KV_CONFLICT,
1159
- `Concurrent KV update conflicted for key "${key}"`,
1170
+ `Concurrent KV update conflicted for key ${JSON.stringify(key)}`,
1160
1171
  "kv",
1161
1172
  { meta: { status: response.status, statusText: response.statusText } }
1162
1173
  ));
@@ -1172,7 +1183,7 @@ var KVService = class extends BaseService {
1172
1183
  return err(
1173
1184
  serviceError(
1174
1185
  ErrorCodes.KV_WRITE_FAILED,
1175
- `Failed to put key "${key}": ${response.status} - ${errorText}`,
1186
+ `Failed to put key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1176
1187
  "kv",
1177
1188
  { meta: { status: response.status, statusText: response.statusText } }
1178
1189
  )
@@ -1266,12 +1277,11 @@ var KVService = class extends BaseService {
1266
1277
  );
1267
1278
  }
1268
1279
  if (response.status === 401 || response.status === 403) {
1269
- const { resource, action } = parseAuthError(errorText);
1270
- return err(authUnauthorizedError("kv", errorText, {
1271
- status: response.status,
1272
- ...action && { requiredAction: action },
1273
- ...resource && { resource }
1274
- }));
1280
+ return this.authorizationFailure(
1281
+ `Failed to batch put ${items.length} key(s)`,
1282
+ response,
1283
+ errorText
1284
+ );
1275
1285
  }
1276
1286
  const quotaError = this.handleQuotaErrorResponse(
1277
1287
  response,
@@ -1392,13 +1402,11 @@ var KVService = class extends BaseService {
1392
1402
  );
1393
1403
  if (!response.ok) {
1394
1404
  if (response.status === 401) {
1395
- const errorText2 = await response.text();
1396
- const { resource, action } = parseAuthError(errorText2);
1397
- return err(authUnauthorizedError("kv", errorText2, {
1398
- status: response.status,
1399
- ...action && { requiredAction: action },
1400
- ...resource && { resource }
1401
- }));
1405
+ return this.authorizationFailure(
1406
+ "Failed to list keys",
1407
+ response,
1408
+ await response.text()
1409
+ );
1402
1410
  }
1403
1411
  const errorText = await response.text();
1404
1412
  return err(
@@ -1447,13 +1455,11 @@ var KVService = class extends BaseService {
1447
1455
  );
1448
1456
  if (!response.ok) {
1449
1457
  if (response.status === 401) {
1450
- const errorText2 = await response.text();
1451
- const { resource, action } = parseAuthError(errorText2);
1452
- return err(authUnauthorizedError("kv", errorText2, {
1453
- status: response.status,
1454
- ...action && { requiredAction: action },
1455
- ...resource && { resource }
1456
- }));
1458
+ return this.authorizationFailure(
1459
+ `Failed to delete key ${JSON.stringify(key)}`,
1460
+ response,
1461
+ await response.text()
1462
+ );
1457
1463
  }
1458
1464
  if (response.status === 404) {
1459
1465
  return this.classifyNotFound(response, key);
@@ -1462,7 +1468,7 @@ var KVService = class extends BaseService {
1462
1468
  if (response.status === 412) {
1463
1469
  return err(serviceError(
1464
1470
  ErrorCodes.KV_PRECONDITION_FAILED,
1465
- `KV precondition failed for key "${key}"`,
1471
+ `KV precondition failed for key ${JSON.stringify(key)}`,
1466
1472
  "kv",
1467
1473
  { meta: { status: response.status, statusText: response.statusText } }
1468
1474
  ));
@@ -1470,7 +1476,7 @@ var KVService = class extends BaseService {
1470
1476
  if (response.status === 503 && options?.ifMatch !== void 0) {
1471
1477
  return err(serviceError(
1472
1478
  ErrorCodes.KV_CONFLICT,
1473
- `Concurrent KV delete conflicted for key "${key}"`,
1479
+ `Concurrent KV delete conflicted for key ${JSON.stringify(key)}`,
1474
1480
  "kv",
1475
1481
  { meta: { status: response.status, statusText: response.statusText } }
1476
1482
  ));
@@ -1478,7 +1484,7 @@ var KVService = class extends BaseService {
1478
1484
  return err(
1479
1485
  serviceError(
1480
1486
  ErrorCodes.NETWORK_ERROR,
1481
- `Failed to delete key "${key}": ${response.status} - ${errorText}`,
1487
+ `Failed to delete key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1482
1488
  "kv",
1483
1489
  { meta: { status: response.status, statusText: response.statusText } }
1484
1490
  )
@@ -1511,13 +1517,11 @@ var KVService = class extends BaseService {
1511
1517
  );
1512
1518
  if (!response.ok) {
1513
1519
  if (response.status === 401) {
1514
- const errorText2 = await response.text();
1515
- const { resource, action } = parseAuthError(errorText2);
1516
- return err(authUnauthorizedError("kv", errorText2, {
1517
- status: response.status,
1518
- ...action && { requiredAction: action },
1519
- ...resource && { resource }
1520
- }));
1520
+ return this.authorizationFailure(
1521
+ `Failed to get metadata for key ${JSON.stringify(key)}`,
1522
+ response,
1523
+ await response.text()
1524
+ );
1521
1525
  }
1522
1526
  if (response.status === 404) {
1523
1527
  return this.classifyNotFound(response, key);
@@ -1526,7 +1530,7 @@ var KVService = class extends BaseService {
1526
1530
  return err(
1527
1531
  serviceError(
1528
1532
  ErrorCodes.NETWORK_ERROR,
1529
- `Failed to get metadata for key "${key}": ${response.status} - ${errorText}`,
1533
+ `Failed to get metadata for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1530
1534
  "kv",
1531
1535
  { meta: { status: response.status, statusText: response.statusText } }
1532
1536
  )