autumn-js 0.0.1 → 0.0.3

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 (85) hide show
  1. package/LICENSE.md +7 -0
  2. package/README.md +110 -0
  3. package/dist/next/AutumnProvider.js +55 -0
  4. package/dist/next/AutumnProvider.mjs +31 -0
  5. package/dist/next/AutumnServer.js +36 -0
  6. package/dist/next/AutumnServer.mjs +12 -0
  7. package/dist/{react → next}/client/AutumnContext.d.mts +2 -3
  8. package/dist/{react → next}/client/AutumnContext.d.ts +2 -3
  9. package/dist/{react → next}/client/AutumnContext.js +4 -1
  10. package/dist/{react → next}/client/AutumnContext.mjs +4 -1
  11. package/dist/next/client/AutumnProvider.d.mts +10 -0
  12. package/dist/next/client/AutumnProvider.d.ts +10 -0
  13. package/dist/{react → next}/client/AutumnProvider.js +5 -4
  14. package/dist/{react → next}/client/AutumnProvider.mjs +5 -4
  15. package/dist/{react/client/cusTypes-BT7wlTNj.d.mts → next/client/cusTypes-BUiIMwvu.d.mts} +1 -5
  16. package/dist/{react/client/cusTypes-BT7wlTNj.d.ts → next/client/cusTypes-BUiIMwvu.d.ts} +1 -5
  17. package/dist/next/client/hooks/useAutumn.d.mts +24 -0
  18. package/dist/next/client/hooks/useAutumn.d.ts +24 -0
  19. package/dist/{react → next}/client/hooks/useAutumn.js +38 -24
  20. package/dist/{react → next}/client/hooks/useAutumn.mjs +38 -24
  21. package/dist/{react → next}/client/hooks/useCustomer.d.mts +1 -1
  22. package/dist/{react → next}/client/hooks/useCustomer.d.ts +1 -1
  23. package/dist/{react → next}/client/hooks/useCustomer.js +7 -4
  24. package/dist/{react → next}/client/hooks/useCustomer.mjs +7 -4
  25. package/dist/next/client/types.d.mts +12 -0
  26. package/dist/next/client/types.d.ts +12 -0
  27. package/dist/next/client/types.js +2 -0
  28. package/dist/next/client/types.mjs +2 -0
  29. package/dist/next/enums.js +31 -0
  30. package/dist/next/enums.mjs +7 -0
  31. package/dist/{react → next}/index.d.mts +43 -59
  32. package/dist/{react → next}/index.d.ts +43 -59
  33. package/dist/{react → next}/index.js +7 -1
  34. package/dist/{react → next}/index.mjs +3 -0
  35. package/dist/next/server/auth/authPlugin.d.mts +13 -0
  36. package/dist/next/server/auth/authPlugin.d.ts +13 -0
  37. package/dist/next/server/auth/authPlugin.js +11 -0
  38. package/dist/next/server/auth/authPlugin.mjs +11 -0
  39. package/dist/next/server/auth/getCusIdDefault.d.mts +14 -0
  40. package/dist/next/server/auth/getCusIdDefault.d.ts +14 -0
  41. package/dist/next/server/auth/getCusIdDefault.js +27 -0
  42. package/dist/next/server/auth/getCusIdDefault.mjs +27 -0
  43. package/dist/next/server/auth/handleAuthProvider.d.mts +37 -0
  44. package/dist/next/server/auth/handleAuthProvider.d.ts +37 -0
  45. package/dist/next/server/auth/handleAuthProvider.js +132 -0
  46. package/dist/next/server/auth/handleAuthProvider.mjs +132 -0
  47. package/dist/next/server/auth/withAuth.d.mts +11 -0
  48. package/dist/next/server/auth/withAuth.d.ts +11 -0
  49. package/dist/next/server/auth/withAuth.js +38 -0
  50. package/dist/next/server/auth/withAuth.mjs +38 -0
  51. package/dist/next/server/cusActions.d.mts +248 -0
  52. package/dist/next/server/cusActions.d.ts +248 -0
  53. package/dist/next/server/cusActions.js +32 -0
  54. package/dist/next/server/cusActions.mjs +32 -0
  55. package/dist/{react/server/genTypes-RkqyS6Mn.d.mts → next/server/cusTypes-B9N9G7Vr.d.mts} +1 -62
  56. package/dist/{react/server/genTypes-RkqyS6Mn.d.ts → next/server/cusTypes-B9N9G7Vr.d.ts} +1 -62
  57. package/dist/next/server/genActions.d.mts +29 -0
  58. package/dist/next/server/genActions.d.ts +29 -0
  59. package/dist/next/server/genActions.js +51 -0
  60. package/dist/next/server/genActions.mjs +51 -0
  61. package/dist/sdk/index.d.mts +65 -16
  62. package/dist/sdk/index.d.ts +65 -16
  63. package/dist/sdk/index.js +33 -6
  64. package/dist/sdk/index.mjs +33 -6
  65. package/dist/utils/encryptUtils.d.mts +4 -0
  66. package/dist/utils/encryptUtils.d.ts +4 -0
  67. package/dist/utils/encryptUtils.js +74 -0
  68. package/dist/utils/encryptUtils.mjs +38 -0
  69. package/package.json +8 -9
  70. package/publish.sh +14 -0
  71. package/tsup.config.ts +34 -12
  72. package/dist/react/client/AutumnProvider.d.mts +0 -12
  73. package/dist/react/client/AutumnProvider.d.ts +0 -12
  74. package/dist/react/client/hooks/useAutumn.d.mts +0 -65
  75. package/dist/react/client/hooks/useAutumn.d.ts +0 -65
  76. package/dist/react/server/cusActions.d.mts +0 -140
  77. package/dist/react/server/cusActions.d.ts +0 -140
  78. package/dist/react/server/cusActions.js +0 -26
  79. package/dist/react/server/cusActions.mjs +0 -26
  80. package/dist/react/server/genActions.d.mts +0 -42
  81. package/dist/react/server/genActions.d.ts +0 -42
  82. package/dist/react/server/genActions.js +0 -41
  83. package/dist/react/server/genActions.mjs +0 -41
  84. /package/dist/{react → next}/client/error-TNbN4XE4.d.mts +0 -0
  85. /package/dist/{react → next}/client/error-TNbN4XE4.d.ts +0 -0
