@betterstore/sdk 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.prettierignore CHANGED
@@ -1,3 +1,3 @@
1
- node_modules
2
- dist
1
+ node_modules
2
+ dist
3
3
  pnpm-lock.yaml
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @betterstore/sdk
2
2
 
3
+ ## 0.2.3
4
+
5
+ ### Patch Changes
6
+
7
+ - cors added to next.js handler
8
+
9
+ ## 0.2.2
10
+
11
+ ### Patch Changes
12
+
13
+ - route handlers added
14
+
3
15
  ## 0.2.1
4
16
 
5
17
  ### Patch Changes
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 Better Store
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Better Store
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
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,6 +203,18 @@ declare class Products {
201
203
  retrieve(productId: string): Promise<Product>;
202
204
  }
203
205
 
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>;
216
+ };
217
+
204
218
  declare class BetterStore {
205
219
  checkout: Checkout;
206
220
  products: Products;
@@ -208,4 +222,4 @@ declare class BetterStore {
208
222
  constructor(apiKey: string);
209
223
  }
210
224
 
211
- 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,6 +203,18 @@ declare class Products {
201
203
  retrieve(productId: string): Promise<Product>;
202
204
  }
203
205
 
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>;
216
+ };
217
+
204
218
  declare class BetterStore {
205
219
  checkout: Checkout;
206
220
  products: Products;
@@ -208,4 +222,4 @@ declare class BetterStore {
208
222
  constructor(apiKey: string);
209
223
  }
210
224
 
211
- export { BetterStore as default };
225
+ export { createNextJSHandler, BetterStore as default };
package/dist/index.js CHANGED
@@ -50,6 +50,7 @@ var __async = (__this, __arguments, generator) => {
50
50
  // src/index.ts
51
51
  var index_exports = {};
52
52
  __export(index_exports, {
53
+ createNextJSHandler: () => createNextJSHandler,
53
54
  default: () => index_default
54
55
  });
55
56
  module.exports = __toCommonJS(index_exports);
@@ -230,6 +231,304 @@ var Products = class {
230
231
  };
231
232
  var products_default = Products;
232
233
 
234
+ // src/route-handelers/next-js.ts
235
+ var defaultBetterStoreRoutes = {
236
+ checkout: {
237
+ GET: (req, betterStore) => __async(void 0, null, function* () {
238
+ const { searchParams } = new URL(req.url);
239
+ const checkoutId = searchParams.get("checkoutId");
240
+ if (!checkoutId) {
241
+ return new Response("Checkout ID is required", { status: 400 });
242
+ }
243
+ try {
244
+ const checkout = yield betterStore.checkout.retrieve(checkoutId);
245
+ return Response.json(checkout);
246
+ } catch (error) {
247
+ return new Response("Failed to fetch checkout", { status: 500 });
248
+ }
249
+ }),
250
+ POST: (req, betterStore) => __async(void 0, null, function* () {
251
+ try {
252
+ const body = yield req.json();
253
+ const checkout = yield betterStore.checkout.create(body);
254
+ return Response.json(checkout);
255
+ } catch (error) {
256
+ return new Response("Failed to create checkout", { status: 500 });
257
+ }
258
+ }),
259
+ PUT: (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 body = yield req.json();
267
+ const checkout = yield betterStore.checkout.update(checkoutId, body);
268
+ return Response.json(checkout);
269
+ } catch (error) {
270
+ return new Response("Failed to update checkout", { status: 500 });
271
+ }
272
+ })
273
+ },
274
+ "checkout/shipping": {
275
+ GET: (req, betterStore) => __async(void 0, null, function* () {
276
+ const { searchParams } = new URL(req.url);
277
+ const checkoutId = searchParams.get("checkoutId");
278
+ if (!checkoutId) {
279
+ return new Response("Checkout ID is required", { status: 400 });
280
+ }
281
+ try {
282
+ const rates = yield betterStore.checkout.getShippingRates(checkoutId);
283
+ return Response.json(rates);
284
+ } catch (error) {
285
+ return new Response("Failed to get shipping rates", { status: 500 });
286
+ }
287
+ })
288
+ },
289
+ "checkout/payment": {
290
+ POST: (req, betterStore) => __async(void 0, null, function* () {
291
+ const { searchParams } = new URL(req.url);
292
+ const checkoutId = searchParams.get("checkoutId");
293
+ if (!checkoutId) {
294
+ return new Response("Checkout ID is required", { status: 400 });
295
+ }
296
+ try {
297
+ const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
298
+ return Response.json({ clientSecret: secret });
299
+ } catch (error) {
300
+ return new Response("Failed to generate payment secret", {
301
+ status: 500
302
+ });
303
+ }
304
+ })
305
+ },
306
+ customer: {
307
+ GET: (req, betterStore) => __async(void 0, null, function* () {
308
+ const { searchParams } = new URL(req.url);
309
+ const idOrEmail = searchParams.get("idOrEmail");
310
+ if (!idOrEmail) {
311
+ return new Response("Customer ID or email is required", {
312
+ status: 400
313
+ });
314
+ }
315
+ try {
316
+ const customer = yield betterStore.customer.retrieve(idOrEmail);
317
+ return Response.json(customer);
318
+ } catch (error) {
319
+ return new Response("Failed to fetch customer", { status: 500 });
320
+ }
321
+ }),
322
+ POST: (req, betterStore) => __async(void 0, null, function* () {
323
+ try {
324
+ const body = yield req.json();
325
+ const customer = yield betterStore.customer.create(body);
326
+ return Response.json(customer);
327
+ } catch (error) {
328
+ return new Response("Failed to create customer", { status: 500 });
329
+ }
330
+ }),
331
+ PUT: (req, betterStore) => __async(void 0, null, function* () {
332
+ const { searchParams } = new URL(req.url);
333
+ const customerId = searchParams.get("customerId");
334
+ if (!customerId) {
335
+ return new Response("Customer ID is required", { status: 400 });
336
+ }
337
+ try {
338
+ const body = yield req.json();
339
+ const customer = yield betterStore.customer.update(customerId, body);
340
+ return Response.json(customer);
341
+ } catch (error) {
342
+ return new Response("Failed to update customer", { status: 500 });
343
+ }
344
+ }),
345
+ DELETE: (req, betterStore) => __async(void 0, null, function* () {
346
+ const { searchParams } = new URL(req.url);
347
+ const customerId = searchParams.get("customerId");
348
+ if (!customerId) {
349
+ return new Response("Customer ID is required", { status: 400 });
350
+ }
351
+ try {
352
+ yield betterStore.customer.delete(customerId);
353
+ return new Response(null, { status: 204 });
354
+ } catch (error) {
355
+ return new Response("Failed to delete customer", { status: 500 });
356
+ }
357
+ })
358
+ },
359
+ product: {
360
+ GET: (req, betterStore) => __async(void 0, null, function* () {
361
+ const { searchParams } = new URL(req.url);
362
+ const productId = searchParams.get("productId");
363
+ try {
364
+ if (productId) {
365
+ const product = yield betterStore.products.retrieve(productId);
366
+ return Response.json(product);
367
+ } else {
368
+ const products = yield betterStore.products.list();
369
+ return Response.json(products);
370
+ }
371
+ } catch (error) {
372
+ return new Response("Failed to fetch products", { status: 500 });
373
+ }
374
+ })
375
+ }
376
+ };
377
+ function addCORSHeaders(response, origin, allowedOrigins) {
378
+ if (origin && allowedOrigins.includes(origin)) {
379
+ response.headers.set("Access-Control-Allow-Origin", origin);
380
+ }
381
+ response.headers.set(
382
+ "Access-Control-Allow-Methods",
383
+ "GET, POST, PUT, DELETE, OPTIONS"
384
+ );
385
+ response.headers.set(
386
+ "Access-Control-Allow-Headers",
387
+ "Content-Type, Authorization"
388
+ );
389
+ return response;
390
+ }
391
+ function createNextJSHandler(betterStore, config = {}) {
392
+ const { apiKey, productionAllowedOrigins = [] } = config;
393
+ const isProduction = process.env.NODE_ENV === "production";
394
+ function validateRequest(req) {
395
+ return __async(this, null, function* () {
396
+ if (apiKey) {
397
+ const authHeader = req.headers.get("Authorization");
398
+ const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
399
+ if (!providedKey || providedKey !== apiKey) {
400
+ return new Response("Unauthorized", {
401
+ status: 401,
402
+ headers: { "WWW-Authenticate": "Bearer" }
403
+ });
404
+ }
405
+ }
406
+ const origin = req.headers.get("origin");
407
+ if (isProduction && productionAllowedOrigins.length > 0) {
408
+ if (!origin || !productionAllowedOrigins.includes(origin)) {
409
+ return new Response("Unauthorized", { status: 403 });
410
+ }
411
+ }
412
+ return null;
413
+ });
414
+ }
415
+ function getRouteFromPath(pathname) {
416
+ const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
417
+ const relevantPath = cleanPath.split("betterstore/")[1] || "";
418
+ return relevantPath;
419
+ }
420
+ return {
421
+ GET(req) {
422
+ return __async(this, null, function* () {
423
+ var _a2;
424
+ const validationError = yield validateRequest(req);
425
+ if (validationError)
426
+ return addCORSHeaders(
427
+ validationError,
428
+ req.headers.get("origin"),
429
+ productionAllowedOrigins
430
+ );
431
+ const route = getRouteFromPath(new URL(req.url).pathname);
432
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
433
+ if (!handler) {
434
+ return addCORSHeaders(
435
+ new Response(`Route not found: ${route}`, { status: 404 }),
436
+ req.headers.get("origin"),
437
+ productionAllowedOrigins
438
+ );
439
+ }
440
+ const response = yield handler(req, betterStore);
441
+ return addCORSHeaders(
442
+ response,
443
+ req.headers.get("origin"),
444
+ productionAllowedOrigins
445
+ );
446
+ });
447
+ },
448
+ POST(req) {
449
+ return __async(this, null, function* () {
450
+ var _a2;
451
+ const validationError = yield validateRequest(req);
452
+ if (validationError)
453
+ return addCORSHeaders(
454
+ validationError,
455
+ req.headers.get("origin"),
456
+ productionAllowedOrigins
457
+ );
458
+ const route = getRouteFromPath(new URL(req.url).pathname);
459
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
460
+ if (!handler) {
461
+ return addCORSHeaders(
462
+ new Response(`Route not found: ${route}`, { status: 404 }),
463
+ req.headers.get("origin"),
464
+ productionAllowedOrigins
465
+ );
466
+ }
467
+ const response = yield handler(req, betterStore);
468
+ return addCORSHeaders(
469
+ response,
470
+ req.headers.get("origin"),
471
+ productionAllowedOrigins
472
+ );
473
+ });
474
+ },
475
+ PUT(req) {
476
+ return __async(this, null, function* () {
477
+ var _a2;
478
+ const validationError = yield validateRequest(req);
479
+ if (validationError)
480
+ return addCORSHeaders(
481
+ validationError,
482
+ req.headers.get("origin"),
483
+ productionAllowedOrigins
484
+ );
485
+ const route = getRouteFromPath(new URL(req.url).pathname);
486
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
487
+ if (!handler) {
488
+ return addCORSHeaders(
489
+ new Response(`Route not found: ${route}`, { status: 404 }),
490
+ req.headers.get("origin"),
491
+ productionAllowedOrigins
492
+ );
493
+ }
494
+ const response = yield handler(req, betterStore);
495
+ return addCORSHeaders(
496
+ response,
497
+ req.headers.get("origin"),
498
+ productionAllowedOrigins
499
+ );
500
+ });
501
+ },
502
+ DELETE(req) {
503
+ return __async(this, null, function* () {
504
+ var _a2;
505
+ const validationError = yield validateRequest(req);
506
+ if (validationError)
507
+ return addCORSHeaders(
508
+ validationError,
509
+ req.headers.get("origin"),
510
+ productionAllowedOrigins
511
+ );
512
+ const route = getRouteFromPath(new URL(req.url).pathname);
513
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
514
+ if (!handler) {
515
+ return addCORSHeaders(
516
+ new Response(`Route not found: ${route}`, { status: 404 }),
517
+ req.headers.get("origin"),
518
+ productionAllowedOrigins
519
+ );
520
+ }
521
+ const response = yield handler(req, betterStore);
522
+ return addCORSHeaders(
523
+ response,
524
+ req.headers.get("origin"),
525
+ productionAllowedOrigins
526
+ );
527
+ });
528
+ }
529
+ };
530
+ }
531
+
233
532
  // src/index.ts
