@tinycloud/sdk-core 3.1.0-beta.3 → 3.1.0-beta.6

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.
@@ -3324,6 +3324,23 @@ var import_sha256 = require("@noble/hashes/sha256");
3324
3324
  var import_blake3 = require("@noble/hashes/blake3");
3325
3325
  var import_ed255192 = require("@noble/curves/ed25519");
3326
3326
  var import_zod = require("zod");
3327
+
3328
+ // src/http-error.ts
3329
+ var import_sdk_services = require("@tinycloud/sdk-services");
3330
+ async function httpResponseError(response, context) {
3331
+ let body;
3332
+ try {
3333
+ body = await response.text();
3334
+ } catch {
3335
+ body = response.statusText;
3336
+ }
3337
+ body = body.trim().slice(0, 512);
3338
+ return Object.assign(new Error(`${context}: HTTP ${response.status}${body ? ` - ${body}` : ""}`), {
3339
+ status: response.status
3340
+ });
3341
+ }
3342
+
3343
+ // src/policy/unified.ts
3327
3344
  var POLICY_V1_SCHEMA = "xyz.tinycloud.policy/policy/v1";
3328
3345
  var POLICY_CAPABILITY_V1_SCHEMA = "xyz.tinycloud.policy/capability/v1";
3329
3346
  var POLICY_ENFORCEMENT_DELEGATION_V2_SCHEMA = "xyz.tinycloud.policy/enforcement-delegation/v2";
@@ -3351,7 +3368,7 @@ async function requestPolicyChallengeV3(input) {
3351
3368
  headers: { accept: "application/json", "content-type": "application/json" },
3352
3369
  body: JSON.stringify({ policyCid: input.policyCid, recipientDid: input.recipientDid, requestedCapabilities: input.requestedCapabilities.map(normalizeUnifiedPolicyCapability) })
3353
3370
  });
3354
- if (!response.ok) throw new Error(`policy challenge rejected (${response.status})`);
3371
+ if (!response.ok) throw await httpResponseError(response, "policy challenge rejected");
3355
3372
  const value = object(await response.json(), "policy challenge");
3356
3373
  if (typeof value.challengeId !== "string" || typeof value.nonce !== "string" || value.policyCid !== input.policyCid || value.recipientDid !== input.recipientDid)
3357
3374
  throw new Error("policy challenge binding is invalid");
@@ -3376,7 +3393,7 @@ async function mintPolicySessionV3(input) {
3376
3393
  headers: { accept: "application/json", "content-type": "application/json" },
3377
3394
  body: JSON.stringify({ policyCid: input.policyCid, challengeId: challenge.challengeId, nonce: challenge.nonce, claim: input.claim, presentation: input.presentation })
3378
3395
  });
3379
- if (!response.ok) throw new Error(`policy delegation rejected (${response.status})`);
3396
+ if (!response.ok) throw await httpResponseError(response, "policy delegation rejected");
3380
3397
  const value = object(await response.json(), "policy delegation");
3381
3398
  if (value.admitted !== true || typeof value.sessionCid !== "string" || typeof value.authorization !== "string")
3382
3399
  throw new Error("policy delegation response is invalid");
@@ -3436,7 +3453,7 @@ async function getPolicyRootStatusV3(input) {
3436
3453
  redirect: "error",
3437
3454
  headers: { accept: "application/json" }
3438
3455
  });
3439
- if (!response.ok) throw new Error(`policy root status rejected (${response.status})`);
3456
+ if (!response.ok) throw await httpResponseError(response, "policy root status rejected");
3440
3457
  const value = object(await response.json(), "policy root status");
3441
3458
  const checkpoint = object(value.checkpoint, "policy root checkpoint");
3442
3459
  if (value.rootCid !== input.rootCid || value.state !== "active" && value.state !== "revoked" || value.state !== checkpoint.state || !verifyPolicyRootStatusCheckpointV3({ rootCid: input.rootCid, checkpoint, expectedNodeAudience: input.expectedNodeAudience, now: input.now }))
