@beep-it/sdk-core 0.1.7 → 0.3.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 +8 -8
- package/dist/errors/index.d.ts +105 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +536 -89
- package/dist/index.mjs +512 -85
- package/dist/modules/invoices.d.ts +1 -1
- package/dist/modules/invoices.d.ts.map +1 -1
- package/dist/modules/payments.d.ts +9 -34
- package/dist/modules/payments.d.ts.map +1 -1
- package/dist/modules/products.d.ts +2 -2
- package/dist/types/common.d.ts +197 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/enhanced-payment.d.ts +311 -0
- package/dist/types/enhanced-payment.d.ts.map +1 -0
- package/dist/types/enhanced-product.d.ts +299 -0
- package/dist/types/enhanced-product.d.ts.map +1 -0
- package/dist/types/index.d.ts +2 -9
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/invoice.d.ts +2 -2
- package/dist/types/invoice.d.ts.map +1 -1
- package/dist/types/payment.d.ts +2 -32
- package/dist/types/payment.d.ts.map +1 -1
- package/dist/types/product.d.ts +3 -3
- package/dist/types/product.d.ts.map +1 -1
- package/dist/types/public.d.ts +1 -1
- package/dist/types/public.d.ts.map +1 -1
- package/dist/types/streaming-payments.d.ts +173 -0
- package/dist/types/streaming-payments.d.ts.map +1 -0
- package/dist/types/token.d.ts +16 -17
- package/dist/types/token.d.ts.map +1 -1
- package/dist/utils/debug.d.ts +75 -0
- package/dist/utils/debug.d.ts.map +1 -0
- package/package.json +19 -16
package/dist/index.js
CHANGED
|
@@ -28,60 +28,95 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
BeepAuthenticationError: () => BeepAuthenticationError,
|
|
33
34
|
BeepClient: () => BeepClient,
|
|
35
|
+
BeepError: () => BeepError,
|
|
36
|
+
BeepErrorCode: () => BeepErrorCode,
|
|
37
|
+
BeepNetworkError: () => BeepNetworkError,
|
|
38
|
+
BeepPaymentError: () => BeepPaymentError,
|
|
34
39
|
BeepPublicClient: () => BeepPublicClient,
|
|
40
|
+
BeepRateLimitError: () => BeepRateLimitError,
|
|
41
|
+
BeepValidationError: () => BeepValidationError,
|
|
42
|
+
BlockchainNetwork: () => BlockchainNetwork,
|
|
43
|
+
EntityStatus: () => EntityStatus,
|
|
44
|
+
InvoiceStatus: () => InvoiceStatus,
|
|
45
|
+
PayoutStatus: () => PayoutStatus,
|
|
46
|
+
SortOrder: () => SortOrder,
|
|
35
47
|
SupportedToken: () => SupportedToken,
|
|
36
|
-
|
|
48
|
+
TOKEN_ADDRESSES: () => TOKEN_ADDRESSES,
|
|
49
|
+
TOKEN_DECIMALS: () => TOKEN_DECIMALS,
|
|
50
|
+
TokenUtils: () => TokenUtils,
|
|
51
|
+
WebhookEventType: () => WebhookEventType,
|
|
52
|
+
createBeepErrorFromAxios: () => createBeepErrorFromAxios,
|
|
53
|
+
formatMoneyAmount: () => formatMoneyAmount,
|
|
54
|
+
isMoneyAmount: () => isMoneyAmount,
|
|
55
|
+
isUUID: () => isUUID,
|
|
56
|
+
parseMoneyAmount: () => parseMoneyAmount
|
|
37
57
|
});
|
|
38
|
-
module.exports = __toCommonJS(
|
|
58
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
59
|
var import_axios = __toESM(require("axios"));
|
|
40
60
|
|
|
61
|
+
// src/types/invoice.ts
|
|
62
|
+
var InvoiceStatus = /* @__PURE__ */ ((InvoiceStatus2) => {
|
|
63
|
+
InvoiceStatus2["PENDING"] = "pending";
|
|
64
|
+
InvoiceStatus2["PAID"] = "paid";
|
|
65
|
+
InvoiceStatus2["CONFIRMED"] = "confirmed";
|
|
66
|
+
InvoiceStatus2["EXPIRED"] = "expired";
|
|
67
|
+
InvoiceStatus2["FAILED"] = "failed";
|
|
68
|
+
return InvoiceStatus2;
|
|
69
|
+
})(InvoiceStatus || {});
|
|
70
|
+
|
|
71
|
+
// src/types/payment.ts
|
|
72
|
+
var PayoutStatus = /* @__PURE__ */ ((PayoutStatus2) => {
|
|
73
|
+
PayoutStatus2["PENDING"] = "PENDING";
|
|
74
|
+
PayoutStatus2["IN_PROGRESS"] = "IN_PROGRESS";
|
|
75
|
+
PayoutStatus2["COMPLETED"] = "COMPLETED";
|
|
76
|
+
PayoutStatus2["FAILED"] = "FAILED";
|
|
77
|
+
PayoutStatus2["CANCELED"] = "CANCELED";
|
|
78
|
+
return PayoutStatus2;
|
|
79
|
+
})(PayoutStatus || {});
|
|
80
|
+
|
|
41
81
|
// src/types/token.ts
|
|
42
82
|
var SupportedToken = /* @__PURE__ */ ((SupportedToken3) => {
|
|
43
83
|
SupportedToken3["USDC"] = "USDC";
|
|
44
|
-
SupportedToken3["USDT"] = "USDT";
|
|
45
84
|
return SupportedToken3;
|
|
46
85
|
})(SupportedToken || {});
|
|
47
86
|
var TOKEN_ADDRESSES = {
|
|
48
|
-
["USDC" /* USDC */]: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyB7u6T"
|
|
49
|
-
// USDC
|
|
50
|
-
["USDT" /* USDT */]: "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB"
|
|
51
|
-
// USDT mint address
|
|
87
|
+
["USDC" /* USDC */]: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyB7u6T"
|
|
88
|
+
// USDC token address
|
|
52
89
|
};
|
|
53
90
|
var TOKEN_DECIMALS = {
|
|
54
|
-
["USDC" /* USDC */]: 6
|
|
91
|
+
["USDC" /* USDC */]: 6
|
|
55
92
|
// USDC uses 6 decimal places
|
|
56
|
-
["USDT" /* USDT */]: 6
|
|
57
|
-
// USDT uses 6 decimal places
|
|
58
93
|
};
|
|
59
94
|
var TokenUtils = class {
|
|
60
95
|
/**
|
|
61
|
-
* Retrieves the
|
|
96
|
+
* Retrieves the token address for a supported token
|
|
62
97
|
*
|
|
63
98
|
* @param token - The supported token enum value
|
|
64
|
-
* @returns The corresponding
|
|
99
|
+
* @returns The corresponding token address on SUI
|
|
65
100
|
*
|
|
66
101
|
* @example
|
|
67
102
|
* ```typescript
|
|
68
|
-
* const address = TokenUtils.getTokenAddress(SupportedToken.
|
|
69
|
-
* console.log(address); // '
|
|
103
|
+
* const address = TokenUtils.getTokenAddress(SupportedToken.USDC);
|
|
104
|
+
* console.log(address); // 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyB7u6T'
|
|
70
105
|
* ```
|
|
71
106
|
*/
|
|
72
107
|
static getTokenAddress(token) {
|
|
73
108
|
return TOKEN_ADDRESSES[token];
|
|
74
109
|
}
|
|
75
110
|
/**
|
|
76
|
-
* Performs reverse lookup to find token enum from
|
|
111
|
+
* Performs reverse lookup to find token enum from token address
|
|
77
112
|
*
|
|
78
|
-
* @param address - The
|
|
113
|
+
* @param address - The token address to look up
|
|
79
114
|
* @returns The corresponding SupportedToken enum value, or null if not found
|
|
80
115
|
*
|
|
81
116
|
* @example
|
|
82
117
|
* ```typescript
|
|
83
|
-
* const token = TokenUtils.getTokenFromAddress('
|
|
84
|
-
* console.log(token); // SupportedToken.
|
|
118
|
+
* const token = TokenUtils.getTokenFromAddress('EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyB7u6T');
|
|
119
|
+
* console.log(token); // SupportedToken.USDC
|
|
85
120
|
* ```
|
|
86
121
|
*/
|
|
87
122
|
static getTokenFromAddress(address) {
|
|
@@ -115,7 +150,7 @@ var TokenUtils = class {
|
|
|
115
150
|
*
|
|
116
151
|
* @example
|
|
117
152
|
* ```typescript
|
|
118
|
-
* const userInput = '
|
|
153
|
+
* const userInput = 'USDC';
|
|
119
154
|
* if (TokenUtils.isTokenSupported(userInput)) {
|
|
120
155
|
* // userInput is now typed as SupportedToken
|
|
121
156
|
* const address = TokenUtils.getTokenAddress(userInput);
|
|
@@ -128,7 +163,7 @@ var TokenUtils = class {
|
|
|
128
163
|
/**
|
|
129
164
|
* Returns the default token for operations when no token is specified
|
|
130
165
|
*
|
|
131
|
-
* @returns The default SupportedToken (currently
|
|
166
|
+
* @returns The default SupportedToken (currently USDC)
|
|
132
167
|
*
|
|
133
168
|
* @example
|
|
134
169
|
* ```typescript
|
|
@@ -141,6 +176,53 @@ var TokenUtils = class {
|
|
|
141
176
|
}
|
|
142
177
|
};
|
|
143
178
|
|
|
179
|
+
// src/types/common.ts
|
|
180
|
+
var BlockchainNetwork = /* @__PURE__ */ ((BlockchainNetwork2) => {
|
|
181
|
+
BlockchainNetwork2["SUI"] = "SUI";
|
|
182
|
+
BlockchainNetwork2["SOLANA"] = "SOLANA";
|
|
183
|
+
return BlockchainNetwork2;
|
|
184
|
+
})(BlockchainNetwork || {});
|
|
185
|
+
var SortOrder = /* @__PURE__ */ ((SortOrder2) => {
|
|
186
|
+
SortOrder2["ASC"] = "asc";
|
|
187
|
+
SortOrder2["DESC"] = "desc";
|
|
188
|
+
return SortOrder2;
|
|
189
|
+
})(SortOrder || {});
|
|
190
|
+
var EntityStatus = /* @__PURE__ */ ((EntityStatus2) => {
|
|
191
|
+
EntityStatus2["ACTIVE"] = "active";
|
|
192
|
+
EntityStatus2["INACTIVE"] = "inactive";
|
|
193
|
+
EntityStatus2["PENDING"] = "pending";
|
|
194
|
+
EntityStatus2["DELETED"] = "deleted";
|
|
195
|
+
EntityStatus2["ARCHIVED"] = "archived";
|
|
196
|
+
return EntityStatus2;
|
|
197
|
+
})(EntityStatus || {});
|
|
198
|
+
var WebhookEventType = /* @__PURE__ */ ((WebhookEventType2) => {
|
|
199
|
+
WebhookEventType2["PAYMENT_COMPLETED"] = "payment.completed";
|
|
200
|
+
WebhookEventType2["PAYMENT_FAILED"] = "payment.failed";
|
|
201
|
+
WebhookEventType2["INVOICE_PAID"] = "invoice.paid";
|
|
202
|
+
WebhookEventType2["INVOICE_EXPIRED"] = "invoice.expired";
|
|
203
|
+
WebhookEventType2["PRODUCT_CREATED"] = "product.created";
|
|
204
|
+
WebhookEventType2["PRODUCT_UPDATED"] = "product.updated";
|
|
205
|
+
WebhookEventType2["SUBSCRIPTION_ACTIVATED"] = "subscription.activated";
|
|
206
|
+
WebhookEventType2["SUBSCRIPTION_CANCELLED"] = "subscription.cancelled";
|
|
207
|
+
return WebhookEventType2;
|
|
208
|
+
})(WebhookEventType || {});
|
|
209
|
+
function isUUID(value) {
|
|
210
|
+
if (typeof value !== "string") return false;
|
|
211
|
+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
212
|
+
return uuidRegex.test(value);
|
|
213
|
+
}
|
|
214
|
+
function isMoneyAmount(value) {
|
|
215
|
+
if (typeof value !== "string") return false;
|
|
216
|
+
const moneyRegex = /^\d+(\.\d{1,2})?$/;
|
|
217
|
+
return moneyRegex.test(value);
|
|
218
|
+
}
|
|
219
|
+
function formatMoneyAmount(amount, decimals = 2) {
|
|
220
|
+
return amount.toFixed(decimals);
|
|
221
|
+
}
|
|
222
|
+
function parseMoneyAmount(amount) {
|
|
223
|
+
return parseFloat(amount);
|
|
224
|
+
}
|
|
225
|
+
|
|
144
226
|
// src/modules/invoices.ts
|
|
145
227
|
var InvoicesModule = class {
|
|
146
228
|
constructor(client) {
|
|
@@ -149,11 +231,11 @@ var InvoicesModule = class {
|
|
|
149
231
|
/**
|
|
150
232
|
* Creates a new invoice for payment processing
|
|
151
233
|
* Supports both product-based invoices (linked to existing products) and custom invoices
|
|
152
|
-
*
|
|
234
|
+
*
|
|
153
235
|
* @param payload - Invoice creation parameters (product-based or custom)
|
|
154
236
|
* @returns Promise that resolves to the created invoice
|
|
155
237
|
* @throws {Error} When invoice creation fails
|
|
156
|
-
*
|
|
238
|
+
*
|
|
157
239
|
* @example
|
|
158
240
|
* ```typescript
|
|
159
241
|
* // Create invoice from existing product
|
|
@@ -161,12 +243,12 @@ var InvoicesModule = class {
|
|
|
161
243
|
* productId: 'prod_123',
|
|
162
244
|
* payerType: 'customer_wallet'
|
|
163
245
|
* });
|
|
164
|
-
*
|
|
246
|
+
*
|
|
165
247
|
* // Create custom invoice
|
|
166
248
|
* const customInvoice = await beep.invoices.createInvoice({
|
|
167
249
|
* description: 'Consulting services',
|
|
168
250
|
* amount: '100.00',
|
|
169
|
-
* token: SupportedToken.
|
|
251
|
+
* token: SupportedToken.USDC,
|
|
170
252
|
* payerType: 'customer_wallet'
|
|
171
253
|
* });
|
|
172
254
|
* ```
|
|
@@ -174,18 +256,20 @@ var InvoicesModule = class {
|
|
|
174
256
|
async createInvoice(payload) {
|
|
175
257
|
const requestPayload = { ...payload };
|
|
176
258
|
if ("token" in requestPayload && !("splTokenAddress" in requestPayload)) {
|
|
177
|
-
requestPayload.splTokenAddress = TokenUtils.getTokenAddress(
|
|
259
|
+
requestPayload.splTokenAddress = TokenUtils.getTokenAddress(
|
|
260
|
+
requestPayload.token
|
|
261
|
+
);
|
|
178
262
|
}
|
|
179
263
|
const response = await this.client.post("/v1/invoices", requestPayload);
|
|
180
264
|
return response.data;
|
|
181
265
|
}
|
|
182
266
|
/**
|
|
183
267
|
* Retrieves a specific invoice by its ID
|
|
184
|
-
*
|
|
268
|
+
*
|
|
185
269
|
* @param invoiceId - The unique identifier of the invoice to retrieve
|
|
186
270
|
* @returns Promise that resolves to the invoice details
|
|
187
271
|
* @throws {Error} When the invoice is not found or retrieval fails
|
|
188
|
-
*
|
|
272
|
+
*
|
|
189
273
|
* @example
|
|
190
274
|
* ```typescript
|
|
191
275
|
* const invoice = await beep.invoices.getInvoice('inv_123abc456def');
|
|
@@ -199,10 +283,10 @@ var InvoicesModule = class {
|
|
|
199
283
|
}
|
|
200
284
|
/**
|
|
201
285
|
* Retrieves all invoices for the current merchant
|
|
202
|
-
*
|
|
286
|
+
*
|
|
203
287
|
* @returns Promise that resolves to an array of invoices
|
|
204
288
|
* @throws {Error} When invoice retrieval fails
|
|
205
|
-
*
|
|
289
|
+
*
|
|
206
290
|
* @example
|
|
207
291
|
* ```typescript
|
|
208
292
|
* const invoices = await beep.invoices.listInvoices();
|
|
@@ -216,13 +300,13 @@ var InvoicesModule = class {
|
|
|
216
300
|
}
|
|
217
301
|
/**
|
|
218
302
|
* Deletes an existing invoice
|
|
219
|
-
*
|
|
303
|
+
*
|
|
220
304
|
* @param invoiceId - The unique identifier of the invoice to delete
|
|
221
305
|
* @returns Promise that resolves when the invoice is successfully deleted
|
|
222
306
|
* @throws {Error} When the invoice is not found or deletion fails
|
|
223
|
-
*
|
|
307
|
+
*
|
|
224
308
|
* @remarks Once deleted, an invoice cannot be recovered. Use with caution.
|
|
225
|
-
*
|
|
309
|
+
*
|
|
226
310
|
* @example
|
|
227
311
|
* ```typescript
|
|
228
312
|
* await beep.invoices.deleteInvoice('inv_123abc456def');
|
|
@@ -245,14 +329,16 @@ var PaymentsModule = class {
|
|
|
245
329
|
*
|
|
246
330
|
* Notes:
|
|
247
331
|
* - Do not pass walletId. The server derives the wallet based on your API key's merchant and requested chain.
|
|
248
|
-
* - amount
|
|
332
|
+
* - amount is a human-readable decimal string (e.g., "1.0" for 1 USDC, "0.5" for 0.5 SUI).
|
|
333
|
+
* The server converts to the token's base units internally.
|
|
334
|
+
* - Minimum: 0.01 per transaction. Maximum: 100,000 per transaction.
|
|
249
335
|
* - This endpoint responds immediately with acceptance/rejection. Actual transfer executes asynchronously after funds are reserved.
|
|
250
336
|
*
|
|
251
337
|
* Example:
|
|
252
338
|
* const res = await beep.payments.createPayout({
|
|
253
|
-
* amount: '
|
|
339
|
+
* amount: '1.50', // 1.50 USDC (human-readable)
|
|
254
340
|
* destinationWalletAddress: 'DEST_ADDRESS',
|
|
255
|
-
* chain: '
|
|
341
|
+
* chain: 'SUI',
|
|
256
342
|
* token: 'USDC',
|
|
257
343
|
* });
|
|
258
344
|
*/
|
|
@@ -321,7 +407,7 @@ var PaymentsModule = class {
|
|
|
321
407
|
*
|
|
322
408
|
* Phase 1 – Request payment (no paymentReference):
|
|
323
409
|
* - Server responds with HTTP 402 Payment Required and a payload containing:
|
|
324
|
-
* referenceKey, paymentUrl
|
|
410
|
+
* referenceKey, paymentUrl, optional qrCode, amount, expiresAt, status.
|
|
325
411
|
* - The SDK normalizes this by returning the payload (even when status code is 402).
|
|
326
412
|
* - The caller must instruct the user to pay via their wallet using paymentUrl/qrCode.
|
|
327
413
|
*
|
|
@@ -370,51 +456,6 @@ var PaymentsModule = class {
|
|
|
370
456
|
return null;
|
|
371
457
|
}
|
|
372
458
|
}
|
|
373
|
-
/**
|
|
374
|
-
* Signs a Solana transaction for direct blockchain payment processing
|
|
375
|
-
*
|
|
376
|
-
* @param input - Transaction parameters including addresses, amounts, and token details
|
|
377
|
-
* @returns Promise that resolves to signed transaction data
|
|
378
|
-
* @throws {Error} When transaction signing fails or required fields are missing
|
|
379
|
-
*
|
|
380
|
-
* @example
|
|
381
|
-
* ```typescript
|
|
382
|
-
* try {
|
|
383
|
-
* const signedTx = await beep.payments.signSolanaTransaction({
|
|
384
|
-
* senderAddress: 'sender_wallet_address',
|
|
385
|
-
* recipientAddress: 'recipient_wallet_address',
|
|
386
|
-
* tokenMintAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
|
|
387
|
-
* amount: 1000000, // 1.0 USDT in base units
|
|
388
|
-
* decimals: 6
|
|
389
|
-
* });
|
|
390
|
-
*
|
|
391
|
-
* if (signedTx) {
|
|
392
|
-
* console.log('Transaction ready for broadcast:', signedTx.signedTransaction);
|
|
393
|
-
* }
|
|
394
|
-
* } catch (error) {
|
|
395
|
-
* console.error('Transaction signing failed:', error);
|
|
396
|
-
* }
|
|
397
|
-
* ```
|
|
398
|
-
*/
|
|
399
|
-
async signSolanaTransaction(input) {
|
|
400
|
-
if (!input.senderAddress || !input.recipientAddress || !input.tokenMintAddress || !input.amount || !input.decimals) {
|
|
401
|
-
console.error("Missing required fields");
|
|
402
|
-
return null;
|
|
403
|
-
}
|
|
404
|
-
try {
|
|
405
|
-
const response = await this.client.post(
|
|
406
|
-
"/v1/payment/sign-solana-transaction",
|
|
407
|
-
input
|
|
408
|
-
);
|
|
409
|
-
if (!response.data || !response.data.data) {
|
|
410
|
-
throw new Error("No data returned from solana transaction signing");
|
|
411
|
-
}
|
|
412
|
-
return response.data.data;
|
|
413
|
-
} catch (error) {
|
|
414
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
415
|
-
throw new Error(`Failed to sign solana transaction: ${errorMessage}`);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
459
|
// --- Streaming Payment Methods ---
|
|
419
460
|
// Note: These methods are ONLY available with BeepClient (secret API keys)
|
|
420
461
|
// They do NOT work with BeepPublicClient (publishable keys)
|
|
@@ -580,7 +621,7 @@ var ProductsModule = class {
|
|
|
580
621
|
* name: 'Premium License',
|
|
581
622
|
* description: 'Lifetime access to premium features',
|
|
582
623
|
* price: '99.99',
|
|
583
|
-
* token: SupportedToken.
|
|
624
|
+
* token: SupportedToken.USDC,
|
|
584
625
|
* isSubscription: false
|
|
585
626
|
* });
|
|
586
627
|
*
|
|
@@ -589,7 +630,7 @@ var ProductsModule = class {
|
|
|
589
630
|
* name: 'Monthly Pro Plan',
|
|
590
631
|
* description: 'Monthly subscription with full access',
|
|
591
632
|
* price: '19.99',
|
|
592
|
-
* token: SupportedToken.
|
|
633
|
+
* token: SupportedToken.USDC,
|
|
593
634
|
* isSubscription: true
|
|
594
635
|
* });
|
|
595
636
|
* ```
|
|
@@ -808,6 +849,381 @@ var UserModule = class {
|
|
|
808
849
|
}
|
|
809
850
|
};
|
|
810
851
|
|
|
852
|
+
// src/errors/index.ts
|
|
853
|
+
var BeepErrorCode = /* @__PURE__ */ ((BeepErrorCode2) => {
|
|
854
|
+
BeepErrorCode2["INVALID_API_KEY"] = "BEEP_1001";
|
|
855
|
+
BeepErrorCode2["MISSING_API_KEY"] = "BEEP_1002";
|
|
856
|
+
BeepErrorCode2["UNAUTHORIZED"] = "BEEP_1003";
|
|
857
|
+
BeepErrorCode2["INVALID_PUBLISHABLE_KEY"] = "BEEP_1004";
|
|
858
|
+
BeepErrorCode2["NETWORK_ERROR"] = "BEEP_2001";
|
|
859
|
+
BeepErrorCode2["TIMEOUT"] = "BEEP_2002";
|
|
860
|
+
BeepErrorCode2["SERVER_ERROR"] = "BEEP_2003";
|
|
861
|
+
BeepErrorCode2["PAYMENT_FAILED"] = "BEEP_3001";
|
|
862
|
+
BeepErrorCode2["PAYMENT_EXPIRED"] = "BEEP_3002";
|
|
863
|
+
BeepErrorCode2["PAYMENT_NOT_FOUND"] = "BEEP_3003";
|
|
864
|
+
BeepErrorCode2["INSUFFICIENT_FUNDS"] = "BEEP_3004";
|
|
865
|
+
BeepErrorCode2["PAYMENT_ALREADY_PROCESSED"] = "BEEP_3005";
|
|
866
|
+
BeepErrorCode2["INVOICE_NOT_FOUND"] = "BEEP_4001";
|
|
867
|
+
BeepErrorCode2["INVOICE_EXPIRED"] = "BEEP_4002";
|
|
868
|
+
BeepErrorCode2["INVOICE_ALREADY_PAID"] = "BEEP_4003";
|
|
869
|
+
BeepErrorCode2["INVALID_PARAMETER"] = "BEEP_5001";
|
|
870
|
+
BeepErrorCode2["MISSING_PARAMETER"] = "BEEP_5002";
|
|
871
|
+
BeepErrorCode2["INVALID_AMOUNT"] = "BEEP_5003";
|
|
872
|
+
BeepErrorCode2["INVALID_TOKEN"] = "BEEP_5004";
|
|
873
|
+
BeepErrorCode2["RATE_LIMIT_EXCEEDED"] = "BEEP_6001";
|
|
874
|
+
BeepErrorCode2["UNKNOWN_ERROR"] = "BEEP_9999";
|
|
875
|
+
return BeepErrorCode2;
|
|
876
|
+
})(BeepErrorCode || {});
|
|
877
|
+
var BeepError = class _BeepError extends Error {
|
|
878
|
+
constructor(message, options) {
|
|
879
|
+
super(message);
|
|
880
|
+
this.name = "BeepError";
|
|
881
|
+
this.code = options.code;
|
|
882
|
+
this.statusCode = options.statusCode;
|
|
883
|
+
this.details = options.details;
|
|
884
|
+
this.timestamp = /* @__PURE__ */ new Date();
|
|
885
|
+
this.requestId = options.requestId;
|
|
886
|
+
if (Error.captureStackTrace) {
|
|
887
|
+
Error.captureStackTrace(this, _BeepError);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Returns a user-friendly error message
|
|
892
|
+
*/
|
|
893
|
+
getUserMessage() {
|
|
894
|
+
switch (this.code) {
|
|
895
|
+
case "BEEP_1001" /* INVALID_API_KEY */:
|
|
896
|
+
return "The API key provided is invalid. Please check your credentials.";
|
|
897
|
+
case "BEEP_1002" /* MISSING_API_KEY */:
|
|
898
|
+
return "No API key provided. Please provide a valid API key.";
|
|
899
|
+
case "BEEP_2001" /* NETWORK_ERROR */:
|
|
900
|
+
return "Unable to connect to BEEP servers. Please check your internet connection.";
|
|
901
|
+
case "BEEP_3002" /* PAYMENT_EXPIRED */:
|
|
902
|
+
return "This payment request has expired. Please create a new one.";
|
|
903
|
+
case "BEEP_3004" /* INSUFFICIENT_FUNDS */:
|
|
904
|
+
return "Insufficient funds to complete this transaction.";
|
|
905
|
+
case "BEEP_6001" /* RATE_LIMIT_EXCEEDED */:
|
|
906
|
+
return "Too many requests. Please try again later.";
|
|
907
|
+
default:
|
|
908
|
+
return this.message || "An unexpected error occurred.";
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
/**
|
|
912
|
+
* Returns a JSON representation of the error for logging
|
|
913
|
+
*/
|
|
914
|
+
toJSON() {
|
|
915
|
+
return {
|
|
916
|
+
name: this.name,
|
|
917
|
+
message: this.message,
|
|
918
|
+
code: this.code,
|
|
919
|
+
statusCode: this.statusCode,
|
|
920
|
+
details: this.details,
|
|
921
|
+
timestamp: this.timestamp,
|
|
922
|
+
requestId: this.requestId,
|
|
923
|
+
stack: this.stack
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
var BeepAuthenticationError = class extends BeepError {
|
|
928
|
+
constructor(message, options = {}) {
|
|
929
|
+
super(message, {
|
|
930
|
+
code: options.code ?? "BEEP_1003" /* UNAUTHORIZED */,
|
|
931
|
+
statusCode: 401,
|
|
932
|
+
details: options.details
|
|
933
|
+
});
|
|
934
|
+
this.name = "BeepAuthenticationError";
|
|
935
|
+
}
|
|
936
|
+
};
|
|
937
|
+
var BeepValidationError = class extends BeepError {
|
|
938
|
+
constructor(message, options = {}) {
|
|
939
|
+
super(message, {
|
|
940
|
+
code: options.code ?? "BEEP_5001" /* INVALID_PARAMETER */,
|
|
941
|
+
statusCode: 400,
|
|
942
|
+
details: options.details
|
|
943
|
+
});
|
|
944
|
+
this.name = "BeepValidationError";
|
|
945
|
+
}
|
|
946
|
+
};
|
|
947
|
+
var BeepPaymentError = class extends BeepError {
|
|
948
|
+
constructor(message, options = {}) {
|
|
949
|
+
super(message, {
|
|
950
|
+
code: options.code ?? "BEEP_3001" /* PAYMENT_FAILED */,
|
|
951
|
+
statusCode: 402,
|
|
952
|
+
details: options.details
|
|
953
|
+
});
|
|
954
|
+
this.name = "BeepPaymentError";
|
|
955
|
+
}
|
|
956
|
+
};
|
|
957
|
+
var BeepNetworkError = class extends BeepError {
|
|
958
|
+
constructor(message, options = {}) {
|
|
959
|
+
super(message, {
|
|
960
|
+
code: options.code ?? "BEEP_2001" /* NETWORK_ERROR */,
|
|
961
|
+
details: options.details
|
|
962
|
+
});
|
|
963
|
+
this.name = "BeepNetworkError";
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
var BeepRateLimitError = class extends BeepError {
|
|
967
|
+
constructor(message, options = {}) {
|
|
968
|
+
super(message, {
|
|
969
|
+
code: "BEEP_6001" /* RATE_LIMIT_EXCEEDED */,
|
|
970
|
+
statusCode: 429,
|
|
971
|
+
details: options.details
|
|
972
|
+
});
|
|
973
|
+
this.name = "BeepRateLimitError";
|
|
974
|
+
this.retryAfter = options.retryAfter;
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
function createBeepErrorFromAxios(error) {
|
|
978
|
+
const response = error.response;
|
|
979
|
+
const requestId = response?.headers?.["x-request-id"];
|
|
980
|
+
if (!response) {
|
|
981
|
+
return new BeepNetworkError(error.message || "Network connection failed", {
|
|
982
|
+
code: "BEEP_2001" /* NETWORK_ERROR */,
|
|
983
|
+
details: { originalError: error }
|
|
984
|
+
});
|
|
985
|
+
}
|
|
986
|
+
const status = response.status;
|
|
987
|
+
const data = response.data;
|
|
988
|
+
const message = data?.message || data?.error || error.message;
|
|
989
|
+
switch (status) {
|
|
990
|
+
case 401:
|
|
991
|
+
return new BeepAuthenticationError(message, {
|
|
992
|
+
code: "BEEP_1003" /* UNAUTHORIZED */,
|
|
993
|
+
details: { response: data }
|
|
994
|
+
});
|
|
995
|
+
case 400: {
|
|
996
|
+
if (message.toLowerCase().includes("api key")) {
|
|
997
|
+
return new BeepAuthenticationError(message, {
|
|
998
|
+
code: "BEEP_1001" /* INVALID_API_KEY */,
|
|
999
|
+
details: { response: data }
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
return new BeepValidationError(message, {
|
|
1003
|
+
code: "BEEP_5001" /* INVALID_PARAMETER */,
|
|
1004
|
+
details: { response: data, fields: data?.fields }
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
case 402:
|
|
1008
|
+
return new BeepPaymentError(message, {
|
|
1009
|
+
code: "BEEP_3001" /* PAYMENT_FAILED */,
|
|
1010
|
+
details: { response: data }
|
|
1011
|
+
});
|
|
1012
|
+
case 404: {
|
|
1013
|
+
if (message.toLowerCase().includes("invoice")) {
|
|
1014
|
+
return new BeepError(message, {
|
|
1015
|
+
code: "BEEP_4001" /* INVOICE_NOT_FOUND */,
|
|
1016
|
+
statusCode: 404,
|
|
1017
|
+
details: { response: data },
|
|
1018
|
+
requestId
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
if (message.toLowerCase().includes("payment")) {
|
|
1022
|
+
return new BeepError(message, {
|
|
1023
|
+
code: "BEEP_3003" /* PAYMENT_NOT_FOUND */,
|
|
1024
|
+
statusCode: 404,
|
|
1025
|
+
details: { response: data },
|
|
1026
|
+
requestId
|
|
1027
|
+
});
|
|
1028
|
+
}
|
|
1029
|
+
break;
|
|
1030
|
+
}
|
|
1031
|
+
case 429: {
|
|
1032
|
+
const retryAfter = response.headers?.["retry-after"];
|
|
1033
|
+
return new BeepRateLimitError(message, {
|
|
1034
|
+
retryAfter: retryAfter ? parseInt(retryAfter) : void 0,
|
|
1035
|
+
details: { response: data }
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
case 500:
|
|
1039
|
+
case 502:
|
|
1040
|
+
case 503:
|
|
1041
|
+
case 504:
|
|
1042
|
+
return new BeepError(message, {
|
|
1043
|
+
code: "BEEP_2003" /* SERVER_ERROR */,
|
|
1044
|
+
statusCode: status,
|
|
1045
|
+
details: { response: data },
|
|
1046
|
+
requestId
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
return new BeepError(message, {
|
|
1050
|
+
code: "BEEP_9999" /* UNKNOWN_ERROR */,
|
|
1051
|
+
statusCode: status,
|
|
1052
|
+
details: { response: data },
|
|
1053
|
+
requestId
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// src/utils/debug.ts
|
|
1058
|
+
var defaultLogger = (options) => {
|
|
1059
|
+
const { level, message, data } = options;
|
|
1060
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
1061
|
+
const prefix = `[BEEP SDK ${level}] ${timestamp}`;
|
|
1062
|
+
switch (level) {
|
|
1063
|
+
case "error":
|
|
1064
|
+
console.error(prefix, message, data);
|
|
1065
|
+
break;
|
|
1066
|
+
case "warn":
|
|
1067
|
+
console.warn(prefix, message, data);
|
|
1068
|
+
break;
|
|
1069
|
+
case "info":
|
|
1070
|
+
console.info(prefix, message, data);
|
|
1071
|
+
break;
|
|
1072
|
+
case "debug":
|
|
1073
|
+
console.debug(prefix, message, data);
|
|
1074
|
+
break;
|
|
1075
|
+
default:
|
|
1076
|
+
console.log(prefix, message, data);
|
|
1077
|
+
}
|
|
1078
|
+
};
|
|
1079
|
+
var BeepDebugger = class {
|
|
1080
|
+
constructor(options = {}) {
|
|
1081
|
+
this.options = options;
|
|
1082
|
+
this.logger = options.logger || defaultLogger;
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
* Log an error with context
|
|
1086
|
+
*/
|
|
1087
|
+
error(message, error) {
|
|
1088
|
+
if (error instanceof BeepError) {
|
|
1089
|
+
this.logger({
|
|
1090
|
+
level: "error",
|
|
1091
|
+
message,
|
|
1092
|
+
data: {
|
|
1093
|
+
code: error.code,
|
|
1094
|
+
statusCode: error.statusCode,
|
|
1095
|
+
details: error.details,
|
|
1096
|
+
requestId: error.requestId,
|
|
1097
|
+
userMessage: error.getUserMessage()
|
|
1098
|
+
}
|
|
1099
|
+
});
|
|
1100
|
+
} else {
|
|
1101
|
+
this.logger({ level: "error", message, data: error });
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Log a warning
|
|
1106
|
+
*/
|
|
1107
|
+
warn(message, data) {
|
|
1108
|
+
this.logger({ level: "warn", message, data });
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Log info message
|
|
1112
|
+
*/
|
|
1113
|
+
info(message, data) {
|
|
1114
|
+
if (this.options.debug) {
|
|
1115
|
+
this.logger({ level: "info", message, data });
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Log debug message
|
|
1120
|
+
*/
|
|
1121
|
+
debug(message, data) {
|
|
1122
|
+
if (this.options.debug) {
|
|
1123
|
+
this.logger({ level: "debug", message, data });
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Log API request
|
|
1128
|
+
*/
|
|
1129
|
+
logRequest(options) {
|
|
1130
|
+
if (this.options.logRequests && this.options.debug) {
|
|
1131
|
+
const { method, url, data } = options;
|
|
1132
|
+
this.logger({
|
|
1133
|
+
level: "debug",
|
|
1134
|
+
message: `API Request: ${method} ${url}`,
|
|
1135
|
+
data: {
|
|
1136
|
+
method,
|
|
1137
|
+
url,
|
|
1138
|
+
data: this.sanitizeData(data)
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
/**
|
|
1144
|
+
* Log API response
|
|
1145
|
+
*/
|
|
1146
|
+
logResponse(options) {
|
|
1147
|
+
if (this.options.logResponses && this.options.debug) {
|
|
1148
|
+
const { method, url, status, data } = options;
|
|
1149
|
+
this.logger({
|
|
1150
|
+
level: "debug",
|
|
1151
|
+
message: `API Response: ${method} ${url} (${status})`,
|
|
1152
|
+
data: {
|
|
1153
|
+
method,
|
|
1154
|
+
url,
|
|
1155
|
+
status,
|
|
1156
|
+
data: this.sanitizeData(data)
|
|
1157
|
+
}
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
/**
|
|
1162
|
+
* Sanitize sensitive data before logging
|
|
1163
|
+
*/
|
|
1164
|
+
sanitizeData(data) {
|
|
1165
|
+
if (!data) return data;
|
|
1166
|
+
const sensitiveFields = ["apiKey", "api_key", "authorization", "password", "secret"];
|
|
1167
|
+
if (typeof data === "string") {
|
|
1168
|
+
return data;
|
|
1169
|
+
}
|
|
1170
|
+
if (Array.isArray(data)) {
|
|
1171
|
+
return data.map((item) => this.sanitizeData(item));
|
|
1172
|
+
}
|
|
1173
|
+
if (typeof data === "object") {
|
|
1174
|
+
const sanitized = {};
|
|
1175
|
+
for (const [key, value] of Object.entries(data)) {
|
|
1176
|
+
if (sensitiveFields.some((field) => key.toLowerCase().includes(field))) {
|
|
1177
|
+
sanitized[key] = "[REDACTED]";
|
|
1178
|
+
} else {
|
|
1179
|
+
sanitized[key] = this.sanitizeData(value);
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
return sanitized;
|
|
1183
|
+
}
|
|
1184
|
+
return data;
|
|
1185
|
+
}
|
|
1186
|
+
};
|
|
1187
|
+
function createDebugInterceptors(axios2, beepDebugger) {
|
|
1188
|
+
axios2.interceptors.request.use(
|
|
1189
|
+
(config) => {
|
|
1190
|
+
beepDebugger.logRequest({
|
|
1191
|
+
method: config.method?.toUpperCase() || "GET",
|
|
1192
|
+
url: config.url || "",
|
|
1193
|
+
data: config.data
|
|
1194
|
+
});
|
|
1195
|
+
return config;
|
|
1196
|
+
},
|
|
1197
|
+
(error) => {
|
|
1198
|
+
beepDebugger.error("Request interceptor error", error);
|
|
1199
|
+
return Promise.reject(error);
|
|
1200
|
+
}
|
|
1201
|
+
);
|
|
1202
|
+
axios2.interceptors.response.use(
|
|
1203
|
+
(response) => {
|
|
1204
|
+
beepDebugger.logResponse({
|
|
1205
|
+
method: response.config.method?.toUpperCase() || "GET",
|
|
1206
|
+
url: response.config.url || "",
|
|
1207
|
+
status: response.status,
|
|
1208
|
+
data: response.data
|
|
1209
|
+
});
|
|
1210
|
+
return response;
|
|
1211
|
+
},
|
|
1212
|
+
(error) => {
|
|
1213
|
+
if (error.response) {
|
|
1214
|
+
beepDebugger.logResponse({
|
|
1215
|
+
method: error.config?.method?.toUpperCase() || "GET",
|
|
1216
|
+
url: error.config?.url || "",
|
|
1217
|
+
status: error.response.status,
|
|
1218
|
+
data: error.response.data
|
|
1219
|
+
});
|
|
1220
|
+
}
|
|
1221
|
+
beepDebugger.error("API Error", error);
|
|
1222
|
+
return Promise.reject(error);
|
|
1223
|
+
}
|
|
1224
|
+
);
|
|
1225
|
+
}
|
|
1226
|
+
|
|
811
1227
|
// src/index.ts
|
|
812
1228
|
var BeepClient = class {
|
|
813
1229
|
/**
|
|
@@ -818,8 +1234,11 @@ var BeepClient = class {
|
|
|
818
1234
|
*/
|
|
819
1235
|
constructor(options) {
|
|
820
1236
|
if (!options.apiKey) {
|
|
821
|
-
throw new
|
|
1237
|
+
throw new BeepAuthenticationError("API key is required to initialize BeepClient", {
|
|
1238
|
+
code: "BEEP_1002" /* MISSING_API_KEY */
|
|
1239
|
+
});
|
|
822
1240
|
}
|
|
1241
|
+
this.debugger = new BeepDebugger(options.debug);
|
|
823
1242
|
this.client = import_axios.default.create({
|
|
824
1243
|
baseURL: options.serverUrl || "https://api.justbeep.it",
|
|
825
1244
|
headers: {
|
|
@@ -828,6 +1247,9 @@ var BeepClient = class {
|
|
|
828
1247
|
"X-Beep-Client": "beep-sdk"
|
|
829
1248
|
}
|
|
830
1249
|
});
|
|
1250
|
+
if (options.debug?.debug) {
|
|
1251
|
+
createDebugInterceptors(this.client, this.debugger);
|
|
1252
|
+
}
|
|
831
1253
|
this.products = new ProductsModule(this.client);
|
|
832
1254
|
this.invoices = new InvoicesModule(this.client);
|
|
833
1255
|
this.payments = new PaymentsModule(this.client);
|
|
@@ -846,7 +1268,7 @@ var BeepClient = class {
|
|
|
846
1268
|
* const res = await beep.payments.createPayout({
|
|
847
1269
|
* amount: '1000000', // 1.0 USDC with 6 decimals
|
|
848
1270
|
* destinationWalletAddress: 'DEST_ADDRESS',
|
|
849
|
-
* chain: '
|
|
1271
|
+
* chain: 'SUI',
|
|
850
1272
|
* token: 'USDC',
|
|
851
1273
|
* });
|
|
852
1274
|
*/
|
|
@@ -884,7 +1306,12 @@ var BeepPublicClient = class {
|
|
|
884
1306
|
*/
|
|
885
1307
|
constructor(options) {
|
|
886
1308
|
if (!options.publishableKey) {
|
|
887
|
-
throw new
|
|
1309
|
+
throw new BeepAuthenticationError(
|
|
1310
|
+
"publishableKey is required to initialize BeepPublicClient",
|
|
1311
|
+
{
|
|
1312
|
+
code: "BEEP_1002" /* MISSING_API_KEY */
|
|
1313
|
+
}
|
|
1314
|
+
);
|
|
888
1315
|
}
|
|
889
1316
|
this.client = import_axios.default.create({
|
|
890
1317
|
baseURL: options.serverUrl || "https://api.justbeep.it",
|
|
@@ -899,8 +1326,28 @@ var BeepPublicClient = class {
|
|
|
899
1326
|
};
|
|
900
1327
|
// Annotate the CommonJS export names for ESM import in node:
|
|
901
1328
|
0 && (module.exports = {
|
|
1329
|
+
BeepAuthenticationError,
|
|
902
1330
|
BeepClient,
|
|
1331
|
+
BeepError,
|
|
1332
|
+
BeepErrorCode,
|
|
1333
|
+
BeepNetworkError,
|
|
1334
|
+
BeepPaymentError,
|
|
903
1335
|
BeepPublicClient,
|
|
1336
|
+
BeepRateLimitError,
|
|
1337
|
+
BeepValidationError,
|
|
1338
|
+
BlockchainNetwork,
|
|
1339
|
+
EntityStatus,
|
|
1340
|
+
InvoiceStatus,
|
|
1341
|
+
PayoutStatus,
|
|
1342
|
+
SortOrder,
|
|
904
1343
|
SupportedToken,
|
|
905
|
-
|
|
1344
|
+
TOKEN_ADDRESSES,
|
|
1345
|
+
TOKEN_DECIMALS,
|
|
1346
|
+
TokenUtils,
|
|
1347
|
+
WebhookEventType,
|
|
1348
|
+
createBeepErrorFromAxios,
|
|
1349
|
+
formatMoneyAmount,
|
|
1350
|
+
isMoneyAmount,
|
|
1351
|
+
isUUID,
|
|
1352
|
+
parseMoneyAmount
|
|
906
1353
|
});
|