234
533
  var BetterStore = class {
235
534
  // private apiKey: string;
@@ -243,3 +542,7 @@ var BetterStore = class {
243
542
  }
244
543
  };
245
544
  var index_default = BetterStore;
545
+ // Annotate the CommonJS export names for ESM import in node:
546
+ 0 && (module.exports = {
547
+ createNextJSHandler
548
+ });
package/dist/index.mjs CHANGED
@@ -195,6 +195,304 @@ var Products = class {
195
195
  };
196
196
  var products_default = Products;
197
197
 
198
+ // src/route-handelers/next-js.ts
199
+ var defaultBetterStoreRoutes = {
200
+ checkout: {
201
+ GET: (req, betterStore) => __async(void 0, null, function* () {
202
+ const { searchParams } = new URL(req.url);
203
+ const checkoutId = searchParams.get("checkoutId");
204
+ if (!checkoutId) {
205
+ return new Response("Checkout ID is required", { status: 400 });
206
+ }
207
+ try {
208
+ const checkout = yield betterStore.checkout.retrieve(checkoutId);
209
+ return Response.json(checkout);
210
+ } catch (error) {
211
+ return new Response("Failed to fetch checkout", { status: 500 });
212
+ }
213
+ }),
214
+ POST: (req, betterStore) => __async(void 0, null, function* () {
215
+ try {
216
+ const body = yield req.json();
217
+ const checkout = yield betterStore.checkout.create(body);
218
+ return Response.json(checkout);
219
+ } catch (error) {
220
+ return new Response("Failed to create checkout", { status: 500 });
221
+ }
222
+ }),
223
+ PUT: (req, betterStore) => __async(void 0, null, function* () {
224
+ const { searchParams } = new URL(req.url);
225
+ const checkoutId = searchParams.get("checkoutId");
226
+ if (!checkoutId) {
227
+ return new Response("Checkout ID is required", { status: 400 });
228
+ }
229
+ try {
230
+ const body = yield req.json();
231
+ const checkout = yield betterStore.checkout.update(checkoutId, body);
232
+ return Response.json(checkout);
233
+ } catch (error) {
234
+ return new Response("Failed to update checkout", { status: 500 });
235
+ }
236
+ })
237
+ },
238
+ "checkout/shipping": {
239
+ GET: (req, betterStore) => __async(void 0, null, function* () {
240
+ const { searchParams } = new URL(req.url);
241
+ const checkoutId = searchParams.get("checkoutId");
242
+ if (!checkoutId) {
243
+ return new Response("Checkout ID is required", { status: 400 });
244
+ }
245
+ try {
246
+ const rates = yield betterStore.checkout.getShippingRates(checkoutId);
247
+ return Response.json(rates);
248
+ } catch (error) {
249
+ return new Response("Failed to get shipping rates", { status: 500 });
250
+ }
251
+ })
252
+ },
253
+ "checkout/payment": {
254
+ POST: (req, betterStore) => __async(void 0, null, function* () {
255
+ const { searchParams } = new URL(req.url);
256
+ const checkoutId = searchParams.get("checkoutId");
257
+ if (!checkoutId) {
258
+ return new Response("Checkout ID is required", { status: 400 });
259
+ }
260
+ try {
261
+ const secret = yield betterStore.checkout.generatePaymentSecret(checkoutId);
262
+ return Response.json({ clientSecret: secret });
263
+ } catch (error) {
264
+ return new Response("Failed to generate payment secret", {
265
+ status: 500
266
+ });
267
+ }
268
+ })
269
+ },
270
+ customer: {
271
+ GET: (req, betterStore) => __async(void 0, null, function* () {
272
+ const { searchParams } = new URL(req.url);
273
+ const idOrEmail = searchParams.get("idOrEmail");
274
+ if (!idOrEmail) {
275
+ return new Response("Customer ID or email is required", {
276
+ status: 400
277
+ });
278
+ }
279
+ try {
280
+ const customer = yield betterStore.customer.retrieve(idOrEmail);
281
+ return Response.json(customer);
282
+ } catch (error) {
283
+ return new Response("Failed to fetch customer", { status: 500 });
284
+ }
285
+ }),
286
+ POST: (req, betterStore) => __async(void 0, null, function* () {
287
+ try {
288
+ const body = yield req.json();
289
+ const customer = yield betterStore.customer.create(body);
290
+ return Response.json(customer);
291
+ } catch (error) {
292
+ return new Response("Failed to create customer", { status: 500 });
293
+ }
294
+ }),
295
+ PUT: (req, betterStore) => __async(void 0, null, function* () {
296
+ const { searchParams } = new URL(req.url);
297
+ const customerId = searchParams.get("customerId");
298
+ if (!customerId) {
299
+ return new Response("Customer ID is required", { status: 400 });
300
+ }
301
+ try {
302
+ const body = yield req.json();
303
+ const customer = yield betterStore.customer.update(customerId, body);
304
+ return Response.json(customer);
305
+ } catch (error) {
306
+ return new Response("Failed to update customer", { status: 500 });
307
+ }
308
+ }),
309
+ DELETE: (req, betterStore) => __async(void 0, null, function* () {
310
+ const { searchParams } = new URL(req.url);
311
+ const customerId = searchParams.get("customerId");
312
+ if (!customerId) {
313
+ return new Response("Customer ID is required", { status: 400 });
314
+ }
315
+ try {
316
+ yield betterStore.customer.delete(customerId);
317
+ return new Response(null, { status: 204 });
318
+ } catch (error) {
319
+ return new Response("Failed to delete customer", { status: 500 });
320
+ }
321
+ })
322
+ },
323
+ product: {
324
+ GET: (req, betterStore) => __async(void 0, null, function* () {
325
+ const { searchParams } = new URL(req.url);
326
+ const productId = searchParams.get("productId");
327
+ try {
328
+ if (productId) {
329
+ const product = yield betterStore.products.retrieve(productId);
330
+ return Response.json(product);
331
+ } else {
332
+ const products = yield betterStore.products.list();
333
+ return Response.json(products);
334
+ }
335
+ } catch (error) {
336
+ return new Response("Failed to fetch products", { status: 500 });
337
+ }
338
+ })
339
+ }
340
+ };
341
+ function addCORSHeaders(response, origin, allowedOrigins) {
342
+ if (origin && allowedOrigins.includes(origin)) {
343
+ response.headers.set("Access-Control-Allow-Origin", origin);
344
+ }
345
+ response.headers.set(
346
+ "Access-Control-Allow-Methods",
347
+ "GET, POST, PUT, DELETE, OPTIONS"
348
+ );
349
+ response.headers.set(
350
+ "Access-Control-Allow-Headers",
351
+ "Content-Type, Authorization"
352
+ );
353
+ return response;
354
+ }
355
+ function createNextJSHandler(betterStore, config = {}) {
356
+ const { apiKey, productionAllowedOrigins = [] } = config;
357
+ const isProduction = process.env.NODE_ENV === "production";
358
+ function validateRequest(req) {
359
+ return __async(this, null, function* () {
360
+ if (apiKey) {
361
+ const authHeader = req.headers.get("Authorization");
362
+ const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
363
+ if (!providedKey || providedKey !== apiKey) {
364
+ return new Response("Unauthorized", {
365
+ status: 401,
366
+ headers: { "WWW-Authenticate": "Bearer" }
367
+ });
368
+ }
369
+ }
370
+ const origin = req.headers.get("origin");
371
+ if (isProduction && productionAllowedOrigins.length > 0) {
372
+ if (!origin || !productionAllowedOrigins.includes(origin)) {
373
+ return new Response("Unauthorized", { status: 403 });
374
+ }
375
+ }
376
+ return null;
377
+ });
378
+ }
379
+ function getRouteFromPath(pathname) {
380
+ const cleanPath = pathname.replace(/^\/|\/$/g, "").replace(/^api\//, "");
381
+ const relevantPath = cleanPath.split("betterstore/")[1] || "";
382
+ return relevantPath;
383
+ }
384
+ return {
385
+ GET(req) {
386
+ return __async(this, null, function* () {
387
+ var _a2;
388
+ const validationError = yield validateRequest(req);
389
+ if (validationError)
390
+ return addCORSHeaders(
391
+ validationError,
392
+ req.headers.get("origin"),
393
+ productionAllowedOrigins
394
+ );
395
+ const route = getRouteFromPath(new URL(req.url).pathname);
396
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
397
+ if (!handler) {
398
+ return addCORSHeaders(
399
+ new Response(`Route not found: ${route}`, { status: 404 }),
400
+ req.headers.get("origin"),
401
+ productionAllowedOrigins
402
+ );
403
+ }
404
+ const response = yield handler(req, betterStore);
405
+ return addCORSHeaders(
406
+ response,
407
+ req.headers.get("origin"),
408
+ productionAllowedOrigins
409
+ );
410
+ });
411
+ },
412
+ POST(req) {
413
+ return __async(this, null, function* () {
414
+ var _a2;
415
+ const validationError = yield validateRequest(req);
416
+ if (validationError)
417
+ return addCORSHeaders(
418
+ validationError,
419
+ req.headers.get("origin"),
420
+ productionAllowedOrigins
421
+ );
422
+ const route = getRouteFromPath(new URL(req.url).pathname);
423
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
424
+ if (!handler) {
425
+ return addCORSHeaders(
426
+ new Response(`Route not found: ${route}`, { status: 404 }),
427
+ req.headers.get("origin"),
428
+ productionAllowedOrigins
429
+ );
430
+ }
431
+ const response = yield handler(req, betterStore);
432
+ return addCORSHeaders(
433
+ response,
434
+ req.headers.get("origin"),
435
+ productionAllowedOrigins
436
+ );
437
+ });
438
+ },
439
+ PUT(req) {
440
+ return __async(this, null, function* () {
441
+ var _a2;
442
+ const validationError = yield validateRequest(req);
443
+ if (validationError)
444
+ return addCORSHeaders(
445
+ validationError,
446
+ req.headers.get("origin"),
447
+ productionAllowedOrigins
448
+ );
449
+ const route = getRouteFromPath(new URL(req.url).pathname);
450
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
451
+ if (!handler) {
452
+ return addCORSHeaders(
453
+ new Response(`Route not found: ${route}`, { status: 404 }),
454
+ req.headers.get("origin"),
455
+ productionAllowedOrigins
456
+ );
457
+ }
458
+ const response = yield handler(req, betterStore);
459
+ return addCORSHeaders(
460
+ response,
461
+ req.headers.get("origin"),
462
+ productionAllowedOrigins
463
+ );
464
+ });
465
+ },
466
+ DELETE(req) {
467
+ return __async(this, null, function* () {
468
+ var _a2;
469
+ const validationError = yield validateRequest(req);
470
+ if (validationError)
471
+ return addCORSHeaders(
472
+ validationError,
473
+ req.headers.get("origin"),
474
+ productionAllowedOrigins
475
+ );
476
+ const route = getRouteFromPath(new URL(req.url).pathname);
477
+ const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
478
+ if (!handler) {
479
+ return addCORSHeaders(
480
+ new Response(`Route not found: ${route}`, { status: 404 }),
481
+ req.headers.get("origin"),
482
+ productionAllowedOrigins
483
+ );
484
+ }
485
+ const response = yield handler(req, betterStore);
486
+ return addCORSHeaders(
487
+ response,
488
+ req.headers.get("origin"),
489
+ productionAllowedOrigins
490
+ );
491
+ });
492
+ }
493
+ };
494
+ }
495
+
198
496
  // src/index.ts
199
497
  var BetterStore = class {
200
498
  // private apiKey: string;
@@ -209,5 +507,6 @@ var BetterStore = class {
209
507
  };
210
508
  var index_default = BetterStore;
211
509
  export {
510
+ createNextJSHandler,
212
511
  index_default as default
213
512
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -9,6 +9,15 @@
9
9
  "main": "dist/index.js",
10
10
  "module": "dist/index.mjs",
11
11
  "types": "dist/index.d.ts",
12
+ "scripts": {
13
+ "prepublish": "changeset && changeset version && git add .",
14
+ "build": "tsup src/index.ts --format cjs,esm --dts",
15
+ "lint": "tsc",
16
+ "format:check": "prettier --check --ignore-path .prettierignore .",
17
+ "format": "prettier --write --ignore-path .prettierignore .",
18
+ "ci": "pnpm run lint && pnpm run format:check && pnpm run build",
19
+ "release": "pnpm run ci && changeset publish"
20
+ },
12
21
  "keywords": [
13
22
  "betterstore",
14
23
  "ecommerce",
@@ -20,19 +29,12 @@
20
29
  "devDependencies": {
21
30
  "@changesets/cli": "^2.28.1",
22
31
  "@types/axios": "^0.14.4",
32
+ "next": "^15.2.1",
23
33
  "prettier": "^3.5.3",
24
34
  "tsup": "^8.4.0",
25
35
  "typescript": "^5.8.2"
26
36
  },
27
37
  "dependencies": {
28
- "axios": "^1.8.1"
29
- },
30
- "scripts": {
31
- "build": "tsup src/index.ts --format cjs,esm --dts",
32
- "lint": "tsc",
33
- "format:check": "prettier --check --ignore-path .prettierignore .",
34
- "format": "prettier --write --ignore-path .prettierignore .",
35
- "ci": "pnpm run lint && pnpm run format:check && pnpm run build",
36
- "release": "pnpm run ci && changeset publish"
38
+ "axios": "^1.8.2"
37
39
  }
38
- }
40
+ }