@revstackhq/node 0.0.0-dev-20260226054346

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.
Files changed (111) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +136 -0
  3. package/dist/errors.d.ts +106 -0
  4. package/dist/errors.d.ts.map +1 -0
  5. package/dist/errors.js +102 -0
  6. package/dist/index.d.ts +75 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +94 -0
  9. package/dist/modules/admin/entitlements.d.ts +68 -0
  10. package/dist/modules/admin/entitlements.d.ts.map +1 -0
  11. package/dist/modules/admin/entitlements.js +87 -0
  12. package/dist/modules/admin/environments.d.ts +71 -0
  13. package/dist/modules/admin/environments.d.ts.map +1 -0
  14. package/dist/modules/admin/environments.js +84 -0
  15. package/dist/modules/admin/index.d.ts +30 -0
  16. package/dist/modules/admin/index.d.ts.map +1 -0
  17. package/dist/modules/admin/index.js +31 -0
  18. package/dist/modules/admin/integrations.d.ts +79 -0
  19. package/dist/modules/admin/integrations.d.ts.map +1 -0
  20. package/dist/modules/admin/integrations.js +97 -0
  21. package/dist/modules/admin/plans.d.ts +72 -0
  22. package/dist/modules/admin/plans.d.ts.map +1 -0
  23. package/dist/modules/admin/plans.js +93 -0
  24. package/dist/modules/admin/system.d.ts +53 -0
  25. package/dist/modules/admin/system.d.ts.map +1 -0
  26. package/dist/modules/admin/system.js +61 -0
  27. package/dist/modules/base.d.ts +43 -0
  28. package/dist/modules/base.d.ts.map +1 -0
  29. package/dist/modules/base.js +84 -0
  30. package/dist/modules/customers.d.ts +56 -0
  31. package/dist/modules/customers.d.ts.map +1 -0
  32. package/dist/modules/customers.js +72 -0
  33. package/dist/modules/entitlements.d.ts +49 -0
  34. package/dist/modules/entitlements.d.ts.map +1 -0
  35. package/dist/modules/entitlements.js +64 -0
  36. package/dist/modules/invoices.d.ts +33 -0
  37. package/dist/modules/invoices.d.ts.map +1 -0
  38. package/dist/modules/invoices.js +37 -0
  39. package/dist/modules/plans.d.ts +35 -0
  40. package/dist/modules/plans.d.ts.map +1 -0
  41. package/dist/modules/plans.js +39 -0
  42. package/dist/modules/subscriptions.d.ts +55 -0
  43. package/dist/modules/subscriptions.d.ts.map +1 -0
  44. package/dist/modules/subscriptions.js +71 -0
  45. package/dist/modules/usage.d.ts +59 -0
  46. package/dist/modules/usage.d.ts.map +1 -0
  47. package/dist/modules/usage.js +80 -0
  48. package/dist/modules/wallets.d.ts +53 -0
  49. package/dist/modules/wallets.d.ts.map +1 -0
  50. package/dist/modules/wallets.js +69 -0
  51. package/dist/modules/webhooks.d.ts +51 -0
  52. package/dist/modules/webhooks.d.ts.map +1 -0
  53. package/dist/modules/webhooks.js +90 -0
  54. package/dist/src/errors.d.ts +91 -0
  55. package/dist/src/errors.d.ts.map +1 -0
  56. package/dist/src/errors.js +101 -0
  57. package/dist/src/index.d.ts +71 -0
  58. package/dist/src/index.d.ts.map +1 -0
  59. package/dist/src/index.js +91 -0
  60. package/dist/src/modules/admin/entitlements.d.ts +73 -0
  61. package/dist/src/modules/admin/entitlements.d.ts.map +1 -0
  62. package/dist/src/modules/admin/entitlements.js +90 -0
  63. package/dist/src/modules/admin/environments.d.ts +76 -0
  64. package/dist/src/modules/admin/environments.d.ts.map +1 -0
  65. package/dist/src/modules/admin/environments.js +87 -0
  66. package/dist/src/modules/admin/index.d.ts +16 -0
  67. package/dist/src/modules/admin/index.d.ts.map +1 -0
  68. package/dist/src/modules/admin/index.js +16 -0
  69. package/dist/src/modules/admin/integrations.d.ts +84 -0
  70. package/dist/src/modules/admin/integrations.d.ts.map +1 -0
  71. package/dist/src/modules/admin/integrations.js +100 -0
  72. package/dist/src/modules/admin/plans.d.ts +77 -0
  73. package/dist/src/modules/admin/plans.d.ts.map +1 -0
  74. package/dist/src/modules/admin/plans.js +96 -0
  75. package/dist/src/modules/admin/system.d.ts +53 -0
  76. package/dist/src/modules/admin/system.d.ts.map +1 -0
  77. package/dist/src/modules/admin/system.js +61 -0
  78. package/dist/src/modules/base.d.ts +43 -0
  79. package/dist/src/modules/base.d.ts.map +1 -0
  80. package/dist/src/modules/base.js +84 -0
  81. package/dist/src/modules/customers.d.ts +61 -0
  82. package/dist/src/modules/customers.d.ts.map +1 -0
  83. package/dist/src/modules/customers.js +75 -0
  84. package/dist/src/modules/entitlements.d.ts +54 -0
  85. package/dist/src/modules/entitlements.d.ts.map +1 -0
  86. package/dist/src/modules/entitlements.js +67 -0
  87. package/dist/src/modules/invoices.d.ts +38 -0
  88. package/dist/src/modules/invoices.d.ts.map +1 -0
  89. package/dist/src/modules/invoices.js +40 -0
  90. package/dist/src/modules/plans.d.ts +40 -0
  91. package/dist/src/modules/plans.d.ts.map +1 -0
  92. package/dist/src/modules/plans.js +42 -0
  93. package/dist/src/modules/subscriptions.d.ts +60 -0
  94. package/dist/src/modules/subscriptions.d.ts.map +1 -0
  95. package/dist/src/modules/subscriptions.js +74 -0
  96. package/dist/src/modules/usage.d.ts +64 -0
  97. package/dist/src/modules/usage.d.ts.map +1 -0
  98. package/dist/src/modules/usage.js +83 -0
  99. package/dist/src/modules/wallets.d.ts +58 -0
  100. package/dist/src/modules/wallets.d.ts.map +1 -0
  101. package/dist/src/modules/wallets.js +72 -0
  102. package/dist/src/modules/webhooks.d.ts +51 -0
  103. package/dist/src/modules/webhooks.d.ts.map +1 -0
  104. package/dist/src/modules/webhooks.js +90 -0
  105. package/dist/src/types.d.ts +676 -0
  106. package/dist/src/types.d.ts.map +1 -0
  107. package/dist/src/types.js +7 -0
  108. package/dist/types.d.ts +720 -0
  109. package/dist/types.d.ts.map +1 -0
  110. package/dist/types.js +7 -0
  111. package/package.json +40 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Revstack Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # @revstackhq/node
