@betterstore/sdk 0.2.2 → 0.2.7

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,35 @@
1
1
  # @betterstore/sdk
2
2
 
3
+ ## 0.2.7
4
+
5
+ ### Patch Changes
6
+
7
+ - cicd test
8
+
9
+ ## 0.2.6
10
+
11
+ ### Patch Changes
12
+
13
+ - adding a changeset
14
+
15
+ ## 0.2.5
16
+
17
+ ### Patch Changes
18
+
19
+ - react helpers
20
+
21
+ ## 0.2.4
22
+
23
+ ### Patch Changes
24
+
25
+ - test cicd
26
+
27
+ ## 0.2.3
28
+
29
+ ### Patch Changes
30
+
31
+ - cors added to next.js handler
32
+
3
33
  ## 0.2.2
4
34
 
5
35
  ### 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
@@ -203,7 +203,14 @@ declare class Products {
203
203
  retrieve(productId: string): Promise<Product>;
204
204
  }
205
205
 
206
+ declare function getCheckoutEmbedProps(betterStore: BetterStore): {
207
+ retrieveCheckout: (idOrSecret: string) => Promise<CheckoutSession>;
208
+ updateCheckout: (checkoutId: string, params: CheckoutUpdateParams) => Promise<CheckoutSession>;
209
+ getShippingRates: (checkoutId: string) => Promise<ShippingRate[]>;
210
+ };
211
+
206
212
  type NextjsRouteConfig = {
213
+ apiKey?: string;
207
214
  productionAllowedOrigins?: string[];
208
215
  };
209
216
  type BSClient = InstanceType<typeof BetterStore>;
@@ -221,4 +228,4 @@ declare class BetterStore {
221
228
  constructor(apiKey: string);
222
229
  }
223
230
 
224
- export { createNextJSHandler, BetterStore as default };
231
+ export { createNextJSHandler, BetterStore as default, getCheckoutEmbedProps };
package/dist/index.d.ts CHANGED
@@ -203,7 +203,14 @@ declare class Products {
203
203
  retrieve(productId: string): Promise<Product>;
204
204
  }
205
205
 
206
+ declare function getCheckoutEmbedProps(betterStore: BetterStore): {
207
+ retrieveCheckout: (idOrSecret: string) => Promise<CheckoutSession>;
208
+ updateCheckout: (checkoutId: string, params: CheckoutUpdateParams) => Promise<CheckoutSession>;
209
+ getShippingRates: (checkoutId: string) => Promise<ShippingRate[]>;
210
+ };
211
+
206
212
  type NextjsRouteConfig = {
213
+ apiKey?: string;
207
214
  productionAllowedOrigins?: string[];
208
215
  };
209
216
  type BSClient = InstanceType<typeof BetterStore>;
@@ -221,4 +228,4 @@ declare class BetterStore {
221
228
  constructor(apiKey: string);
222
229
  }
223
230
 
224
- export { createNextJSHandler, BetterStore as default };
231
+ export { createNextJSHandler, BetterStore as default, getCheckoutEmbedProps };
package/dist/index.js CHANGED
@@ -51,7 +51,8 @@ var __async = (__this, __arguments, generator) => {
51
51
  var index_exports = {};
52
52
  __export(index_exports, {
53
53
  createNextJSHandler: () => createNextJSHandler,
54
- default: () => index_default
54
+ default: () => index_default,
55
+ getCheckoutEmbedProps: () => getCheckoutEmbedProps
55
56
  });
56
57
  module.exports = __toCommonJS(index_exports);
57
58
 
@@ -231,6 +232,15 @@ var Products = class {
231
232
  };
232
233
  var products_default = Products;
233
234
 
235
+ // src/helpers/react.ts
236
+ function getCheckoutEmbedProps(betterStore) {
237
+ return {
238
+ retrieveCheckout: betterStore.checkout.retrieve,
239
+ updateCheckout: betterStore.checkout.update,
240
+ getShippingRates: betterStore.checkout.getShippingRates
241
+ };
242
+ }
243
+
234
244
  // src/route-handelers/next-js.ts
235
245
  var defaultBetterStoreRoutes = {
236
246
  checkout: {
@@ -374,13 +384,37 @@ var defaultBetterStoreRoutes = {
374
384
  })
375
385
  }
376
386
  };
