@dream-api/sdk 0.1.1 → 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 +20 -3
- package/dist/index.d.ts +20 -3
- package/dist/index.js +18 -4
- package/dist/index.mjs +18 -4
- 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;
|
|
@@ -137,7 +145,16 @@ declare class DreamClient {
|
|
|
137
145
|
private clerkUrl;
|
|
138
146
|
private userToken;
|
|
139
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;
|
|
140
153
|
constructor(config: DreamAPIConfig);
|
|
154
|
+
/**
|
|
155
|
+
* Check if running in frontend-only mode
|
|
156
|
+
*/
|
|
157
|
+
isFrontendOnly(): boolean;
|
|
141
158
|
/**
|
|
142
159
|
* Set the end-user JWT token for user-specific operations.
|
|
143
160
|
* Call this after the user signs in via Clerk.
|
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;
|
|
@@ -137,7 +145,16 @@ declare class DreamClient {
|
|
|
137
145
|
private clerkUrl;
|
|
138
146
|
private userToken;
|
|
139
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;
|
|
140
153
|
constructor(config: DreamAPIConfig);
|
|
154
|
+
/**
|
|
155
|
+
* Check if running in frontend-only mode
|
|
156
|
+
*/
|
|
157
|
+
isFrontendOnly(): boolean;
|
|
141
158
|
/**
|
|
142
159
|
* Set the end-user JWT token for user-specific operations.
|
|
143
160
|
* Call this after the user signs in via Clerk.
|
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) {
|
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) {
|