2
+
3
+ The official Node.js / TypeScript server SDK for [Revstack](https://revstack.dev) — billing infrastructure for SaaS.
4
+
5
+ Provides a single `Revstack` client with two namespaces:
6
+
7
+ - **Data Plane** (`revstack.*`) — Daily backend operations: entitlement checks, usage reporting, subscriptions, customers, wallets, plans, invoices, and webhook verification.
8
+ - **Control Plane** (`revstack.admin.*`) — Infrastructure management: plan CRUD, entitlement CRUD, integrations, and environments.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install @revstackhq/node
14
+ ```
15
+
16
+ ## Quick Start
17
+
18
+ ```typescript
19
+ import { Revstack } from "@revstackhq/node";
20
+
21
+ const revstack = new Revstack({
22
+ secretKey: process.env.REVSTACK_SECRET_KEY!,
23
+ });
24
+ ```
25
+
26
+ ### Check Feature Access
27
+
28
+ ```typescript
29
+ const { allowed, limit, usage } = await revstack.entitlements.check(
30
+ "cus_abc123",
31
+ "api-calls",
32
+ );
33
+
34
+ if (!allowed) {
35
+ return res.status(403).json({ error: "Upgrade required" });
36
+ }
37
+ ```
38
+
39
+ ### Report Usage
40
+
41
+ ```typescript
42
+ await revstack.usage.report({
43
+ customer_id: "cus_abc123",
44
+ feature_id: "api-calls",
45
+ delta: 1,
46
+ });
47
+ ```
48
+
49
+ ### Manage Subscriptions
50
+
51
+ ```typescript
52
+ // Create a subscription
53
+ const sub = await revstack.subscriptions.create({
54
+ customer_id: "cus_abc123",
55
+ plan_id: "plan_pro",
56
+ });
57
+
58
+ // Change plan
59
+ await revstack.subscriptions.changePlan(sub.id, {
60
+ plan_id: "plan_enterprise",
61
+ });
62
+ ```
63
+
64
+ ### Verify Webhooks
65
+
66
+ ```typescript
67
+ const event = revstack.webhooks.constructEvent(
68
+ requestBody,
69
+ signatureHeader,
70
+ webhookSecret,
71
+ );
72
+ ```
73
+
74
+ ## API Reference
75
+
76
+ ### Data Plane Modules
77
+
78
+ | Module | Description |
79
+ | --------------- | ------------------------------------------- |
80
+ | `customers` | Create, update, and query customer records |
81
+ | `subscriptions` | Create, cancel, and manage subscriptions |
82
+ | `entitlements` | Check feature access and query entitlements |
83
+ | `usage` | Report and query metered feature usage |
84
+ | `wallets` | Manage customer wallet balances |
85
+ | `webhooks` | Verify inbound webhook signatures |
86
+ | `plans` | Query billing plans (read-only) |
87
+ | `invoices` | Query billing invoices (read-only) |
88
+
89
+ ### Control Plane (`admin`)
90
+
91
+ | Module | Description |
92
+ | -------------------- | ------------------------------------ |
93
+ | `admin.plans` | CRUD operations for billing plans |
94
+ | `admin.entitlements` | CRUD operations for entitlements |
95
+ | `admin.integrations` | Manage payment provider integrations |
96
+ | `admin.environments` | Manage deployment environments |
97
+
98
+ ### Error Hierarchy
99
+
100
+ The SDK provides a structured error hierarchy for precise error handling:
101
+
102
+ ```typescript
103
+ import { Revstack, RateLimitError, RevstackAPIError } from "@revstackhq/node";
104
+
105
+ try {
106
+ await revstack.entitlements.check("cus_abc", "api-calls");
107
+ } catch (error) {
108
+ if (error instanceof RateLimitError) {
109
+ // Retry after error.retryAfter seconds
110
+ } else if (error instanceof RevstackAPIError) {
111
+ // API returned a non-2xx status
112
+ console.error(error.status, error.message);
113
+ }
114
+ }
115
+ ```
116
+
117
+ | Error | Description |
118
+ | ---------------------------- | ------------------------------------------- |
119
+ | `RevstackError` | Base error class for all SDK errors |
120
+ | `RevstackAPIError` | API returned a non-2xx response |
121
+ | `RateLimitError` | Rate limit exceeded (includes `retryAfter`) |
122
+ | `SignatureVerificationError` | Webhook signature verification failed |
123
+
124
+ ### Constructor Options
125
+
126
+ ```typescript
127
+ new Revstack({
128
+ secretKey: "sk_live_...", // Required — your Revstack Secret Key
129
+ baseUrl: "https://...", // Optional — API base URL (default: https://app.revstack.dev/api/v1)
130
+ timeout: 10000, // Optional — request timeout in ms (default: 10000)
131
+ });
132
+ ```
133
+
134
+ ## License
135
+
136
+ MIT
@@ -0,0 +1,106 @@
1
+ /**
2
+ * @module @revstackhq/node/errors
3
+ * @description Typed error hierarchy for the Revstack SDK. All errors thrown by the
4
+ * SDK extend {@link RevstackError}, enabling precise error handling with `instanceof`.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { RateLimitError, RevstackAPIError } from "@revstackhq/node";
9
+ *
10
+ * try {
11
+ * await revstack.entitlements.check(userId, "api-calls");
12
+ * } catch (error) {
13
+ * if (error instanceof RateLimitError) {
14
+ * console.log(`Rate limited. Retry after ${error.retryAfter}s`);
15
+ * } else if (error instanceof RevstackAPIError) {
16
+ * console.log(`API error ${error.status}: ${error.code}`);
17
+ * }
18
+ * }
19
+ * ```
20
+ */
21
+ /**
22
+ * Base class for all Revstack SDK errors.
23
+ * Use `instanceof RevstackError` to catch any SDK-originated error.
24
+ */
25
+ export declare class RevstackError extends Error {
26
+ constructor(message: string);
27
+ }
28
+ /** Shape of the JSON error body returned by the Revstack API. */
29
+ export interface APIErrorResponse {
30
+ /** Human-readable error message. */
31
+ message?: string;
32
+ /** Machine-readable error code (e.g. `INSUFFICIENT_BALANCE`). */
33
+ code?: string;
34
+ /** Unique request identifier for debugging and support tickets. */
35
+ requestId?: string;
36
+ /** Seconds to wait before retrying (for 429 responses). */
37
+ retryAfter?: number;
38
+ /** List of conflicts (for 409 sync responses). */
39
+ conflicts?: SyncConflict[];
40
+ }
41
+ /**
42
+ * Thrown when the Revstack API returns a non-2xx HTTP response.
43
+ * Contains structured fields for programmatic error handling.
44
+ */
45
+ export declare class RevstackAPIError extends RevstackError {
46
+ /** HTTP status code (e.g. `400`, `401`, `404`, `500`). */
47
+ readonly status: number;
48
+ /** Machine-readable error code (e.g. `INSUFFICIENT_BALANCE`, `NOT_FOUND`). */
49
+ readonly code: string;
50
+ /** Unique request ID for tracing. Include this in support tickets. */
51
+ readonly requestId?: string;
52
+ constructor(message: string, status: number, code: string, requestId?: string);
53
+ }
54
+ /**
55
+ * Thrown when the API returns HTTP 429 (Too Many Requests).
56
+ * The `retryAfter` field indicates how long to wait before retrying.
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * try {
61
+ * await revstack.usage.report(params);
62
+ * } catch (error) {
63
+ * if (error instanceof RateLimitError) {
64
+ * await sleep(error.retryAfter * 1000);
65
+ * await revstack.usage.report(params); // retry
66
+ * }
67
+ * }
68
+ * ```
69
+ */
70
+ export declare class RateLimitError extends RevstackAPIError {
71
+ /** Number of seconds to wait before retrying the request. */
72
+ readonly retryAfter: number;
73
+ constructor(message: string, retryAfter: number, requestId?: string);
74
+ }
75
+ /**
76
+ * Thrown when webhook signature verification fails.
77
+ * This indicates the webhook payload was tampered with, the signing secret is wrong,
78
+ * or the webhook timestamp has expired (replay protection).
79
+ */
80
+ export declare class SignatureVerificationError extends RevstackError {
81
+ constructor(message?: string);
82
+ }
83
+ /** Describes a single field-level conflict detected during a sync operation. */
84
+ export interface SyncConflict {
85
+ /** The type of resource that has a conflict. */
86
+ resource: string;
87
+ /** The slug (natural key) of the conflicting resource. */
88
+ slug: string;
89
+ /** The field that has a conflicting value. */
90
+ field: string;
91
+ /** The value from the sync config. */
92
+ expected: unknown;
93
+ /** The current value in the database. */
94
+ actual: unknown;
95
+ }
96
+ /**
97
+ * Thrown when `admin.system.sync()` detects a conflict (HTTP 409).
98
+ * This means the remote state has drifted from the expected state.
99
+ * Inspect the `conflicts` array to understand what changed.
100
+ */
101
+ export declare class SyncConflictError extends RevstackAPIError {
102
+ /** Detailed list of field-level conflicts between local config and remote state. */
103
+ readonly conflicts: SyncConflict[];
104
+ constructor(message: string, conflicts: SyncConflict[], requestId?: string);
105
+ }
106
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAIH;;;GAGG;AACH,qBAAa,aAAc,SAAQ,KAAK;gBAC1B,OAAO,EAAE,MAAM;CAI5B;AAID,iEAAiE;AACjE,MAAM,WAAW,gBAAgB;IAC/B,oCAAoC;IACpC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mEAAmE;IACnE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,2DAA2D;IAC3D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kDAAkD;IAClD,SAAS,CAAC,EAAE,YAAY,EAAE,CAAC;CAC5B;AAED;;;GAGG;AACH,qBAAa,gBAAiB,SAAQ,aAAa;IACjD,0DAA0D;IAC1D,SAAgB,MAAM,EAAE,MAAM,CAAC;IAC/B,8EAA8E;IAC9E,SAAgB,IAAI,EAAE,MAAM,CAAC;IAC7B,sEAAsE;IACtE,SAAgB,SAAS,CAAC,EAAE,MAAM,CAAC;gBAGjC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,SAAS,CAAC,EAAE,MAAM;CAQrB;AAID;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,cAAe,SAAQ,gBAAgB;IAClD,6DAA6D;IAC7D,SAAgB,UAAU,EAAE,MAAM,CAAC;gBAEvB,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM;CAKpE;AAID;;;;GAIG;AACH,qBAAa,0BAA2B,SAAQ,aAAa;gBAC/C,OAAO,GAAE,MAAgD;CAItE;AAID,gFAAgF;AAChF,MAAM,WAAW,YAAY;IAC3B,gDAAgD;IAChD,QAAQ,EAAE,MAAM,CAAC;IACjB,0DAA0D;IAC1D,IAAI,EAAE,MAAM,CAAC;IACb,8CAA8C;IAC9C,KAAK,EAAE,MAAM,CAAC;IACd,sCAAsC;IACtC,QAAQ,EAAE,OAAO,CAAC;IAClB,yCAAyC;IACzC,MAAM,EAAE,OAAO,CAAC;CACjB;AAED;;;;GAIG;AACH,qBAAa,iBAAkB,SAAQ,gBAAgB;IACrD,oFAAoF;IACpF,SAAgB,SAAS,EAAE,YAAY,EAAE,CAAC;gBAE9B,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE,SAAS,CAAC,EAAE,MAAM;CAK3E"}
package/dist/errors.js ADDED
@@ -0,0 +1,102 @@
1
+ /**
2
+ * @module @revstackhq/node/errors
3
+ * @description Typed error hierarchy for the Revstack SDK. All errors thrown by the
4
+ * SDK extend {@link RevstackError}, enabling precise error handling with `instanceof`.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { RateLimitError, RevstackAPIError } from "@revstackhq/node";
9
+ *
10
+ * try {
11
+ * await revstack.entitlements.check(userId, "api-calls");
12
+ * } catch (error) {
13
+ * if (error instanceof RateLimitError) {
14
+ * console.log(`Rate limited. Retry after ${error.retryAfter}s`);
15
+ * } else if (error instanceof RevstackAPIError) {
16
+ * console.log(`API error ${error.status}: ${error.code}`);
17
+ * }
18
+ * }
19
+ * ```
20
+ */
21
+ // ─── Base Error ──────────────────────────────────────────────
22
+ /**
23
+ * Base class for all Revstack SDK errors.
24
+ * Use `instanceof RevstackError` to catch any SDK-originated error.
25
+ */
26
+ export class RevstackError extends Error {
27
+ constructor(message) {
28
+ super(message);
29
+ this.name = "RevstackError";
30
+ }
31
+ }
32
+ /**
33
+ * Thrown when the Revstack API returns a non-2xx HTTP response.
34
+ * Contains structured fields for programmatic error handling.
35
+ */
36
+ export class RevstackAPIError extends RevstackError {
37
+ /** HTTP status code (e.g. `400`, `401`, `404`, `500`). */
38
+ status;
39
+ /** Machine-readable error code (e.g. `INSUFFICIENT_BALANCE`, `NOT_FOUND`). */
40
+ code;
41
+ /** Unique request ID for tracing. Include this in support tickets. */
42
+ requestId;
43
+ constructor(message, status, code, requestId) {
44
+ super(message);
45
+ this.name = "RevstackAPIError";
46
+ this.status = status;
47
+ this.code = code;
48
+ this.requestId = requestId;
49
+ }
50
+ }
51
+ // ─── Rate Limit ──────────────────────────────────────────────
52
+ /**
53
+ * Thrown when the API returns HTTP 429 (Too Many Requests).
54
+ * The `retryAfter` field indicates how long to wait before retrying.
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * try {
59
+ * await revstack.usage.report(params);
60
+ * } catch (error) {
61
+ * if (error instanceof RateLimitError) {
62
+ * await sleep(error.retryAfter * 1000);
63
+ * await revstack.usage.report(params); // retry
64
+ * }
65
+ * }
66
+ * ```
67
+ */
68
+ export class RateLimitError extends RevstackAPIError {
69
+ /** Number of seconds to wait before retrying the request. */
70
+ retryAfter;
71
+ constructor(message, retryAfter, requestId) {
72
+ super(message, 429, "RATE_LIMIT_EXCEEDED", requestId);
73
+ this.name = "RateLimitError";
74
+ this.retryAfter = retryAfter;
75
+ }
76
+ }
77
+ // ─── Webhook Signature ──────────────────────────────────────
78
+ /**
79
+ * Thrown when webhook signature verification fails.
80
+ * This indicates the webhook payload was tampered with, the signing secret is wrong,
81
+ * or the webhook timestamp has expired (replay protection).
82
+ */
83
+ export class SignatureVerificationError extends RevstackError {
84
+ constructor(message = "Webhook signature verification failed") {
85
+ super(message);
86
+ this.name = "SignatureVerificationError";
87
+ }
88
+ }
89
+ /**
90
+ * Thrown when `admin.system.sync()` detects a conflict (HTTP 409).
91
+ * This means the remote state has drifted from the expected state.
92
+ * Inspect the `conflicts` array to understand what changed.
93
+ */
94
+ export class SyncConflictError extends RevstackAPIError {
95
+ /** Detailed list of field-level conflicts between local config and remote state. */
96
+ conflicts;
97
+ constructor(message, conflicts, requestId) {
98
+ super(message, 409, "SYNC_CONFLICT", requestId);
99
+ this.name = "SyncConflictError";
100
+ this.conflicts = conflicts;
101
+ }
102
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * @module @revstackhq/node
3
+ * @description Official Node.js SDK for Revstack — billing infrastructure for SaaS.
4
+ *
5
+ * The SDK provides two namespaces:
6
+ * - **Data Plane** (`revstack.*`) — Daily backend operations: entitlement checks, usage reporting, subscriptions.
7
+ * - **Control Plane** (`revstack.admin.*`) — Infrastructure management: plan CRUD, Billing as Code sync.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { Revstack, RateLimitError } from "@revstackhq/node";
12
+ *
13
+ * const revstack = new Revstack({ secretKey: "sk_live_..." });
14
+ *
15
+ * // Data Plane: check if a customer can use a feature
16
+ * const { allowed } = await revstack.entitlements.check("usr_abc", "api-calls");
17
+ *
18
+ * // Control Plane: sync billing config from code
19
+ * await revstack.admin.system.sync({ plans: [...], entitlements: [...] });
20
+ * ```
21
+ *
22
+ * @packageDocumentation
23
+ */
24
+ import { RevstackOptions } from "@/types";
25
+ import { EntitlementsClient } from "@/modules/entitlements";
26
+ import { WalletsClient } from "@/modules/wallets";
27
+ import { CustomersClient } from "@/modules/customers";
28
+ import { UsageClient } from "@/modules/usage";
29
+ import { SubscriptionsClient } from "@/modules/subscriptions";
30
+ import { PlansClient } from "@/modules/plans";
31
+ import { InvoicesClient } from "@/modules/invoices";
32
+ import { WebhooksClient } from "@/modules/webhooks";
33
+ import { AdminClient } from "@/modules/admin";
34
+ /**
35
+ * Main SDK client. Initialize with your secret key to access all modules.
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * const revstack = new Revstack({
40
+ * secretKey: process.env.REVSTACK_SECRET_KEY!,
41
+ * });
42
+ * ```
43
+ */
44
+ export declare class Revstack {
45
+ /** Manage end-user (customer) records. */
46
+ readonly customers: CustomersClient;
47
+ /** Create, cancel, and manage subscriptions. */
48
+ readonly subscriptions: SubscriptionsClient;
49
+ /** Check feature access and query entitlements. */
50
+ readonly entitlements: EntitlementsClient;
51
+ /** Report and query metered feature usage. */
52
+ readonly usage: UsageClient;
53
+ /** Manage customer wallet balances. */
54
+ readonly wallets: WalletsClient;
55
+ /** Verify inbound webhook signatures. */
56
+ readonly webhooks: WebhooksClient;
57
+ /** Query billing plans (read-only). */
58
+ readonly plans: PlansClient;
59
+ /** Query billing invoices (read-only). */
60
+ readonly invoices: InvoicesClient;
61
+ /** Administrative operations for plans, entitlements, integrations, and environments. */
62
+ readonly admin: AdminClient;
63
+ private readonly config;
64
+ /**
65
+ * Create a new Revstack SDK instance.
66
+ *
67
+ * @param options - Configuration options including the secret API key.
68
+ * @throws {Error} If `secretKey` is missing.
69
+ */
70
+ constructor(options: RevstackOptions);
71
+ }
72
+ export { RevstackError, RevstackAPIError, RateLimitError, SignatureVerificationError, SyncConflictError, } from "@/errors";
73
+ export type { SyncConflict } from "@/errors";
74
+ export type { RevstackOptions, Customer, IdentifyCustomerParams, UpdateCustomerParams, Subscription, CreateSubscriptionParams, ChangePlanParams, ListSubscriptionsParams, Entitlement, EntitlementCheckOptions, EntitlementCheckResult, UsageMeter, ReportUsageParams, RevertUsageParams, BalanceResponse, GrantBalanceParams, RevokeBalanceParams, Plan, Price, PlanEntitlement, ListPlansParams, Invoice, ListInvoicesParams, WebhookEvent, ListParams, PaginatedResponse, CreatePlanParams, UpdatePlanParams, UpsertPlanParams, UpsertPriceInput, UpsertPlanEntitlementInput, CreateEntitlementParams, UpdateEntitlementParams, UpsertEntitlementParams, Integration, CreateIntegrationParams, UpdateIntegrationParams, ProviderEvent, IntegrationMetric, ListIntegrationsParams, ListProviderEventsParams, ListMetricsParams, Environment, CreateEnvironmentParams, UpdateEnvironmentParams, SyncConfig, SyncPreview, SyncChange, SyncResult, } from "@/types";
75
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C;;;;;;;;;GASG;AACH,qBAAa,QAAQ;IAGnB,0CAA0C;IAC1C,SAAgB,SAAS,EAAE,eAAe,CAAC;IAC3C,gDAAgD;IAChD,SAAgB,aAAa,EAAE,mBAAmB,CAAC;IACnD,mDAAmD;IACnD,SAAgB,YAAY,EAAE,kBAAkB,CAAC;IACjD,8CAA8C;IAC9C,SAAgB,KAAK,EAAE,WAAW,CAAC;IACnC,uCAAuC;IACvC,SAAgB,OAAO,EAAE,aAAa,CAAC;IACvC,yCAAyC;IACzC,SAAgB,QAAQ,EAAE,cAAc,CAAC;IACzC,uCAAuC;IACvC,SAAgB,KAAK,EAAE,WAAW,CAAC;IACnC,0CAA0C;IAC1C,SAAgB,QAAQ,EAAE,cAAc,CAAC;IAIzC,yFAAyF;IACzF,SAAgB,KAAK,EAAE,WAAW,CAAC;IAEnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAIrB;IAEF;;;;;OAKG;gBACS,OAAO,EAAE,eAAe;CA0BrC;AAGD,OAAO,EACL,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,0BAA0B,EAC1B,iBAAiB,GAClB,MAAM,UAAU,CAAC;AAElB,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAG7C,YAAY,EACV,eAAe,EACf,QAAQ,EACR,sBAAsB,EACtB,oBAAoB,EACpB,YAAY,EACZ,wBAAwB,EACxB,gBAAgB,EAChB,uBAAuB,EACvB,WAAW,EACX,uBAAuB,EACvB,sBAAsB,EACtB,UAAU,EACV,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,kBAAkB,EAClB,mBAAmB,EACnB,IAAI,EACJ,KAAK,EACL,eAAe,EACf,eAAe,EACf,OAAO,EACP,kBAAkB,EAClB,YAAY,EACZ,UAAU,EACV,iBAAiB,EAGjB,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,0BAA0B,EAC1B,uBAAuB,EACvB,uBAAuB,EACvB,uBAAuB,EACvB,WAAW,EACX,uBAAuB,EACvB,uBAAuB,EACvB,aAAa,EACb,iBAAiB,EACjB,sBAAsB,EACtB,wBAAwB,EACxB,iBAAiB,EACjB,WAAW,EACX,uBAAuB,EACvB,uBAAuB,EACvB,UAAU,EACV,WAAW,EACX,UAAU,EACV,UAAU,GACX,MAAM,SAAS,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @module @revstackhq/node
3
+ * @description Official Node.js SDK for Revstack — billing infrastructure for SaaS.
4
+ *
5
+ * The SDK provides two namespaces:
6
+ * - **Data Plane** (`revstack.*`) — Daily backend operations: entitlement checks, usage reporting, subscriptions.
7
+ * - **Control Plane** (`revstack.admin.*`) — Infrastructure management: plan CRUD, Billing as Code sync.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { Revstack, RateLimitError } from "@revstackhq/node";
12
+ *
13
+ * const revstack = new Revstack({ secretKey: "sk_live_..." });
14
+ *
15
+ * // Data Plane: check if a customer can use a feature
16
+ * const { allowed } = await revstack.entitlements.check("usr_abc", "api-calls");
17
+ *
18
+ * // Control Plane: sync billing config from code
19
+ * await revstack.admin.system.sync({ plans: [...], entitlements: [...] });
20
+ * ```
21
+ *
22
+ * @packageDocumentation
23
+ */
24
+ import { EntitlementsClient } from "@/modules/entitlements";
25
+ import { WalletsClient } from "@/modules/wallets";
26
+ import { CustomersClient } from "@/modules/customers";
27
+ import { UsageClient } from "@/modules/usage";
28
+ import { SubscriptionsClient } from "@/modules/subscriptions";
29
+ import { PlansClient } from "@/modules/plans";
30
+ import { InvoicesClient } from "@/modules/invoices";
31
+ import { WebhooksClient } from "@/modules/webhooks";
32
+ import { AdminClient } from "@/modules/admin";
33
+ /**
34
+ * Main SDK client. Initialize with your secret key to access all modules.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * const revstack = new Revstack({
39
+ * secretKey: process.env.REVSTACK_SECRET_KEY!,
40
+ * });
41
+ * ```
42
+ */
43
+ export class Revstack {
44
+ // ─── Data Plane (S2S) ──────────────────────────
45
+ /** Manage end-user (customer) records. */
46
+ customers;
47
+ /** Create, cancel, and manage subscriptions. */
48
+ subscriptions;
49
+ /** Check feature access and query entitlements. */
50
+ entitlements;
51
+ /** Report and query metered feature usage. */
52
+ usage;
53
+ /** Manage customer wallet balances. */
54
+ wallets;
55
+ /** Verify inbound webhook signatures. */
56
+ webhooks;
57
+ /** Query billing plans (read-only). */
58
+ plans;
59
+ /** Query billing invoices (read-only). */
60
+ invoices;
61
+ // ─── Control Plane (CLI / Admin) ───────────────
62
+ /** Administrative operations for plans, entitlements, integrations, and environments. */
63
+ admin;
64
+ config;
65
+ /**
66
+ * Create a new Revstack SDK instance.
67
+ *
68
+ * @param options - Configuration options including the secret API key.
69
+ * @throws {Error} If `secretKey` is missing.
70
+ */
71
+ constructor(options) {
72
+ if (!options.secretKey) {
73
+ throw new Error("Revstack: Secret Key is required. Get yours at https://app.revstack.dev");
74
+ }
75
+ this.config = {
76
+ secretKey: options.secretKey,
77
+ baseUrl: options.baseUrl || "https://app.revstack.dev/api/v1",
78
+ timeout: options.timeout || 10000,
79
+ };
80
+ // Data Plane
81
+ this.customers = new CustomersClient(this.config);
82
+ this.subscriptions = new SubscriptionsClient(this.config);
83
+ this.entitlements = new EntitlementsClient(this.config);
84
+ this.usage = new UsageClient(this.config);
85
+ this.wallets = new WalletsClient(this.config);
86
+ this.webhooks = new WebhooksClient();
87
+ this.plans = new PlansClient(this.config);
88
+ this.invoices = new InvoicesClient(this.config);
89
+ // Control Plane
90
+ this.admin = new AdminClient(this.config);
91
+ }
92
+ }
93
+ // ─── Errors ──────────────────────────────────────
94
+ export { RevstackError, RevstackAPIError, RateLimitError, SignatureVerificationError, SyncConflictError, } from "@/errors";
@@ -0,0 +1,68 @@
1
+ import { BaseClient } from "@/modules/base";
2
+ import { Entitlement, CreateEntitlementParams, UpdateEntitlementParams, UpsertEntitlementParams, ListParams, PaginatedResponse } from "@/types";
3
+ /**
4
+ * Admin client for managing entitlement definitions (CRUD + upsert).
5
+ * Entitlements define the features and capabilities that can be gated behind plans.
6
+ *
7
+ * The `upsert()` method is idempotent by `slug`, making it safe for repeated
8
+ * CLI deployments without creating duplicates.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * await revstack.admin.entitlements.upsert({
13
+ * slug: "api-calls",
14
+ * name: "API Calls",
15
+ * type: "metered",
16
+ * unitType: "count",
17
+ * });
18
+ * ```
19
+ */
20
+ export declare class AdminEntitlementsClient extends BaseClient {
21
+ /**
22
+ * List all entitlement definitions with optional pagination.
23
+ *
24
+ * @param params - Pagination parameters.
25
+ * @returns A paginated list of entitlements.
26
+ */
27
+ list(params?: ListParams): Promise<PaginatedResponse<Entitlement>>;
28
+ /**
29
+ * Retrieve a single entitlement definition by ID.
30
+ *
31
+ * @param entitlementId - The entitlement's unique identifier.
32
+ * @returns The entitlement definition.
33
+ */
34
+ get(entitlementId: string): Promise<Entitlement>;
35
+ /**
36
+ * Create a new entitlement definition.
37
+ *
38
+ * @param params - Entitlement creation parameters.
39
+ * @returns The newly created entitlement.
40
+ */
41
+ create(params: CreateEntitlementParams): Promise<Entitlement>;
42
+ /**
43
+ * Partially update an existing entitlement definition.
44
+ *
45
+ * @param entitlementId - The entitlement's unique identifier.
46
+ * @param params - Fields to update.
47
+ * @returns The updated entitlement.
48
+ */
49
+ update(entitlementId: string, params: UpdateEntitlementParams): Promise<Entitlement>;
50
+ /**
51
+ * Delete an entitlement definition. Fails if any active plans reference it.
52
+ *
53
+ * @param entitlementId - The entitlement's unique identifier.
54
+ * @returns Confirmation of deletion.
55
+ */
56
+ delete(entitlementId: string): Promise<{
57
+ success: boolean;
58
+ }>;
59
+ /**
60
+ * Idempotently create or update an entitlement by its `slug`.
61
+ * If an entitlement with the given slug exists, it is updated; otherwise, it is created.
62
+ *
63
+ * @param params - Full entitlement state.
64
+ * @returns The created or updated entitlement.
65
+ */
66
+ upsert(params: UpsertEntitlementParams): Promise<Entitlement>;
67
+ }
68
+ //# sourceMappingURL=entitlements.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entitlements.d.ts","sourceRoot":"","sources":["../../../src/modules/admin/entitlements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EACL,WAAW,EACX,uBAAuB,EACvB,uBAAuB,EACvB,uBAAuB,EACvB,UAAU,EACV,iBAAiB,EAClB,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,uBAAwB,SAAQ,UAAU;IACrD;;;;;OAKG;IACG,IAAI,CAAC,MAAM,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;IAOxE;;;;;OAKG;IACG,GAAG,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAMtD;;;;;OAKG;IACG,MAAM,CAAC,MAAM,EAAE,uBAAuB,GAAG,OAAO,CAAC,WAAW,CAAC;IAOnE;;;;;;OAMG;IACG,MAAM,CACV,aAAa,EAAE,MAAM,EACrB,MAAM,EAAE,uBAAuB,GAC9B,OAAO,CAAC,WAAW,CAAC;IAOvB;;;;;OAKG;IACG,MAAM,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;IAOlE;;;;;;OAMG;IACG,MAAM,CAAC,MAAM,EAAE,uBAAuB,GAAG,OAAO,CAAC,WAAW,CAAC;CAMpE"}