@dream-api/sdk 0.1.1 → 0.1.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.
- package/README.md +9 -1
- package/dist/index.d.mts +20 -3
- package/dist/index.d.ts +20 -3
- package/dist/index.js +24 -4
- package/dist/index.mjs +24 -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.
|
|
@@ -106,12 +116,19 @@ var DreamClient = class {
|
|
|
106
116
|
*/
|
|
107
117
|
async request(method, endpoint, options = {}) {
|
|
108
118
|
const { body, requiresUserToken = false } = options;
|
|
119
|
+
if (requiresUserToken || this.userToken) {
|
|
120
|
+
await this.ensureFreshToken();
|
|
121
|
+
}
|
|
109
122
|
const headers = {
|
|
110
|
-
"Authorization": `Bearer ${this.secretKey}`,
|
|
111
123
|
"Content-Type": "application/json"
|
|
112
124
|
};
|
|
113
|
-
if (this.
|
|
125
|
+
if (this.frontendOnly) {
|
|
114
126
|
headers["X-Publishable-Key"] = this.publishableKey;
|
|
127
|
+
} else {
|
|
128
|
+
headers["Authorization"] = `Bearer ${this.secretKey}`;
|
|
129
|
+
if (this.publishableKey) {
|
|
130
|
+
headers["X-Publishable-Key"] = this.publishableKey;
|
|
131
|
+
}
|
|
115
132
|
}
|
|
116
133
|
if (requiresUserToken) {
|
|
117
134
|
if (!this.userToken) {
|
|
@@ -310,6 +327,9 @@ var AuthHelpers = class {
|
|
|
310
327
|
async init() {
|
|
311
328
|
if (this.initialized) return;
|
|
312
329
|
await this.clerk.load();
|
|
330
|
+
this.client.setTokenRefresher(async () => {
|
|
331
|
+
return await this.clerk.refreshToken();
|
|
332
|
+
});
|
|
313
333
|
this.initialized = true;
|
|
314
334
|
}
|
|
315
335
|
/**
|
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.
|
|
@@ -78,12 +88,19 @@ var DreamClient = class {
|
|
|
78
88
|
*/
|
|
79
89
|
async request(method, endpoint, options = {}) {
|
|
80
90
|
const { body, requiresUserToken = false } = options;
|
|
91
|
+
if (requiresUserToken || this.userToken) {
|
|
92
|
+
await this.ensureFreshToken();
|
|
93
|
+
}
|
|
81
94
|
const headers = {
|
|
82
|
-
"Authorization": `Bearer ${this.secretKey}`,
|
|
83
95
|
"Content-Type": "application/json"
|
|
84
96
|
};
|
|
85
|
-
if (this.
|
|
97
|
+
if (this.frontendOnly) {
|
|
86
98
|
headers["X-Publishable-Key"] = this.publishableKey;
|
|
99
|
+
} else {
|
|
100
|
+
headers["Authorization"] = `Bearer ${this.secretKey}`;
|
|
101
|
+
if (this.publishableKey) {
|
|
102
|
+
headers["X-Publishable-Key"] = this.publishableKey;
|
|
103
|
+
}
|
|
87
104
|
}
|
|
88
105
|
if (requiresUserToken) {
|
|
89
106
|
if (!this.userToken) {
|
|
@@ -282,6 +299,9 @@ var AuthHelpers = class {
|
|
|
282
299
|
async init() {
|
|
283
300
|
if (this.initialized) return;
|
|
284
301
|
await this.clerk.load();
|
|
302
|
+
this.client.setTokenRefresher(async () => {
|
|
303
|
+
return await this.clerk.refreshToken();
|
|
304
|
+
});
|
|
285
305
|
this.initialized = true;
|
|
286
306
|
}
|
|
287
307
|
/**
|