package/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ The MIT License (MIT) Copyright (c) 2023 - present, Recase Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -0,0 +1,110 @@
1
+ # Autumn.js
2
+
3
+ Autumn.js is a comprehensive JavaScript/TypeScript library for interacting with the Autumn pricing platform. This package provides both a server-side SDK for the Autumn API and a Next.js integration package for seamless client-side implementation.
4
+
5
+ ## Features
6
+
7
+ - 🚀 Complete Autumn API SDK
8
+ - ⚡ Next.js Integration
9
+ - 🔒 Type-safe API interactions
10
+ - 🛠️ Easy-to-use hooks and components
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm install autumn-js
16
+ ```
17
+
18
+ ## Configuration
19
+
20
+ Add your Autumn secret key to your environment variables:
21
+
22
+ ```env
23
+ AUTUMN_SECRET_KEY=your_secret_key_here
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ### Server-Side SDK
29
+
30
+ For server-side applications (Node.js, Express, etc.), use the SDK like this:
31
+
32
+ ```typescript
33
+ import { Autumn } from 'autumn-js'
34
+
35
+ // Initialize Autumn
36
+ const autumn = new Autumn()
37
+
38
+ // Create a customer
39
+ await autumn.customers.create({
40
+ id: "customer_123",
41
+ name: "John Doe"
42
+ })
43
+
44
+ // Other available operations
45
+ await autumn.products.list()
46
+ await autumn.prices.create(...)
47
+ await autumn.subscriptions.create(...)
48
+ ```
49
+
50
+ ### Next.js Integration
51
+
52
+ For Next.js applications, Autumn.js provides a dedicated integration with helpful hooks and components.
53
+
54
+ 1. First, wrap your application with the `AutumnProvider` in your root layout:
55
+
56
+ ```typescript
57
+ // app/layout.tsx
58
+ import { AutumnProvider } from 'autumn-js/next'
59
+
60
+ export default function RootLayout({
61
+ children,
62
+ }: {
63
+ children: React.ReactNode
64
+ }) {
65
+ return (
66
+ <html>
67
+ <body>
68
+ <AutumnProvider customerId="YOUR_CUSTOMER_ID">
69
+ {children}
70
+ </AutumnProvider>
71
+ </body>
72
+ </html>
73
+ )
74
+ }
75
+ ```
76
+
77
+ 2. Then use the `useAutumn` hook in your components:
78
+
79
+ ```typescript
80
+ import { useAutumn } from 'autumn-js/next'
81
+
82
+ export default function BillingPage() {
83
+ const { customer, attach, openBillingPortal } = useAutumn()
84
+
85
+ return (
86
+ <div>
87
+ <h1>Welcome {customer?.name}</h1>
88
+ <button onClick={() => openBillingPortal()}>
89
+ Manage Billing
90
+ </button>
91
+ <button onClick={() => attach()}>
92
+ Upgrade to Pro
93
+ </button>
94
+ </div>
95
+ )
96
+ }
97
+ ```
98
+
99
+
100
+ ## API Reference
101
+
102
+ For detailed API documentation, visit [docs.useautumn.com](https://docs.useautumn.com)
103
+
104
+ ## Support
105
+
106
+ For support, email hey@useautumn.com!
107
+
108
+ ## License
109
+
110
+ MIT
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var AutumnProvider_exports = {};
20
+ __export(AutumnProvider_exports, {
21
+ AutumnProvider: () => AutumnProvider
22
+ });
23
+ module.exports = __toCommonJS(AutumnProvider_exports);
24
+ var import_jsx_runtime = require("react/jsx-runtime");
25
+ var import_AutumnProvider = require("./client/AutumnProvider");
26
+ var import_encryptUtils = require("../utils/encryptUtils");
27
+ var import_authPlugin = require("./server/auth/authPlugin");
28
+ const AutumnProvider = ({
29
+ customerId,
30
+ customerData,
31
+ authPlugin,
32
+ children
33
+ }) => {
34
+ if (typeof window !== "undefined") {
35
+ throw new Error(
36
+ "AutumnProvider must be used in a server component. It cannot be used in client components."
37
+ );
38
+ }
39
+ let encryptedCustomerId = customerId ? (0, import_encryptUtils.encryptData)(customerId) : void 0;
40
+ if (authPlugin) {
41
+ (0, import_authPlugin.setupAuthPlugin)(authPlugin);
42
+ }
43
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
44
+ import_AutumnProvider.AutumnClientProvider,
45
+ {
46
+ encryptedCustomerId,
47
+ customerData,
48
+ children
49
+ }
50
+ );
51
+ };
52
+ // Annotate the CommonJS export names for ESM import in node:
53
+ 0 && (module.exports = {
54
+ AutumnProvider
55
+ });
@@ -0,0 +1,31 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { AutumnClientProvider } from "./client/AutumnProvider";
3
+ import { encryptData } from "../utils/encryptUtils";
4
+ import { setupAuthPlugin } from "./server/auth/authPlugin";
5
+ const AutumnProvider = ({
6
+ customerId,
7
+ customerData,
8
+ authPlugin,
9
+ children
10
+ }) => {
11
+ if (typeof window !== "undefined") {
12
+ throw new Error(
13
+ "AutumnProvider must be used in a server component. It cannot be used in client components."
14
+ );
15
+ }
16
+ let encryptedCustomerId = customerId ? encryptData(customerId) : void 0;
17
+ if (authPlugin) {
18
+ setupAuthPlugin(authPlugin);
19
+ }
20
+ return /* @__PURE__ */ jsx(
21
+ AutumnClientProvider,
22
+ {
23
+ encryptedCustomerId,
24
+ customerData,
25
+ children
26
+ }
27
+ );
28
+ };
29
+ export {
30
+ AutumnProvider
31
+ };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var AutumnServer_exports = {};
20
+ __export(AutumnServer_exports, {
21
+ AutumnProvider: () => AutumnProvider
22
+ });
23
+ module.exports = __toCommonJS(AutumnServer_exports);
24
+ var import_jsx_runtime = require("react/jsx-runtime");
25
+ const AutumnProvider = ({ children }) => {
26
+ if (typeof window !== "undefined") {
27
+ throw new Error(
28
+ "AutumnServer must be used in a server component. It cannot be used in client components (those marked with 'use client')."
29
+ );
30
+ }
31
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children });
32
+ };
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ AutumnProvider
36
+ });
@@ -0,0 +1,12 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ const AutumnProvider = ({ children }) => {
3
+ if (typeof window !== "undefined") {
4
+ throw new Error(
5
+ "AutumnServer must be used in a server component. It cannot be used in client components (those marked with 'use client')."
6
+ );
7
+ }
8
+ return /* @__PURE__ */ jsx("div", { children });
9
+ };
10
+ export {
11
+ AutumnProvider
12
+ };
@@ -1,7 +1,6 @@
1
1
  import * as react from 'react';
