spaps-sdk 1.0.1 → 1.1.0
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 +204 -545
- package/dist/index.d.mts +302 -0
- package/dist/index.d.ts +302 -0
- package/dist/index.js +379 -850
- package/dist/index.mjs +370 -836
- package/package.json +40 -34
- package/.env.example +0 -23
- package/admin-utils.ts +0 -243
package/dist/index.js
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __esm = (fn, res) => function __init() {
|
|
9
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
10
|
+
};
|
|
6
11
|
var __export = (target, all) => {
|
|
7
12
|
for (var name in all)
|
|
8
13
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -15,929 +20,453 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
20
|
}
|
|
16
21
|
return to;
|
|
17
22
|
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
18
31
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
32
|
|
|
20
|
-
//
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
// src/permissions.ts
|
|
34
|
+
var permissions_exports = {};
|
|
35
|
+
__export(permissions_exports, {
|
|
36
|
+
DEFAULT_ADMIN_ACCOUNTS: () => DEFAULT_ADMIN_ACCOUNTS,
|
|
37
|
+
PermissionChecker: () => PermissionChecker,
|
|
38
|
+
canAccessAdmin: () => canAccessAdmin,
|
|
39
|
+
createPermissionChecker: () => createPermissionChecker,
|
|
40
|
+
defaultPermissionChecker: () => defaultPermissionChecker,
|
|
41
|
+
getRoleAwareErrorMessage: () => getRoleAwareErrorMessage,
|
|
42
|
+
getUserDisplay: () => getUserDisplay,
|
|
43
|
+
getUserRole: () => getUserRole,
|
|
44
|
+
hasPermission: () => hasPermission,
|
|
45
|
+
isAdminAccount: () => isAdminAccount
|
|
32
46
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
super(message);
|
|
39
|
-
this.name = "SweetPotatoAPIError";
|
|
40
|
-
if (code !== void 0) this.code = code;
|
|
41
|
-
if (status !== void 0) this.status = status;
|
|
42
|
-
if (details !== void 0) this.details = details;
|
|
47
|
+
function isAdminAccount(identifier, customAdmins = []) {
|
|
48
|
+
if (!identifier) return false;
|
|
49
|
+
const normalized = identifier.toLowerCase();
|
|
50
|
+
if (normalized === DEFAULT_ADMIN_ACCOUNTS.email.toLowerCase() || normalized === DEFAULT_ADMIN_ACCOUNTS.wallets.ethereum.toLowerCase() || normalized === DEFAULT_ADMIN_ACCOUNTS.wallets.solana.toLowerCase()) {
|
|
51
|
+
return true;
|
|
43
52
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (typeof process !== "undefined" && process.env) {
|
|
50
|
-
if (process.env.NODE_ENV === "development" || process.env.SPAPS_LOCAL_MODE === "true") {
|
|
53
|
+
return customAdmins.some((admin) => {
|
|
54
|
+
if (typeof admin === "string") {
|
|
55
|
+
return admin.toLowerCase() === normalized;
|
|
56
|
+
}
|
|
57
|
+
if ("email" in admin && admin.email.toLowerCase() === normalized) {
|
|
51
58
|
return true;
|
|
52
59
|
}
|
|
60
|
+
if ("wallets" in admin) {
|
|
61
|
+
return Object.values(admin.wallets).some(
|
|
62
|
+
(wallet) => wallet.toLowerCase() === normalized
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function getUserRole(identifier, customAdmins = []) {
|
|
69
|
+
if (!identifier) return "guest";
|
|
70
|
+
if (isAdminAccount(identifier, customAdmins)) {
|
|
71
|
+
return "admin";
|
|
53
72
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
73
|
+
return "user";
|
|
74
|
+
}
|
|
75
|
+
function hasPermission(user, requiredPermissions, customAdmins = []) {
|
|
76
|
+
if (!user) return false;
|
|
77
|
+
const identifier = user.email || user.wallet_address;
|
|
78
|
+
const userRole = getUserRole(identifier, customAdmins);
|
|
79
|
+
if (userRole === "admin") {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (Array.isArray(requiredPermissions)) {
|
|
83
|
+
return requiredPermissions.every(
|
|
84
|
+
(permission) => user.permissions?.includes(permission)
|
|
85
|
+
);
|
|
57
86
|
}
|
|
58
|
-
return false;
|
|
87
|
+
return user.permissions?.includes(requiredPermissions) || false;
|
|
59
88
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
...config,
|
|
68
|
-
localMode
|
|
89
|
+
function canAccessAdmin(user, customAdmins = []) {
|
|
90
|
+
if (!user) {
|
|
91
|
+
return {
|
|
92
|
+
allowed: false,
|
|
93
|
+
reason: "Authentication required",
|
|
94
|
+
userRole: "guest",
|
|
95
|
+
requiredRole: "admin"
|
|
69
96
|
};
|
|
70
|
-
this.isLocalMode = this.config.localMode || false;
|
|
71
|
-
this.accessToken = void 0;
|
|
72
|
-
if (this.isLocalMode && typeof console !== "undefined") {
|
|
73
|
-
console.log("[SPAPS SDK] Running in local development mode - authentication will be automatic");
|
|
74
|
-
}
|
|
75
97
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
98
|
+
const identifier = user.email || user.wallet_address;
|
|
99
|
+
const userRole = getUserRole(identifier, customAdmins);
|
|
100
|
+
const isAdmin = userRole === "admin";
|
|
101
|
+
return {
|
|
102
|
+
allowed: isAdmin,
|
|
103
|
+
reason: isAdmin ? void 0 : "Admin privileges required",
|
|
104
|
+
userRole,
|
|
105
|
+
requiredRole: "admin"
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function getRoleAwareErrorMessage(requiredRole, userRole, action = "perform this action") {
|
|
109
|
+
const messages = {
|
|
110
|
+
admin: {
|
|
111
|
+
user: `\u{1F512} Admin privileges required to ${action}. Please authenticate with an admin account.`,
|
|
112
|
+
guest: `\u{1F510} Authentication required. Please sign in with an admin account to ${action}.`
|
|
113
|
+
},
|
|
114
|
+
user: {
|
|
115
|
+
guest: `\u{1F510} Authentication required. Please sign in to ${action}.`
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return messages[requiredRole]?.[userRole] || `Access denied. Required role: ${requiredRole}, current role: ${userRole}`;
|
|
119
|
+
}
|
|
120
|
+
function getUserDisplay(user, customAdmins = []) {
|
|
121
|
+
if (!user) {
|
|
122
|
+
return {
|
|
123
|
+
displayName: "Guest",
|
|
124
|
+
role: "guest",
|
|
125
|
+
badge: null,
|
|
126
|
+
isAdmin: false
|
|
127
|
+
};
|
|
87
128
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
129
|
+
const identifier = user.email || user.wallet_address;
|
|
130
|
+
const role = getUserRole(identifier, customAdmins);
|
|
131
|
+
const isAdmin = role === "admin";
|
|
132
|
+
return {
|
|
133
|
+
displayName: user.email || `${user.wallet_address?.slice(0, 6)}...${user.wallet_address?.slice(-4)}` || "User",
|
|
134
|
+
role,
|
|
135
|
+
badge: isAdmin ? "\u{1F451} Admin" : null,
|
|
136
|
+
isAdmin
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function createPermissionChecker(customAdmins = []) {
|
|
140
|
+
return new PermissionChecker(customAdmins);
|
|
141
|
+
}
|
|
142
|
+
var DEFAULT_ADMIN_ACCOUNTS, PermissionChecker, defaultPermissionChecker;
|
|
143
|
+
var init_permissions = __esm({
|
|
144
|
+
"src/permissions.ts"() {
|
|
145
|
+
"use strict";
|
|
146
|
+
DEFAULT_ADMIN_ACCOUNTS = {
|
|
147
|
+
email: "buildooor@gmail.com",
|
|
148
|
+
wallets: {
|
|
149
|
+
ethereum: "0xa72bb7CeF1e4B2Cc144373d8dE0Add7CCc8DF4Ba",
|
|
150
|
+
solana: "HVEbdiYU3Rr34NHBSgKs7q8cvdTeZLqNL77Z1FB2vjLy"
|
|
151
|
+
}
|
|
98
152
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
153
|
+
PermissionChecker = class {
|
|
154
|
+
customAdmins;
|
|
155
|
+
constructor(customAdmins = []) {
|
|
156
|
+
this.customAdmins = customAdmins;
|
|
157
|
+
}
|
|
158
|
+
isAdmin(identifier) {
|
|
159
|
+
return isAdminAccount(identifier, this.customAdmins);
|
|
160
|
+
}
|
|
161
|
+
getRole(identifier) {
|
|
162
|
+
return getUserRole(identifier, this.customAdmins);
|
|
163
|
+
}
|
|
164
|
+
hasPermission(user, permissions) {
|
|
165
|
+
return hasPermission(user, permissions, this.customAdmins);
|
|
166
|
+
}
|
|
167
|
+
canAccessAdmin(user) {
|
|
168
|
+
return canAccessAdmin(user, this.customAdmins);
|
|
169
|
+
}
|
|
170
|
+
getErrorMessage(requiredRole, userRole, action) {
|
|
171
|
+
return getRoleAwareErrorMessage(requiredRole, userRole, action);
|
|
172
|
+
}
|
|
173
|
+
getUserDisplay(user) {
|
|
174
|
+
return getUserDisplay(user, this.customAdmins);
|
|
175
|
+
}
|
|
176
|
+
// Convenience methods
|
|
177
|
+
requiresAuth(user) {
|
|
178
|
+
return !user;
|
|
179
|
+
}
|
|
180
|
+
requiresAdmin(user) {
|
|
181
|
+
return !this.canAccessAdmin(user).allowed;
|
|
182
|
+
}
|
|
183
|
+
addCustomAdmin(admin) {
|
|
184
|
+
this.customAdmins.push(admin);
|
|
185
|
+
}
|
|
186
|
+
removeCustomAdmin(admin) {
|
|
187
|
+
this.customAdmins = this.customAdmins.filter((a) => a !== admin);
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
defaultPermissionChecker = new PermissionChecker();
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// src/index.ts
|
|
195
|
+
var index_exports = {};
|
|
196
|
+
__export(index_exports, {
|
|
197
|
+
DEFAULT_ADMIN_ACCOUNTS: () => DEFAULT_ADMIN_ACCOUNTS,
|
|
198
|
+
PermissionChecker: () => PermissionChecker,
|
|
199
|
+
SPAPS: () => SPAPSClient,
|
|
200
|
+
SPAPSClient: () => SPAPSClient,
|
|
201
|
+
SweetPotatoSDK: () => SPAPSClient,
|
|
202
|
+
canAccessAdmin: () => canAccessAdmin,
|
|
203
|
+
createPermissionChecker: () => createPermissionChecker,
|
|
204
|
+
default: () => index_default,
|
|
205
|
+
defaultPermissionChecker: () => defaultPermissionChecker,
|
|
206
|
+
getRoleAwareErrorMessage: () => getRoleAwareErrorMessage,
|
|
207
|
+
getUserDisplay: () => getUserDisplay,
|
|
208
|
+
getUserRole: () => getUserRole,
|
|
209
|
+
hasPermission: () => hasPermission,
|
|
210
|
+
isAdminAccount: () => isAdminAccount
|
|
211
|
+
});
|
|
212
|
+
module.exports = __toCommonJS(index_exports);
|
|
213
|
+
var import_axios = __toESM(require("axios"));
|
|
214
|
+
init_permissions();
|
|
215
|
+
var SPAPSClient = class {
|
|
216
|
+
client;
|
|
217
|
+
apiKey;
|
|
218
|
+
accessToken;
|
|
219
|
+
refreshToken;
|
|
220
|
+
_isLocalMode = false;
|
|
221
|
+
// Admin namespace for cleaner API
|
|
222
|
+
admin = {
|
|
223
|
+
createProduct: (productData) => this.createProduct(productData),
|
|
224
|
+
updateProduct: (productId, updates) => this.updateProduct(productId, updates),
|
|
225
|
+
deleteProduct: (productId) => this.deleteProduct(productId),
|
|
226
|
+
createPrice: (priceData) => this.createPrice(priceData),
|
|
227
|
+
syncProducts: () => this.syncProducts(),
|
|
228
|
+
getProducts: () => this.getProducts()
|
|
229
|
+
};
|
|
230
|
+
constructor(config = {}) {
|
|
231
|
+
const apiUrl = config.apiUrl || process.env.SPAPS_API_URL || process.env.NEXT_PUBLIC_SPAPS_API_URL;
|
|
232
|
+
if (!apiUrl || apiUrl.includes("localhost") || apiUrl.includes("127.0.0.1")) {
|
|
233
|
+
this._isLocalMode = true;
|
|
234
|
+
this.client = import_axios.default.create({
|
|
235
|
+
baseURL: apiUrl || "http://localhost:3300",
|
|
236
|
+
timeout: config.timeout || 1e4,
|
|
237
|
+
headers: {
|
|
238
|
+
"Content-Type": "application/json"
|
|
137
239
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
240
|
+
});
|
|
241
|
+
} else {
|
|
242
|
+
if (!config.apiKey && !process.env.SPAPS_API_KEY) {
|
|
243
|
+
console.warn("\u26A0\uFE0F SPAPS: No API key provided. Some features may not work.");
|
|
244
|
+
}
|
|
245
|
+
this.apiKey = config.apiKey || process.env.SPAPS_API_KEY;
|
|
246
|
+
this.client = import_axios.default.create({
|
|
247
|
+
baseURL: apiUrl,
|
|
248
|
+
timeout: config.timeout || 1e4,
|
|
249
|
+
headers: {
|
|
250
|
+
"Content-Type": "application/json",
|
|
251
|
+
...this.apiKey && { "X-API-Key": this.apiKey }
|
|
145
252
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
this.client.interceptors.request.use((config2) => {
|
|
256
|
+
if (this.accessToken && !config2.headers.Authorization) {
|
|
257
|
+
config2.headers.Authorization = `Bearer ${this.accessToken}`;
|
|
258
|
+
}
|
|
259
|
+
return config2;
|
|
260
|
+
});
|
|
261
|
+
this.client.interceptors.response.use(
|
|
262
|
+
(response) => response,
|
|
263
|
+
async (error) => {
|
|
264
|
+
if (error.response?.status === 401 && this.refreshToken) {
|
|
265
|
+
try {
|
|
266
|
+
const { data } = await this.refresh(this.refreshToken);
|
|
267
|
+
this.accessToken = data.access_token;
|
|
268
|
+
this.refreshToken = data.refresh_token;
|
|
269
|
+
if (error.config) {
|
|
270
|
+
error.config.headers.Authorization = `Bearer ${this.accessToken}`;
|
|
271
|
+
return this.client.request(error.config);
|
|
272
|
+
}
|
|
273
|
+
} catch (refreshError) {
|
|
274
|
+
this.accessToken = void 0;
|
|
275
|
+
this.refreshToken = void 0;
|
|
152
276
|
}
|
|
153
277
|
}
|
|
154
|
-
|
|
155
|
-
break;
|
|
156
|
-
}
|
|
157
|
-
const delayMs = Math.min(1e3 * Math.pow(2, attempt - 1), 1e4);
|
|
158
|
-
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
278
|
+
return Promise.reject(error);
|
|
159
279
|
}
|
|
160
|
-
}
|
|
161
|
-
if (lastError instanceof SweetPotatoAPIError) {
|
|
162
|
-
throw lastError;
|
|
163
|
-
}
|
|
164
|
-
throw new SweetPotatoAPIError(
|
|
165
|
-
(lastError == null ? void 0 : lastError.message) || "Request failed after all retries",
|
|
166
|
-
"REQUEST_FAILED",
|
|
167
|
-
void 0,
|
|
168
|
-
{ originalError: lastError }
|
|
169
280
|
);
|
|
170
281
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
method: "GET",
|
|
177
|
-
url,
|
|
178
|
-
requiresAuth
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Convenience method for POST requests
|
|
183
|
-
*/
|
|
184
|
-
async post(url, data, requiresAuth = false) {
|
|
185
|
-
return this.request({
|
|
186
|
-
method: "POST",
|
|
187
|
-
url,
|
|
188
|
-
data,
|
|
189
|
-
requiresAuth
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Convenience method for PUT requests
|
|
194
|
-
*/
|
|
195
|
-
async put(url, data, requiresAuth = false) {
|
|
196
|
-
return this.request({
|
|
197
|
-
method: "PUT",
|
|
198
|
-
url,
|
|
199
|
-
data,
|
|
200
|
-
requiresAuth
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Convenience method for DELETE requests
|
|
205
|
-
*/
|
|
206
|
-
async delete(url, requiresAuth = false) {
|
|
207
|
-
return this.request({
|
|
208
|
-
method: "DELETE",
|
|
209
|
-
url,
|
|
210
|
-
requiresAuth
|
|
282
|
+
// Authentication Methods
|
|
283
|
+
async login(email, password) {
|
|
284
|
+
const response = await this.client.post("/api/auth/login", {
|
|
285
|
+
email,
|
|
286
|
+
password
|
|
211
287
|
});
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Get a nonce for wallet signature
|
|
222
|
-
* @param walletAddress - The wallet address to generate a nonce for
|
|
223
|
-
* @returns Promise<NonceResponse>
|
|
224
|
-
*/
|
|
225
|
-
async getNonce(walletAddress) {
|
|
226
|
-
var _a;
|
|
227
|
-
const response = await this.httpClient.post("/api/auth/nonce", {
|
|
228
|
-
wallet_address: walletAddress
|
|
288
|
+
this.accessToken = response.data.access_token;
|
|
289
|
+
this.refreshToken = response.data.refresh_token;
|
|
290
|
+
return response;
|
|
291
|
+
}
|
|
292
|
+
async register(email, password) {
|
|
293
|
+
const response = await this.client.post("/api/auth/register", {
|
|
294
|
+
email,
|
|
295
|
+
password
|
|
229
296
|
});
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return response.data;
|
|
297
|
+
this.accessToken = response.data.access_token;
|
|
298
|
+
this.refreshToken = response.data.refresh_token;
|
|
299
|
+
return response;
|
|
234
300
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
* @param request - Wallet sign-in request data
|
|
238
|
-
* @returns Promise<AuthResponse>
|
|
239
|
-
*/
|
|
240
|
-
async signInWithWallet(request) {
|
|
241
|
-
var _a;
|
|
242
|
-
const response = await this.httpClient.post("/api/auth/wallet-sign-in", request);
|
|
243
|
-
if (!response.success || !response.data) {
|
|
244
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Wallet sign-in failed");
|
|
245
|
-
}
|
|
246
|
-
this.httpClient.setAccessToken(response.data.access_token);
|
|
247
|
-
return response.data;
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Complete wallet authentication flow
|
|
251
|
-
* This is a convenience method that combines getNonce and signInWithWallet
|
|
252
|
-
* @param walletAddress - The wallet address
|
|
253
|
-
* @param signatureFunction - Function that signs the auth message
|
|
254
|
-
* @param chainType - Optional chain type (will be auto-detected if not provided)
|
|
255
|
-
* @param username - Optional username for new users
|
|
256
|
-
* @returns Promise<AuthResponse>
|
|
257
|
-
*/
|
|
258
|
-
async authenticateWallet(walletAddress, signatureFunction, chainType, username) {
|
|
259
|
-
const nonceData = await this.getNonce(walletAddress);
|
|
260
|
-
const signature = await signatureFunction(nonceData.message);
|
|
261
|
-
const request = {
|
|
301
|
+
async walletSignIn(walletAddress, signature, message, chainType = "solana") {
|
|
302
|
+
const response = await this.client.post("/api/auth/wallet-sign-in", {
|
|
262
303
|
wallet_address: walletAddress,
|
|
263
304
|
signature,
|
|
264
|
-
message
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
request.username = username;
|
|
271
|
-
}
|
|
272
|
-
return this.signInWithWallet(request);
|
|
273
|
-
}
|
|
274
|
-
/**
|
|
275
|
-
* Sign in with email and password
|
|
276
|
-
* @param request - Traditional login request data
|
|
277
|
-
* @returns Promise<AuthResponse>
|
|
278
|
-
*/
|
|
279
|
-
async signInWithPassword(request) {
|
|
280
|
-
var _a;
|
|
281
|
-
const response = await this.httpClient.post("/api/auth/login", request);
|
|
282
|
-
if (!response.success || !response.data) {
|
|
283
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Login failed");
|
|
284
|
-
}
|
|
285
|
-
this.httpClient.setAccessToken(response.data.access_token);
|
|
286
|
-
return response.data;
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Request a magic link for email authentication
|
|
290
|
-
* @param request - Magic link request data
|
|
291
|
-
* @returns Promise<void>
|
|
292
|
-
*/
|
|
293
|
-
async requestMagicLink(request) {
|
|
294
|
-
var _a;
|
|
295
|
-
const response = await this.httpClient.post("/api/auth/magic-link", request);
|
|
296
|
-
if (!response.success) {
|
|
297
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to send magic link");
|
|
298
|
-
}
|
|
305
|
+
message,
|
|
306
|
+
chain_type: chainType
|
|
307
|
+
});
|
|
308
|
+
this.accessToken = response.data.access_token;
|
|
309
|
+
this.refreshToken = response.data.refresh_token;
|
|
310
|
+
return response;
|
|
299
311
|
}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
* @returns Promise<TokenPair>
|
|
304
|
-
*/
|
|
305
|
-
async refreshToken(refreshToken) {
|
|
306
|
-
var _a;
|
|
307
|
-
const response = await this.httpClient.post("/api/auth/refresh", {
|
|
308
|
-
refresh_token: refreshToken
|
|
312
|
+
async refresh(refreshToken) {
|
|
313
|
+
const response = await this.client.post("/api/auth/refresh", {
|
|
314
|
+
refresh_token: refreshToken || this.refreshToken
|
|
309
315
|
});
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
this.httpClient.setAccessToken(response.data.access_token);
|
|
314
|
-
return response.data;
|
|
316
|
+
this.accessToken = response.data.access_token;
|
|
317
|
+
this.refreshToken = response.data.refresh_token;
|
|
318
|
+
return response;
|
|
315
319
|
}
|
|
316
|
-
/**
|
|
317
|
-
* Log out and invalidate tokens
|
|
318
|
-
* @returns Promise<void>
|
|
319
|
-
*/
|
|
320
320
|
async logout() {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
if (!response.success) {
|
|
325
|
-
console.warn("Logout API call failed:", ((_a = response.error) == null ? void 0 : _a.message) || "Unknown error");
|
|
326
|
-
}
|
|
327
|
-
} catch (error) {
|
|
328
|
-
console.warn("Logout API call failed:", error);
|
|
329
|
-
} finally {
|
|
330
|
-
this.httpClient.clearAccessToken();
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Get current user profile
|
|
335
|
-
* @returns Promise<User>
|
|
336
|
-
*/
|
|
337
|
-
async getCurrentUser() {
|
|
338
|
-
var _a;
|
|
339
|
-
const response = await this.httpClient.get("/api/auth/user", true);
|
|
340
|
-
if (!response.success || !response.data) {
|
|
341
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to get user profile");
|
|
342
|
-
}
|
|
343
|
-
return response.data.user;
|
|
344
|
-
}
|
|
345
|
-
/**
|
|
346
|
-
* Check if user is currently authenticated
|
|
347
|
-
* @returns boolean
|
|
348
|
-
*/
|
|
349
|
-
isAuthenticated() {
|
|
350
|
-
return this.httpClient["accessToken"] !== void 0;
|
|
351
|
-
}
|
|
352
|
-
/**
|
|
353
|
-
* Clear authentication state (useful for client-side logout)
|
|
354
|
-
*/
|
|
355
|
-
clearAuth() {
|
|
356
|
-
this.httpClient.clearAccessToken();
|
|
357
|
-
}
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
// payments.ts
|
|
361
|
-
var PaymentsService = class {
|
|
362
|
-
constructor(httpClient) {
|
|
363
|
-
this.httpClient = httpClient;
|
|
364
|
-
}
|
|
365
|
-
// Checkout Sessions
|
|
366
|
-
/**
|
|
367
|
-
* Create a new Stripe checkout session
|
|
368
|
-
* @param request - Checkout session creation parameters
|
|
369
|
-
* @returns Promise<CheckoutSession>
|
|
370
|
-
*/
|
|
371
|
-
async createCheckoutSession(request) {
|
|
372
|
-
var _a;
|
|
373
|
-
const response = await this.httpClient.post(
|
|
374
|
-
"/api/stripe/checkout-sessions",
|
|
375
|
-
request,
|
|
376
|
-
true
|
|
377
|
-
);
|
|
378
|
-
if (!response.success || !response.data) {
|
|
379
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to create checkout session");
|
|
380
|
-
}
|
|
381
|
-
return response.data;
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Retrieve a checkout session by ID
|
|
385
|
-
* @param sessionId - The checkout session ID
|
|
386
|
-
* @returns Promise<CheckoutSession>
|
|
387
|
-
*/
|
|
388
|
-
async getCheckoutSession(sessionId) {
|
|
389
|
-
var _a;
|
|
390
|
-
const response = await this.httpClient.get(
|
|
391
|
-
`/api/stripe/checkout-sessions/${sessionId}`,
|
|
392
|
-
true
|
|
393
|
-
);
|
|
394
|
-
if (!response.success || !response.data) {
|
|
395
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to retrieve checkout session");
|
|
396
|
-
}
|
|
397
|
-
return response.data;
|
|
398
|
-
}
|
|
399
|
-
/**
|
|
400
|
-
* Expire a checkout session
|
|
401
|
-
* @param sessionId - The checkout session ID to expire
|
|
402
|
-
* @returns Promise<{id: string, status: string, expired: boolean}>
|
|
403
|
-
*/
|
|
404
|
-
async expireCheckoutSession(sessionId) {
|
|
405
|
-
var _a;
|
|
406
|
-
const response = await this.httpClient.post(
|
|
407
|
-
`/api/stripe/checkout-sessions/${sessionId}/expire`,
|
|
408
|
-
{},
|
|
409
|
-
true
|
|
410
|
-
);
|
|
411
|
-
if (!response.success || !response.data) {
|
|
412
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to expire checkout session");
|
|
413
|
-
}
|
|
414
|
-
return response.data;
|
|
415
|
-
}
|
|
416
|
-
/**
|
|
417
|
-
* List checkout sessions for the current user
|
|
418
|
-
* @param options - List options (limit, pagination)
|
|
419
|
-
* @returns Promise<CheckoutSessionListResponse>
|
|
420
|
-
*/
|
|
421
|
-
async listCheckoutSessions(options = {}) {
|
|
422
|
-
var _a;
|
|
423
|
-
const params = new URLSearchParams();
|
|
424
|
-
if (options.limit) params.append("limit", options.limit.toString());
|
|
425
|
-
if (options.starting_after) params.append("starting_after", options.starting_after);
|
|
426
|
-
const url = `/api/stripe/checkout-sessions${params.toString() ? `?${params.toString()}` : ""}`;
|
|
427
|
-
const response = await this.httpClient.get(url, true);
|
|
428
|
-
if (!response.success || !response.data) {
|
|
429
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to list checkout sessions");
|
|
430
|
-
}
|
|
431
|
-
return response.data;
|
|
432
|
-
}
|
|
433
|
-
// Products
|
|
434
|
-
/**
|
|
435
|
-
* List available products
|
|
436
|
-
* @param request - Product list filters
|
|
437
|
-
* @returns Promise<ProductsListResponse>
|
|
438
|
-
*/
|
|
439
|
-
async listProducts(request = {}) {
|
|
440
|
-
var _a;
|
|
441
|
-
const params = new URLSearchParams();
|
|
442
|
-
if (request.category) params.append("category", request.category);
|
|
443
|
-
if (request.active !== void 0) params.append("active", request.active.toString());
|
|
444
|
-
if (request.limit) params.append("limit", request.limit.toString());
|
|
445
|
-
if (request.starting_after) params.append("starting_after", request.starting_after);
|
|
446
|
-
const url = `/api/stripe/products${params.toString() ? `?${params.toString()}` : ""}`;
|
|
447
|
-
const response = await this.httpClient.get(url, true);
|
|
448
|
-
if (!response.success || !response.data) {
|
|
449
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to list products");
|
|
450
|
-
}
|
|
451
|
-
return response.data;
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
|
-
* Get a specific product by ID
|
|
455
|
-
* @param productId - The product ID
|
|
456
|
-
* @param includePrices - Whether to include associated prices (default: true)
|
|
457
|
-
* @returns Promise<StripeProduct>
|
|
458
|
-
*/
|
|
459
|
-
async getProduct(productId, includePrices = true) {
|
|
460
|
-
var _a;
|
|
461
|
-
const params = new URLSearchParams();
|
|
462
|
-
if (!includePrices) params.append("include_prices", "false");
|
|
463
|
-
const url = `/api/stripe/products/${productId}${params.toString() ? `?${params.toString()}` : ""}`;
|
|
464
|
-
const response = await this.httpClient.get(url, true);
|
|
465
|
-
if (!response.success || !response.data) {
|
|
466
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to retrieve product");
|
|
467
|
-
}
|
|
468
|
-
return response.data;
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Create a new product (Admin only)
|
|
472
|
-
* @param request - Product creation parameters
|
|
473
|
-
* @returns Promise<StripeProduct>
|
|
474
|
-
*/
|
|
475
|
-
async createProduct(request) {
|
|
476
|
-
var _a;
|
|
477
|
-
const response = await this.httpClient.post(
|
|
478
|
-
"/api/stripe/products",
|
|
479
|
-
request,
|
|
480
|
-
true
|
|
481
|
-
);
|
|
482
|
-
if (!response.success || !response.data) {
|
|
483
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to create product");
|
|
484
|
-
}
|
|
485
|
-
return response.data;
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Update an existing product (Admin only)
|
|
489
|
-
* @param productId - The product ID to update
|
|
490
|
-
* @param request - Product update parameters
|
|
491
|
-
* @returns Promise<StripeProduct>
|
|
492
|
-
*/
|
|
493
|
-
async updateProduct(productId, request) {
|
|
494
|
-
var _a;
|
|
495
|
-
const response = await this.httpClient.put(
|
|
496
|
-
`/api/stripe/products/${productId}`,
|
|
497
|
-
request,
|
|
498
|
-
true
|
|
499
|
-
);
|
|
500
|
-
if (!response.success || !response.data) {
|
|
501
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to update product");
|
|
502
|
-
}
|
|
503
|
-
return response.data;
|
|
504
|
-
}
|
|
505
|
-
/**
|
|
506
|
-
* Archive a product (Admin only)
|
|
507
|
-
* @param productId - The product ID to archive
|
|
508
|
-
* @returns Promise<{id: string, archived: boolean, active: boolean}>
|
|
509
|
-
*/
|
|
510
|
-
async archiveProduct(productId) {
|
|
511
|
-
var _a;
|
|
512
|
-
const response = await this.httpClient.delete(
|
|
513
|
-
`/api/stripe/products/${productId}`,
|
|
514
|
-
true
|
|
515
|
-
);
|
|
516
|
-
if (!response.success || !response.data) {
|
|
517
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to archive product");
|
|
518
|
-
}
|
|
519
|
-
return response.data;
|
|
520
|
-
}
|
|
521
|
-
/**
|
|
522
|
-
* Sync products from Stripe (Admin only)
|
|
523
|
-
* @returns Promise<{synced_count: number, message: string}>
|
|
524
|
-
*/
|
|
525
|
-
async syncProducts() {
|
|
526
|
-
var _a;
|
|
527
|
-
const response = await this.httpClient.post(
|
|
528
|
-
"/api/stripe/products/sync",
|
|
529
|
-
{},
|
|
530
|
-
true
|
|
531
|
-
);
|
|
532
|
-
if (!response.success || !response.data) {
|
|
533
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to sync products");
|
|
534
|
-
}
|
|
535
|
-
return response.data;
|
|
321
|
+
await this.client.post("/api/auth/logout");
|
|
322
|
+
this.accessToken = void 0;
|
|
323
|
+
this.refreshToken = void 0;
|
|
536
324
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
* Create a new price for a product (Admin only)
|
|
540
|
-
* @param request - Price creation parameters
|
|
541
|
-
* @returns Promise<StripePrice>
|
|
542
|
-
*/
|
|
543
|
-
async createPrice(request) {
|
|
544
|
-
var _a;
|
|
545
|
-
const response = await this.httpClient.post(
|
|
546
|
-
"/api/stripe/prices",
|
|
547
|
-
request,
|
|
548
|
-
true
|
|
549
|
-
);
|
|
550
|
-
if (!response.success || !response.data) {
|
|
551
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to create price");
|
|
552
|
-
}
|
|
553
|
-
return response.data;
|
|
325
|
+
async getUser() {
|
|
326
|
+
return this.client.get("/api/auth/user");
|
|
554
327
|
}
|
|
555
|
-
//
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
var _a;
|
|
563
|
-
const response = await this.httpClient.post(
|
|
564
|
-
"/api/stripe/subscriptions",
|
|
565
|
-
request,
|
|
566
|
-
true
|
|
567
|
-
);
|
|
568
|
-
if (!response.success || !response.data) {
|
|
569
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to create subscription");
|
|
570
|
-
}
|
|
571
|
-
return response.data;
|
|
328
|
+
// Stripe Methods
|
|
329
|
+
async createCheckoutSession(priceId, successUrl, cancelUrl) {
|
|
330
|
+
return this.client.post("/api/stripe/create-checkout-session", {
|
|
331
|
+
price_id: priceId,
|
|
332
|
+
success_url: successUrl,
|
|
333
|
+
cancel_url: cancelUrl
|
|
334
|
+
});
|
|
572
335
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
* @param subscriptionId - The subscription ID
|
|
576
|
-
* @returns Promise<Subscription>
|
|
577
|
-
*/
|
|
578
|
-
async getSubscription(subscriptionId) {
|
|
579
|
-
var _a;
|
|
580
|
-
const response = await this.httpClient.get(
|
|
581
|
-
`/api/stripe/subscriptions/${subscriptionId}`,
|
|
582
|
-
true
|
|
583
|
-
);
|
|
584
|
-
if (!response.success || !response.data) {
|
|
585
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to retrieve subscription");
|
|
586
|
-
}
|
|
587
|
-
return response.data;
|
|
336
|
+
async getSubscription() {
|
|
337
|
+
return this.client.get("/api/stripe/subscription");
|
|
588
338
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
* @param options - List options
|
|
592
|
-
* @returns Promise<{subscriptions: Subscription[], has_more: boolean}>
|
|
593
|
-
*/
|
|
594
|
-
async listSubscriptions(options = {}) {
|
|
595
|
-
var _a;
|
|
596
|
-
const params = new URLSearchParams();
|
|
597
|
-
if (options.limit) params.append("limit", options.limit.toString());
|
|
598
|
-
if (options.starting_after) params.append("starting_after", options.starting_after);
|
|
599
|
-
if (options.status) params.append("status", options.status);
|
|
600
|
-
const url = `/api/stripe/subscriptions${params.toString() ? `?${params.toString()}` : ""}`;
|
|
601
|
-
const response = await this.httpClient.get(url, true);
|
|
602
|
-
if (!response.success || !response.data) {
|
|
603
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to list subscriptions");
|
|
604
|
-
}
|
|
605
|
-
return response.data;
|
|
339
|
+
async cancelSubscription() {
|
|
340
|
+
await this.client.delete("/api/stripe/subscription");
|
|
606
341
|
}
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
* @returns Promise<Subscription>
|
|
611
|
-
*/
|
|
612
|
-
async cancelSubscription(subscriptionId) {
|
|
613
|
-
var _a;
|
|
614
|
-
const response = await this.httpClient.post(
|
|
615
|
-
`/api/stripe/subscriptions/${subscriptionId}/cancel`,
|
|
616
|
-
{},
|
|
617
|
-
true
|
|
618
|
-
);
|
|
619
|
-
if (!response.success || !response.data) {
|
|
620
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to cancel subscription");
|
|
621
|
-
}
|
|
622
|
-
return response.data;
|
|
342
|
+
// Usage Methods
|
|
343
|
+
async getUsageBalance() {
|
|
344
|
+
return this.client.get("/api/usage/balance");
|
|
623
345
|
}
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
*/
|
|
630
|
-
async updateSubscription(subscriptionId, request) {
|
|
631
|
-
var _a;
|
|
632
|
-
const response = await this.httpClient.put(
|
|
633
|
-
`/api/stripe/subscriptions/${subscriptionId}`,
|
|
634
|
-
request,
|
|
635
|
-
true
|
|
636
|
-
);
|
|
637
|
-
if (!response.success || !response.data) {
|
|
638
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to update subscription");
|
|
639
|
-
}
|
|
640
|
-
return response.data;
|
|
346
|
+
async recordUsage(feature, amount) {
|
|
347
|
+
await this.client.post("/api/usage/record", {
|
|
348
|
+
feature,
|
|
349
|
+
amount
|
|
350
|
+
});
|
|
641
351
|
}
|
|
642
|
-
//
|
|
352
|
+
// Admin Methods (Require admin privileges)
|
|
643
353
|
/**
|
|
644
|
-
* Create a
|
|
645
|
-
* @param request - Portal session parameters
|
|
646
|
-
* @returns Promise<CustomerPortalSession>
|
|
354
|
+
* Create a new Stripe product (Admin required)
|
|
647
355
|
*/
|
|
648
|
-
async
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
"/api/stripe/customer-portal",
|
|
652
|
-
request,
|
|
653
|
-
true
|
|
654
|
-
);
|
|
655
|
-
if (!response.success || !response.data) {
|
|
656
|
-
throw new Error(((_a = response.error) == null ? void 0 : _a.message) || "Failed to create customer portal session");
|
|
356
|
+
async createProduct(productData) {
|
|
357
|
+
if (!this.accessToken) {
|
|
358
|
+
throw new Error("Authentication required. Please authenticate first.");
|
|
657
359
|
}
|
|
658
|
-
return
|
|
360
|
+
return this.client.post("/api/stripe/products", productData, {
|
|
361
|
+
headers: {
|
|
362
|
+
"Authorization": `Bearer ${this.accessToken}`
|
|
363
|
+
}
|
|
364
|
+
});
|
|
659
365
|
}
|
|
660
|
-
// Utility Methods
|
|
661
366
|
/**
|
|
662
|
-
*
|
|
663
|
-
* Convenience method for simple one-time payments
|
|
664
|
-
* @param params - Payment parameters
|
|
665
|
-
* @returns Promise<CheckoutSession>
|
|
367
|
+
* Update an existing Stripe product (Admin required)
|
|
666
368
|
*/
|
|
667
|
-
async
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
lineItems.push({
|
|
671
|
-
price_id: params.price_id,
|
|
672
|
-
quantity: params.quantity || 1
|
|
673
|
-
});
|
|
674
|
-
} else if (params.product_name && params.amount && params.currency) {
|
|
675
|
-
lineItems.push({
|
|
676
|
-
quantity: params.quantity || 1,
|
|
677
|
-
price_data: {
|
|
678
|
-
currency: params.currency,
|
|
679
|
-
unit_amount: params.amount,
|
|
680
|
-
product_data: {
|
|
681
|
-
name: params.product_name
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
});
|
|
685
|
-
} else {
|
|
686
|
-
throw new Error("Either price_id or (product_name, amount, currency) must be provided");
|
|
369
|
+
async updateProduct(productId, updates) {
|
|
370
|
+
if (!this.accessToken) {
|
|
371
|
+
throw new Error("Authentication required. Please authenticate first.");
|
|
687
372
|
}
|
|
688
|
-
return this.
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
cancel_url: params.cancel_url,
|
|
693
|
-
...params.metadata && { metadata: params.metadata }
|
|
373
|
+
return this.client.put(`/api/stripe/products/${productId}`, updates, {
|
|
374
|
+
headers: {
|
|
375
|
+
"Authorization": `Bearer ${this.accessToken}`
|
|
376
|
+
}
|
|
694
377
|
});
|
|
695
378
|
}
|
|
696
379
|
/**
|
|
697
|
-
*
|
|
698
|
-
* Convenience method for subscription creation
|
|
699
|
-
* @param params - Subscription parameters
|
|
700
|
-
* @returns Promise<CheckoutSession>
|
|
380
|
+
* Archive (soft delete) a Stripe product (Admin required)
|
|
701
381
|
*/
|
|
702
|
-
async
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
success_url: params.success_url,
|
|
710
|
-
cancel_url: params.cancel_url,
|
|
711
|
-
subscription_data: {
|
|
712
|
-
...params.trial_period_days !== void 0 && { trial_period_days: params.trial_period_days },
|
|
713
|
-
...params.metadata && { metadata: params.metadata }
|
|
382
|
+
async deleteProduct(productId) {
|
|
383
|
+
if (!this.accessToken) {
|
|
384
|
+
throw new Error("Authentication required. Please authenticate first.");
|
|
385
|
+
}
|
|
386
|
+
return this.client.delete(`/api/stripe/products/${productId}`, {
|
|
387
|
+
headers: {
|
|
388
|
+
"Authorization": `Bearer ${this.accessToken}`
|
|
714
389
|
}
|
|
715
390
|
});
|
|
716
391
|
}
|
|
717
|
-
};
|
|
718
|
-
|
|
719
|
-
// index.ts
|
|
720
|
-
var SweetPotatoSDK = class {
|
|
721
|
-
constructor(config) {
|
|
722
|
-
if (!config.apiUrl) {
|
|
723
|
-
throw new Error("apiUrl is required");
|
|
724
|
-
}
|
|
725
|
-
this.httpClient = new HttpClient(config);
|
|
726
|
-
this.auth = new AuthService(this.httpClient);
|
|
727
|
-
this.payments = new PaymentsService(this.httpClient);
|
|
728
|
-
this.isLocalMode = this.httpClient["isLocalMode"] || false;
|
|
729
|
-
if (this.isLocalMode) {
|
|
730
|
-
console.log("[SPAPS SDK] Initialized in local development mode");
|
|
731
|
-
console.log("[SPAPS SDK] API URL:", config.apiUrl);
|
|
732
|
-
console.log("[SPAPS SDK] Authentication will be automatic");
|
|
733
|
-
}
|
|
734
|
-
}
|
|
735
392
|
/**
|
|
736
|
-
*
|
|
737
|
-
* @param token - Access token
|
|
393
|
+
* Create a new price for a product (Admin required)
|
|
738
394
|
*/
|
|
739
|
-
|
|
740
|
-
this.
|
|
741
|
-
|
|
742
|
-
/**
|
|
743
|
-
* Clear access token
|
|
744
|
-
*/
|
|
745
|
-
clearAccessToken() {
|
|
746
|
-
this.httpClient.clearAccessToken();
|
|
747
|
-
}
|
|
748
|
-
/**
|
|
749
|
-
* Get SDK configuration
|
|
750
|
-
*/
|
|
751
|
-
getConfig() {
|
|
752
|
-
return {
|
|
753
|
-
apiUrl: this.httpClient["config"].apiUrl,
|
|
754
|
-
timeout: this.httpClient["config"].timeout,
|
|
755
|
-
retries: this.httpClient["config"].retries
|
|
756
|
-
};
|
|
757
|
-
}
|
|
758
|
-
/**
|
|
759
|
-
* Health check endpoint
|
|
760
|
-
* @returns Promise<boolean>
|
|
761
|
-
*/
|
|
762
|
-
async healthCheck() {
|
|
763
|
-
try {
|
|
764
|
-
const response = await this.httpClient.get("/api/health");
|
|
765
|
-
return response.success;
|
|
766
|
-
} catch (e) {
|
|
767
|
-
return false;
|
|
395
|
+
async createPrice(priceData) {
|
|
396
|
+
if (!this.accessToken) {
|
|
397
|
+
throw new Error("Authentication required. Please authenticate first.");
|
|
768
398
|
}
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
*/
|
|
774
|
-
async request(method, url, data, requiresAuth = false) {
|
|
775
|
-
return this.httpClient.request({
|
|
776
|
-
method,
|
|
777
|
-
url,
|
|
778
|
-
data,
|
|
779
|
-
requiresAuth
|
|
399
|
+
return this.client.post("/api/stripe/prices", priceData, {
|
|
400
|
+
headers: {
|
|
401
|
+
"Authorization": `Bearer ${this.accessToken}`
|
|
402
|
+
}
|
|
780
403
|
});
|
|
781
404
|
}
|
|
782
|
-
};
|
|
783
|
-
function createSweetPotatoSDK(config) {
|
|
784
|
-
return new SweetPotatoSDK(config);
|
|
785
|
-
}
|
|
786
|
-
var _TokenManager = class _TokenManager {
|
|
787
405
|
/**
|
|
788
|
-
*
|
|
406
|
+
* Sync all products from Stripe to local database (Super Admin required)
|
|
789
407
|
*/
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
return globalThis.localStorage;
|
|
794
|
-
}
|
|
795
|
-
if (typeof globalThis !== "undefined" && ((_a = globalThis.window) == null ? void 0 : _a.localStorage)) {
|
|
796
|
-
return globalThis.window.localStorage;
|
|
797
|
-
}
|
|
798
|
-
if (typeof global !== "undefined" && ((_b = global.window) == null ? void 0 : _b.localStorage)) {
|
|
799
|
-
return global.window.localStorage;
|
|
408
|
+
async syncProducts() {
|
|
409
|
+
if (!this.accessToken) {
|
|
410
|
+
throw new Error("Authentication required. Please authenticate first.");
|
|
800
411
|
}
|
|
801
|
-
return
|
|
412
|
+
return this.client.post("/api/stripe/products/sync", {}, {
|
|
413
|
+
headers: {
|
|
414
|
+
"Authorization": `Bearer ${this.accessToken}`
|
|
415
|
+
}
|
|
416
|
+
});
|
|
802
417
|
}
|
|
803
418
|
/**
|
|
804
|
-
*
|
|
419
|
+
* Get products with admin metadata (if user is admin)
|
|
805
420
|
*/
|
|
806
|
-
|
|
807
|
-
const
|
|
808
|
-
if (
|
|
809
|
-
|
|
810
|
-
localStorage.setItem(_TokenManager.REFRESH_TOKEN_KEY, tokens.refresh_token);
|
|
811
|
-
localStorage.setItem(_TokenManager.USER_KEY, JSON.stringify(tokens.user));
|
|
421
|
+
async getProducts() {
|
|
422
|
+
const headers = {};
|
|
423
|
+
if (this.accessToken) {
|
|
424
|
+
headers["Authorization"] = `Bearer ${this.accessToken}`;
|
|
812
425
|
}
|
|
426
|
+
return this.client.get("/api/stripe/products", { headers });
|
|
813
427
|
}
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
static getAccessToken() {
|
|
818
|
-
const localStorage = _TokenManager.getStorage();
|
|
819
|
-
return localStorage ? localStorage.getItem(_TokenManager.ACCESS_TOKEN_KEY) : null;
|
|
820
|
-
}
|
|
821
|
-
/**
|
|
822
|
-
* Get stored refresh token (browser only)
|
|
823
|
-
*/
|
|
824
|
-
static getRefreshToken() {
|
|
825
|
-
const localStorage = _TokenManager.getStorage();
|
|
826
|
-
return localStorage ? localStorage.getItem(_TokenManager.REFRESH_TOKEN_KEY) : null;
|
|
428
|
+
// Utility Methods
|
|
429
|
+
isAuthenticated() {
|
|
430
|
+
return !!this.accessToken;
|
|
827
431
|
}
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
*/
|
|
831
|
-
static getStoredUser() {
|
|
832
|
-
const localStorage = _TokenManager.getStorage();
|
|
833
|
-
if (localStorage) {
|
|
834
|
-
const userData = localStorage.getItem(_TokenManager.USER_KEY);
|
|
835
|
-
return userData ? JSON.parse(userData) : null;
|
|
836
|
-
}
|
|
837
|
-
return null;
|
|
432
|
+
getAccessToken() {
|
|
433
|
+
return this.accessToken;
|
|
838
434
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
*/
|
|
842
|
-
static clearTokens() {
|
|
843
|
-
const localStorage = _TokenManager.getStorage();
|
|
844
|
-
if (localStorage) {
|
|
845
|
-
localStorage.removeItem(_TokenManager.ACCESS_TOKEN_KEY);
|
|
846
|
-
localStorage.removeItem(_TokenManager.REFRESH_TOKEN_KEY);
|
|
847
|
-
localStorage.removeItem(_TokenManager.USER_KEY);
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
|
-
/**
|
|
851
|
-
* Check if access token is expired (basic check, doesn't verify signature)
|
|
852
|
-
*/
|
|
853
|
-
static isTokenExpired(token) {
|
|
854
|
-
try {
|
|
855
|
-
const parts = token.split(".");
|
|
856
|
-
if (parts.length !== 3 || !parts[1]) return true;
|
|
857
|
-
const payload = JSON.parse(atob(parts[1]));
|
|
858
|
-
const currentTime = Math.floor(Date.now() / 1e3);
|
|
859
|
-
return payload.exp < currentTime;
|
|
860
|
-
} catch (e) {
|
|
861
|
-
return true;
|
|
862
|
-
}
|
|
435
|
+
setAccessToken(token) {
|
|
436
|
+
this.accessToken = token;
|
|
863
437
|
}
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
*/
|
|
867
|
-
static async autoRefreshToken(sdk) {
|
|
868
|
-
const accessToken = _TokenManager.getAccessToken();
|
|
869
|
-
const refreshToken = _TokenManager.getRefreshToken();
|
|
870
|
-
if (!accessToken || !refreshToken) {
|
|
871
|
-
return false;
|
|
872
|
-
}
|
|
873
|
-
if (!_TokenManager.isTokenExpired(accessToken)) {
|
|
874
|
-
sdk.setAccessToken(accessToken);
|
|
875
|
-
return true;
|
|
876
|
-
}
|
|
877
|
-
try {
|
|
878
|
-
const newTokens = await sdk.auth.refreshToken(refreshToken);
|
|
879
|
-
_TokenManager.storeTokens(newTokens);
|
|
880
|
-
return true;
|
|
881
|
-
} catch (e) {
|
|
882
|
-
_TokenManager.clearTokens();
|
|
883
|
-
return false;
|
|
884
|
-
}
|
|
438
|
+
isLocalMode() {
|
|
439
|
+
return this._isLocalMode;
|
|
885
440
|
}
|
|
886
|
-
};
|
|
887
|
-
_TokenManager.ACCESS_TOKEN_KEY = "sweet_potato_access_token";
|
|
888
|
-
_TokenManager.REFRESH_TOKEN_KEY = "sweet_potato_refresh_token";
|
|
889
|
-
_TokenManager.USER_KEY = "sweet_potato_user";
|
|
890
|
-
var TokenManager = _TokenManager;
|
|
891
|
-
var WalletUtils = class _WalletUtils {
|
|
892
441
|
/**
|
|
893
|
-
*
|
|
442
|
+
* Check if current user has admin privileges
|
|
443
|
+
* Note: This requires the user object from authentication
|
|
894
444
|
*/
|
|
895
|
-
|
|
896
|
-
if (
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
}
|
|
902
|
-
if (/^[1-9A-HJ-NP-Za-km-z]{32}$/.test(address) || /^[1-9A-HJ-NP-Za-km-z]{44}$/.test(address)) {
|
|
903
|
-
return "solana";
|
|
904
|
-
}
|
|
905
|
-
if (/^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$/.test(address) && address.length >= 26 && address.length <= 35) {
|
|
906
|
-
return "bitcoin";
|
|
907
|
-
}
|
|
908
|
-
if (/^[1-9A-HJ-NP-Za-km-z]{35,44}$/.test(address)) {
|
|
909
|
-
return "solana";
|
|
910
|
-
}
|
|
911
|
-
return null;
|
|
445
|
+
isAdmin(user) {
|
|
446
|
+
if (!user) return false;
|
|
447
|
+
const identifier = user.email || user.wallet_address;
|
|
448
|
+
if (!identifier) return false;
|
|
449
|
+
const { isAdminAccount: isAdminAccount2 } = (init_permissions(), __toCommonJS(permissions_exports));
|
|
450
|
+
return isAdminAccount2(identifier);
|
|
912
451
|
}
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
*/
|
|
916
|
-
static isValidAddress(address, chainType) {
|
|
917
|
-
if (!chainType) {
|
|
918
|
-
chainType = _WalletUtils.detectChainType(address) || "ethereum";
|
|
919
|
-
}
|
|
920
|
-
switch (chainType) {
|
|
921
|
-
case "solana":
|
|
922
|
-
return /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address);
|
|
923
|
-
case "ethereum":
|
|
924
|
-
case "base":
|
|
925
|
-
return /^0x[a-fA-F0-9]{40}$/.test(address);
|
|
926
|
-
case "bitcoin":
|
|
927
|
-
return /^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$/.test(address) || /^bc1[a-z0-9]{39,59}$/.test(address);
|
|
928
|
-
default:
|
|
929
|
-
return false;
|
|
930
|
-
}
|
|
452
|
+
async health() {
|
|
453
|
+
return this.client.get("/health");
|
|
931
454
|
}
|
|
932
455
|
};
|
|
456
|
+
var index_default = SPAPSClient;
|
|
933
457
|
// Annotate the CommonJS export names for ESM import in node:
|
|
934
458
|
0 && (module.exports = {
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
459
|
+
DEFAULT_ADMIN_ACCOUNTS,
|
|
460
|
+
PermissionChecker,
|
|
461
|
+
SPAPS,
|
|
462
|
+
SPAPSClient,
|
|
939
463
|
SweetPotatoSDK,
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
464
|
+
canAccessAdmin,
|
|
465
|
+
createPermissionChecker,
|
|
466
|
+
defaultPermissionChecker,
|
|
467
|
+
getRoleAwareErrorMessage,
|
|
468
|
+
getUserDisplay,
|
|
469
|
+
getUserRole,
|
|
470
|
+
hasPermission,
|
|
471
|
+
isAdminAccount
|
|
943
472
|
});
|