@@ -3496,7 +3513,7 @@ async function renewPolicyRootStatusV3(input) {
3496
3513
  headers: { accept: "application/json", "content-type": "application/json" },
3497
3514
  body: JSON.stringify({ rootCid: input.rootCid, renewal: { ...unsigned, signature: { suite: "Ed25519", signerDid: input.ownerDid, value: encodeBase64Url(signature) } } })
3498
3515
  });
3499
- if (!response.ok) throw new Error(`policy root renewal rejected (${response.status})`);
3516
+ if (!response.ok) throw await httpResponseError(response, "policy root renewal rejected");
3500
3517
  return object(await response.json(), "policy root renewal");
3501
3518
  }
3502
3519
  function rfc3339Seconds(value) {
@@ -3523,7 +3540,7 @@ async function revokePolicyRootV3(input) {
3523
3540
  headers: { accept: "application/json", "content-type": "application/json" },
3524
3541
  body: JSON.stringify({ revocation: { ...unsigned, signature: { suite: "Ed25519", signerDid: input.issuerDid, value: encodeBase64Url(signature) } } })
3525
3542
  });
3526
- if (!response.ok) throw new Error(`policy root revocation rejected (${response.status})`);
3543
+ if (!response.ok) throw await httpResponseError(response, "policy root revocation rejected");
3527
3544
  return object(await response.json(), "policy root revocation");
3528
3545
  }
3529
3546
  var POLICY_SESSION_FACT_KEYS = [
@@ -3792,7 +3809,7 @@ async function registerPolicyV3(input) {
3792
3809
  }
3793
3810
  );
3794
3811
  if (!bindingResponse.ok) {
3795
- throw new Error(`policy enforcer binding rejected (${bindingResponse.status})`);
3812
+ throw await httpResponseError(bindingResponse, "policy enforcer binding rejected");
3796
3813
  }
3797
3814
  const binding = exactObject(
3798
3815
  await bindingResponse.json(),
@@ -3858,7 +3875,7 @@ async function registerPolicyV3(input) {
3858
3875
  }
3859
3876
  );
3860
3877
  if (!response.ok) {
3861
- throw new Error(`policy registration rejected (${response.status})`);
3878
+ throw await httpResponseError(response, "policy registration rejected");
3862
3879
  }
3863
3880
  const receipt = exactObject(
3864
3881
  await response.json(),
@@ -4615,7 +4632,7 @@ async function admitPolicyCredentialV3(input) {
4615
4632
  requestedSessionExpiry(input.requestedExpiresAt, policy),
4616
4633
  input.signal
4617
4634
  );
4618
- if (!response.ok) throw new Error(`policy delegation rejected (${response.status})`);
4635
+ if (!response.ok) throw await httpResponseError(response, "policy delegation rejected");
4619
4636
  const value = record(await response.json(), "policy delegation");
4620
4637
  if (value.admitted !== true || typeof value.sessionCid !== "string" || typeof value.authorization !== "string") {
4621
4638
  throw new Error("policy delegation response is invalid");
@@ -4668,7 +4685,7 @@ async function admitPolicyCredentialV4(input) {
4668
4685
  requestedSessionExpiry(input.requestedExpiresAt, policy),
4669
4686
  input.signal
4670
4687
  );
4671
- if (!response.ok) throw new Error(`policy delegation rejected (${response.status})`);
4688
+ if (!response.ok) throw await httpResponseError(response, "policy delegation rejected");
4672
4689
  const value = record(await response.json(), "policy delegation");
4673
4690
  if (value.admitted !== true || typeof value.sessionCid !== "string" || typeof value.authorization !== "string") throw new Error("policy delegation response is invalid");
4674
4691
  const session = parsePolicySessionUcan(value.authorization, [
@@ -4687,7 +4704,7 @@ async function admitPolicyCredentialV4(input) {
4687
4704
  signal: input.signal,
4688
4705
  headers: { Authorization: session.authorization }
4689
4706
  });
4690
- if (!importResponse.ok) throw new Error(`delegation import rejected (${importResponse.status})`);
4707
+ if (!importResponse.ok) throw await httpResponseError(importResponse, "delegation import rejected");
4691
4708
  input.signal?.throwIfAborted();
4692
4709
  return Object.freeze({ challenge, presentation, session, delegationImported: true });
4693
4710
  }