@betterstore/sdk 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @betterstore/sdk
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 111a656: sdk items added, cart comming soon
8
+
9
+ ## 0.1.1
10
+
11
+ ### Patch Changes
12
+
13
+ - readme changed, checkout types changed
14
+
3
15
  ## 0.1.0
4
16
 
5
17
  ### Minor Changes
package/README.md CHANGED
@@ -1,38 +1,29 @@
1
1
  # Better Store SDK
2
2
 
3
- 🚀 **Better Store SDK** is a modern, developer-friendly eCommerce toolkit designed to help developers build flexible and powerful online stores with ease.
4
-
5
- ## ✨ Features
6
-
7
- - 🛒 **Custom Checkout** – Build checkout flows that suit your business needs.
8
- - 💳 **Payments** – Integrate with Stripe, PayPal, and custom gateways.
9
- - 📦 **Product & Order Management** – API-driven store management.
10
- - 🎨 **Customizable UI** – Prebuilt components and themes.
11
- - 📡 **Webhooks & API Events** – Extend and automate eCommerce operations.
12
- - 🚀 **Optimized for DX** – Built with TypeScript and fully documented.
3
+ 🚀 **Better Store SDK** is a modern, developer-friendly sdk toolkit designed to help developers build flexible and powerful e-commerce stores with ease.
13
4
 
14
5
  ## 📦 Installation
15
6
 
16
7
  ```sh
17
- npm install better-store-sdk
8
+ npm install @betterstore/sdk
18
9
  ```
19
10
 
20
11
  ## 🚀 Quick Start
21
12
 
22
13
  ```javascript
23
- import { BetterStore } from "better-store-sdk";
14
+ import { BetterStore } from "@betterstore/sdk";
24
15
 
25
- const store = new BetterStore({ apiKey: "YOUR_API_KEY" });
16
+ const betterStore = new BetterStore("YOUR_API_KEY");
26
17
 
27
- store.checkout.start({
28
- cart: [{ id: "prod_1", name: "T-Shirt", price: 20 }],
29
- currency: "USD",
18
+ betterStore.checkout.create({
19
+ type: "hosted",
20
+ lineItems: [{ productId: "example_id", quantity: 1 }],
30
21
  });
31
22
  ```
32
23
 
33
24
  ## 📚 Documentation
34
25
 