387
+ function addCORSHeaders(response, origin, allowedOrigins) {
388
+ if (origin && allowedOrigins.includes(origin)) {
389
+ response.headers.set("Access-Control-Allow-Origin", origin);
390
+ }
391
+ response.headers.set(
392
+ "Access-Control-Allow-Methods",
393
+ "GET, POST, PUT, DELETE, OPTIONS"
394
+ );
395
+ response.headers.set(
396
+ "Access-Control-Allow-Headers",
397
+ "Content-Type, Authorization"
398
+ );
399
+ return response;
400
+ }
377
401
  function createNextJSHandler(betterStore, config = {}) {
378
- const { productionAllowedOrigins = [] } = config;
402
+ const { apiKey, productionAllowedOrigins = [] } = config;
379
403
  const isProduction = process.env.NODE_ENV === "production";
380
404
  function validateRequest(req) {
381
405
  return __async(this, null, function* () {
406
+ if (apiKey) {
407
+ const authHeader = req.headers.get("Authorization");
408
+ const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
409
+ if (!providedKey || providedKey !== apiKey) {
410
+ return new Response("Unauthorized", {
411
+ status: 401,
412
+ headers: { "WWW-Authenticate": "Bearer" }
413
+ });
414
+ }
415
+ }
416
+ const origin = req.headers.get("origin");
382
417
  if (isProduction && productionAllowedOrigins.length > 0) {
383
- const origin = req.headers.get("origin");
384
418
  if (!origin || !productionAllowedOrigins.includes(origin)) {
385
419
  return new Response("Unauthorized", { status: 403 });
386
420
  }
@@ -398,52 +432,108 @@ function createNextJSHandler(betterStore, config = {}) {
398
432
  return __async(this, null, function* () {
399
433
  var _a2;
400
434
  const validationError = yield validateRequest(req);
401
- if (validationError) return validationError;
435
+ if (validationError)
436
+ return addCORSHeaders(
437
+ validationError,
438
+ req.headers.get("origin"),
439
+ productionAllowedOrigins
440
+ );
402
441
  const route = getRouteFromPath(new URL(req.url).pathname);
403
442
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
404
443
  if (!handler) {
405
- return new Response(`Route not found: ${route}`, { status: 404 });
444
+ return addCORSHeaders(
445
+ new Response(`Route not found: ${route}`, { status: 404 }),
446
+ req.headers.get("origin"),
447
+ productionAllowedOrigins
448
+ );
406
449
  }
407
- return handler(req, betterStore);
450
+ const response = yield handler(req, betterStore);
451
+ return addCORSHeaders(
452
+ response,
453
+ req.headers.get("origin"),
454
+ productionAllowedOrigins
455
+ );
408
456
  });
409
457
  },
410
458
  POST(req) {
411
459
  return __async(this, null, function* () {
412
460
  var _a2;
413
461
  const validationError = yield validateRequest(req);
414
- if (validationError) return validationError;
462
+ if (validationError)
463
+ return addCORSHeaders(
464
+ validationError,
465
+ req.headers.get("origin"),
466
+ productionAllowedOrigins
467
+ );
415
468
  const route = getRouteFromPath(new URL(req.url).pathname);
416
469
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
417
470
  if (!handler) {
418
- return new Response(`Route not found: ${route}`, { status: 404 });
471
+ return addCORSHeaders(
472
+ new Response(`Route not found: ${route}`, { status: 404 }),
473
+ req.headers.get("origin"),
474
+ productionAllowedOrigins
475
+ );
419
476
  }
420
- return handler(req, betterStore);
477
+ const response = yield handler(req, betterStore);
478
+ return addCORSHeaders(
479
+ response,
480
+ req.headers.get("origin"),
481
+ productionAllowedOrigins
482
+ );
421
483
  });
422
484
  },
423
485
  PUT(req) {
424
486
  return __async(this, null, function* () {
425
487
  var _a2;
426
488
  const validationError = yield validateRequest(req);
427
- if (validationError) return validationError;
489
+ if (validationError)
490
+ return addCORSHeaders(
491
+ validationError,
492
+ req.headers.get("origin"),
493
+ productionAllowedOrigins
494
+ );
428
495
  const route = getRouteFromPath(new URL(req.url).pathname);
429
496
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
430
497
  if (!handler) {
431
- return new Response(`Route not found: ${route}`, { status: 404 });
498
+ return addCORSHeaders(
499
+ new Response(`Route not found: ${route}`, { status: 404 }),
500
+ req.headers.get("origin"),
501
+ productionAllowedOrigins
502
+ );
432
503
  }
433
- return handler(req, betterStore);
504
+ const response = yield handler(req, betterStore);
505
+ return addCORSHeaders(
506
+ response,
507
+ req.headers.get("origin"),
508
+ productionAllowedOrigins
509
+ );
434
510
  });
435
511
  },
436
512
  DELETE(req) {
437
513
  return __async(this, null, function* () {
438
514
  var _a2;
439
515
  const validationError = yield validateRequest(req);
440
- if (validationError) return validationError;
516
+ if (validationError)
517
+ return addCORSHeaders(
518
+ validationError,
519
+ req.headers.get("origin"),
520
+ productionAllowedOrigins
521
+ );
441
522
  const route = getRouteFromPath(new URL(req.url).pathname);
442
523
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
443
524
  if (!handler) {
444
- return new Response(`Route not found: ${route}`, { status: 404 });
525
+ return addCORSHeaders(
526
+ new Response(`Route not found: ${route}`, { status: 404 }),
527
+ req.headers.get("origin"),
528
+ productionAllowedOrigins
529
+ );
445
530
  }
446
- return handler(req, betterStore);
531
+ const response = yield handler(req, betterStore);
532
+ return addCORSHeaders(
533
+ response,
534
+ req.headers.get("origin"),
535
+ productionAllowedOrigins
536
+ );
447
537
  });
448
538
  }
449
539
  };
@@ -464,5 +554,6 @@ var BetterStore = class {
464
554
  var index_default = BetterStore;
465
555
  // Annotate the CommonJS export names for ESM import in node:
466
556
  0 && (module.exports = {
467
- createNextJSHandler
557
+ createNextJSHandler,
558
+ getCheckoutEmbedProps
468
559
  });
package/dist/index.mjs CHANGED
@@ -195,6 +195,15 @@ var Products = class {
195
195
  };
196
196
  var products_default = Products;
197
197
 
198
+ // src/helpers/react.ts
199
+ function getCheckoutEmbedProps(betterStore) {
200
+ return {
201
+ retrieveCheckout: betterStore.checkout.retrieve,
202
+ updateCheckout: betterStore.checkout.update,
203
+ getShippingRates: betterStore.checkout.getShippingRates
204
+ };
205
+ }
206
+
198
207
  // src/route-handelers/next-js.ts
199
208
  var defaultBetterStoreRoutes = {
200
209
  checkout: {
@@ -338,13 +347,37 @@ var defaultBetterStoreRoutes = {
338
347
  })
339
348
  }
340
349
  };
350
+ function addCORSHeaders(response, origin, allowedOrigins) {
351
+ if (origin && allowedOrigins.includes(origin)) {
352
+ response.headers.set("Access-Control-Allow-Origin", origin);
353
+ }
354
+ response.headers.set(
355
+ "Access-Control-Allow-Methods",
356
+ "GET, POST, PUT, DELETE, OPTIONS"
357
+ );
358
+ response.headers.set(
359
+ "Access-Control-Allow-Headers",
360
+ "Content-Type, Authorization"
361
+ );
362
+ return response;
363
+ }
341
364
  function createNextJSHandler(betterStore, config = {}) {
342
- const { productionAllowedOrigins = [] } = config;
365
+ const { apiKey, productionAllowedOrigins = [] } = config;
343
366
  const isProduction = process.env.NODE_ENV === "production";
344
367
  function validateRequest(req) {
345
368
  return __async(this, null, function* () {
369
+ if (apiKey) {
370
+ const authHeader = req.headers.get("Authorization");
371
+ const providedKey = authHeader == null ? void 0 : authHeader.replace("Bearer ", "");
372
+ if (!providedKey || providedKey !== apiKey) {
373
+ return new Response("Unauthorized", {
374
+ status: 401,
375
+ headers: { "WWW-Authenticate": "Bearer" }
376
+ });
377
+ }
378
+ }
379
+ const origin = req.headers.get("origin");
346
380
  if (isProduction && productionAllowedOrigins.length > 0) {
347
- const origin = req.headers.get("origin");
348
381
  if (!origin || !productionAllowedOrigins.includes(origin)) {
349
382
  return new Response("Unauthorized", { status: 403 });
350
383
  }
@@ -362,52 +395,108 @@ function createNextJSHandler(betterStore, config = {}) {
362
395
  return __async(this, null, function* () {
363
396
  var _a2;
364
397
  const validationError = yield validateRequest(req);
365
- if (validationError) return validationError;
398
+ if (validationError)
399
+ return addCORSHeaders(
400
+ validationError,
401
+ req.headers.get("origin"),
402
+ productionAllowedOrigins
403
+ );
366
404
  const route = getRouteFromPath(new URL(req.url).pathname);
367
405
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.GET;
368
406
  if (!handler) {
369
- return new Response(`Route not found: ${route}`, { status: 404 });
407
+ return addCORSHeaders(
408
+ new Response(`Route not found: ${route}`, { status: 404 }),
409
+ req.headers.get("origin"),
410
+ productionAllowedOrigins
411
+ );
370
412
  }
371
- return handler(req, betterStore);
413
+ const response = yield handler(req, betterStore);
414
+ return addCORSHeaders(
415
+ response,
416
+ req.headers.get("origin"),
417
+ productionAllowedOrigins
418
+ );
372
419
  });
373
420
  },
374
421
  POST(req) {
375
422
  return __async(this, null, function* () {
376
423
  var _a2;
377
424
  const validationError = yield validateRequest(req);
378
- if (validationError) return validationError;
425
+ if (validationError)
426
+ return addCORSHeaders(
427
+ validationError,
428
+ req.headers.get("origin"),
429
+ productionAllowedOrigins
430
+ );
379
431
  const route = getRouteFromPath(new URL(req.url).pathname);
380
432
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.POST;
381
433
  if (!handler) {
382
- return new Response(`Route not found: ${route}`, { status: 404 });
434
+ return addCORSHeaders(
435
+ new Response(`Route not found: ${route}`, { status: 404 }),
436
+ req.headers.get("origin"),
437
+ productionAllowedOrigins
438
+ );
383
439
  }
384
- return handler(req, betterStore);
440
+ const response = yield handler(req, betterStore);
441
+ return addCORSHeaders(
442
+ response,
443
+ req.headers.get("origin"),
444
+ productionAllowedOrigins
445
+ );
385
446
  });
386
447
  },
387
448
  PUT(req) {
388
449
  return __async(this, null, function* () {
389
450
  var _a2;
390
451
  const validationError = yield validateRequest(req);
391
- if (validationError) return validationError;
452
+ if (validationError)
453
+ return addCORSHeaders(
454
+ validationError,
455
+ req.headers.get("origin"),
456
+ productionAllowedOrigins
457
+ );
392
458
  const route = getRouteFromPath(new URL(req.url).pathname);
393
459
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.PUT;
394
460
  if (!handler) {
395
- return new Response(`Route not found: ${route}`, { status: 404 });
461
+ return addCORSHeaders(
462
+ new Response(`Route not found: ${route}`, { status: 404 }),
463
+ req.headers.get("origin"),
464
+ productionAllowedOrigins
465
+ );
396
466
  }
397
- return handler(req, betterStore);
467
+ const response = yield handler(req, betterStore);
468
+ return addCORSHeaders(
469
+ response,
470
+ req.headers.get("origin"),
471
+ productionAllowedOrigins
472
+ );
398
473
  });
399
474
  },
400
475
  DELETE(req) {
401
476
  return __async(this, null, function* () {
402
477
  var _a2;
403
478
  const validationError = yield validateRequest(req);
404
- if (validationError) return validationError;
479
+ if (validationError)
480
+ return addCORSHeaders(
481
+ validationError,
482
+ req.headers.get("origin"),
483
+ productionAllowedOrigins
484
+ );
405
485
  const route = getRouteFromPath(new URL(req.url).pathname);
406
486
  const handler = (_a2 = defaultBetterStoreRoutes[route]) == null ? void 0 : _a2.DELETE;
407
487
  if (!handler) {
408
- return new Response(`Route not found: ${route}`, { status: 404 });
488
+ return addCORSHeaders(
489
+ new Response(`Route not found: ${route}`, { status: 404 }),
490
+ req.headers.get("origin"),
491
+ productionAllowedOrigins
492
+ );
409
493
  }
410
- return handler(req, betterStore);
494
+ const response = yield handler(req, betterStore);
495
+ return addCORSHeaders(
496
+ response,
497
+ req.headers.get("origin"),
498
+ productionAllowedOrigins
499
+ );
411
500
  });
412
501
  }
413
502
  };
@@ -428,5 +517,6 @@ var BetterStore = class {
428
517
  var index_default = BetterStore;
429
518
  export {
430
519
  createNextJSHandler,
431
- index_default as default
520
+ index_default as default,
521
+ getCheckoutEmbedProps
432
522
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.7",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -26,9 +26,10 @@
26
26
  "typescript": "^5.8.2"
27
27
  },
28
28
  "dependencies": {
29
- "axios": "^1.8.1"
29
+ "axios": "^1.8.2"
30
30
  },
31
31
  "scripts": {
32
+ "prepublish": "changeset && changeset version && git add .",
32
33
  "build": "tsup src/index.ts --format cjs,esm --dts",
33
34
  "lint": "tsc",
34
35
  "format:check": "prettier --check --ignore-path .prettierignore .",