@betterstore/sdk 0.2.10 → 0.2.12
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/index.d.mts +13 -5
- package/dist/index.d.ts +13 -5
- package/dist/index.js +299 -8
- package/dist/index.mjs +298 -7
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
|
|
1
3
|
interface LineItem {
|
|
2
4
|
quantity: number;
|
|
3
5
|
productId?: string;
|
|
@@ -201,10 +203,16 @@ declare class Products {
|
|
|
201
203
|
retrieve(productId: string): Promise<Product>;
|
|
202
204
|
}
|
|
203
205
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
206
|
+
type NextjsRouteConfig = {
|
|
207
|
+
apiKey?: string;
|
|
208
|
+
productionAllowedOrigins?: string[];
|
|
209
|
+
};
|
|
210
|
+
type BSClient = InstanceType<typeof BetterStore>;
|
|
211
|
+
declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
|
|
212
|
+
GET(req: NextRequest): Promise<Response>;
|
|
213
|
+
POST(req: NextRequest): Promise<Response>;
|
|
214
|
+
PUT(req: NextRequest): Promise<Response>;
|
|
215
|
+
DELETE(req: NextRequest): Promise<Response>;
|
|
208
216
|
};
|
|
209
217
|
|
|
210
218
|
declare class BetterStore {
|
|
@@ -214,4 +222,4 @@ declare class BetterStore {
|
|
|
214
222
|
constructor(apiKey: string);
|
|
215
223
|
}
|
|
216
224
|
|
|
217
|
-
export { BetterStore as default
|
|
225
|
+
export { createNextJSHandler, BetterStore as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { NextRequest } from 'next/server';
|
|
2
|
+
|
|
1
3
|
interface LineItem {
|
|
2
4
|
quantity: number;
|
|
3
5
|
productId?: string;
|
|
@@ -201,10 +203,16 @@ declare class Products {
|
|
|
201
203
|
retrieve(productId: string): Promise<Product>;
|
|
202
204
|
}
|
|
203
205
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
206
|
+
type NextjsRouteConfig = {
|
|
207
|
+
apiKey?: string;
|
|
208
|
+
productionAllowedOrigins?: string[];
|
|
209
|
+
};
|
|
210
|
+
type BSClient = InstanceType<typeof BetterStore>;
|
|
211
|
+
declare function createNextJSHandler(betterStore: BSClient, config?: NextjsRouteConfig): {
|
|
212
|
+
GET(req: NextRequest): Promise<Response>;
|
|
213
|
+
POST(req: NextRequest): Promise<Response>;
|
|
214
|
+
PUT(req: NextRequest): Promise<Response>;
|
|
215
|
+
DELETE(req: NextRequest): Promise<Response>;
|
|
208
216
|
};
|
|
209
217
|
|
|
210
218
|
declare class BetterStore {
|
|
@@ -214,4 +222,4 @@ declare class BetterStore {
|
|
|
214
222
|
constructor(apiKey: string);
|
|
215
223
|
}
|
|
216
224
|
|
|
217
|
-
export { BetterStore as default
|
|
225
|
+
export { createNextJSHandler, BetterStore as default };
|
package/dist/index.js
CHANGED
|
@@ -50,8 +50,8 @@ var __async = (__this, __arguments, generator) => {
|
|
|
50
50
|
// src/index.ts
|
|
51
51
|
var index_exports = {};
|
|
52
52
|
__export(index_exports, {
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
createNextJSHandler: () => createNextJSHandler,
|
|
54
|
+
default: () => index_default
|
|
55
55
|
});
|
|
56
56
|
module.exports = __toCommonJS(index_exports);
|
|
57
57
|
|
|
@@ -86,10 +86,12 @@ var createApiClient = (apiKey) => {
|
|
|
86
86
|
apiError.status = 503;
|
|
87
87
|
apiError.message = "Service unavailable - no response from server";
|
|
88
88
|
apiError.code = "SERVICE_UNAVAILABLE";
|
|
89
|
+
apiError.details = error.request;
|
|
89
90
|
} else {
|
|
90
91
|
apiError.status = 500;
|
|
91
92
|
apiError.message = "Request configuration error";
|
|
92
93
|
apiError.code = "REQUEST_SETUP_ERROR";
|
|
94
|
+
apiError.details = error;
|
|
93
95
|
}
|
|
94
96
|
throw apiError;
|
|
95
97
|
}
|
|
@@ -234,12 +236,301 @@ var Products = class {
|
|
|
234
236
|
};
|
|
235
237
|
var products_default = Products;
|
|
236
238
|
|
|
237
|
-
// src/
|
|
238
|
-
|
|
239
|
+
// src/proxies/next-js.ts
|
|
240
|
+
var defaultBetterStoreRoutes = {
|
|
241
|
+
checkout: {
|
|
242
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
243
|
+
const { searchParams } = new URL(req.url);
|
|
244
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
245
|
+
if (!checkoutId) {
|
|
246
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
const checkout = yield betterStore.checkout.retrieve(checkoutId);
|
|
250
|
+
return Response.json(checkout);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
return new Response("Failed to fetch checkout", { status: 500 });
|
|
253
|
+
}
|
|
254
|
+
}),
|
|
255
|
+
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
256
|
+
try {
|
|
257
|
+
const body = yield req.json();
|
|
258
|
+
const checkout = yield betterStore.checkout.create(body);
|
|
259
|
+
return Response.json(checkout);
|
|
260
|
+
} catch (error) {
|
|
261
|
+
return new Response("Failed to create checkout", { status: 500 });
|
|
262
|
+
}
|
|
263
|
+
}),
|
|
264
|
+
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
265
|
+
const { searchParams } = new URL(req.url);
|
|
266
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
267
|
+
if (!checkoutId) {
|
|
268
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
const body = yield req.json();
|
|
272
|
+
const checkout = yield betterStore.checkout.update(checkoutId, body);
|
|
273
|
+
return Response.json(checkout);
|
|
274
|
+
} catch (error) {
|
|
275
|
+
return new Response("Failed to update checkout", { status: 500 });
|
|
276
|
+
}
|
|
277
|
+
})
|
|
278
|
+
},
|
|
279
|
+
"checkout/shipping": {
|
|
280
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
281
|
+
const { searchParams } = new URL(req.url);
|
|
282
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
283
|
+
if (!checkoutId) {
|
|
284
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
const rates = yield betterStore.checkout.getShippingRates(checkoutId);
|
|
288
|
+
return Response.json(rates);
|
|
289
|
+
} catch (error) {
|
|
290
|
+
return new Response("Failed to get shipping rates", { status: 500 });
|
|
291
|
+
}
|
|
292
|
+
})
|
|
293
|
+
},
|
|
294
|
+
"checkout/payment": {
|
|
295
|
+
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
296
|
+
const { searchParams } = new URL(req.url);
|
|
297
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
298
|
+
if (!checkoutId) {
|
|
299
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
|
|
303
|
+
return Response.json({ clientSecret: secret });
|
|
304
|
+
} catch (error) {
|
|
305
|
+
return new Response("Failed to generate payment secret", {
|
|
306
|
+
status: 500
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
})
|
|
310
|
+
},
|
|
311
|
+
customer: {
|
|
312
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
313
|
+
const { searchParams } = new URL(req.url);
|
|
314
|
+
const idOrEmail = searchParams.get("idOrEmail");
|
|
315
|
+
if (!idOrEmail) {
|
|
316
|
+
return new Response("Customer ID or email is required", {
|
|
317
|
+
status: 400
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
const customer = yield betterStore.customer.retrieve(idOrEmail);
|
|
322
|
+
return Response.json(customer);
|
|
323
|
+
} catch (error) {
|
|
324
|
+
return new Response("Failed to fetch customer", { status: 500 });
|
|
325
|
+
}
|
|
326
|
+
}),
|
|
327
|
+
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
328
|
+
try {
|
|
329
|
+
const body = yield req.json();
|
|
330
|
+
const customer = yield betterStore.customer.create(body);
|
|
331
|
+
return Response.json(customer);
|
|
332
|
+
} catch (error) {
|
|
333
|
+
return new Response("Failed to create customer", { status: 500 });
|
|
334
|
+
}
|
|
335
|
+
}),
|
|
336
|
+
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
337
|
+
const { searchParams } = new URL(req.url);
|
|
338
|
+
const customerId = searchParams.get("customerId");
|
|
339
|
+
if (!customerId) {
|
|
340
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
341
|
+
}
|
|
342
|
+
try {
|
|
343
|
+
const body = yield req.json();
|
|
344
|
+
const customer = yield betterStore.customer.update(customerId, body);
|
|
345
|
+
return Response.json(customer);
|
|
346
|
+
} catch (error) {
|
|
347
|
+
return new Response("Failed to update customer", { status: 500 });
|
|
348
|
+
}
|
|
349
|
+
}),
|
|
350
|
+
DELETE: (req, betterStore) => __async(void 0, null, function* () {
|
|
351
|
+
const { searchParams } = new URL(req.url);
|
|
352
|
+
const customerId = searchParams.get("customerId");
|
|
353
|
+
if (!customerId) {
|
|
354
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
yield betterStore.customer.delete(customerId);
|
|
358
|
+
return new Response(null, { status: 204 });
|
|
359
|
+
} catch (error) {
|
|
360
|
+
return new Response("Failed to delete customer", { status: 500 });
|
|
361
|
+
}
|
|
362
|
+
})
|
|
363
|
+
},
|
|
364
|
+
product: {
|
|
365
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
366
|
+
const { searchParams } = new URL(req.url);
|
|
367
|
+
const productId = searchParams.get("productId");
|
|
368
|
+
try {
|
|
369
|
+
if (productId) {
|
|
370
|
+
const product = yield betterStore.products.retrieve(productId);
|
|
371
|
+
return Response.json(product);
|
|
372
|
+
} else {
|
|
373
|
+
const products = yield betterStore.products.list();
|
|
374
|
+
return Response.json(products);
|
|
375
|
+
}
|
|
376
|
+
} catch (error) {
|
|
377
|
+
return new Response("Failed to fetch products", { status: 500 });
|
|
378
|
+
}
|
|
379
|
+
})
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
function addCORSHeaders(response, origin, allowedOrigins) {
|
|
383
|
+
if (origin && allowedOrigins.includes(origin)) {
|
|
384
|
+
response.headers.set("Access-Control-Allow-Origin", origin);
|
|
385
|
+
}
|
|
386
|
+
response.headers.set(
|
|
387
|
+
"Access-Control-Allow-Methods",
|
|
388
|
+
"GET, POST, PUT, DELETE, OPTIONS"
|
|
389
|
+
);
|
|
390
|
+
response.headers.set(
|
|
391
|
+
"Access-Control-Allow-Headers",
|
|
392
|
+
"Content-Type, Authorization"
|
|
393
|
+
);
|
|
394
|
+
return response;
|
|
395
|
+
}
|
|
396
|
+
function createNextJSHandler(betterStore, config = {}) {
|
|
397
|
+
const { apiKey, productionAllowedOrigins = [] } = config;
|
|
398
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
399
|
+
function validateRequest(req) {
|
|
400
|
+
return __async(this, null, function* () {
|
|
401
|
+
if (apiKey) {
|
|
402
|
+
const authHeader = req.headers.get("Authorization");
|
|
403
|
+
const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
|
|
404
|
+
if (!providedKey || providedKey !== apiKey) {
|
|
405
|
+
return new Response("Unauthorized", {
|
|
406
|
+
status: 401,
|
|
407
|
+
headers: { "WWW-Authenticate": "Bearer" }
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const origin = req.headers.get("origin");
|
|
412
|
+
if (isProduction && productionAllowedOrigins.length > 0) {
|
|
413
|
+
if (!origin || !productionAllowedOrigins.includes(origin)) {
|
|
414
|
+
return new Response("Unauthorized", { status: 403 });
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return null;
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
function getRouteFromPath(pathname) {
|
|
421
|
+
const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
|
|
422
|
+
const relevantPath = cleanPath.split("betterstore/")[1] || "";
|
|
423
|
+
return relevantPath;
|
|
424
|
+
}
|
|
239
425
|
return {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
426
|
+
GET(req) {
|
|
427
|
+
return __async(this, null, function* () {
|
|
428
|
+
var _a2;
|
|
429
|
+
const validationError = yield validateRequest(req);
|
|
430
|
+
if (validationError)
|
|
431
|
+
return addCORSHeaders(
|
|
432
|
+
validationError,
|
|
433
|
+
req.headers.get("origin"),
|
|
434
|
+
productionAllowedOrigins
|
|
435
|
+
);
|
|
436
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
437
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
|
|
438
|
+
if (!handler) {
|
|
439
|
+
return addCORSHeaders(
|
|
440
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
441
|
+
req.headers.get("origin"),
|
|
442
|
+
productionAllowedOrigins
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
const response = yield handler(req, betterStore);
|
|
446
|
+
return addCORSHeaders(
|
|
447
|
+
response,
|
|
448
|
+
req.headers.get("origin"),
|
|
449
|
+
productionAllowedOrigins
|
|
450
|
+
);
|
|
451
|
+
});
|
|
452
|
+
},
|
|
453
|
+
POST(req) {
|
|
454
|
+
return __async(this, null, function* () {
|
|
455
|
+
var _a2;
|
|
456
|
+
const validationError = yield validateRequest(req);
|
|
457
|
+
if (validationError)
|
|
458
|
+
return addCORSHeaders(
|
|
459
|
+
validationError,
|
|
460
|
+
req.headers.get("origin"),
|
|
461
|
+
productionAllowedOrigins
|
|
462
|
+
);
|
|
463
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
464
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
|
|
465
|
+
if (!handler) {
|
|
466
|
+
return addCORSHeaders(
|
|
467
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
468
|
+
req.headers.get("origin"),
|
|
469
|
+
productionAllowedOrigins
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
const response = yield handler(req, betterStore);
|
|
473
|
+
return addCORSHeaders(
|
|
474
|
+
response,
|
|
475
|
+
req.headers.get("origin"),
|
|
476
|
+
productionAllowedOrigins
|
|
477
|
+
);
|
|
478
|
+
});
|
|
479
|
+
},
|
|
480
|
+
PUT(req) {
|
|
481
|
+
return __async(this, null, function* () {
|
|
482
|
+
var _a2;
|
|
483
|
+
const validationError = yield validateRequest(req);
|
|
484
|
+
if (validationError)
|
|
485
|
+
return addCORSHeaders(
|
|
486
|
+
validationError,
|
|
487
|
+
req.headers.get("origin"),
|
|
488
|
+
productionAllowedOrigins
|
|
489
|
+
);
|
|
490
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
491
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
|
|
492
|
+
if (!handler) {
|
|
493
|
+
return addCORSHeaders(
|
|
494
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
495
|
+
req.headers.get("origin"),
|
|
496
|
+
productionAllowedOrigins
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
const response = yield handler(req, betterStore);
|
|
500
|
+
return addCORSHeaders(
|
|
501
|
+
response,
|
|
502
|
+
req.headers.get("origin"),
|
|
503
|
+
productionAllowedOrigins
|
|
504
|
+
);
|
|
505
|
+
});
|
|
506
|
+
},
|
|
507
|
+
DELETE(req) {
|
|
508
|
+
return __async(this, null, function* () {
|
|
509
|
+
var _a2;
|
|
510
|
+
const validationError = yield validateRequest(req);
|
|
511
|
+
if (validationError)
|
|
512
|
+
return addCORSHeaders(
|
|
513
|
+
validationError,
|
|
514
|
+
req.headers.get("origin"),
|
|
515
|
+
productionAllowedOrigins
|
|
516
|
+
);
|
|
517
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
518
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
|
|
519
|
+
if (!handler) {
|
|
520
|
+
return addCORSHeaders(
|
|
521
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
522
|
+
req.headers.get("origin"),
|
|
523
|
+
productionAllowedOrigins
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
const response = yield handler(req, betterStore);
|
|
527
|
+
return addCORSHeaders(
|
|
528
|
+
response,
|
|
529
|
+
req.headers.get("origin"),
|
|
530
|
+
productionAllowedOrigins
|
|
531
|
+
);
|
|
532
|
+
});
|
|
533
|
+
}
|
|
243
534
|
};
|
|
244
535
|
}
|
|
245
536
|
|
|
@@ -258,5 +549,5 @@ var BetterStore = class {
|
|
|
258
549
|
var index_default = BetterStore;
|
|
259
550
|
// Annotate the CommonJS export names for ESM import in node:
|
|
260
551
|
0 && (module.exports = {
|
|
261
|
-
|
|
552
|
+
createNextJSHandler
|
|
262
553
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -50,10 +50,12 @@ var createApiClient = (apiKey) => {
|
|
|
50
50
|
apiError.status = 503;
|
|
51
51
|
apiError.message = "Service unavailable - no response from server";
|
|
52
52
|
apiError.code = "SERVICE_UNAVAILABLE";
|
|
53
|
+
apiError.details = error.request;
|
|
53
54
|
} else {
|
|
54
55
|
apiError.status = 500;
|
|
55
56
|
apiError.message = "Request configuration error";
|
|
56
57
|
apiError.code = "REQUEST_SETUP_ERROR";
|
|
58
|
+
apiError.details = error;
|
|
57
59
|
}
|
|
58
60
|
throw apiError;
|
|
59
61
|
}
|
|
@@ -198,12 +200,301 @@ var Products = class {
|
|
|
198
200
|
};
|
|
199
201
|
var products_default = Products;
|
|
200
202
|
|
|
201
|
-
// src/
|
|
202
|
-
|
|
203
|
+
// src/proxies/next-js.ts
|
|
204
|
+
var defaultBetterStoreRoutes = {
|
|
205
|
+
checkout: {
|
|
206
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
207
|
+
const { searchParams } = new URL(req.url);
|
|
208
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
209
|
+
if (!checkoutId) {
|
|
210
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
const checkout = yield betterStore.checkout.retrieve(checkoutId);
|
|
214
|
+
return Response.json(checkout);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
return new Response("Failed to fetch checkout", { status: 500 });
|
|
217
|
+
}
|
|
218
|
+
}),
|
|
219
|
+
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
220
|
+
try {
|
|
221
|
+
const body = yield req.json();
|
|
222
|
+
const checkout = yield betterStore.checkout.create(body);
|
|
223
|
+
return Response.json(checkout);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
return new Response("Failed to create checkout", { status: 500 });
|
|
226
|
+
}
|
|
227
|
+
}),
|
|
228
|
+
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
229
|
+
const { searchParams } = new URL(req.url);
|
|
230
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
231
|
+
if (!checkoutId) {
|
|
232
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
const body = yield req.json();
|
|
236
|
+
const checkout = yield betterStore.checkout.update(checkoutId, body);
|
|
237
|
+
return Response.json(checkout);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
return new Response("Failed to update checkout", { status: 500 });
|
|
240
|
+
}
|
|
241
|
+
})
|
|
242
|
+
},
|
|
243
|
+
"checkout/shipping": {
|
|
244
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
245
|
+
const { searchParams } = new URL(req.url);
|
|
246
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
247
|
+
if (!checkoutId) {
|
|
248
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
const rates = yield betterStore.checkout.getShippingRates(checkoutId);
|
|
252
|
+
return Response.json(rates);
|
|
253
|
+
} catch (error) {
|
|
254
|
+
return new Response("Failed to get shipping rates", { status: 500 });
|
|
255
|
+
}
|
|
256
|
+
})
|
|
257
|
+
},
|
|
258
|
+
"checkout/payment": {
|
|
259
|
+
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
260
|
+
const { searchParams } = new URL(req.url);
|
|
261
|
+
const checkoutId = searchParams.get("checkoutId");
|
|
262
|
+
if (!checkoutId) {
|
|
263
|
+
return new Response("Checkout ID is required", { status: 400 });
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
|
|
267
|
+
return Response.json({ clientSecret: secret });
|
|
268
|
+
} catch (error) {
|
|
269
|
+
return new Response("Failed to generate payment secret", {
|
|
270
|
+
status: 500
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
})
|
|
274
|
+
},
|
|
275
|
+
customer: {
|
|
276
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
277
|
+
const { searchParams } = new URL(req.url);
|
|
278
|
+
const idOrEmail = searchParams.get("idOrEmail");
|
|
279
|
+
if (!idOrEmail) {
|
|
280
|
+
return new Response("Customer ID or email is required", {
|
|
281
|
+
status: 400
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
const customer = yield betterStore.customer.retrieve(idOrEmail);
|
|
286
|
+
return Response.json(customer);
|
|
287
|
+
} catch (error) {
|
|
288
|
+
return new Response("Failed to fetch customer", { status: 500 });
|
|
289
|
+
}
|
|
290
|
+
}),
|
|
291
|
+
POST: (req, betterStore) => __async(void 0, null, function* () {
|
|
292
|
+
try {
|
|
293
|
+
const body = yield req.json();
|
|
294
|
+
const customer = yield betterStore.customer.create(body);
|
|
295
|
+
return Response.json(customer);
|
|
296
|
+
} catch (error) {
|
|
297
|
+
return new Response("Failed to create customer", { status: 500 });
|
|
298
|
+
}
|
|
299
|
+
}),
|
|
300
|
+
PUT: (req, betterStore) => __async(void 0, null, function* () {
|
|
301
|
+
const { searchParams } = new URL(req.url);
|
|
302
|
+
const customerId = searchParams.get("customerId");
|
|
303
|
+
if (!customerId) {
|
|
304
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
305
|
+
}
|
|
306
|
+
try {
|
|
307
|
+
const body = yield req.json();
|
|
308
|
+
const customer = yield betterStore.customer.update(customerId, body);
|
|
309
|
+
return Response.json(customer);
|
|
310
|
+
} catch (error) {
|
|
311
|
+
return new Response("Failed to update customer", { status: 500 });
|
|
312
|
+
}
|
|
313
|
+
}),
|
|
314
|
+
DELETE: (req, betterStore) => __async(void 0, null, function* () {
|
|
315
|
+
const { searchParams } = new URL(req.url);
|
|
316
|
+
const customerId = searchParams.get("customerId");
|
|
317
|
+
if (!customerId) {
|
|
318
|
+
return new Response("Customer ID is required", { status: 400 });
|
|
319
|
+
}
|
|
320
|
+
try {
|
|
321
|
+
yield betterStore.customer.delete(customerId);
|
|
322
|
+
return new Response(null, { status: 204 });
|
|
323
|
+
} catch (error) {
|
|
324
|
+
return new Response("Failed to delete customer", { status: 500 });
|
|
325
|
+
}
|
|
326
|
+
})
|
|
327
|
+
},
|
|
328
|
+
product: {
|
|
329
|
+
GET: (req, betterStore) => __async(void 0, null, function* () {
|
|
330
|
+
const { searchParams } = new URL(req.url);
|
|
331
|
+
const productId = searchParams.get("productId");
|
|
332
|
+
try {
|
|
333
|
+
if (productId) {
|
|
334
|
+
const product = yield betterStore.products.retrieve(productId);
|
|
335
|
+
return Response.json(product);
|
|
336
|
+
} else {
|
|
337
|
+
const products = yield betterStore.products.list();
|
|
338
|
+
return Response.json(products);
|
|
339
|
+
}
|
|
340
|
+
} catch (error) {
|
|
341
|
+
return new Response("Failed to fetch products", { status: 500 });
|
|
342
|
+
}
|
|
343
|
+
})
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
function addCORSHeaders(response, origin, allowedOrigins) {
|
|
347
|
+
if (origin && allowedOrigins.includes(origin)) {
|
|
348
|
+
response.headers.set("Access-Control-Allow-Origin", origin);
|
|
349
|
+
}
|
|
350
|
+
response.headers.set(
|
|
351
|
+
"Access-Control-Allow-Methods",
|
|
352
|
+
"GET, POST, PUT, DELETE, OPTIONS"
|
|
353
|
+
);
|
|
354
|
+
response.headers.set(
|
|
355
|
+
"Access-Control-Allow-Headers",
|
|
356
|
+
"Content-Type, Authorization"
|
|
357
|
+
);
|
|
358
|
+
return response;
|
|
359
|
+
}
|
|
360
|
+
function createNextJSHandler(betterStore, config = {}) {
|
|
361
|
+
const { apiKey, productionAllowedOrigins = [] } = config;
|
|
362
|
+
const isProduction = process.env.NODE_ENV === "production";
|
|
363
|
+
function validateRequest(req) {
|
|
364
|
+
return __async(this, null, function* () {
|
|
365
|
+
if (apiKey) {
|
|
366
|
+
const authHeader = req.headers.get("Authorization");
|
|
367
|
+
const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
|
|
368
|
+
if (!providedKey || providedKey !== apiKey) {
|
|
369
|
+
return new Response("Unauthorized", {
|
|
370
|
+
status: 401,
|
|
371
|
+
headers: { "WWW-Authenticate": "Bearer" }
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const origin = req.headers.get("origin");
|
|
376
|
+
if (isProduction && productionAllowedOrigins.length > 0) {
|
|
377
|
+
if (!origin || !productionAllowedOrigins.includes(origin)) {
|
|
378
|
+
return new Response("Unauthorized", { status: 403 });
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return null;
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
function getRouteFromPath(pathname) {
|
|
385
|
+
const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
|
|
386
|
+
const relevantPath = cleanPath.split("betterstore/")[1] || "";
|
|
387
|
+
return relevantPath;
|
|
388
|
+
}
|
|
203
389
|
return {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
390
|
+
GET(req) {
|
|
391
|
+
return __async(this, null, function* () {
|
|
392
|
+
var _a2;
|
|
393
|
+
const validationError = yield validateRequest(req);
|
|
394
|
+
if (validationError)
|
|
395
|
+
return addCORSHeaders(
|
|
396
|
+
validationError,
|
|
397
|
+
req.headers.get("origin"),
|
|
398
|
+
productionAllowedOrigins
|
|
399
|
+
);
|
|
400
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
401
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
|
|
402
|
+
if (!handler) {
|
|
403
|
+
return addCORSHeaders(
|
|
404
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
405
|
+
req.headers.get("origin"),
|
|
406
|
+
productionAllowedOrigins
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
const response = yield handler(req, betterStore);
|
|
410
|
+
return addCORSHeaders(
|
|
411
|
+
response,
|
|
412
|
+
req.headers.get("origin"),
|
|
413
|
+
productionAllowedOrigins
|
|
414
|
+
);
|
|
415
|
+
});
|
|
416
|
+
},
|
|
417
|
+
POST(req) {
|
|
418
|
+
return __async(this, null, function* () {
|
|
419
|
+
var _a2;
|
|
420
|
+
const validationError = yield validateRequest(req);
|
|
421
|
+
if (validationError)
|
|
422
|
+
return addCORSHeaders(
|
|
423
|
+
validationError,
|
|
424
|
+
req.headers.get("origin"),
|
|
425
|
+
productionAllowedOrigins
|
|
426
|
+
);
|
|
427
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
428
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
|
|
429
|
+
if (!handler) {
|
|
430
|
+
return addCORSHeaders(
|
|
431
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
432
|
+
req.headers.get("origin"),
|
|
433
|
+
productionAllowedOrigins
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
const response = yield handler(req, betterStore);
|
|
437
|
+
return addCORSHeaders(
|
|
438
|
+
response,
|
|
439
|
+
req.headers.get("origin"),
|
|
440
|
+
productionAllowedOrigins
|
|
441
|
+
);
|
|
442
|
+
});
|
|
443
|
+
},
|
|
444
|
+
PUT(req) {
|
|
445
|
+
return __async(this, null, function* () {
|
|
446
|
+
var _a2;
|
|
447
|
+
const validationError = yield validateRequest(req);
|
|
448
|
+
if (validationError)
|
|
449
|
+
return addCORSHeaders(
|
|
450
|
+
validationError,
|
|
451
|
+
req.headers.get("origin"),
|
|
452
|
+
productionAllowedOrigins
|
|
453
|
+
);
|
|
454
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
455
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
|
|
456
|
+
if (!handler) {
|
|
457
|
+
return addCORSHeaders(
|
|
458
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
459
|
+
req.headers.get("origin"),
|
|
460
|
+
productionAllowedOrigins
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
const response = yield handler(req, betterStore);
|
|
464
|
+
return addCORSHeaders(
|
|
465
|
+
response,
|
|
466
|
+
req.headers.get("origin"),
|
|
467
|
+
productionAllowedOrigins
|
|
468
|
+
);
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
DELETE(req) {
|
|
472
|
+
return __async(this, null, function* () {
|
|
473
|
+
var _a2;
|
|
474
|
+
const validationError = yield validateRequest(req);
|
|
475
|
+
if (validationError)
|
|
476
|
+
return addCORSHeaders(
|
|
477
|
+
validationError,
|
|
478
|
+
req.headers.get("origin"),
|
|
479
|
+
productionAllowedOrigins
|
|
480
|
+
);
|
|
481
|
+
const route = getRouteFromPath(new URL(req.url).pathname);
|
|
482
|
+
const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
|
|
483
|
+
if (!handler) {
|
|
484
|
+
return addCORSHeaders(
|
|
485
|
+
new Response(`Route not found: ${route}`, { status: 404 }),
|
|
486
|
+
req.headers.get("origin"),
|
|
487
|
+
productionAllowedOrigins
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
const response = yield handler(req, betterStore);
|
|
491
|
+
return addCORSHeaders(
|
|
492
|
+
response,
|
|
493
|
+
req.headers.get("origin"),
|
|
494
|
+
productionAllowedOrigins
|
|
495
|
+
);
|
|
496
|
+
});
|
|
497
|
+
}
|
|
207
498
|
};
|
|
208
499
|
}
|
|
209
500
|
|
|
@@ -221,6 +512,6 @@ var BetterStore = class {
|
|
|
221
512
|
};
|
|
222
513
|
var index_default = BetterStore;
|
|
223
514
|
export {
|
|
224
|
-
|
|
225
|
-
|
|
515
|
+
createNextJSHandler,
|
|
516
|
+
index_default as default
|
|
226
517
|
};
|