@dream-api/sdk 0.1.0 → 0.1.2
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/README.md +9 -1
- package/dist/index.d.mts +39 -6
- package/dist/index.d.ts +39 -6
- package/dist/index.js +37 -5
- package/dist/index.mjs +37 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -13,10 +13,18 @@ npm install @dream-api/sdk
|
|
|
13
13
|
```typescript
|
|
14
14
|
import { DreamAPI } from '@dream-api/sdk';
|
|
15
15
|
|
|
16
|
+
// FRONTEND (React, Vue, browser) - PK only, safe to expose
|
|
17
|
+
const api = new DreamAPI({
|
|
18
|
+
publishableKey: 'pk_test_xxx',
|
|
19
|
+
});
|
|
20
|
+
// Can access: tiers, products, usage (with JWT), billing (with JWT)
|
|
21
|
+
|
|
22
|
+
// BACKEND (Node, Workers, API routes) - Full access
|
|
16
23
|
const api = new DreamAPI({
|
|
17
24
|
secretKey: process.env.DREAM_API_SECRET_KEY,
|
|
18
25
|
publishableKey: process.env.DREAM_API_PUBLISHABLE_KEY,
|
|
19
26
|
});
|
|
27
|
+
// Can access: everything including customers, dashboard
|
|
20
28
|
```
|
|
21
29
|
|
|
22
30
|
## Backend Operations (SK Only)
|
|
@@ -183,8 +191,8 @@ DREAM_API_PUBLISHABLE_KEY=pk_test_xxx
|
|
|
183
191
|
import { DreamAPI } from '@dream-api/sdk';
|
|
184
192
|
import { useAuth } from '@clerk/clerk-react';
|
|
185
193
|
|
|
194
|
+
// Frontend: PK only (secret key stays on your backend!)
|
|
186
195
|
const api = new DreamAPI({
|
|
187
|
-
secretKey: import.meta.env.VITE_DREAM_API_SECRET_KEY,
|
|
188
196
|
publishableKey: import.meta.env.VITE_DREAM_API_PUBLISHABLE_KEY,
|
|
189
197
|
});
|
|
190
198
|
|
package/dist/index.d.mts
CHANGED
|
@@ -2,9 +2,17 @@
|
|
|
2
2
|
* Dream API SDK - Type Definitions
|
|
3
3
|
*/
|
|
4
4
|
interface DreamAPIConfig {
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Your secret key (sk_test_xxx or sk_live_xxx)
|
|
7
|
+
* Required for backend/admin operations (customers, dashboard)
|
|
8
|
+
* Optional for frontend operations (tiers, products, usage with JWT)
|
|
9
|
+
*/
|
|
10
|
+
secretKey?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Your publishable key (pk_test_xxx or pk_live_xxx)
|
|
13
|
+
* Required for frontend-only mode (when no secretKey provided)
|
|
14
|
+
* Also used for auth URL helpers
|
|
15
|
+
*/
|
|
8
16
|
publishableKey?: string;
|
|
9
17
|
/** Base URL override (for testing) */
|
|
10
18
|
baseUrl?: string;
|
|
@@ -53,13 +61,18 @@ interface Tier {
|
|
|
53
61
|
popular?: boolean;
|
|
54
62
|
}
|
|
55
63
|
interface Product {
|
|
64
|
+
id?: string;
|
|
56
65
|
name: string;
|
|
66
|
+
displayName?: string;
|
|
67
|
+
description?: string;
|
|
57
68
|
price: number;
|
|
69
|
+
currency?: string;
|
|
58
70
|
priceId: string;
|
|
59
71
|
productId: string;
|
|
60
|
-
description?: string;
|
|
61
72
|
imageUrl?: string;
|
|
62
|
-
inventory?: number;
|
|
73
|
+
inventory?: number | null;
|
|
74
|
+
soldOut?: boolean;
|
|
75
|
+
features?: string[];
|
|
63
76
|
}
|
|
64
77
|
interface CheckoutResult {
|
|
65
78
|
url: string;
|
|
@@ -132,7 +145,16 @@ declare class DreamClient {
|
|
|
132
145
|
private clerkUrl;
|
|
133
146
|
private userToken;
|
|
134
147
|
private tokenRefresher;
|
|
148
|
+
/**
|
|
149
|
+
* Frontend-only mode: When only publishableKey is provided (no secretKey)
|
|
150
|
+
* In this mode, only public endpoints and JWT-authenticated endpoints work
|
|
151
|
+
*/
|
|
152
|
+
private readonly frontendOnly;
|
|
135
153
|
constructor(config: DreamAPIConfig);
|
|
154
|
+
/**
|
|
155
|
+
* Check if running in frontend-only mode
|
|
156
|
+
*/
|
|
157
|
+
isFrontendOnly(): boolean;
|
|
136
158
|
/**
|
|
137
159
|
* Set the end-user JWT token for user-specific operations.
|
|
138
160
|
* Call this after the user signs in via Clerk.
|
|
@@ -481,13 +503,24 @@ declare class ProductAPI {
|
|
|
481
503
|
}>;
|
|
482
504
|
/**
|
|
483
505
|
* Create a cart checkout (guest checkout for store)
|
|
506
|
+
*
|
|
507
|
+
* @example
|
|
508
|
+
* ```typescript
|
|
509
|
+
* const { url } = await api.products.cartCheckout({
|
|
510
|
+
* items: [{ priceId: 'price_xxx', quantity: 1 }],
|
|
511
|
+
* customerEmail: 'customer@example.com',
|
|
512
|
+
* successUrl: '/success',
|
|
513
|
+
* cancelUrl: '/cart',
|
|
514
|
+
* });
|
|
515
|
+
* window.location.href = url;
|
|
516
|
+
* ```
|
|
484
517
|
*/
|
|
485
518
|
cartCheckout(params: {
|
|
486
519
|
items: Array<{
|
|
487
520
|
priceId: string;
|
|
488
521
|
quantity: number;
|
|
489
522
|
}>;
|
|
490
|
-
customerEmail
|
|
523
|
+
customerEmail?: string;
|
|
491
524
|
customerName?: string;
|
|
492
525
|
successUrl?: string;
|
|
493
526
|
cancelUrl?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,9 +2,17 @@
|
|
|
2
2
|
* Dream API SDK - Type Definitions
|
|
3
3
|
*/
|
|
4
4
|
interface DreamAPIConfig {
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Your secret key (sk_test_xxx or sk_live_xxx)
|
|
7
|
+
* Required for backend/admin operations (customers, dashboard)
|
|
8
|
+
* Optional for frontend operations (tiers, products, usage with JWT)
|
|
9
|
+
*/
|
|
10
|
+
secretKey?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Your publishable key (pk_test_xxx or pk_live_xxx)
|
|
13
|
+
* Required for frontend-only mode (when no secretKey provided)
|
|
14
|
+
* Also used for auth URL helpers
|
|
15
|
+
*/
|
|
8
16
|
publishableKey?: string;
|
|
9
17
|
/** Base URL override (for testing) */
|
|
10
18
|
baseUrl?: string;
|
|
@@ -53,13 +61,18 @@ interface Tier {
|
|
|
53
61
|
popular?: boolean;
|
|
54
62
|
}
|
|
55
63
|
interface Product {
|
|
64
|
+
id?: string;
|
|
56
65
|
name: string;
|
|
66
|
+
displayName?: string;
|
|
67
|
+
description?: string;
|
|
57
68
|
price: number;
|
|
69
|
+
currency?: string;
|
|
58
70
|
priceId: string;
|
|
59
71
|
productId: string;
|
|
60
|
-
description?: string;
|
|
61
72
|
imageUrl?: string;
|
|
62
|
-
inventory?: number;
|
|
73
|
+
inventory?: number | null;
|
|
74
|
+
soldOut?: boolean;
|
|
75
|
+
features?: string[];
|
|
63
76
|
}
|
|
64
77
|
interface CheckoutResult {
|
|
65
78
|
url: string;
|
|
@@ -132,7 +145,16 @@ declare class DreamClient {
|
|
|
132
145
|
private clerkUrl;
|
|
133
146
|
private userToken;
|
|
134
147
|
private tokenRefresher;
|
|
148
|
+
/**
|
|
149
|
+
* Frontend-only mode: When only publishableKey is provided (no secretKey)
|
|
150
|
+
* In this mode, only public endpoints and JWT-authenticated endpoints work
|
|
151
|
+
*/
|
|
152
|
+
private readonly frontendOnly;
|
|
135
153
|
constructor(config: DreamAPIConfig);
|
|
154
|
+
/**
|
|
155
|
+
* Check if running in frontend-only mode
|
|
156
|
+
*/
|
|
157
|
+
isFrontendOnly(): boolean;
|
|
136
158
|
/**
|
|
137
159
|
* Set the end-user JWT token for user-specific operations.
|
|
138
160
|
* Call this after the user signs in via Clerk.
|
|
@@ -481,13 +503,24 @@ declare class ProductAPI {
|
|
|
481
503
|
}>;
|
|
482
504
|
/**
|
|
483
505
|
* Create a cart checkout (guest checkout for store)
|
|
506
|
+
*
|
|
507
|
+
* @example
|
|
508
|
+
* ```typescript
|
|
509
|
+
* const { url } = await api.products.cartCheckout({
|
|
510
|
+
* items: [{ priceId: 'price_xxx', quantity: 1 }],
|
|
511
|
+
* customerEmail: 'customer@example.com',
|
|
512
|
+
* successUrl: '/success',
|
|
513
|
+
* cancelUrl: '/cart',
|
|
514
|
+
* });
|
|
515
|
+
* window.location.href = url;
|
|
516
|
+
* ```
|
|
484
517
|
*/
|
|
485
518
|
cartCheckout(params: {
|
|
486
519
|
items: Array<{
|
|
487
520
|
priceId: string;
|
|
488
521
|
quantity: number;
|
|
489
522
|
}>;
|
|
490
|
-
customerEmail
|
|
523
|
+
customerEmail?: string;
|
|
491
524
|
customerName?: string;
|
|
492
525
|
successUrl?: string;
|
|
493
526
|
cancelUrl?: string;
|
package/dist/index.js
CHANGED
|
@@ -44,14 +44,24 @@ var DreamClient = class {
|
|
|
44
44
|
constructor(config) {
|
|
45
45
|
this.userToken = null;
|
|
46
46
|
this.tokenRefresher = null;
|
|
47
|
-
if (!config.secretKey) {
|
|
48
|
-
throw new Error("DreamAPI: secretKey is required");
|
|
47
|
+
if (!config.secretKey && !config.publishableKey) {
|
|
48
|
+
throw new Error("DreamAPI: Either secretKey or publishableKey is required");
|
|
49
49
|
}
|
|
50
50
|
this.secretKey = config.secretKey;
|
|
51
51
|
this.publishableKey = config.publishableKey;
|
|
52
52
|
this.baseUrl = config.baseUrl || DEFAULT_BASE_URL;
|
|
53
53
|
this.signupUrl = config.signupUrl || DEFAULT_SIGNUP_URL;
|
|
54
54
|
this.clerkUrl = config.clerkBaseUrl || DEFAULT_CLERK_URL;
|
|
55
|
+
this.frontendOnly = !config.secretKey && !!config.publishableKey;
|
|
56
|
+
if (this.frontendOnly) {
|
|
57
|
+
console.log("[DreamAPI] Running in frontend-only mode (PK auth)");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if running in frontend-only mode
|
|
62
|
+
*/
|
|
63
|
+
isFrontendOnly() {
|
|
64
|
+
return this.frontendOnly;
|
|
55
65
|
}
|
|
56
66
|
/**
|
|
57
67
|
* Set the end-user JWT token for user-specific operations.
|
|
@@ -107,11 +117,15 @@ var DreamClient = class {
|
|
|
107
117
|
async request(method, endpoint, options = {}) {
|
|
108
118
|
const { body, requiresUserToken = false } = options;
|
|
109
119
|
const headers = {
|
|
110
|
-
"Authorization": `Bearer ${this.secretKey}`,
|
|
111
120
|
"Content-Type": "application/json"
|
|
112
121
|
};
|
|
113
|
-
if (this.
|
|
122
|
+
if (this.frontendOnly) {
|
|
114
123
|
headers["X-Publishable-Key"] = this.publishableKey;
|
|
124
|
+
} else {
|
|
125
|
+
headers["Authorization"] = `Bearer ${this.secretKey}`;
|
|
126
|
+
if (this.publishableKey) {
|
|
127
|
+
headers["X-Publishable-Key"] = this.publishableKey;
|
|
128
|
+
}
|
|
115
129
|
}
|
|
116
130
|
if (requiresUserToken) {
|
|
117
131
|
if (!this.userToken) {
|
|
@@ -562,9 +576,27 @@ var ProductAPI = class {
|
|
|
562
576
|
}
|
|
563
577
|
/**
|
|
564
578
|
* Create a cart checkout (guest checkout for store)
|
|
579
|
+
*
|
|
580
|
+
* @example
|
|
581
|
+
* ```typescript
|
|
582
|
+
* const { url } = await api.products.cartCheckout({
|
|
583
|
+
* items: [{ priceId: 'price_xxx', quantity: 1 }],
|
|
584
|
+
* customerEmail: 'customer@example.com',
|
|
585
|
+
* successUrl: '/success',
|
|
586
|
+
* cancelUrl: '/cart',
|
|
587
|
+
* });
|
|
588
|
+
* window.location.href = url;
|
|
589
|
+
* ```
|
|
565
590
|
*/
|
|
566
591
|
async cartCheckout(params) {
|
|
567
|
-
|
|
592
|
+
const apiParams = {
|
|
593
|
+
items: params.items,
|
|
594
|
+
email: params.customerEmail,
|
|
595
|
+
name: params.customerName,
|
|
596
|
+
successUrl: params.successUrl,
|
|
597
|
+
cancelUrl: params.cancelUrl
|
|
598
|
+
};
|
|
599
|
+
return this.client.post("/api/cart/checkout", apiParams);
|
|
568
600
|
}
|
|
569
601
|
};
|
|
570
602
|
var DashboardAPI = class {
|
package/dist/index.mjs
CHANGED
|
@@ -16,14 +16,24 @@ var DreamClient = class {
|
|
|
16
16
|
constructor(config) {
|
|
17
17
|
this.userToken = null;
|
|
18
18
|
this.tokenRefresher = null;
|
|
19
|
-
if (!config.secretKey) {
|
|
20
|
-
throw new Error("DreamAPI: secretKey is required");
|
|
19
|
+
if (!config.secretKey && !config.publishableKey) {
|
|
20
|
+
throw new Error("DreamAPI: Either secretKey or publishableKey is required");
|
|
21
21
|
}
|
|
22
22
|
this.secretKey = config.secretKey;
|
|
23
23
|
this.publishableKey = config.publishableKey;
|
|
24
24
|
this.baseUrl = config.baseUrl || DEFAULT_BASE_URL;
|
|
25
25
|
this.signupUrl = config.signupUrl || DEFAULT_SIGNUP_URL;
|
|
26
26
|
this.clerkUrl = config.clerkBaseUrl || DEFAULT_CLERK_URL;
|
|
27
|
+
this.frontendOnly = !config.secretKey && !!config.publishableKey;
|
|
28
|
+
if (this.frontendOnly) {
|
|
29
|
+
console.log("[DreamAPI] Running in frontend-only mode (PK auth)");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Check if running in frontend-only mode
|
|
34
|
+
*/
|
|
35
|
+
isFrontendOnly() {
|
|
36
|
+
return this.frontendOnly;
|
|
27
37
|
}
|
|
28
38
|
/**
|
|
29
39
|
* Set the end-user JWT token for user-specific operations.
|
|
@@ -79,11 +89,15 @@ var DreamClient = class {
|
|
|
79
89
|
async request(method, endpoint, options = {}) {
|
|
80
90
|
const { body, requiresUserToken = false } = options;
|
|
81
91
|
const headers = {
|
|
82
|
-
"Authorization": `Bearer ${this.secretKey}`,
|
|
83
92
|
"Content-Type": "application/json"
|
|
84
93
|
};
|
|
85
|
-
if (this.
|
|
94
|
+
if (this.frontendOnly) {
|
|
86
95
|
headers["X-Publishable-Key"] = this.publishableKey;
|
|
96
|
+
} else {
|
|
97
|
+
headers["Authorization"] = `Bearer ${this.secretKey}`;
|
|
98
|
+
if (this.publishableKey) {
|
|
99
|
+
headers["X-Publishable-Key"] = this.publishableKey;
|
|
100
|
+
}
|
|
87
101
|
}
|
|
88
102
|
if (requiresUserToken) {
|
|
89
103
|
if (!this.userToken) {
|
|
@@ -534,9 +548,27 @@ var ProductAPI = class {
|
|
|
534
548
|
}
|
|
535
549
|
/**
|
|
536
550
|
* Create a cart checkout (guest checkout for store)
|
|
551
|
+
*
|
|
552
|
+
* @example
|
|
553
|
+
* ```typescript
|
|
554
|
+
* const { url } = await api.products.cartCheckout({
|
|
555
|
+
* items: [{ priceId: 'price_xxx', quantity: 1 }],
|
|
556
|
+
* customerEmail: 'customer@example.com',
|
|
557
|
+
* successUrl: '/success',
|
|
558
|
+
* cancelUrl: '/cart',
|
|
559
|
+
* });
|
|
560
|
+
* window.location.href = url;
|
|
561
|
+
* ```
|
|
537
562
|
*/
|
|
538
563
|
async cartCheckout(params) {
|
|
539
|
-
|
|
564
|
+
const apiParams = {
|
|
565
|
+
items: params.items,
|
|
566
|
+
email: params.customerEmail,
|
|
567
|
+
name: params.customerName,
|
|
568
|
+
successUrl: params.successUrl,
|
|
569
|
+
cancelUrl: params.cancelUrl
|
|
570
|
+
};
|
|
571
|
+
return this.client.post("/api/cart/checkout", apiParams);
|
|
540
572
|
}
|
|
541
573
|
};
|
|
542
574
|
var DashboardAPI = class {
|