@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.
@@ -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 } : {}
@@ -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
  )
@@ -1105,21 +1119,18 @@ var KVService = class extends BaseService {
1105
1119
  }
1106
1120
  );
1107
1121
  if (response.status === 401 || response.status === 403) {
1108
- const errorText = await response.text();
1109
- const message = errorText.trim().length > 0 ? errorText : `Failed to put key "${key}": ${response.status} - ${response.statusText || "authorization failed"}`;
1110
- const { resource, action: requiredAction } = parseAuthError(errorText);
1111
- return err(authUnauthorizedError("kv", message, {
1112
- status: response.status,
1113
- ...requiredAction && { requiredAction },
1114
- ...resource && { resource }
1115
- }));
1122
+ return this.authorizationFailure(
1123
+ `Failed to put key ${JSON.stringify(key)}`,
1124
+ response,
1125
+ await response.text()
1126
+ );
1116
1127
  }
1117
1128
  if (!response.ok) {
1118
1129
  const errorText = await response.text();
1119
1130
  if (response.status === 412) {
1120
1131
  return err(serviceError(
1121
1132
  ErrorCodes.KV_PRECONDITION_FAILED,
1122
- `KV precondition failed for key "${key}"`,
1133
+ `KV precondition failed for key ${JSON.stringify(key)}`,
1123
1134
  "kv",
1124
1135
  { meta: { status: response.status, statusText: response.statusText } }
1125
1136
  ));
@@ -1127,7 +1138,7 @@ var KVService = class extends BaseService {
1127
1138
  if (response.status === 503 && (options?.ifMatch !== void 0 || options?.ifNoneMatch !== void 0)) {
1128
1139
  return err(serviceError(
1129
1140
  ErrorCodes.KV_CONFLICT,
1130
- `Concurrent KV update conflicted for key "${key}"`,
1141
+ `Concurrent KV update conflicted for key ${JSON.stringify(key)}`,
1131
1142
  "kv",
1132
1143
  { meta: { status: response.status, statusText: response.statusText } }
1133
1144
  ));
@@ -1143,7 +1154,7 @@ var KVService = class extends BaseService {
1143
1154
  return err(
1144
1155
  serviceError(
1145
1156
  ErrorCodes.KV_WRITE_FAILED,
1146
- `Failed to put key "${key}": ${response.status} - ${errorText}`,
1157
+ `Failed to put key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1147
1158
  "kv",
1148
1159
  { meta: { status: response.status, statusText: response.statusText } }
1149
1160
  )
@@ -1237,12 +1248,11 @@ var KVService = class extends BaseService {
1237
1248
  );
1238
1249
  }
1239
1250
  if (response.status === 401 || response.status === 403) {
1240
- const { resource, action } = parseAuthError(errorText);
1241
- return err(authUnauthorizedError("kv", errorText, {
1242
- status: response.status,
1243
- ...action && { requiredAction: action },
1244
- ...resource && { resource }
1245
- }));
1251
+ return this.authorizationFailure(
1252
+ `Failed to batch put ${items.length} key(s)`,
1253
+ response,
1254
+ errorText
1255
+ );
1246
1256
  }
1247
1257
  const quotaError = this.handleQuotaErrorResponse(
1248
1258
  response,
@@ -1363,13 +1373,11 @@ var KVService = class extends BaseService {
1363
1373
  );
1364
1374
  if (!response.ok) {
1365
1375
  if (response.status === 401) {
1366
- const errorText2 = await response.text();
1367
- const { resource, action } = parseAuthError(errorText2);
1368
- return err(authUnauthorizedError("kv", errorText2, {
1369
- status: response.status,
1370
- ...action && { requiredAction: action },
1371
- ...resource && { resource }
1372
- }));
1376
+ return this.authorizationFailure(
1377
+ "Failed to list keys",
1378
+ response,
1379
+ await response.text()
1380
+ );
1373
1381
  }
1374
1382
  const errorText = await response.text();
1375
1383
  return err(
@@ -1418,13 +1426,11 @@ var KVService = class extends BaseService {
1418
1426
  );
1419
1427
  if (!response.ok) {
1420
1428
  if (response.status === 401) {
1421
- const errorText2 = await response.text();
1422
- const { resource, action } = parseAuthError(errorText2);
1423
- return err(authUnauthorizedError("kv", errorText2, {
1424
- status: response.status,
1425
- ...action && { requiredAction: action },
1426
- ...resource && { resource }
1427
- }));
1429
+ return this.authorizationFailure(
1430
+ `Failed to delete key ${JSON.stringify(key)}`,
1431
+ response,
1432
+ await response.text()
1433
+ );
1428
1434
  }
1429
1435
  if (response.status === 404) {
1430
1436
  return this.classifyNotFound(response, key);
@@ -1433,7 +1439,7 @@ var KVService = class extends BaseService {
1433
1439
  if (response.status === 412) {
1434
1440
  return err(serviceError(
1435
1441
  ErrorCodes.KV_PRECONDITION_FAILED,
1436
- `KV precondition failed for key "${key}"`,
1442
+ `KV precondition failed for key ${JSON.stringify(key)}`,
1437
1443
  "kv",
1438
1444
  { meta: { status: response.status, statusText: response.statusText } }
1439
1445
  ));
@@ -1441,7 +1447,7 @@ var KVService = class extends BaseService {
1441
1447
  if (response.status === 503 && options?.ifMatch !== void 0) {
1442
1448
  return err(serviceError(
1443
1449
  ErrorCodes.KV_CONFLICT,
1444
- `Concurrent KV delete conflicted for key "${key}"`,
1450
+ `Concurrent KV delete conflicted for key ${JSON.stringify(key)}`,
1445
1451
  "kv",
1446
1452
  { meta: { status: response.status, statusText: response.statusText } }
1447
1453
  ));
@@ -1449,7 +1455,7 @@ var KVService = class extends BaseService {
1449
1455
  return err(
1450
1456
  serviceError(
1451
1457
  ErrorCodes.NETWORK_ERROR,
1452
- `Failed to delete key "${key}": ${response.status} - ${errorText}`,
1458
+ `Failed to delete key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1453
1459
  "kv",
1454
1460
  { meta: { status: response.status, statusText: response.statusText } }
1455
1461
  )
@@ -1482,13 +1488,11 @@ var KVService = class extends BaseService {
1482
1488
  );
1483
1489
  if (!response.ok) {
1484
1490
  if (response.status === 401) {
1485
- const errorText2 = await response.text();
1486
- const { resource, action } = parseAuthError(errorText2);
1487
- return err(authUnauthorizedError("kv", errorText2, {
1488
- status: response.status,
1489
- ...action && { requiredAction: action },
1490
- ...resource && { resource }
1491
- }));
1491
+ return this.authorizationFailure(
1492
+ `Failed to get metadata for key ${JSON.stringify(key)}`,
1493
+ response,
1494
+ await response.text()
1495
+ );
1492
1496
  }
1493
1497
  if (response.status === 404) {
1494
1498
  return this.classifyNotFound(response, key);
@@ -1497,7 +1501,7 @@ var KVService = class extends BaseService {
1497
1501
  return err(
1498
1502
  serviceError(
1499
1503
  ErrorCodes.NETWORK_ERROR,
1500
- `Failed to get metadata for key "${key}": ${response.status} - ${errorText}`,
1504
+ `Failed to get metadata for key ${JSON.stringify(key)}: ${response.status} - ${errorText}`,
1501
1505
  "kv",
1502
1506
  { meta: { status: response.status, statusText: response.statusText } }
1503
1507
  )