@formata/limitr-ui 0.1.6 → 0.1.7

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.
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Limitr UI Helpers
3
+ *
4
+ * Simplified initialization functions for Limitr UI web components.
5
+ * These helpers handle all the boilerplate for creating and configuring components.
6
+ */
7
+ /**
8
+ * Configuration for creating a pricing table
9
+ */
10
+ export interface PricingTableConfig {
11
+ /** CSS selector or HTMLElement where the table should be rendered */
12
+ container: string | HTMLElement;
13
+ /** Limitr cloud token for fetching policy */
14
+ token: string;
15
+ /** Whether the table should be interactive (allow plan selection) */
16
+ interactive?: boolean;
17
+ /** Optional current customer ID. */
18
+ customerId?: string;
19
+ /** Prevent users from changing their policy/plan */
20
+ denyPolicyChanges?: boolean;
21
+ /** Pre-initialized Limitr policy (optional, will fetch from cloud if not provided) */
22
+ policy?: any;
23
+ /** Theme "dark" or "light". */
24
+ theme?: "dark" | "light";
25
+ }
26
+ /**
27
+ * Configuration for creating a current plan component
28
+ */
29
+ export interface CurrentPlanConfig {
30
+ /** CSS selector or HTMLElement where the component should be rendered */
31
+ container: string | HTMLElement;
32
+ /** Limitr cloud token for fetching policy */
33
+ token: string;
34
+ /** Customer ID to display plan for */
35
+ customerId: string;
36
+ /** Show Stripe billing information */
37
+ showStripeInfo?: boolean;
38
+ /** Cancel subscription immediately instead of at period end */
39
+ cancelImmediately?: boolean;
40
+ /** Request Stripe invoices data */
41
+ requestStripeInvoices?: boolean;
42
+ /** Request Stripe portal URL */
43
+ requestStripePortalUrl?: boolean;
44
+ /** Pre-initialized Limitr policy (optional, will fetch from cloud if not provided) */
45
+ policy?: any;
46
+ }
47
+ /**
48
+ * Initialize STOF WASM (required for Limitr)
49
+ * This is called automatically by helper functions, but can be called manually if needed.
50
+ *
51
+ * @param wasmUrl - URL to the STOF WASM file (defaults to unpkg)
52
+ */
53
+ export declare function ensureStofInitialized(wasmUrl?: string): Promise<void>;
54
+ /**
55
+ * Create and configure a pricing table component
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * await createPricingTable({
60
+ * container: '#pricing-table',
61
+ * token: 'test_abc123',
62
+ * interactive: false,
63
+ * denyPolicyChanges: true
64
+ * });
65
+ * ```
66
+ */
67
+ export declare function createPricingTable(config: PricingTableConfig): Promise<HTMLElement>;
68
+ /**
69
+ * Create and configure a current plan component
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * await createCurrentPlan({
74
+ * container: '#current-plan',
75
+ * token: 'test_abc123',
76
+ * customerId: 'cust_xyz789',
77
+ * showStripeInfo: true,
78
+ * requestStripeInvoices: true
79
+ * });
80
+ * ```
81
+ */
82
+ export declare function createCurrentPlan(config: CurrentPlanConfig): Promise<HTMLElement>;
83
+ /**
84
+ * Initialize Limitr UI with custom WASM URL
85
+ * This is optional - the helper functions will use defaults if not called.
86
+ *
87
+ * @example
88
+ * ```typescript
89
+ * await initLimitrUI({
90
+ * wasmUrl: 'https://cdn.example.com/stof.wasm'
91
+ * });
92
+ * ```
93
+ */
94
+ export declare function initLimitrUI(config?: {
95
+ wasmUrl?: string;
96
+ }): Promise<void>;
97
+ //# sourceMappingURL=helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,qEAAqE;IACrE,SAAS,EAAE,MAAM,GAAG,WAAW,CAAC;IAChC,6CAA6C;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,qEAAqE;IACrE,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,oCAAoC;IACpC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oDAAoD;IACpD,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,sFAAsF;IACtF,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,+BAA+B;IAC/B,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CAC5B;AAGD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAC9B,yEAAyE;IACzE,SAAS,EAAE,MAAM,GAAG,WAAW,CAAC;IAChC,6CAA6C;IAC7C,KAAK,EAAE,MAAM,CAAC;IACd,sCAAsC;IACtC,UAAU,EAAE,MAAM,CAAC;IACnB,sCAAsC;IACtC,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,+DAA+D;IAC/D,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,mCAAmC;IACnC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,gCAAgC;IAChC,sBAAsB,CAAC,EAAE,OAAO,CAAC;IACjC,sFAAsF;IACtF,MAAM,CAAC,EAAE,GAAG,CAAC;CAChB;AAUD;;;;;GAKG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAM3E;AA2CD;;;;;;;;;;;;GAYG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,WAAW,CAAC,CA4CzF;AAGD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC,CA8CvF;AAGD;;;;;;;;;;GAUG;AACH,wBAAsB,YAAY,CAAC,MAAM,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAG/E"}
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Limitr UI Helpers
3
+ *
4
+ * Simplified initialization functions for Limitr UI web components.
5
+ * These helpers handle all the boilerplate for creating and configuring components.
6
+ */
7
+ import { Limitr } from '@formata/limitr';
8
+ import { initStof } from '@formata/stof';
9
+ /**
10
+ * Global initialization state
11
+ */
12
+ let stofInitialized = false;
13
+ let limitrUiImported = false;
14
+ /**
15
+ * Initialize STOF WASM (required for Limitr)
16
+ * This is called automatically by helper functions, but can be called manually if needed.
17
+ *
18
+ * @param wasmUrl - URL to the STOF WASM file (defaults to unpkg)
19
+ */
20
+ export async function ensureStofInitialized(wasmUrl) {
21
+ if (stofInitialized)
22
+ return;
23
+ const url = wasmUrl || 'https://unpkg.com/@formata/stof@latest/wasm';
24
+ await initStof(url);
25
+ stofInitialized = true;
26
+ }
27
+ /**
28
+ * Ensure Limitr UI components are imported and registered
29
+ */
30
+ async function ensureLimitrUiImported() {
31
+ if (limitrUiImported)
32
+ return;
33
+ // Import the components (this registers them as custom elements)
34
+ await import('@formata/limitr-ui');
35
+ limitrUiImported = true;
36
+ }
37
+ /**
38
+ * Resolve a container selector to an HTMLElement
39
+ */
40
+ function resolveContainer(container) {
41
+ if (typeof container === 'string') {
42
+ const element = document.querySelector(container);
43
+ if (!element) {
44
+ throw new Error(`Container not found: ${container}`);
45
+ }
46
+ return element;
47
+ }
48
+ return container;
49
+ }
50
+ /**
51
+ * Fetch or use provided policy
52
+ */
53
+ async function getPolicy(token, providedPolicy) {
54
+ if (providedPolicy) {
55
+ return providedPolicy;
56
+ }
57
+ await ensureStofInitialized();
58
+ return await Limitr.cloud({ token });
59
+ }
60
+ /**
61
+ * Create and configure a pricing table component
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * await createPricingTable({
66
+ * container: '#pricing-table',
67
+ * token: 'test_abc123',
68
+ * interactive: false,
69
+ * denyPolicyChanges: true
70
+ * });
71
+ * ```
72
+ */
73
+ export async function createPricingTable(config) {
74
+ const { container, token, interactive = false, denyPolicyChanges = true, customerId, theme = "light", policy: providedPolicy } = config;
75
+ // Ensure everything is initialized
76
+ await ensureLimitrUiImported();
77
+ // Get or fetch the policy
78
+ const policy = await getPolicy(token, providedPolicy);
79
+ // Add the customer to the policy if defined
80
+ if (customerId)
81
+ await policy.addCloudCustomer(customerId);
82
+ // Resolve the container
83
+ const containerElement = resolveContainer(container);
84
+ // Create the component
85
+ const table = document.createElement('limitr-pricing-table');
86
+ containerElement.appendChild(table);
87
+ // Wait for the custom element to be defined
88
+ await customElements.whenDefined('limitr-pricing-table');
89
+ // Set properties
90
+ table.policy = policy;
91
+ if (customerId)
92
+ table.customerId = customerId;
93
+ table.interactive = interactive;
94
+ table.denyPolicyChanges = denyPolicyChanges;
95
+ if (theme)
96
+ table.theme = theme;
97
+ // Request an update
98
+ table.requestUpdate();
99
+ // Wait for the update to complete
100
+ await table.updateComplete;
101
+ return table;
102
+ }
103
+ /**
104
+ * Create and configure a current plan component
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * await createCurrentPlan({
109
+ * container: '#current-plan',
110
+ * token: 'test_abc123',
111
+ * customerId: 'cust_xyz789',
112
+ * showStripeInfo: true,
113
+ * requestStripeInvoices: true
114
+ * });
115
+ * ```
116
+ */
117
+ export async function createCurrentPlan(config) {
118
+ const { container, token, customerId, showStripeInfo = true, cancelImmediately = false, requestStripeInvoices = true, requestStripePortalUrl = true, policy: providedPolicy } = config;
119
+ // Ensure everything is initialized
120
+ await ensureLimitrUiImported();
121
+ // Get or fetch the policy
122
+ const policy = await getPolicy(token, providedPolicy);
123
+ // Add the customer to the policy if needed
124
+ await policy.addCloudCustomer(customerId);
125
+ // Resolve the container
126
+ const containerElement = resolveContainer(container);
127
+ // Create the component
128
+ const currentPlan = document.createElement('limitr-current-plan');
129
+ containerElement.appendChild(currentPlan);
130
+ // Wait for the custom element to be defined
131
+ await customElements.whenDefined('limitr-current-plan');
132
+ // Set properties
133
+ currentPlan.policy = policy;
134
+ currentPlan.customerId = customerId;
135
+ currentPlan.showStripeInfo = showStripeInfo;
136
+ currentPlan.cancelImmediately = cancelImmediately;
137
+ currentPlan.requestStripeInvoices = requestStripeInvoices;
138
+ currentPlan.requestStripePortalUrl = requestStripePortalUrl;
139
+ // Request an update
140
+ currentPlan.requestUpdate();
141
+ // Wait for the update to complete
142
+ await currentPlan.updateComplete;
143
+ return currentPlan;
144
+ }
145
+ /**
146
+ * Initialize Limitr UI with custom WASM URL
147
+ * This is optional - the helper functions will use defaults if not called.
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * await initLimitrUI({
152
+ * wasmUrl: 'https://cdn.example.com/stof.wasm'
153
+ * });
154
+ * ```
155
+ */
156
+ export async function initLimitrUI(config) {
157
+ await ensureStofInitialized(config?.wasmUrl);
158
+ await ensureLimitrUiImported();
159
+ }
160
+ //# sourceMappingURL=helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAiDzC;;GAEG;AACH,IAAI,eAAe,GAAG,KAAK,CAAC;AAC5B,IAAI,gBAAgB,GAAG,KAAK,CAAC;AAG7B;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,OAAgB;IACxD,IAAI,eAAe;QAAE,OAAO;IAE5B,MAAM,GAAG,GAAG,OAAO,IAAI,6CAA6C,CAAC;IACrE,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC;IACpB,eAAe,GAAG,IAAI,CAAC;AAC3B,CAAC;AAGD;;GAEG;AACH,KAAK,UAAU,sBAAsB;IACjC,IAAI,gBAAgB;QAAE,OAAO;IAE7B,iEAAiE;IACjE,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAC;IACnC,gBAAgB,GAAG,IAAI,CAAC;AAC5B,CAAC;AAGD;;GAEG;AACH,SAAS,gBAAgB,CAAC,SAA+B;IACrD,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QAChC,MAAM,OAAO,GAAG,QAAQ,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,CAAC,OAAO,EAAE,CAAC;YACX,MAAM,IAAI,KAAK,CAAC,wBAAwB,SAAS,EAAE,CAAC,CAAC;Q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AC,qBAAqB,GAAG,qBAAqB,CAAC;IAC1D,WAAW,CAAC,sBAAsB,GAAG,sBAAsB,CAAC;IAE5D,oBAAoB;IACpB,WAAW,CAAC,aAAa,EAAE,CAAC;IAE5B,kCAAkC;IAClC,MAAM,WAAW,CAAC,cAAc,CAAC;IAEjC,OAAO,WAAW,CAAC;AACvB,CAAC;AAGD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,MAA6B;IAC5D,MAAM,qBAAqB,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC7C,MAAM,sBAAsB,EAAE,CAAC;AACnC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@formata/limitr-ui",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "main": "./dist/mod.js",
6
6
  "types": "./dist/mod.d.ts",
@@ -8,6 +8,10 @@
8
8
  ".": {
9
9
  "types": "./dist/mod.d.ts",
10
10
  "default": "./dist/mod.js"
11
+ },
12
+ "./helpers": {
13
+ "types": "./dist/helpers.d.ts",
14
+ "default": "./dist/helpers.js"
11
15
  }
12
16
  },
13
17
  "files": [