@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
@@ -0,0 +1,58 @@
1
+ import { BaseClient } from "@/modules/base";
2
+ import { BalanceResponse, GrantBalanceParams, RevokeBalanceParams } from "@/types";
3
+ /**
4
+ * Client for managing customer wallet balances.
5
+ * Supports granting credits (bonuses, top-ups) and revoking them (refunds, penalties).
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // Grant a welcome bonus
10
+ * await revstack.wallets.grantBalance({
11
+ * customerId: "usr_abc",
12
+ * currency: "USD",
13
+ * amount: 10.00,
14
+ * description: "Welcome bonus",
15
+ * });
16
+ *
17
+ * // Check remaining balance
18
+ * const { amount } = await revstack.wallets.getBalance("usr_abc", "USD");
19
+ * ```
20
+ */
21
+ export declare class WalletsClient extends BaseClient {
22
+ constructor(config: {
23
+ secretKey: string;
24
+ baseUrl: string;
25
+ timeout: number;
26
+ });
27
+ /**
28
+ * Retrieve a customer's balance for a specific currency.
29
+ *
30
+ * @param customerId - The customer whose balance to query.
31
+ * @param currency - ISO 4217 currency code (e.g. `USD`).
32
+ * @returns The balance response with currency and amount.
33
+ */
34
+ getBalance(customerId: string, currency: string): Promise<BalanceResponse>;
35
+ /**
36
+ * List all balances across all currencies for a customer.
37
+ *
38
+ * @param customerId - The customer whose balances to list.
39
+ * @returns Array of balance responses (one per currency).
40
+ */
41
+ listBalances(customerId: string): Promise<BalanceResponse[]>;
42
+ /**
43
+ * Grant (add) credit to a customer's wallet.
44
+ *
45
+ * @param params - Grant parameters including amount, currency, and description.
46
+ * @returns The updated balance after the grant.
47
+ */
48
+ grantBalance(params: GrantBalanceParams): Promise<BalanceResponse>;
49
+ /**
50
+ * Revoke (deduct) credit from a customer's wallet.
51
+ * Used for manual deductions, penalties, or credit refunds.
52
+ *
53
+ * @param params - Revoke parameters including amount, currency, and audit reason.
54
+ * @returns The updated balance after the deduction.
55
+ */
56
+ revokeBalance(params: RevokeBalanceParams): Promise<BalanceResponse>;
57
+ }
58
+ //# sourceMappingURL=wallets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wallets.d.ts","sourceRoot":"","sources":["../../../src/modules/wallets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,aAAc,SAAQ,UAAU;gBAC/B,MAAM,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE;IAI3E;;;;;;OAMG;IACG,UAAU,CACd,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,eAAe,CAAC;IAM3B;;;;;OAKG;IACG,YAAY,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAMlE;;;;;OAKG;IACG,YAAY,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,eAAe,CAAC;IAOxE;;;;;;OAMG;IACG,aAAa,CAAC,MAAM,EAAE,mBAAmB,GAAG,OAAO,CAAC,eAAe,CAAC;CAM3E"}
@@ -0,0 +1,72 @@
1
+ import { BaseClient } from "@/modules/base";
2
+ /**
3
+ * Client for managing customer wallet balances.
4
+ * Supports granting credits (bonuses, top-ups) and revoking them (refunds, penalties).
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * // Grant a welcome bonus
9
+ * await revstack.wallets.grantBalance({
10
+ * customerId: "usr_abc",
11
+ * currency: "USD",
12
+ * amount: 10.00,
13
+ * description: "Welcome bonus",
14
+ * });
15
+ *
16
+ * // Check remaining balance
17
+ * const { amount } = await revstack.wallets.getBalance("usr_abc", "USD");
18
+ * ```
19
+ */
20
+ export class WalletsClient extends BaseClient {
21
+ constructor(config) {
22
+ super(config);
23
+ }
24
+ /**
25
+ * Retrieve a customer's balance for a specific currency.
26
+ *
27
+ * @param customerId - The customer whose balance to query.
28
+ * @param currency - ISO 4217 currency code (e.g. `USD`).
29
+ * @returns The balance response with currency and amount.
30
+ */
31
+ async getBalance(customerId, currency) {
32
+ return this.request(`/wallets/${customerId}/${currency}`, {
33
+ method: "GET",
34
+ });
35
+ }
36
+ /**
37
+ * List all balances across all currencies for a customer.
38
+ *
39
+ * @param customerId - The customer whose balances to list.
40
+ * @returns Array of balance responses (one per currency).
41
+ */
42
+ async listBalances(customerId) {
43
+ return this.request(`/wallets/${customerId}`, {
44
+ method: "GET",
45
+ });
46
+ }
47
+ /**
48
+ * Grant (add) credit to a customer's wallet.
49
+ *
50
+ * @param params - Grant parameters including amount, currency, and description.
51
+ * @returns The updated balance after the grant.
52
+ */
53
+ async grantBalance(params) {
54
+ return this.request("/wallets/grant", {
55
+ method: "POST",
56
+ body: JSON.stringify(params),
57
+ });
58
+ }
59
+ /**
60
+ * Revoke (deduct) credit from a customer's wallet.
61
+ * Used for manual deductions, penalties, or credit refunds.
62
+ *
63
+ * @param params - Revoke parameters including amount, currency, and audit reason.
64
+ * @returns The updated balance after the deduction.
65
+ */
66
+ async revokeBalance(params) {
67
+ return this.request("/wallets/revoke", {
68
+ method: "POST",
69
+ body: JSON.stringify(params),
70
+ });
71
+ }
72
+ }
@@ -0,0 +1,51 @@
1
+ import type { WebhookEvent } from "@/types";
2
+ /**
3
+ * Client for verifying inbound webhooks from Revstack Cloud.
4
+ * Uses HMAC-SHA256 with constant-time comparison to prevent timing attacks,
5
+ * plus timestamp validation for replay protection.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // Express.js webhook handler
10
+ * app.post("/webhooks/revstack", express.raw({ type: "application/json" }), (req, res) => {
11
+ * try {
12
+ * const event = revstack.webhooks.constructEvent(
13
+ * req.body,
14
+ * req.headers["revstack-signature"] as string,
15
+ * process.env.REVSTACK_WEBHOOK_SECRET!
16
+ * );
17
+ * console.log(`Received event: ${event.type}`);
18
+ * res.sendStatus(200);
19
+ * } catch (error) {
20
+ * if (error instanceof SignatureVerificationError) {
21
+ * res.status(400).send("Invalid signature");
22
+ * }
23
+ * }
24
+ * });
25
+ * ```
26
+ */
27
+ export declare class WebhooksClient {
28
+ /**
29
+ * Verify the signature of an incoming webhook and parse the event payload.
30
+ *
31
+ * @param payload - Raw request body (`Buffer` or `string`). **Must NOT be parsed JSON.**
32
+ * @param signature - Value of the `revstack-signature` HTTP header.
33
+ * @param secret - Webhook signing secret (from the Revstack Dashboard).
34
+ * @param tolerance - Maximum age of the webhook in seconds. Defaults to `300` (5 min). Set to `0` to disable replay protection.
35
+ * @returns The verified and parsed webhook event.
36
+ *
37
+ * @throws {SignatureVerificationError} If the signature is invalid, the header is malformed, or the timestamp exceeds the tolerance.
38
+ */
39
+ constructEvent(payload: string | Buffer, signature: string, secret: string, tolerance?: number): WebhookEvent;
40
+ /**
41
+ * Parse the `revstack-signature` header into its component parts.
42
+ *
43
+ * @param header - Raw header value in the format `t=<timestamp>,v1=<signature>`.
44
+ * @returns Parsed timestamp and v1 signature.
45
+ *
46
+ * @throws {SignatureVerificationError} If the header format is invalid.
47
+ * @internal
48
+ */
49
+ private parseSignatureHeader;
50
+ }
51
+ //# sourceMappingURL=webhooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webhooks.d.ts","sourceRoot":"","sources":["../../../src/modules/webhooks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAK5C;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,cAAc;IACzB;;;;;;;;;;OAUG;IACH,cAAc,CACZ,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,EACd,SAAS,GAAE,MAAkC,GAC5C,YAAY;IAoCf;;;;;;;;OAQG;IACH,OAAO,CAAC,oBAAoB;CAsB7B"}
@@ -0,0 +1,90 @@
1
+ import { createHmac, timingSafeEqual } from "node:crypto";
2
+ import { SignatureVerificationError } from "@/errors";
3
+ /** Default maximum age for webhook timestamps (5 minutes). */
4
+ const DEFAULT_TOLERANCE_SECONDS = 300;
5
+ /**
6
+ * Client for verifying inbound webhooks from Revstack Cloud.
7
+ * Uses HMAC-SHA256 with constant-time comparison to prevent timing attacks,
8
+ * plus timestamp validation for replay protection.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * // Express.js webhook handler
13
+ * app.post("/webhooks/revstack", express.raw({ type: "application/json" }), (req, res) => {
14
+ * try {
15
+ * const event = revstack.webhooks.constructEvent(
16
+ * req.body,
17
+ * req.headers["revstack-signature"] as string,
18
+ * process.env.REVSTACK_WEBHOOK_SECRET!
19
+ * );
20
+ * console.log(`Received event: ${event.type}`);
21
+ * res.sendStatus(200);
22
+ * } catch (error) {
23
+ * if (error instanceof SignatureVerificationError) {
24
+ * res.status(400).send("Invalid signature");
25
+ * }
26
+ * }
27
+ * });
28
+ * ```
29
+ */
30
+ export class WebhooksClient {
31
+ /**
32
+ * Verify the signature of an incoming webhook and parse the event payload.
33
+ *
34
+ * @param payload - Raw request body (`Buffer` or `string`). **Must NOT be parsed JSON.**
35
+ * @param signature - Value of the `revstack-signature` HTTP header.
36
+ * @param secret - Webhook signing secret (from the Revstack Dashboard).
37
+ * @param tolerance - Maximum age of the webhook in seconds. Defaults to `300` (5 min). Set to `0` to disable replay protection.
38
+ * @returns The verified and parsed webhook event.
39
+ *
40
+ * @throws {SignatureVerificationError} If the signature is invalid, the header is malformed, or the timestamp exceeds the tolerance.
41
+ */
42
+ constructEvent(payload, signature, secret, tolerance = DEFAULT_TOLERANCE_SECONDS) {
43
+ const rawBody = typeof payload === "string" ? payload : payload.toString("utf8");
44
+ const parts = this.parseSignatureHeader(signature);
45
+ // Compute expected HMAC-SHA256 signature
46
+ const expectedSignature = createHmac("sha256", secret)
47
+ .update(`${parts.timestamp}.${rawBody}`)
48
+ .digest("hex");
49
+ // Constant-time comparison to prevent timing attacks
50
+ const expectedBuffer = Buffer.from(expectedSignature, "utf8");
51
+ const receivedBuffer = Buffer.from(parts.v1, "utf8");
52
+ if (expectedBuffer.length !== receivedBuffer.length ||
53
+ !timingSafeEqual(expectedBuffer, receivedBuffer)) {
54
+ throw new SignatureVerificationError("Webhook signature does not match the expected signature");
55
+ }
56
+ // Replay protection: reject stale timestamps
57
+ if (tolerance > 0) {
58
+ const timestampAge = Math.floor(Date.now() / 1000) - parts.timestamp;
59
+ if (timestampAge > tolerance) {
60
+ throw new SignatureVerificationError(`Webhook timestamp too old (${timestampAge}s > ${tolerance}s tolerance)`);
61
+ }
62
+ }
63
+ return JSON.parse(rawBody);
64
+ }
65
+ /**
66
+ * Parse the `revstack-signature` header into its component parts.
67
+ *
68
+ * @param header - Raw header value in the format `t=<timestamp>,v1=<signature>`.
69
+ * @returns Parsed timestamp and v1 signature.
70
+ *
71
+ * @throws {SignatureVerificationError} If the header format is invalid.
72
+ * @internal
73
+ */
74
+ parseSignatureHeader(header) {
75
+ const pairs = header.split(",");
76
+ let timestamp = 0;
77
+ let v1 = "";
78
+ for (const pair of pairs) {
79
+ const [key, value] = pair.trim().split("=");
80
+ if (key === "t")
81
+ timestamp = parseInt(value, 10);
82
+ if (key === "v1")
83
+ v1 = value;
84
+ }
85
+ if (!timestamp || !v1) {
86
+ throw new SignatureVerificationError("Invalid signature header format. Expected: t=<timestamp>,v1=<signature>");
87
+ }
88
+ return { timestamp, v1 };
89
+ }
90
+ }