35
- Full documentation is available at **[betterstore.dev](https://betterstore.dev)**.
26
+ Full documentation is available at **[betterstore.io](https://betterstore.io)**.
36
27
 
37
28
  ## 🤝 Contributing
38
29
 
package/dist/index.d.mts CHANGED
@@ -1,33 +1,210 @@
1
- interface BaseCheckoutParams {
2
- line_items: {
3
- productId: string;
4
- variantOptions?: {
1
+ interface LineItem {
2
+ quantity: number;
3
+ productId?: string;
4
+ variantOptions: {
5
+ name: string;
6
+ value: string;
7
+ }[];
8
+ discountId?: string;
9
+ }
10
+ interface CheckoutCreateParams {
11
+ type: "hosted" | "embed";
12
+ customerId?: string;
13
+ lineItems: LineItem[];
14
+ }
15
+ interface CheckoutUpdateParams {
16
+ email?: string;
17
+ phone?: string;
18
+ lineItems?: LineItem[];
19
+ customerId?: string;
20
+ }
21
+ interface ShippingRate {
22
+ id: string;
23
+ rate: number;
24
+ provider: string;
25
+ service: string;
26
+ estimatedDays: number;
27
+ }
28
+ interface Address {
29
+ name: string;
30
+ company?: string;
31
+ address: string;
32
+ city: string;
33
+ state: string;
34
+ country: string;
35
+ apartment?: string;
36
+ postalCode: string;
37
+ phone: string;
38
+ }
39
+ interface CheckoutSession {
40
+ id: string;
41
+ createdAt: Date;
42
+ updatedAt: Date;
43
+ email?: string;
44
+ phone?: string;
45
+ clientSecret: string;
46
+ lineItems: {
47
+ quantity: number;
48
+ discount?: any;
49
+ variantOptions: {
5
50
  name: string;
6
51
  value: string;
7
52
  }[];
8
- quantity: number;
53
+ product?: {
54
+ id: string;
55
+ title: string;
56
+ description?: string;
57
+ images: string[];
58
+ category: string;
59
+ tags: string[];
60
+ priceInCents: number;
61
+ };
9
62
  }[];
10
- discount: string;
63
+ total?: number;
64
+ subtotal?: number;
65
+ tax?: number;
66
+ shipping?: number;
67
+ currency: string;
68
+ status: "IN_PROGRESS" | "PAYMENT_PENDING" | "ABANDONED" | "CANCELED" | "FAILED";
69
+ customer?: {
70
+ address?: Address;
71
+ email?: string;
72
+ };
11
73
  }
12
- interface HostedCheckoutParams extends BaseCheckoutParams {
13
- type: "hosted";
14
- successUrl: string;
15
- cancelUrl: string;
74
+ declare class Checkout {
75
+ private apiClient;
76
+ constructor(apiKey: string);
77
+ /**
78
+ * Create a new checkout session
79
+ */
80
+ create(params: CheckoutCreateParams): Promise<CheckoutSession>;
81
+ /**
82
+ * Retrieve a checkout session by ID or client secret
83
+ */
84
+ retrieve(idOrSecret: string): Promise<CheckoutSession>;
85
+ /**
86
+ * Update a checkout session
87
+ */
88
+ update(checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession>;
89
+ /**
90
+ * Get shipping rates for a checkout session
91
+ */
92
+ getShippingRates(checkoutId: string): Promise<ShippingRate[]>;
93
+ /**
94
+ * Generate payment secret for a checkout session
95
+ */
96
+ generatePaymentSecret(checkoutId: string): Promise<string>;
16
97
  }
17
- interface EmbedCheckoutParams extends BaseCheckoutParams {
18
- type: "embed";
98
+
99
+ interface CustomerAddress {
100
+ name: string;
101
+ company?: string;
102
+ address: string;
103
+ city: string;
104
+ state: string;
105
+ country: string;
106
+ apartment?: string;
107
+ postalCode: string;
108
+ phone: string;
19
109
  }
20
- declare class Checkout {
21
- private apiKey;
110
+ interface CustomerCreateParams {
111
+ firstName: string;
112
+ lastName: string;
113
+ email: string;
114
+ phone?: string;
115
+ address?: CustomerAddress;
116
+ isSubscribedEmail?: boolean;
117
+ isSubscribedSMS?: boolean;
118
+ }
119
+ interface CustomerUpdateParams {
120
+ firstName?: string;
121
+ lastName?: string;
122
+ email?: string;
123
+ phone?: string;
124
+ address?: CustomerAddress;
125
+ isSubscribedEmail?: boolean;
126
+ isSubscribedSMS?: boolean;
127
+ }
128
+ interface Customer extends CustomerCreateParams {
129
+ id: string;
130
+ createdAt: string;
131
+ updatedAt: string;
132
+ }
133
+ declare class Customer {
134
+ private apiClient;
135
+ constructor(apiKey: string);
136
+ /**
137
+ * Create a new customer
138
+ */
139
+ create(params: CustomerCreateParams): Promise<Customer>;
140
+ /**
141
+ * Retrieve a customer by ID or email
142
+ */
143
+ retrieve(idOrEmail: string): Promise<Customer>;
144
+ /**
145
+ * Update a customer
146
+ */
147
+ update(customerId: string, params: CustomerUpdateParams): Promise<Customer>;
148
+ /**
149
+ * Delete a customer
150
+ */
151
+ delete(customerId: string): Promise<void>;
152
+ }
153
+
154
+ interface VariantOption {
155
+ name: string;
156
+ value: string;
157
+ }
158
+ interface ProductVariant {
159
+ sku: string;
160
+ images: string[];
161
+ stockAvailable: number;
162
+ stockCommited: number;
163
+ stockUnavailable: number;
164
+ priceInCents: number;
165
+ productId: string;
166
+ variantOptions: VariantOption[];
167
+ }
168
+ interface ProductOption {
169
+ name: string;
170
+ values: string[];
171
+ }
172
+ declare enum ProductStatus {
173
+ DRAFT = "DRAFT",
174
+ ACTIVE = "ACTIVE",
175
+ ARCHIVED = "ARCHIVED"
176
+ }
177
+ interface Product {
178
+ id: string;
179
+ title: string;
180
+ description?: string;
181
+ images: string[];
182
+ category: string;
183
+ tags: string[];
184
+ isPhysical: boolean;
185
+ weightInGrams?: number;
186
+ heightInCm?: number;
187
+ widthInCm?: number;
188
+ lengthInCm?: number;
189
+ priceInCents: number;
190
+ stockAvailable: number;
191
+ stockCommited: number;
192
+ stockUnavailable: number;
193
+ status: ProductStatus;
194
+ options: ProductOption[];
195
+ productVariants: ProductVariant[];
196
+ }
197
+ declare class Product {
198
+ private apiClient;
22
199
  constructor(apiKey: string);
23
- create(params: HostedCheckoutParams | EmbedCheckoutParams): Promise<string | {
24
- client_secret: any;
25
- }>;
200
+ list(): Promise<Omit<Product, "productVariants">[]>;
201
+ retrieve(productId: string): Promise<Product>;
26
202
  }
27
203
 
28
204
  declare class BetterStore {
29
205
  checkout: Checkout;
30
- private apiKey;
206
+ product: Product;
207
+ customer: Customer;
31
208
  constructor(apiKey: string);
32
209
  }
33
210
 
package/dist/index.d.ts CHANGED
@@ -1,33 +1,210 @@
1
- interface BaseCheckoutParams {
2
- line_items: {
3
- productId: string;
4
- variantOptions?: {
1
+ interface LineItem {
2
+ quantity: number;
3
+ productId?: string;
4
+ variantOptions: {
5
+ name: string;
6
+ value: string;
7
+ }[];
8
+ discountId?: string;
9
+ }
10
+ interface CheckoutCreateParams {
11
+ type: "hosted" | "embed";
12
+ customerId?: string;
13
+ lineItems: LineItem[];
14
+ }
15
+ interface CheckoutUpdateParams {
16
+ email?: string;
17
+ phone?: string;
18
+ lineItems?: LineItem[];
19
+ customerId?: string;
20
+ }
21
+ interface ShippingRate {
22
+ id: string;
23
+ rate: number;
24
+ provider: string;
25
+ service: string;
26
+ estimatedDays: number;
27
+ }
28
+ interface Address {
29
+ name: string;
30
+ company?: string;
31
+ address: string;
32
+ city: string;
33
+ state: string;
34
+ country: string;
35
+ apartment?: string;
36
+ postalCode: string;
37
+ phone: string;
38
+ }
39
+ interface CheckoutSession {
40
+ id: string;
41
+ createdAt: Date;
42
+ updatedAt: Date;
43
+ email?: string;
44
+ phone?: string;
45
+ clientSecret: string;
46
+ lineItems: {
47
+ quantity: number;
48
+ discount?: any;
49
+ variantOptions: {
5
50
  name: string;
6
51
  value: string;
7
52
  }[];
8
- quantity: number;
53
+ product?: {
54
+ id: string;
55
+ title: string;
56
+ description?: string;
57
+ images: string[];
58
+ category: string;
59
+ tags: string[];
60
+ priceInCents: number;
61
+ };
9
62
  }[];
10
- discount: string;
63
+ total?: number;
64
+ subtotal?: number;
65
+ tax?: number;
66
+ shipping?: number;
67
+ currency: string;
68
+ status: "IN_PROGRESS" | "PAYMENT_PENDING" | "ABANDONED" | "CANCELED" | "FAILED";
69
+ customer?: {
70
+ address?: Address;
71
+ email?: string;
72
+ };
11
73
  }
12
- interface HostedCheckoutParams extends BaseCheckoutParams {
13
- type: "hosted";
14
- successUrl: string;
15
- cancelUrl: string;
74
+ declare class Checkout {
75
+ private apiClient;
76
+ constructor(apiKey: string);
77
+ /**
78
+ * Create a new checkout session
79
+ */
80
+ create(params: CheckoutCreateParams): Promise<CheckoutSession>;
81
+ /**
82
+ * Retrieve a checkout session by ID or client secret
83
+ */
84
+ retrieve(idOrSecret: string): Promise<CheckoutSession>;
85
+ /**
86
+ * Update a checkout session
87
+ */
88
+ update(checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession>;
89
+ /**
90
+ * Get shipping rates for a checkout session
91
+ */
92
+ getShippingRates(checkoutId: string): Promise<ShippingRate[]>;
93
+ /**
94
+ * Generate payment secret for a checkout session
95
+ */
96
+ generatePaymentSecret(checkoutId: string): Promise<string>;
16
97
  }
17
- interface EmbedCheckoutParams extends BaseCheckoutParams {
18
- type: "embed";
98
+
99
+ interface CustomerAddress {
100
+ name: string;
101
+ company?: string;
102
+ address: string;
103
+ city: string;
104
+ state: string;
105
+ country: string;
106
+ apartment?: string;
107
+ postalCode: string;
108
+ phone: string;
19
109
  }
20
- declare class Checkout {
21
- private apiKey;
110
+ interface CustomerCreateParams {
111
+ firstName: string;
112
+ lastName: string;
113
+ email: string;
114
+ phone?: string;
115
+ address?: CustomerAddress;
116
+ isSubscribedEmail?: boolean;
117
+ isSubscribedSMS?: boolean;
118
+ }
119
+ interface CustomerUpdateParams {
120
+ firstName?: string;
121
+ lastName?: string;
122
+ email?: string;
123
+ phone?: string;
124
+ address?: CustomerAddress;
125
+ isSubscribedEmail?: boolean;
126
+ isSubscribedSMS?: boolean;
127
+ }
128
+ interface Customer extends CustomerCreateParams {
129
+ id: string;
130
+ createdAt: string;
131
+ updatedAt: string;
132
+ }
133
+ declare class Customer {
134
+ private apiClient;
135
+ constructor(apiKey: string);
136
+ /**
137
+ * Create a new customer
138
+ */
139
+ create(params: CustomerCreateParams): Promise<Customer>;
140
+ /**
141
+ * Retrieve a customer by ID or email
142
+ */
143
+ retrieve(idOrEmail: string): Promise<Customer>;
144
+ /**
145
+ * Update a customer
146
+ */
147
+ update(customerId: string, params: CustomerUpdateParams): Promise<Customer>;
148
+ /**
149
+ * Delete a customer
150
+ */
151
+ delete(customerId: string): Promise<void>;
152
+ }
153
+
154
+ interface VariantOption {
155
+ name: string;
156
+ value: string;
157
+ }
158
+ interface ProductVariant {
159
+ sku: string;
160
+ images: string[];
161
+ stockAvailable: number;
162
+ stockCommited: number;
163
+ stockUnavailable: number;
164
+ priceInCents: number;
165
+ productId: string;
166
+ variantOptions: VariantOption[];
167
+ }
168
+ interface ProductOption {
169
+ name: string;
170
+ values: string[];
171
+ }
172
+ declare enum ProductStatus {
173
+ DRAFT = "DRAFT",
174
+ ACTIVE = "ACTIVE",
175
+ ARCHIVED = "ARCHIVED"
176
+ }
177
+ interface Product {
178
+ id: string;
179
+ title: string;
180
+ description?: string;
181
+ images: string[];
182
+ category: string;
183
+ tags: string[];
184
+ isPhysical: boolean;
185
+ weightInGrams?: number;
186
+ heightInCm?: number;
187
+ widthInCm?: number;
188
+ lengthInCm?: number;
189
+ priceInCents: number;
190
+ stockAvailable: number;
191
+ stockCommited: number;
192
+ stockUnavailable: number;
193
+ status: ProductStatus;
194
+ options: ProductOption[];
195
+ productVariants: ProductVariant[];
196
+ }
197
+ declare class Product {
198
+ private apiClient;
22
199
  constructor(apiKey: string);
23
- create(params: HostedCheckoutParams | EmbedCheckoutParams): Promise<string | {
24
- client_secret: any;
25
- }>;
200
+ list(): Promise<Omit<Product, "productVariants">[]>;
201
+ retrieve(productId: string): Promise<Product>;
26
202
  }
27
203
 
28
204
  declare class BetterStore {
29
205
  checkout: Checkout;
30
- private apiKey;
206
+ product: Product;
207
+ customer: Customer;
31
208
  constructor(apiKey: string);
32
209
  }
33
210
 
package/dist/index.js CHANGED
@@ -1,25 +1,10 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
- var __defProps = Object.defineProperties;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __getProtoOf = Object.getPrototypeOf;
8
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
- var __spreadValues = (a, b) => {
12
- for (var prop in b || (b = {}))
13
- if (__hasOwnProp.call(b, prop))
14
- __defNormalProp(a, prop, b[prop]);
15
- if (__getOwnPropSymbols)
16
- for (var prop of __getOwnPropSymbols(b)) {
17
- if (__propIsEnum.call(b, prop))
18
- __defNormalProp(a, prop, b[prop]);
19
- }
20
- return a;
21
- };
22
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
23
8
  var __export = (target, all) => {
24
9
  for (var name in all)
25
10
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -32,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
32
17
  }
33
18
  return to;
34
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
35
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
36
29
  var __async = (__this, __arguments, generator) => {
37
30
  return new Promise((resolve, reject) => {
@@ -61,64 +54,192 @@ __export(index_exports, {
61
54
  });
62
55
  module.exports = __toCommonJS(index_exports);
63
56
 
57
+ // src/utils/axios.ts
58
+ var import_axios = __toESM(require("axios"));
59
+ var API_BASE_URL = "https://api.betterstore.io/api/v1";
60
+ var createApiClient = (apiKey) => {
61
+ const client = import_axios.default.create({
62
+ baseURL: API_BASE_URL,
63
+ headers: {
64
+ "Content-Type": "application/json",
65
+ Authorization: `Bearer ${apiKey}`
66
+ }
67
+ });
68
+ client.interceptors.response.use(
69
+ (response) => response.data,
70
+ (error) => {
71
+ var _a, _b;
72
+ const apiError = {
73
+ status: 500,
74
+ message: "An unexpected error occurred"
75
+ };
76
+ if (error.response) {
77
+ apiError.status = error.response.status;
78
+ apiError.message = ((_a = error.response.data) == null ? void 0 : _a.error) || "Server error occurred";
79
+ apiError.code = (_b = error.response.data) == null ? void 0 : _b.code;
80
+ apiError.details = error.response.data;
81
+ } else if (error.request) {
82
+ apiError.status = 503;
83
+ apiError.message = "Service unavailable - no response from server";
84
+ apiError.code = "SERVICE_UNAVAILABLE";
85
+ } else {
86
+ apiError.status = 500;
87
+ apiError.message = "Request configuration error";
88
+ apiError.code = "REQUEST_SETUP_ERROR";
89
+ }
90
+ throw apiError;
91
+ }
92
+ );
93
+ return client;
94
+ };
95
+
64
96
  // src/checkout.ts
65
97
  var Checkout = class {
66
98
  constructor(apiKey) {
67
- this.apiKey = apiKey;
99
+ this.apiClient = createApiClient(apiKey);
68
100
  }
101
+ /**
102
+ * Create a new checkout session
103
+ */
69
104
  create(params) {
70
105
  return __async(this, null, function* () {
71
- const lineItems = params.line_items.map((item) => {
72
- var _a;
73
- return __spreadProps(__spreadValues({}, item), {
74
- variant_options: (_a = item.variantOptions) != null ? _a : []
75
- });
76
- });
77
- const response = yield fetch("https://betterstore.io/api/checkout", {
78
- method: "POST",
79
- body: JSON.stringify(__spreadValues({
80
- type: params.type,
81
- line_items: lineItems,
82
- discount: params.discount
83
- }, params.type === "hosted" && {
84
- success_url: params.successUrl,
85
- cancel_url: params.cancelUrl
86
- })),
87
- headers: {
88
- "Content-Type": "application/json",
89
- Authorization: `Bearer ${this.apiKey}`
90
- }
91
- });
92
- const data = yield response.json();
93
- if (params.type === "hosted") {
94
- const checkoutId = data.checkoutId;
95
- if (!checkoutId) {
96
- throw new Error("Failed to create checkout");
97
- }
98
- const searchParams = new URLSearchParams({
99
- successUrl: params.successUrl,
100
- cancelUrl: params.cancelUrl
101
- });
102
- return `https://checkout.betterstore.io/${checkoutId}?${searchParams.toString()}`;
106
+ const data = yield this.apiClient.post(
107
+ "/checkout",
108
+ params
109
+ );
110
+ return data;
111
+ });
112
+ }
113
+ /**
114
+ * Retrieve a checkout session by ID or client secret
115
+ */
116
+ retrieve(idOrSecret) {
117
+ return __async(this, null, function* () {
118
+ const data = yield this.apiClient.get(
119
+ `/checkout/${idOrSecret}`
120
+ );
121
+ return data;
122
+ });
123
+ }
124
+ /**
125
+ * Update a checkout session
126
+ */
127
+ update(checkoutId, params) {
128
+ return __async(this, null, function* () {
129
+ const data = yield this.apiClient.put(
130
+ `/checkout/${checkoutId}`,
131
+ params
132
+ );
133
+ return data;
134
+ });
135
+ }
136
+ /**
137
+ * Get shipping rates for a checkout session
138
+ */
139
+ getShippingRates(checkoutId) {
140
+ return __async(this, null, function* () {
141
+ const data = yield this.apiClient.get(
142
+ `/checkout/shipping/${checkoutId}`
143
+ );
144
+ return data;
145
+ });
146
+ }
147
+ /**
148
+ * Generate payment secret for a checkout session
149
+ */
150
+ generatePaymentSecret(checkoutId) {
151
+ return __async(this, null, function* () {
152
+ const data = yield this.apiClient.post(
153
+ `/checkout/payment/${checkoutId}`
154
+ );
155
+ return data;
156
+ });
157
+ }
158
+ };
159
+ var checkout_default = Checkout;
160
+
161
+ // src/customer.ts
162
+ var Customer = class {
163
+ constructor(apiKey) {
164
+ this.apiClient = createApiClient(apiKey);
165
+ }
166
+ /**
167
+ * Create a new customer
168
+ */
169
+ create(params) {
170
+ return __async(this, null, function* () {
171
+ const data = yield this.apiClient.post("/customers", params);
172
+ return data;
173
+ });
174
+ }
175
+ /**
176
+ * Retrieve a customer by ID or email
177
+ */
178
+ retrieve(idOrEmail) {
179
+ return __async(this, null, function* () {
180
+ const data = yield this.apiClient.get(`/customers/${idOrEmail}`);
181
+ if (!data) {
182
+ throw new Error("Customer not found");
103
183
  }
104
- const clientSecret = data.clientSecret;
105
- if (!clientSecret) {
106
- throw new Error("Failed to create checkout");
184
+ return data;
185
+ });
186
+ }
187
+ /**
188
+ * Update a customer
189
+ */
190
+ update(customerId, params) {
191
+ return __async(this, null, function* () {
192
+ const data = yield this.apiClient.put(
193
+ `/customers/${customerId}`,
194
+ params
195
+ );
196
+ return data;
197
+ });
198
+ }
199
+ /**
200
+ * Delete a customer
201
+ */
202
+ delete(customerId) {
203
+ return __async(this, null, function* () {
204
+ yield this.apiClient.delete(`/customers/${customerId}`);
205
+ });
206
+ }
207
+ };
208
+ var customer_default = Customer;
209
+
210
+ // src/product.ts
211
+ var Product = class {
212
+ constructor(apiKey) {
213
+ this.apiClient = createApiClient(apiKey);
214
+ }
215
+ list() {
216
+ return __async(this, null, function* () {
217
+ const data = yield this.apiClient.get("/products");
218
+ return data;
219
+ });
220
+ }
221
+ retrieve(productId) {
222
+ return __async(this, null, function* () {
223
+ const data = yield this.apiClient.get(`/products/${productId}`);
224
+ if (!data) {
225
+ throw new Error("Product not found");
107
226
  }
108
- return { client_secret: clientSecret };
227
+ return data;
109
228
  });
110
229
  }
111
230
  };
112
- var checkout_default = Checkout;
231
+ var product_default = Product;
113
232
 
114
233
  // src/index.ts
115
234
  var BetterStore = class {
235
+ // private apiKey: string;
116
236
  constructor(apiKey) {
117
237
  if (!apiKey) {
118
238
  throw new Error("API key is required.");
119
239
  }
120
- this.apiKey = apiKey;
121
240
  this.checkout = new checkout_default(apiKey);
241
+ this.product = new product_default(apiKey);
242
+ this.customer = new customer_default(apiKey);
122
243
  }
123
244
  };
124
245
  var index_default = BetterStore;
package/dist/index.mjs CHANGED
@@ -1,22 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
1
  var __async = (__this, __arguments, generator) => {
21
2
  return new Promise((resolve, reject) => {
22
3
  var fulfilled = (value) => {
@@ -38,64 +19,192 @@ var __async = (__this, __arguments, generator) => {
38
19
  });
39
20
  };
40
21
 
22
+ // src/utils/axios.ts
23
+ import axios from "axios";
24
+ var API_BASE_URL = "https://api.betterstore.io/api/v1";
25
+ var createApiClient = (apiKey) => {
26
+ const client = axios.create({
27
+ baseURL: API_BASE_URL,
28
+ headers: {
29
+ "Content-Type": "application/json",
30
+ Authorization: `Bearer ${apiKey}`
31
+ }
32
+ });
33
+ client.interceptors.response.use(
34
+ (response) => response.data,
35
+ (error) => {
36
+ var _a, _b;
37
+ const apiError = {
38
+ status: 500,
39
+ message: "An unexpected error occurred"
40
+ };
41
+ if (error.response) {
42
+ apiError.status = error.response.status;
43
+ apiError.message = ((_a = error.response.data) == null ? void 0 : _a.error) || "Server error occurred";
44
+ apiError.code = (_b = error.response.data) == null ? void 0 : _b.code;
45
+ apiError.details = error.response.data;
46
+ } else if (error.request) {
47
+ apiError.status = 503;
48
+ apiError.message = "Service unavailable - no response from server";
49
+ apiError.code = "SERVICE_UNAVAILABLE";
50
+ } else {
51
+ apiError.status = 500;
52
+ apiError.message = "Request configuration error";
53
+ apiError.code = "REQUEST_SETUP_ERROR";
54
+ }
55
+ throw apiError;
56
+ }
57
+ );
58
+ return client;
59
+ };
60
+
41
61
  // src/checkout.ts
42
62
  var Checkout = class {
43
63
  constructor(apiKey) {
44
- this.apiKey = apiKey;
64
+ this.apiClient = createApiClient(apiKey);
45
65
  }
66
+ /**
67
+ * Create a new checkout session
68
+ */
46
69
  create(params) {
47
70
  return __async(this, null, function* () {
48
- const lineItems = params.line_items.map((item) => {
49
- var _a;
50
- return __spreadProps(__spreadValues({}, item), {
51
- variant_options: (_a = item.variantOptions) != null ? _a : []
52
- });
53
- });
54
- const response = yield fetch("https://betterstore.io/api/checkout", {
55
- method: "POST",
56
- body: JSON.stringify(__spreadValues({
57
- type: params.type,
58
- line_items: lineItems,
59
- discount: params.discount
60
- }, params.type === "hosted" && {
61
- success_url: params.successUrl,
62
- cancel_url: params.cancelUrl
63
- })),
64
- headers: {
65
- "Content-Type": "application/json",
66
- Authorization: `Bearer ${this.apiKey}`
67
- }
68
- });
69
- const data = yield response.json();
70
- if (params.type === "hosted") {
71
- const checkoutId = data.checkoutId;
72
- if (!checkoutId) {
73
- throw new Error("Failed to create checkout");
74
- }
75
- const searchParams = new URLSearchParams({
76
- successUrl: params.successUrl,
77
- cancelUrl: params.cancelUrl
78
- });
79
- return `https://checkout.betterstore.io/${checkoutId}?${searchParams.toString()}`;
71
+ const data = yield this.apiClient.post(
72
+ "/checkout",
73
+ params
74
+ );
75
+ return data;
76
+ });
77
+ }
78
+ /**
79
+ * Retrieve a checkout session by ID or client secret
80
+ */
81
+ retrieve(idOrSecret) {
82
+ return __async(this, null, function* () {
83
+ const data = yield this.apiClient.get(
84
+ `/checkout/${idOrSecret}`
85
+ );
86
+ return data;
87
+ });
88
+ }
89
+ /**
90
+ * Update a checkout session
91
+ */
92
+ update(checkoutId, params) {
93
+ return __async(this, null, function* () {
94
+ const data = yield this.apiClient.put(
95
+ `/checkout/${checkoutId}`,
96
+ params
97
+ );
98
+ return data;
99
+ });
100
+ }
101
+ /**
102
+ * Get shipping rates for a checkout session
103
+ */
104
+ getShippingRates(checkoutId) {
105
+ return __async(this, null, function* () {
106
+ const data = yield this.apiClient.get(
107
+ `/checkout/shipping/${checkoutId}`
108
+ );
109
+ return data;
110
+ });
111
+ }
112
+ /**
113
+ * Generate payment secret for a checkout session
114
+ */
115
+ generatePaymentSecret(checkoutId) {
116
+ return __async(this, null, function* () {
117
+ const data = yield this.apiClient.post(
118
+ `/checkout/payment/${checkoutId}`
119
+ );
120
+ return data;
121
+ });
122
+ }
123
+ };
124
+ var checkout_default = Checkout;
125
+
126
+ // src/customer.ts
127
+ var Customer = class {
128
+ constructor(apiKey) {
129
+ this.apiClient = createApiClient(apiKey);
130
+ }
131
+ /**
132
+ * Create a new customer
133
+ */
134
+ create(params) {
135
+ return __async(this, null, function* () {
136
+ const data = yield this.apiClient.post("/customers", params);
137
+ return data;
138
+ });
139
+ }
140
+ /**
141
+ * Retrieve a customer by ID or email
142
+ */
143
+ retrieve(idOrEmail) {
144
+ return __async(this, null, function* () {
145
+ const data = yield this.apiClient.get(`/customers/${idOrEmail}`);
146
+ if (!data) {
147
+ throw new Error("Customer not found");
80
148
  }
81
- const clientSecret = data.clientSecret;
82
- if (!clientSecret) {
83
- throw new Error("Failed to create checkout");
149
+ return data;
150
+ });
151
+ }
152
+ /**
153
+ * Update a customer
154
+ */
155
+ update(customerId, params) {
156
+ return __async(this, null, function* () {
157
+ const data = yield this.apiClient.put(
158
+ `/customers/${customerId}`,
159
+ params
160
+ );
161
+ return data;
162
+ });
163
+ }
164
+ /**
165
+ * Delete a customer
166
+ */
167
+ delete(customerId) {
168
+ return __async(this, null, function* () {
169
+ yield this.apiClient.delete(`/customers/${customerId}`);
170
+ });
171
+ }
172
+ };
173
+ var customer_default = Customer;
174
+
175
+ // src/product.ts
176
+ var Product = class {
177
+ constructor(apiKey) {
178
+ this.apiClient = createApiClient(apiKey);
179
+ }
180
+ list() {
181
+ return __async(this, null, function* () {
182
+ const data = yield this.apiClient.get("/products");
183
+ return data;
184
+ });
185
+ }
186
+ retrieve(productId) {
187
+ return __async(this, null, function* () {
188
+ const data = yield this.apiClient.get(`/products/${productId}`);
189
+ if (!data) {
190
+ throw new Error("Product not found");
84
191
  }
85
- return { client_secret: clientSecret };
192
+ return data;
86
193
  });
87
194
  }
88
195
  };
89
- var checkout_default = Checkout;
196
+ var product_default = Product;
90
197
 
91
198
  // src/index.ts
92
199
  var BetterStore = class {
200
+ // private apiKey: string;
93
201
  constructor(apiKey) {
94
202
  if (!apiKey) {
95
203
  throw new Error("API key is required.");
96
204
  }
97
- this.apiKey = apiKey;
98
205
  this.checkout = new checkout_default(apiKey);
206
+ this.product = new product_default(apiKey);
207
+ this.customer = new customer_default(apiKey);
99
208
  }
100
209
  };
101
210
  var index_default = BetterStore;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@betterstore/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "E-commerce for Developers",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -19,10 +19,14 @@
19
19
  "license": "MIT",
20
20
  "devDependencies": {
21
21
  "@changesets/cli": "^2.28.1",
22
+ "@types/axios": "^0.14.4",
22
23
  "prettier": "^3.5.3",
23
24
  "tsup": "^8.4.0",
24
25
  "typescript": "^5.8.2"
25
26
  },
27
+ "dependencies": {
28
+ "axios": "^1.8.1"
29
+ },
26
30
  "scripts": {
27
31
  "build": "tsup src/index.ts --format cjs,esm --dts",
28
32
  "lint": "tsc",