@sparkvault/sdk-js 15.5.0 → 15.7.0

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/errors.d.ts CHANGED
@@ -21,7 +21,7 @@ export declare class SparkVaultError extends Error {
21
21
  * answer named none (a gateway's HTML). A typed refusal's `code` names its
22
22
  * class of failure: one per 400 (`validation_error`), two per 401
23
23
  * (`authentication_error`, `UNAUTHORIZED`), 402 (`PLAN_REQUIRED`,
24
- * `QUOTA_EXCEEDED`) and 403 (`authorization_error`, `NOTIFY_SUBSCRIPTION_REQUIRED`;
24
+ * `QUOTA_EXCEEDED`, `USAGE_CREDITS_EXHAUSTED`) and 403 (`authorization_error`, `NOTIFY_SUBSCRIPTION_REQUIRED`;
25
25
  * http.ts createErrorFromResponse), so this is what tells apart two
26
26
  * refusals that share a code.
27
27
  * Undefined on an error the SDK raised itself, on the Identity product's
@@ -66,6 +66,17 @@ export type QuotaResource = 'storage' | 'bandwidth';
66
66
  export declare class PlanRequiredError extends SparkVaultError {
67
67
  constructor(message: string, details?: Record<string, unknown>, retryAfterMs?: number | null, serviceCode?: string);
68
68
  }
69
+ /**
70
+ * Prepaid usage is exhausted (402 USAGE_CREDITS_EXHAUSTED). The subscription
71
+ * can still be active: an admin replenishes the shared usage-credit wallet.
72
+ */
73
+ export declare class UsageCreditsExhaustedError extends SparkVaultError {
74
+ constructor(message: string, details?: Record<string, unknown>, retryAfterMs?: number | null, serviceCode?: string);
75
+ }
76
+ /** A payment reversal requires billing review; buying more credits cannot clear it. */
77
+ export declare class UsageCreditFundingReviewError extends SparkVaultError {
78
+ constructor(message: string, details?: Record<string, unknown>, retryAfterMs?: number | null, serviceCode?: string);
79
+ }
69
80
  /**
70
81
  * Quota exceeded gate (402 QUOTA_EXCEEDED) — a pooled-capacity limit is
71
82
  * exhausted. `resource` identifies which pool so callers can point the CTA at
package/dist/index.cjs CHANGED
@@ -849,6 +849,25 @@ class PlanRequiredError extends SparkVaultError {
849
849
  Object.setPrototypeOf(this, PlanRequiredError.prototype);
850
850
  }
851
851
  }
852
+ /**
853
+ * Prepaid usage is exhausted (402 USAGE_CREDITS_EXHAUSTED). The subscription
854
+ * can still be active: an admin replenishes the shared usage-credit wallet.
855
+ */
856
+ class UsageCreditsExhaustedError extends SparkVaultError {
857
+ constructor(message, details, retryAfterMs = null, serviceCode) {
858
+ super(message, 'USAGE_CREDITS_EXHAUSTED', 402, details, retryAfterMs, serviceCode);
859
+ this.name = 'UsageCreditsExhaustedError';
860
+ Object.setPrototypeOf(this, UsageCreditsExhaustedError.prototype);
861
+ }
862
+ }
863
+ /** A payment reversal requires billing review; buying more credits cannot clear it. */
864
+ class UsageCreditFundingReviewError extends SparkVaultError {
865
+ constructor(message, details, retryAfterMs = null, serviceCode) {
866
+ super(message, 'USAGE_CREDIT_FUNDING_REVIEW', 402, details, retryAfterMs, serviceCode);
867
+ this.name = 'UsageCreditFundingReviewError';
868
+ Object.setPrototypeOf(this, UsageCreditFundingReviewError.prototype);
869
+ }
870
+ }
852
871
  /**
853
872
  * Quota exceeded gate (402 QUOTA_EXCEEDED) — a pooled-capacity limit is
854
873
  * exhausted. `resource` identifies which pool so callers can point the CTA at
@@ -1663,6 +1682,10 @@ class HttpClient {
1663
1682
  return new UnauthorizedError(message, details, retryAfterMs, code);
1664
1683
  return new AuthenticationError(message, details, retryAfterMs, code);
1665
1684
  case 402:
1685
+ if (code === 'USAGE_CREDIT_FUNDING_REVIEW')
1686
+ return new UsageCreditFundingReviewError(message, details, retryAfterMs, code);
1687
+ if (code === 'USAGE_CREDITS_EXHAUSTED')
1688
+ return new UsageCreditsExhaustedError(message, details, retryAfterMs, code);
1666
1689
  if (code === 'QUOTA_EXCEEDED')
1667
1690
  return new QuotaExceededError(message, details, retryAfterMs, code);
1668
1691
  // PLAN_REQUIRED is the canonical 402 gate; treat any other 402 as one too.
@@ -11609,6 +11632,11 @@ function requireNotifyProjectId(projectId) {
11609
11632
  field: 'projectId', received: projectId,
11610
11633
  });
11611
11634
  }
11635
+ function subscriptionPlan(options) {
11636
+ return options.billingModel === 'usage_credits'
11637
+ ? { billing_model: 'usage_credits' }
11638
+ : { notifications_per_month: options.notificationsPerMonth };
11639
+ }
11612
11640
  /** Company-level project management. Creation saves a draft and charges nothing. */
11613
11641
  class NotifyProjectsModule {
11614
11642
  constructor(http) {
@@ -11641,14 +11669,14 @@ class NotifyProjectsModule {
11641
11669
  /** Quote a named project's plan. Requires an interactive company-admin session. */
11642
11670
  async previewSubscription(projectId, options) {
11643
11671
  const id = requireNotifyProjectId(projectId);
11644
- const response = await this.http.post(apiPath `/v1/products/notify/projects/${id}/subscription/preview`, { notifications_per_month: options.notificationsPerMonth });
11672
+ const response = await this.http.post(apiPath `/v1/products/notify/projects/${id}/subscription/preview`, subscriptionPlan(options));
11645
11673
  return response.data;
11646
11674
  }
11647
11675
  /** Purchase the reviewed plan on the company's invoice; an API key cannot authorize spending. */
11648
11676
  async subscribe(projectId, options) {
11649
11677
  const id = requireNotifyProjectId(projectId);
11650
11678
  const response = await this.http.post(apiPath `/v1/products/notify/projects/${id}/subscription`, {
11651
- notifications_per_month: options.notificationsPerMonth,
11679
+ ...subscriptionPlan(options),
11652
11680
  quoted_amount_due_now_usd: options.quotedAmountDueNowUsd,
11653
11681
  proration_date: options.prorationDate,
11654
11682
  });
@@ -12871,6 +12899,16 @@ async function attemptDownload(link, state, options) {
12871
12899
  */
12872
12900
  async function refusedDownloadError(response, outcome, controller) {
12873
12901
  const statusMessage = `Download failed with status ${response.status}`;
12902
+ if (response.status === 402) {
12903
+ const failure = await withDeadline(readApiFailure(response), DOWNLOAD_IDLE_TIMEOUT_MS, controller, 'Download timed out reading the funding refusal');
12904
+ if (failure.code === 'USAGE_CREDITS_EXHAUSTED')
12905
+ return new UsageCreditsExhaustedError(failure.message, failure.details, failure.retryAfterMs, failure.code);
12906
+ if (failure.code === 'USAGE_CREDIT_FUNDING_REVIEW')
12907
+ return new UsageCreditFundingReviewError(failure.message, failure.details, failure.retryAfterMs, failure.code);
12908
+ if (failure.code === 'PLAN_REQUIRED')
12909
+ return new PlanRequiredError(failure.message, failure.details, failure.retryAfterMs, failure.code);
12910
+ return new SparkVaultError(statusMessage, 'download_failed', response.status);
12911
+ }
12874
12912
  const { coreCode } = outcome;
12875
12913
  if (outcome.verdict !== TRANSFER_VERDICT.REFUSED || coreCode === null) {
12876
12914
  return new SparkVaultError(statusMessage, 'download_failed', response.status);
@@ -25121,7 +25159,7 @@ class SparkVault {
25121
25159
  }
25122
25160
  /** Get the SDK version */
25123
25161
  static get version() {
25124
- return '15.5.0';
25162
+ return '15.7.0';
25125
25163
  }
25126
25164
  }
25127
25165
  if (typeof window !== 'undefined') {
@@ -25156,6 +25194,8 @@ exports.SparkVaultError = SparkVaultError;
25156
25194
  exports.SparksModule = SparksModule;
25157
25195
  exports.TimeoutError = TimeoutError;
25158
25196
  exports.UnauthorizedError = UnauthorizedError;
25197
+ exports.UsageCreditFundingReviewError = UsageCreditFundingReviewError;
25198
+ exports.UsageCreditsExhaustedError = UsageCreditsExhaustedError;
25159
25199
  exports.UserCancelledError = UserCancelledError;
25160
25200
  exports.ValidationError = ValidationError;
25161
25201
  exports.VaultsModule = VaultsModule;