2
- import { AutumnContextParams } from './AutumnProvider.mjs';
3
- import 'react/jsx-runtime';
4
- import './cusTypes-BT7wlTNj.mjs';
2
+ import { AutumnContextParams } from './types.mjs';
3
+ import './cusTypes-BUiIMwvu.mjs';
5
4
 
6
5
  declare const AutumnContext: react.Context<AutumnContextParams>;
7
6
  declare const useAutumnContext: () => AutumnContextParams;
@@ -1,7 +1,6 @@
1
1
  import * as react from 'react';
2
- import { AutumnContextParams } from './AutumnProvider.js';
3
- import 'react/jsx-runtime';
4
- import './cusTypes-BT7wlTNj.js';
2
+ import { AutumnContextParams } from './types.js';
3
+ import './cusTypes-BUiIMwvu.js';
5
4
 
6
5
  declare const AutumnContext: react.Context<AutumnContextParams>;
7
6
  declare const useAutumnContext: () => AutumnContextParams;
@@ -1,7 +1,10 @@
1
1
  "use client";
2
+
2
3
  import { createContext, useContext } from "react";
3
4
  const AutumnContext = createContext({
4
- customerId: ""
5
+ encryptedCustomerId: "",
6
+ customerData: {},
7
+ authProvider: "better-auth"
5
8
  });
