@betterstore/react 0.1.10 → 0.2.1
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/CHANGELOG.md +12 -0
- package/dist/components/checkout-embed/index.d.ts +1 -3
- package/dist/components/index.d.ts +1 -0
- package/dist/components/route-handelers/index.d.ts +1 -0
- package/dist/components/route-handelers/next-js.d.ts +13 -0
- package/dist/index.cjs.js +238 -3
- package/dist/index.mjs +238 -4
- package/package.json +3 -4
- package/rollup.config.mjs +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import BetterStore from "@betterstore/sdk";
|
|
2
1
|
import React from "react";
|
|
3
|
-
declare function CheckoutEmbed({
|
|
4
|
-
betterStore: InstanceType<typeof BetterStore>;
|
|
2
|
+
declare function CheckoutEmbed({ checkoutId }: {
|
|
5
3
|
checkoutId: string;
|
|
6
4
|
}): React.JSX.Element;
|
|
7
5
|
declare const _default: React.MemoExoticComponent<typeof CheckoutEmbed>;
|
|
@@ -2,3 +2,4 @@ export { useCart } from "./cart/useCart";
|
|
|
2
2
|
export { default as CheckoutEmbed } from "./checkout-embed";
|
|
3
3
|
export { default as PaymentElement } from "./payment-element";
|
|
4
4
|
export { useCheckout } from "./payment-element/useCheckout";
|
|
5
|
+
export { createNextJSHandler } from "./route-handelers";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createNextJSHandler } from "./next-js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import BetterStore from "@betterstore/sdk";
|
|
2
|
+
import { type NextRequest } from "next/server";
|
|
3
|
+
type NextjsRouteConfig = {
|
|
4
|
+
allowedOrigins?: string[];
|
|
5
|
+
};
|
|
6
|
+
type BSClient = InstanceType<typeof BetterStore>;
|
|
7
|
+
export declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
|
|
8
|
+
GET(req: NextRequest): Promise<Response>;
|
|
9
|
+
POST(req: NextRequest): Promise<Response>;
|
|
10
|
+
PUT(req: NextRequest): Promise<Response>;
|
|
11
|
+
DELETE(req: NextRequest): Promise<Response>;
|
|
12
|
+
};
|
|
13
|
+
export {};
|
package/dist/index.cjs.js
CHANGED
|
@@ -315,14 +315,15 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, }) {
|
|
|
315
315
|
React.createElement("span", { className: "text-2xl font-bold" }, formatPrice(total)))))));
|
|
316
316
|
}
|
|
317
317
|
|
|
318
|
-
function CheckoutEmbed({
|
|
318
|
+
function CheckoutEmbed({ checkoutId }) {
|
|
319
319
|
const [checkout, setCheckout] = React.useState(null);
|
|
320
320
|
const [loading, setLoading] = React.useState(true);
|
|
321
321
|
React.useEffect(() => {
|
|
322
322
|
function fetchCheckout() {
|
|
323
323
|
return __awaiter(this, void 0, void 0, function* () {
|
|
324
324
|
try {
|
|
325
|
-
const
|
|
325
|
+
const response = yield fetch(`/api/betterstore/checkout?checkoutId=${checkoutId}`);
|
|
326
|
+
const data = yield response.json();
|
|
326
327
|
setCheckout(data);
|
|
327
328
|
}
|
|
328
329
|
catch (error) {
|
|
@@ -334,7 +335,7 @@ function CheckoutEmbed({ betterStore, checkoutId, }) {
|
|
|
334
335
|
});
|
|
335
336
|
}
|
|
336
337
|
fetchCheckout();
|
|
337
|
-
}, [
|
|
338
|
+
}, [checkoutId]);
|
|
338
339
|
if (loading) {
|
|
339
340
|
return React.createElement("div", null, "Loading...");
|
|
340
341
|
}
|
|
@@ -398,7 +399,241 @@ function PaymentElement({ paymentSecret, checkoutAppearance, onSuccess, onError,
|
|
|
398
399
|
}
|
|
399
400
|
var index = React.memo(PaymentElement);
|
|
400
401
|
|
|
402
|
+
const defaultBetterStoreRoutes = {
|
|
403
|
+
checkout: {
|
|
404
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
405
|
+
const { searchParams } = new URL(req.url);
|
|
406
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
407
|
+
if (!checkoutId) {
|
|
408
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
409
|
+
}
|
|
410
|
+
try {
|
|
411
|
+
const checkout = yield betterStore.checkout.retrieve(checkoutId);
|
|
412
|
+
return Response.json(checkout);
|
|
413
|
+
}
|
|
414
|
+
catch (error) {
|
|
415
|
+
return new Response("Failed to fetch checkout", { status: 500 });
|
|
416
|
+
}
|
|
417
|
+
}),
|
|
418
|
+
POST: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
419
|
+
try {
|
|
420
|
+
const body = yield req.json();
|
|
421
|
+
const checkout = yield betterStore.checkout.create(body);
|
|
422
|
+
return Response.json(checkout);
|
|
423
|
+
}
|
|
424
|
+
catch (error) {
|
|
425
|
+
return new Response("Failed to create checkout", { status: 500 });
|
|
426
|
+
}
|
|
427
|
+
}),
|
|
428
|
+
PUT: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
429
|
+
const { searchParams } = new URL(req.url);
|
|
430
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
431
|
+
if (!checkoutId) {
|
|
432
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
433
|
+
}
|
|
434
|
+
try {
|
|
435
|
+
const body = yield req.json();
|
|
436
|
+
const checkout = yield betterStore.checkout.update(checkoutId, body);
|
|
437
|
+
return Response.json(checkout);
|
|
438
|
+
}
|
|
439
|
+
catch (error) {
|
|
440
|
+
return new Response("Failed to update checkout", { status: 500 });
|
|
441
|
+
}
|
|
442
|
+
}),
|
|
443
|
+
},
|
|
444
|
+
"checkout/shipping": {
|
|
445
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
446
|
+
const { searchParams } = new URL(req.url);
|
|
447
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
448
|
+
if (!checkoutId) {
|
|
449
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
450
|
+
}
|
|
451
|
+
try {
|
|
452
|
+
const rates = yield betterStore.checkout.getShippingRates(checkoutId);
|
|
453
|
+
return Response.json(rates);
|
|
454
|
+
}
|
|
455
|
+
catch (error) {
|
|
456
|
+
return new Response("Failed to get shipping rates", { status: 500 });
|
|
457
|
+
}
|
|
458
|
+
}),
|
|
459
|
+
},
|
|
460
|
+
"checkout/payment": {
|
|
461
|
+
POST: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
462
|
+
const { searchParams } = new URL(req.url);
|
|
463
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
464
|
+
if (!checkoutId) {
|
|
465
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
466
|
+
}
|
|
467
|
+
try {
|
|
468
|
+
const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
|
|
469
|
+
return Response.json({ clientSecret: secret });
|
|
470
|
+
}
|
|
471
|
+
catch (error) {
|
|
472
|
+
return new Response("Failed to generate payment secret", {
|
|
473
|
+
status: 500,
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}),
|
|
477
|
+
},
|
|
478
|
+
customer: {
|
|
479
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
480
|
+
const { searchParams } = new URL(req.url);
|
|
481
|
+
const idOrEmail = searchParams.get("idOrEmail");
|
|
482
|
+
if (!idOrEmail) {
|
|
483
|
+
return new Response("Customer ID or email is required", {
|
|
484
|
+
status: 400,
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
try {
|
|
488
|
+
const customer = yield betterStore.customer.retrieve(idOrEmail);
|
|
489
|
+
return Response.json(customer);
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
return new Response("Failed to fetch customer", { status: 500 });
|
|
493
|
+
}
|
|
494
|
+
}),
|
|
495
|
+
POST: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
496
|
+
try {
|
|
497
|
+
const body = yield req.json();
|
|
498
|
+
const customer = yield betterStore.customer.create(body);
|
|
499
|
+
return Response.json(customer);
|
|
500
|
+
}
|
|
501
|
+
catch (error) {
|
|
502
|
+
return new Response("Failed to create customer", { status: 500 });
|
|
503
|
+
}
|
|
504
|
+
}),
|
|
505
|
+
PUT: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
506
|
+
const { searchParams } = new URL(req.url);
|
|
507
|
+
const customerId = searchParams.get("customerId");
|
|
508
|
+
if (!customerId) {
|
|
509
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
510
|
+
}
|
|
511
|
+
try {
|
|
512
|
+
const body = yield req.json();
|
|
513
|
+
const customer = yield betterStore.customer.update(customerId, body);
|
|
514
|
+
return Response.json(customer);
|
|
515
|
+
}
|
|
516
|
+
catch (error) {
|
|
517
|
+
return new Response("Failed to update customer", { status: 500 });
|
|
518
|
+
}
|
|
519
|
+
}),
|
|
520
|
+
DELETE: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
521
|
+
const { searchParams } = new URL(req.url);
|
|
522
|
+
const customerId = searchParams.get("customerId");
|
|
523
|
+
if (!customerId) {
|
|
524
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
yield betterStore.customer.delete(customerId);
|
|
528
|
+
return new Response(null, { status: 204 });
|
|
529
|
+
}
|
|
530
|
+
catch (error) {
|
|
531
|
+
return new Response("Failed to delete customer", { status: 500 });
|
|
532
|
+
}
|
|
533
|
+
}),
|
|
534
|
+
},
|
|
535
|
+
product: {
|
|
536
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
537
|
+
const { searchParams } = new URL(req.url);
|
|
538
|
+
const productId = searchParams.get("productId");
|
|
539
|
+
try {
|
|
540
|
+
if (productId) {
|
|
541
|
+
const product = yield betterStore.products.retrieve(productId);
|
|
542
|
+
return Response.json(product);
|
|
543
|
+
}
|
|
544
|
+
else {
|
|
545
|
+
const products = yield betterStore.products.list();
|
|
546
|
+
return Response.json(products);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
catch (error) {
|
|
550
|
+
return new Response("Failed to fetch products", { status: 500 });
|
|
551
|
+
}
|
|
552
|
+
}),
|
|
553
|
+
},
|
|
554
|
+
};
|
|
555
|
+
function createNextJSHandler(betterStore, config = {}) {
|
|
556
|
+
const { allowedOrigins = [] } = config;
|
|
557
|
+
function validateRequest(req) {
|
|
558
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
559
|
+
if (allowedOrigins.length > 0) {
|
|
560
|
+
const origin = req.headers.get("origin");
|
|
561
|
+
if (!origin || !allowedOrigins.includes(origin)) {
|
|
562
|
+
return new Response("Unauthorized", { status: 403 });
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return null;
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
function getRouteFromPath(pathname) {
|
|
569
|
+
// Remove leading and trailing slashes and 'api' prefix if present
|
|
570
|
+
const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
|
|
571
|
+
// Get the relevant part of the path (everything after betterstore/)
|
|
572
|
+
const relevantPath = cleanPath.split("betterstore/")[1] || "";
|
|
573
|
+
return relevantPath;
|
|
574
|
+
}
|
|
575
|
+
return {
|
|
576
|
+
GET(req) {
|
|
577
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
578
|
+
var _a;
|
|
579
|
+
const validationError = yield validateRequest(req);
|
|
580
|
+
if (validationError)
|
|
581
|
+
return validationError;
|
|
582
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
583
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.GET;
|
|
584
|
+
if (!handler) {
|
|
585
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
586
|
+
}
|
|
587
|
+
return handler(req, betterStore);
|
|
588
|
+
});
|
|
589
|
+
},
|
|
590
|
+
POST(req) {
|
|
591
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
592
|
+
var _a;
|
|
593
|
+
const validationError = yield validateRequest(req);
|
|
594
|
+
if (validationError)
|
|
595
|
+
return validationError;
|
|
596
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
597
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.POST;
|
|
598
|
+
if (!handler) {
|
|
599
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
600
|
+
}
|
|
601
|
+
return handler(req, betterStore);
|
|
602
|
+
});
|
|
603
|
+
},
|
|
604
|
+
PUT(req) {
|
|
605
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
606
|
+
var _a;
|
|
607
|
+
const validationError = yield validateRequest(req);
|
|
608
|
+
if (validationError)
|
|
609
|
+
return validationError;
|
|
610
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
611
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.PUT;
|
|
612
|
+
if (!handler) {
|
|
613
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
614
|
+
}
|
|
615
|
+
return handler(req, betterStore);
|
|
616
|
+
});
|
|
617
|
+
},
|
|
618
|
+
DELETE(req) {
|
|
619
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
620
|
+
var _a;
|
|
621
|
+
const validationError = yield validateRequest(req);
|
|
622
|
+
if (validationError)
|
|
623
|
+
return validationError;
|
|
624
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
625
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.DELETE;
|
|
626
|
+
if (!handler) {
|
|
627
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
628
|
+
}
|
|
629
|
+
return handler(req, betterStore);
|
|
630
|
+
});
|
|
631
|
+
},
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
|
|
401
635
|
exports.CheckoutEmbed = index$1;
|
|
402
636
|
exports.PaymentElement = index;
|
|
637
|
+
exports.createNextJSHandler = createNextJSHandler;
|
|
403
638
|
exports.useCart = useCart;
|
|
404
639
|
exports.useCheckout = useCheckout;
|
package/dist/index.mjs
CHANGED
|
@@ -313,14 +313,15 @@ function CheckoutSummary({ lineItems, shipping, tax, currency, }) {
|
|
|
313
313
|
React.createElement("span", { className: "text-2xl font-bold" }, formatPrice(total)))))));
|
|
314
314
|
}
|
|
315
315
|
|
|
316
|
-
function CheckoutEmbed({
|
|
316
|
+
function CheckoutEmbed({ checkoutId }) {
|
|
317
317
|
const [checkout, setCheckout] = useState(null);
|
|
318
318
|
const [loading, setLoading] = useState(true);
|
|
319
319
|
useEffect(() => {
|
|
320
320
|
function fetchCheckout() {
|
|
321
321
|
return __awaiter(this, void 0, void 0, function* () {
|
|
322
322
|
try {
|
|
323
|
-
const
|
|
323
|
+
const response = yield fetch(`/api/betterstore/checkout?checkoutId=${checkoutId}`);
|
|
324
|
+
const data = yield response.json();
|
|
324
325
|
setCheckout(data);
|
|
325
326
|
}
|
|
326
327
|
catch (error) {
|
|
@@ -332,7 +333,7 @@ function CheckoutEmbed({ betterStore, checkoutId, }) {
|
|
|
332
333
|
});
|
|
333
334
|
}
|
|
334
335
|
fetchCheckout();
|
|
335
|
-
}, [
|
|
336
|
+
}, [checkoutId]);
|
|
336
337
|
if (loading) {
|
|
337
338
|
return React.createElement("div", null, "Loading...");
|
|
338
339
|
}
|
|
@@ -396,4 +397,237 @@ function PaymentElement({ paymentSecret, checkoutAppearance, onSuccess, onError,
|
|
|
396
397
|
}
|
|
397
398
|
var index = memo(PaymentElement);
|
|
398
399
|
|
|
399
|
-
|
|
400
|
+
const defaultBetterStoreRoutes = {
|
|
401
|
+
checkout: {
|
|
402
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
403
|
+
const { searchParams } = new URL(req.url);
|
|
404
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
405
|
+
if (!checkoutId) {
|
|
406
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
const checkout = yield betterStore.checkout.retrieve(checkoutId);
|
|
410
|
+
return Response.json(checkout);
|
|
411
|
+
}
|
|
412
|
+
catch (error) {
|
|
413
|
+
return new Response("Failed to fetch checkout", { status: 500 });
|
|
414
|
+
}
|
|
415
|
+
}),
|
|
416
|
+
POST: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
417
|
+
try {
|
|
418
|
+
const body = yield req.json();
|
|
419
|
+
const checkout = yield betterStore.checkout.create(body);
|
|
420
|
+
return Response.json(checkout);
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
return new Response("Failed to create checkout", { status: 500 });
|
|
424
|
+
}
|
|
425
|
+
}),
|
|
426
|
+
PUT: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
427
|
+
const { searchParams } = new URL(req.url);
|
|
428
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
429
|
+
if (!checkoutId) {
|
|
430
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
431
|
+
}
|
|
432
|
+
try {
|
|
433
|
+
const body = yield req.json();
|
|
434
|
+
const checkout = yield betterStore.checkout.update(checkoutId, body);
|
|
435
|
+
return Response.json(checkout);
|
|
436
|
+
}
|
|
437
|
+
catch (error) {
|
|
438
|
+
return new Response("Failed to update checkout", { status: 500 });
|
|
439
|
+
}
|
|
440
|
+
}),
|
|
441
|
+
},
|
|
442
|
+
"checkout/shipping": {
|
|
443
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
444
|
+
const { searchParams } = new URL(req.url);
|
|
445
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
446
|
+
if (!checkoutId) {
|
|
447
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
448
|
+
}
|
|
449
|
+
try {
|
|
450
|
+
const rates = yield betterStore.checkout.getShippingRates(checkoutId);
|
|
451
|
+
return Response.json(rates);
|
|
452
|
+
}
|
|
453
|
+
catch (error) {
|
|
454
|
+
return new Response("Failed to get shipping rates", { status: 500 });
|
|
455
|
+
}
|
|
456
|
+
}),
|
|
457
|
+
},
|
|
458
|
+
"checkout/payment": {
|
|
459
|
+
POST: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
460
|
+
const { searchParams } = new URL(req.url);
|
|
461
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
462
|
+
if (!checkoutId) {
|
|
463
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
464
|
+
}
|
|
465
|
+
try {
|
|
466
|
+
const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
|
|
467
|
+
return Response.json({ clientSecret: secret });
|
|
468
|
+
}
|
|
469
|
+
catch (error) {
|
|
470
|
+
return new Response("Failed to generate payment secret", {
|
|
471
|
+
status: 500,
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
}),
|
|
475
|
+
},
|
|
476
|
+
customer: {
|
|
477
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
478
|
+
const { searchParams } = new URL(req.url);
|
|
479
|
+
const idOrEmail = searchParams.get("idOrEmail");
|
|
480
|
+
if (!idOrEmail) {
|
|
481
|
+
return new Response("Customer ID or email is required", {
|
|
482
|
+
status: 400,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
try {
|
|
486
|
+
const customer = yield betterStore.customer.retrieve(idOrEmail);
|
|
487
|
+
return Response.json(customer);
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
return new Response("Failed to fetch customer", { status: 500 });
|
|
491
|
+
}
|
|
492
|
+
}),
|
|
493
|
+
POST: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
494
|
+
try {
|
|
495
|
+
const body = yield req.json();
|
|
496
|
+
const customer = yield betterStore.customer.create(body);
|
|
497
|
+
return Response.json(customer);
|
|
498
|
+
}
|
|
499
|
+
catch (error) {
|
|
500
|
+
return new Response("Failed to create customer", { status: 500 });
|
|
501
|
+
}
|
|
502
|
+
}),
|
|
503
|
+
PUT: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
504
|
+
const { searchParams } = new URL(req.url);
|
|
505
|
+
const customerId = searchParams.get("customerId");
|
|
506
|
+
if (!customerId) {
|
|
507
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
508
|
+
}
|
|
509
|
+
try {
|
|
510
|
+
const body = yield req.json();
|
|
511
|
+
const customer = yield betterStore.customer.update(customerId, body);
|
|
512
|
+
return Response.json(customer);
|
|
513
|
+
}
|
|
514
|
+
catch (error) {
|
|
515
|
+
return new Response("Failed to update customer", { status: 500 });
|
|
516
|
+
}
|
|
517
|
+
}),
|
|
518
|
+
DELETE: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
519
|
+
const { searchParams } = new URL(req.url);
|
|
520
|
+
const customerId = searchParams.get("customerId");
|
|
521
|
+
if (!customerId) {
|
|
522
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
523
|
+
}
|
|
524
|
+
try {
|
|
525
|
+
yield betterStore.customer.delete(customerId);
|
|
526
|
+
return new Response(null, { status: 204 });
|
|
527
|
+
}
|
|
528
|
+
catch (error) {
|
|
529
|
+
return new Response("Failed to delete customer", { status: 500 });
|
|
530
|
+
}
|
|
531
|
+
}),
|
|
532
|
+
},
|
|
533
|
+
product: {
|
|
534
|
+
GET: (req, betterStore) => __awaiter(void 0, void 0, void 0, function* () {
|
|
535
|
+
const { searchParams } = new URL(req.url);
|
|
536
|
+
const productId = searchParams.get("productId");
|
|
537
|
+
try {
|
|
538
|
+
if (productId) {
|
|
539
|
+
const product = yield betterStore.products.retrieve(productId);
|
|
540
|
+
return Response.json(product);
|
|
541
|
+
}
|
|
542
|
+
else {
|
|
543
|
+
const products = yield betterStore.products.list();
|
|
544
|
+
return Response.json(products);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
catch (error) {
|
|
548
|
+
return new Response("Failed to fetch products", { status: 500 });
|
|
549
|
+
}
|
|
550
|
+
}),
|
|
551
|
+
},
|
|
552
|
+
};
|
|
553
|
+
function createNextJSHandler(betterStore, config = {}) {
|
|
554
|
+
const { allowedOrigins = [] } = config;
|
|
555
|
+
function validateRequest(req) {
|
|
556
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
557
|
+
if (allowedOrigins.length > 0) {
|
|
558
|
+
const origin = req.headers.get("origin");
|
|
559
|
+
if (!origin || !allowedOrigins.includes(origin)) {
|
|
560
|
+
return new Response("Unauthorized", { status: 403 });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return null;
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
function getRouteFromPath(pathname) {
|
|
567
|
+
// Remove leading and trailing slashes and 'api' prefix if present
|
|
568
|
+
const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
|
|
569
|
+
// Get the relevant part of the path (everything after betterstore/)
|
|
570
|
+
const relevantPath = cleanPath.split("betterstore/")[1] || "";
|
|
571
|
+
return relevantPath;
|
|
572
|
+
}
|
|
573
|
+
return {
|
|
574
|
+
GET(req) {
|
|
575
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
576
|
+
var _a;
|
|
577
|
+
const validationError = yield validateRequest(req);
|
|
578
|
+
if (validationError)
|
|
579
|
+
return validationError;
|
|
580
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
581
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.GET;
|
|
582
|
+
if (!handler) {
|
|
583
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
584
|
+
}
|
|
585
|
+
return handler(req, betterStore);
|
|
586
|
+
});
|
|
587
|
+
},
|
|
588
|
+
POST(req) {
|
|
589
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
590
|
+
var _a;
|
|
591
|
+
const validationError = yield validateRequest(req);
|
|
592
|
+
if (validationError)
|
|
593
|
+
return validationError;
|
|
594
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
595
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.POST;
|
|
596
|
+
if (!handler) {
|
|
597
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
598
|
+
}
|
|
599
|
+
return handler(req, betterStore);
|
|
600
|
+
});
|
|
601
|
+
},
|
|
602
|
+
PUT(req) {
|
|
603
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
604
|
+
var _a;
|
|
605
|
+
const validationError = yield validateRequest(req);
|
|
606
|
+
if (validationError)
|
|
607
|
+
return validationError;
|
|
608
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
609
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.PUT;
|
|
610
|
+
if (!handler) {
|
|
611
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
612
|
+
}
|
|
613
|
+
return handler(req, betterStore);
|
|
614
|
+
});
|
|
615
|
+
},
|
|
616
|
+
DELETE(req) {
|
|
617
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
618
|
+
var _a;
|
|
619
|
+
const validationError = yield validateRequest(req);
|
|
620
|
+
if (validationError)
|
|
621
|
+
return validationError;
|
|
622
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
623
|
+
const handler = (_a = defaultBetterStoreRoutes[route]) === null || _a === void 0 ? void 0 : _a.DELETE;
|
|
624
|
+
if (!handler) {
|
|
625
|
+
return new Response(`Route not found: ${route}`, { status: 404 });
|
|
626
|
+
}
|
|
627
|
+
return handler(req, betterStore);
|
|
628
|
+
});
|
|
629
|
+
},
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
export { index$1 as CheckoutEmbed, index as PaymentElement, createNextJSHandler, useCart, useCheckout };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@betterstore/react",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "E-commerce for Developers",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"author": "Better Store",
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"devDependencies": {
|
|
22
|
+
"@betterstore/sdk": "^0.2.1",
|
|
22
23
|
"@changesets/cli": "^2.28.1",
|
|
23
|
-
"@rollup/plugin-babel": "^6.0.4",
|
|
24
24
|
"@rollup/plugin-commonjs": "^28.0.2",
|
|
25
25
|
"@rollup/plugin-node-resolve": "^16.0.0",
|
|
26
26
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@types/react": "^18.0.0",
|
|
29
29
|
"@types/react-dom": "^18.2.0",
|
|
30
30
|
"autoprefixer": "^10.4.20",
|
|
31
|
-
"
|
|
31
|
+
"next": "^15.2.1",
|
|
32
32
|
"postcss": "^8.4.31",
|
|
33
33
|
"prettier": "^3.5.3",
|
|
34
34
|
"rollup": "^4.34.9",
|
|
@@ -42,7 +42,6 @@
|
|
|
42
42
|
"react-dom": "^18.0.0"
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
|
-
"@betterstore/sdk": "^0.2.0",
|
|
46
45
|
"@stripe/react-stripe-js": "^3.3.0",
|
|
47
46
|
"@stripe/stripe-js": "^5.10.0",
|
|
48
47
|
"rollup-plugin-postcss": "^4.0.2",
|