6
9
  const useAutumnContext = () => {
7
10
  const context = useContext(AutumnContext);
@@ -1,7 +1,10 @@
1
1
  "use client";
2
+
2
3
  import { createContext, useContext } from "react";
3
4
  const AutumnContext = createContext({
4
- customerId: ""
5
+ encryptedCustomerId: "",
6
+ customerData: {},
7
+ authProvider: "better-auth"
5
8
  });
6
9
  const useAutumnContext = () => {
7
10
  const context = useContext(AutumnContext);
@@ -0,0 +1,10 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { AutumnContextParams } from './types.mjs';
3
+ import './cusTypes-BUiIMwvu.mjs';
4
+
5
+ interface AutumnProviderProps extends AutumnContextParams {
6
+ children?: React.ReactNode;
7
+ }
8
+ declare const AutumnClientProvider: ({ children, encryptedCustomerId, customerData, }: AutumnProviderProps) => react_jsx_runtime.JSX.Element;
9
+
10
+ export { AutumnClientProvider, type AutumnProviderProps };
@@ -0,0 +1,10 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { AutumnContextParams } from './types.js';
3
+ import './cusTypes-BUiIMwvu.js';
4
+
5
+ interface AutumnProviderProps extends AutumnContextParams {
6
+ children?: React.ReactNode;
7
+ }
8
+ declare const AutumnClientProvider: ({ children, encryptedCustomerId, customerData, }: AutumnProviderProps) => react_jsx_runtime.JSX.Element;
9
+
10
+ export { AutumnClientProvider, type AutumnProviderProps };
@@ -1,16 +1,17 @@
1
1
  "use client";
2
+
2
3
  import { jsx } from "react/jsx-runtime";
3
4
  import { AutumnContext } from "./AutumnContext";
4
- const AutumnProvider = ({
5
+ const AutumnClientProvider = ({
5
6
  children,
6
- customerId,
7
+ encryptedCustomerId,
7
8
  customerData
8
9
  }) => {
9
10
  return /* @__PURE__ */ jsx(
10
11
  AutumnContext.Provider,
11
12
  {
12
13
  value: {
13
- customerId,
14
+ encryptedCustomerId,
14
15
  customerData
15
16
  },
16
17
  children
@@ -18,5 +19,5 @@ const AutumnProvider = ({
18
19
  );
19
20
  };
20
21
  export {
21
- AutumnProvider
22
+ AutumnClientProvider
22
23
  };
@@ -1,16 +1,17 @@
1
1
  "use client";
2
+
2
3
  import { jsx } from "react/jsx-runtime";
3
4
  import { AutumnContext } from "./AutumnContext";
4
- const AutumnProvider = ({
5
+ const AutumnClientProvider = ({
5
6
  children,
6
- customerId,
7
+ encryptedCustomerId,
7
8
  customerData
8
9
  }) => {
9
10
  return /* @__PURE__ */ jsx(
10
11
  AutumnContext.Provider,
11
12
  {
12
13
  value: {
13
- customerId,
14
+ encryptedCustomerId,
14
15
  customerData
15
16
  },
16
17
  children
@@ -18,5 +19,5 @@ const AutumnProvider = ({
18
19
  );
19
20
  };
20
21
  export {
21
- AutumnProvider
22
+ AutumnClientProvider
22
23
  };
@@ -59,9 +59,5 @@ interface CustomerData {
59
59
  email?: string;
60
60
  fingerprint?: string;
61
61
  }
62
- interface BillingPortalResponse {
63
- customer_id: string;
64
- url: string;
65
- }
66
62
 
67
- export type { BillingPortalResponse as B, CustomerData as C, Customer as a };
63
+ export type { CustomerData as C, Customer as a };
@@ -59,9 +59,5 @@ interface CustomerData {
59
59
  email?: string;
60
60
  fingerprint?: string;
61
61
  }
62
- interface BillingPortalResponse {
63
- customer_id: string;
64
- url: string;
65
- }
66
62
 
67
- export type { BillingPortalResponse as B, CustomerData as C, Customer as a };
63
+ export type { CustomerData as C, Customer as a };
@@ -0,0 +1,24 @@
1
+ import { A as AutumnError } from '../error-TNbN4XE4.mjs';
2
+ import { a as Customer } from '../cusTypes-BUiIMwvu.mjs';
3
+
4
+ interface UseAutumnOptions {
5
+ autoCreate?: boolean;
6
+ }
7
+ declare const useAutumn: (options?: UseAutumnOptions) => {
8
+ customer: Customer | null;
9
+ loading: boolean;
10
+ error: AutumnError | null;
11
+ attach: ({ productId }: {
12
+ productId: string;
13
+ }) => Promise<any>;
14
+ sendEvent: ({ featureId, value, }: {
15
+ featureId: string;
16
+ value: number;
17
+ }) => Promise<any>;
18
+ entitled: ({ featureId }: {
19
+ featureId: string;
20
+ }) => Promise<any>;
21
+ openBillingPortal: () => Promise<any>;
22
+ };
23
+
24
+ export { type UseAutumnOptions, useAutumn };
@@ -0,0 +1,24 @@
1
+ import { A as AutumnError } from '../error-TNbN4XE4.js';
2
+ import { a as Customer } from '../cusTypes-BUiIMwvu.js';
3
+
4
+ interface UseAutumnOptions {
5
+ autoCreate?: boolean;
6
+ }
7
+ declare const useAutumn: (options?: UseAutumnOptions) => {
8
+ customer: Customer | null;
9
+ loading: boolean;
10
+ error: AutumnError | null;
11
+ attach: ({ productId }: {
12
+ productId: string;
13
+ }) => Promise<any>;
14
+ sendEvent: ({ featureId, value, }: {
15
+ featureId: string;
16
+ value: number;
17
+ }) => Promise<any>;
18
+ entitled: ({ featureId }: {
19
+ featureId: string;
20
+ }) => Promise<any>;
21
+ openBillingPortal: () => Promise<any>;
22
+ };
23
+
24
+ export { type UseAutumnOptions, useAutumn };
@@ -1,17 +1,15 @@
1
1
  "use client";
2
+
2
3
  import {
3
4
  attachAction,
4
5
  entitledAction,
5
- getBillingPortalAction
6
+ getBillingPortalAction,
7
+ sendEventAction
6
8
  } from "../../server/genActions";
7
- import { sendEventAction } from "../../server/genActions";
8
9
  import { useAutumnContext } from "../AutumnContext";
9
10
  import { useCustomer } from "./useCustomer";
10
11
  const useAutumn = (options) => {
11
- const { customerId } = useAutumnContext();
12
- if (!customerId) {
13
- throw new Error("Customer ID is required");
14
- }
12
+ const { encryptedCustomerId } = useAutumnContext();
15
13
  const {
16
14
  customer,
17
15
  isLoading: loading,
@@ -19,39 +17,55 @@ const useAutumn = (options) => {
19
17
  } = useCustomer({
20
18
  autoCreate: options?.autoCreate
21
19
  });
22
- const entitled = ({ featureId }) => {
23
- return entitledAction({ customerId, featureId });
20
+ const attach = async ({ productId }) => {
21
+ const result = await attachAction({
22
+ encryptedCustomerId,
23
+ productId
24
+ });
25
+ if (result.error) {
26
+ throw result.error;
27
+ }
28
+ let data = result.data;
29
+ if (data?.checkout_url && typeof window !== "undefined") {
30
+ window.open(data.checkout_url, "_blank");
31
+ }
32
+ return result;
33
+ };
34
+ const entitled = async ({ featureId }) => {
35
+ const { data, error: error2 } = await entitledAction({
36
+ encryptedCustomerId,
37
+ featureId
38
+ });
39
+ if (error2) {
40
+ throw error2;
41
+ }
42
+ return data;
24
43
  };
25
- const sendEvent = ({
44
+ const sendEvent = async ({
26
45
  featureId,
27
46
  value
28
47
  }) => {
29
- return sendEventAction({ customerId, featureId, value });
30
- };
31
- const attach = async ({ productId }) => {
32
- const { data, error: error2 } = await attachAction({
33
- customerId,
34
- productId
48
+ const { data, error: error2 } = await sendEventAction({
49
+ encryptedCustomerId,
50
+ featureId,
51
+ value
35
52
  });
36
53
  if (error2) {
37
54
  throw error2;
38
55
  }
39
- if (data?.checkout_url && typeof window !== "undefined") {
40
- window.open(data.checkout_url, "_blank");
41
- } else {
42
- return data;
43
- }
56
+ return data;
44
57
  };
45
58
  const openBillingPortal = async () => {
46
- const { data, error: error2 } = await getBillingPortalAction({
47
- customerId,
59
+ const result = await getBillingPortalAction({
60
+ encryptedCustomerId,
48
61
  params: {
49
62
  return_url: "https://example.com"
50
63
  }
51
64
  });
52
- if (error2) {
53
- throw error2;
65
+ if (result.error) {
66
+ throw result.error;
54
67
  }
68
+ let data = result.data;
55
69
  if (data?.url && typeof window !== "undefined") {
56
70
  window.open(data.url, "_blank");
57
71
  return data;
@@ -1,17 +1,15 @@
1
1
  "use client";
2
+
2
3
  import {
3
4
  attachAction,
4
5
  entitledAction,
5
- getBillingPortalAction
6
+ getBillingPortalAction,
7
+ sendEventAction
6
8
  } from "../../server/genActions";
7
- import { sendEventAction } from "../../server/genActions";
8
9
  import { useAutumnContext } from "../AutumnContext";
9
10
  import { useCustomer } from "./useCustomer";
10
11
  const useAutumn = (options) => {
11
- const { customerId } = useAutumnContext();
12
- if (!customerId) {
13
- throw new Error("Customer ID is required");
14
- }
12
+ const { encryptedCustomerId } = useAutumnContext();
15
13
  const {
16
14
  customer,
17
15
  isLoading: loading,
@@ -19,39 +17,55 @@ const useAutumn = (options) => {
19
17
  } = useCustomer({
20
18
  autoCreate: options?.autoCreate
21
19
  });
22
- const entitled = ({ featureId }) => {
23
- return entitledAction({ customerId, featureId });
20
+ const attach = async ({ productId }) => {
21
+ const result = await attachAction({
22
+ encryptedCustomerId,
23
+ productId
24
+ });
25
+ if (result.error) {
26
+ throw result.error;
27
+ }
28
+ let data = result.data;
29
+ if (data?.checkout_url && typeof window !== "undefined") {
30
+ window.open(data.checkout_url, "_blank");
31
+ }
32
+ return result;
33
+ };
34
+ const entitled = async ({ featureId }) => {
35
+ const { data, error: error2 } = await entitledAction({
36
+ encryptedCustomerId,
37
+ featureId
38
+ });
39
+ if (error2) {
40
+ throw error2;
41
+ }
42
+ return data;
24
43
  };
25
- const sendEvent = ({
44
+ const sendEvent = async ({
26
45
  featureId,
27
46
  value
28
47
  }) => {
29
- return sendEventAction({ customerId, featureId, value });
30
- };
31
- const attach = async ({ productId }) => {
32
- const { data, error: error2 } = await attachAction({
33
- customerId,
34
- productId
48
+ const { data, error: error2 } = await sendEventAction({
49
+ encryptedCustomerId,
50
+ featureId,
51
+ value
35
52
  });
36
53
  if (error2) {
37
54
  throw error2;
38
55
  }
39
- if (data?.checkout_url && typeof window !== "undefined") {
40
- window.open(data.checkout_url, "_blank");
41
- } else {
42
- return data;
43
- }
56
+ return data;
44
57
  };
45
58
  const openBillingPortal = async () => {
46
- const { data, error: error2 } = await getBillingPortalAction({
47
- customerId,
59
+ const result = await getBillingPortalAction({
60
+ encryptedCustomerId,
48
61
  params: {
49
62
  return_url: "https://example.com"
50
63
  }
51
64
  });
52
- if (error2) {
53
- throw error2;
65
+ if (result.error) {
66
+ throw result.error;
54
67
  }
68
+ let data = result.data;
55
69
  if (data?.url && typeof window !== "undefined") {
56
70
  window.open(data.url, "_blank");
57
71
  return data;
@@ -1,5 +1,5 @@
1
1
  import { A as AutumnError } from '../error-TNbN4XE4.mjs';
2
- import { C as CustomerData, a as Customer } from '../cusTypes-BT7wlTNj.mjs';
2
+ import { C as CustomerData, a as Customer } from '../cusTypes-BUiIMwvu.mjs';
3
3
 
4
4
  interface UseCustomerProps {
5
5
  autoCreate?: boolean;
@@ -1,5 +1,5 @@
1
1
  import { A as AutumnError } from '../error-TNbN4XE4.js';
2
- import { C as CustomerData, a as Customer } from '../cusTypes-BT7wlTNj.js';
2
+ import { C as CustomerData, a as Customer } from '../cusTypes-BUiIMwvu.js';
3
3
 
4
4
  interface UseCustomerProps {
5
5
  autoCreate?: boolean;