@betterstore/sdk 0.2.12 → 0.3.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 +12 -0
- package/dist/index.d.mts +44 -22
- package/dist/index.d.ts +44 -22
- package/dist/index.js +119 -314
- package/dist/index.mjs +117 -313
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { NextRequest } from 'next/server';
|
|
2
|
-
|
|
3
1
|
interface LineItem {
|
|
4
2
|
quantity: number;
|
|
5
3
|
productId?: string;
|
|
@@ -73,6 +71,7 @@ interface CheckoutSession {
|
|
|
73
71
|
email?: string;
|
|
74
72
|
};
|
|
75
73
|
}
|
|
74
|
+
|
|
76
75
|
declare class Checkout {
|
|
77
76
|
private apiClient;
|
|
78
77
|
constructor(apiKey: string);
|
|
@@ -127,26 +126,58 @@ interface CustomerUpdateParams {
|
|
|
127
126
|
isSubscribedEmail?: boolean;
|
|
128
127
|
isSubscribedSMS?: boolean;
|
|
129
128
|
}
|
|
130
|
-
interface Customer extends CustomerCreateParams {
|
|
129
|
+
interface Customer$1 extends CustomerCreateParams {
|
|
131
130
|
id: string;
|
|
132
131
|
createdAt: string;
|
|
133
132
|
updatedAt: string;
|
|
134
133
|
}
|
|
134
|
+
|
|
135
|
+
declare class Client {
|
|
136
|
+
/**
|
|
137
|
+
* Retrieve a checkout session by ID
|
|
138
|
+
*/
|
|
139
|
+
retrieveCheckout(clientSecret: string, checkoutId: string): Promise<CheckoutSession>;
|
|
140
|
+
/**
|
|
141
|
+
* Update a checkout session
|
|
142
|
+
*/
|
|
143
|
+
updateCheckout(clientSecret: string, checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession>;
|
|
144
|
+
/**
|
|
145
|
+
* Get shipping rates for a checkout session
|
|
146
|
+
*/
|
|
147
|
+
getCheckoutShippingRates(clientSecret: string, checkoutId: string): Promise<ShippingRate[]>;
|
|
148
|
+
/**
|
|
149
|
+
* Generate payment secret for a checkout session
|
|
150
|
+
*/
|
|
151
|
+
generateCheckoutsPaymentSecret(clientSecret: string, checkoutId: string): Promise<string>;
|
|
152
|
+
/**
|
|
153
|
+
* Create a new customer
|
|
154
|
+
*/
|
|
155
|
+
createCustomer(clientSecret: string, params: CustomerCreateParams): Promise<Customer$1>;
|
|
156
|
+
/**
|
|
157
|
+
* Retrieve a customer by ID or email
|
|
158
|
+
*/
|
|
159
|
+
retrieveCustomer(clientSecret: string, idOrEmail: string): Promise<Customer$1>;
|
|
160
|
+
/**
|
|
161
|
+
* Update a customer
|
|
162
|
+
*/
|
|
163
|
+
updateCustomer(clientSecret: string, customerId: string, params: CustomerUpdateParams): Promise<Customer$1>;
|
|
164
|
+
}
|
|
165
|
+
|
|
135
166
|
declare class Customer {
|
|
136
167
|
private apiClient;
|
|
137
168
|
constructor(apiKey: string);
|
|
138
169
|
/**
|
|
139
170
|
* Create a new customer
|
|
140
171
|
*/
|
|
141
|
-
create(params: CustomerCreateParams): Promise<Customer>;
|
|
172
|
+
create(params: CustomerCreateParams): Promise<Customer$1>;
|
|
142
173
|
/**
|
|
143
174
|
* Retrieve a customer by ID or email
|
|
144
175
|
*/
|
|
145
|
-
retrieve(idOrEmail: string): Promise<Customer>;
|
|
176
|
+
retrieve(idOrEmail: string): Promise<Customer$1>;
|
|
146
177
|
/**
|
|
147
178
|
* Update a customer
|
|
148
179
|
*/
|
|
149
|
-
update(customerId: string, params: CustomerUpdateParams): Promise<Customer>;
|
|
180
|
+
update(customerId: string, params: CustomerUpdateParams): Promise<Customer$1>;
|
|
150
181
|
/**
|
|
151
182
|
* Delete a customer
|
|
152
183
|
*/
|
|
@@ -196,6 +227,7 @@ interface Product {
|
|
|
196
227
|
options: ProductOption[];
|
|
197
228
|
productVariants: ProductVariant[];
|
|
198
229
|
}
|
|
230
|
+
|
|
199
231
|
declare class Products {
|
|
200
232
|
private apiClient;
|
|
201
233
|
constructor(apiKey: string);
|
|
@@ -203,23 +235,13 @@ declare class Products {
|
|
|
203
235
|
retrieve(productId: string): Promise<Product>;
|
|
204
236
|
}
|
|
205
237
|
|
|
206
|
-
|
|
207
|
-
apiKey
|
|
208
|
-
|
|
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
|
-
|
|
218
|
-
declare class BetterStore {
|
|
238
|
+
declare function betterStore(config: {
|
|
239
|
+
apiKey: string;
|
|
240
|
+
}): {
|
|
219
241
|
checkout: Checkout;
|
|
220
242
|
products: Products;
|
|
221
243
|
customer: Customer;
|
|
222
|
-
|
|
223
|
-
|
|
244
|
+
};
|
|
245
|
+
declare function createStoreClient(): Client;
|
|
224
246
|
|
|
225
|
-
export {
|
|
247
|
+
export { type Address, type CheckoutCreateParams, type CheckoutSession, type CheckoutUpdateParams, type Customer$1 as Customer, type CustomerAddress, type CustomerCreateParams, type CustomerUpdateParams, type LineItem, type Product, type ProductOption, ProductStatus, type ProductVariant, type ShippingRate, type VariantOption, createStoreClient, betterStore as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { NextRequest } from 'next/server';
|
|
2
|
-
|
|
3
1
|
interface LineItem {
|
|
4
2
|
quantity: number;
|
|
5
3
|
productId?: string;
|
|
@@ -73,6 +71,7 @@ interface CheckoutSession {
|
|
|
73
71
|
email?: string;
|
|
74
72
|
};
|
|
75
73
|
}
|
|
74
|
+
|
|
76
75
|
declare class Checkout {
|
|
77
76
|
private apiClient;
|
|
78
77
|
constructor(apiKey: string);
|
|
@@ -127,26 +126,58 @@ interface CustomerUpdateParams {
|
|
|
127
126
|
isSubscribedEmail?: boolean;
|
|
128
127
|
isSubscribedSMS?: boolean;
|
|
129
128
|
}
|
|
130
|
-
interface Customer extends CustomerCreateParams {
|
|
129
|
+
interface Customer$1 extends CustomerCreateParams {
|
|
131
130
|
id: string;
|
|
132
131
|
createdAt: string;
|
|
133
132
|
updatedAt: string;
|
|
134
133
|
}
|
|
134
|
+
|
|
135
|
+
declare class Client {
|
|
136
|
+
/**
|
|
137
|
+
* Retrieve a checkout session by ID
|
|
138
|
+
*/
|
|
139
|
+
retrieveCheckout(clientSecret: string, checkoutId: string): Promise<CheckoutSession>;
|
|
140
|
+
/**
|
|
141
|
+
* Update a checkout session
|
|
142
|
+
*/
|
|
143
|
+
updateCheckout(clientSecret: string, checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession>;
|
|
144
|
+
/**
|
|
145
|
+
* Get shipping rates for a checkout session
|
|
146
|
+
*/
|
|
147
|
+
getCheckoutShippingRates(clientSecret: string, checkoutId: string): Promise<ShippingRate[]>;
|
|
148
|
+
/**
|
|
149
|
+
* Generate payment secret for a checkout session
|
|
150
|
+
*/
|
|
151
|
+
generateCheckoutsPaymentSecret(clientSecret: string, checkoutId: string): Promise<string>;
|
|
152
|
+
/**
|
|
153
|
+
* Create a new customer
|
|
154
|
+
*/
|
|
155
|
+
createCustomer(clientSecret: string, params: CustomerCreateParams): Promise<Customer$1>;
|
|
156
|
+
/**
|
|
157
|
+
* Retrieve a customer by ID or email
|
|
158
|
+
*/
|
|
159
|
+
retrieveCustomer(clientSecret: string, idOrEmail: string): Promise<Customer$1>;
|
|
160
|
+
/**
|
|
161
|
+
* Update a customer
|
|
162
|
+
*/
|
|
163
|
+
updateCustomer(clientSecret: string, customerId: string, params: CustomerUpdateParams): Promise<Customer$1>;
|
|
164
|
+
}
|
|
165
|
+
|
|
135
166
|
declare class Customer {
|
|
136
167
|
private apiClient;
|
|
137
168
|
constructor(apiKey: string);
|
|
138
169
|
/**
|
|
139
170
|
* Create a new customer
|
|
140
171
|
*/
|
|
141
|
-
create(params: CustomerCreateParams): Promise<Customer>;
|
|
172
|
+
create(params: CustomerCreateParams): Promise<Customer$1>;
|
|
142
173
|
/**
|
|
143
174
|
* Retrieve a customer by ID or email
|
|
144
175
|
*/
|
|
145
|
-
retrieve(idOrEmail: string): Promise<Customer>;
|
|
176
|
+
retrieve(idOrEmail: string): Promise<Customer$1>;
|
|
146
177
|
/**
|
|
147
178
|
* Update a customer
|
|
148
179
|
*/
|
|
149
|
-
update(customerId: string, params: CustomerUpdateParams): Promise<Customer>;
|
|
180
|
+
update(customerId: string, params: CustomerUpdateParams): Promise<Customer$1>;
|
|
150
181
|
/**
|
|
151
182
|
* Delete a customer
|
|
152
183
|
*/
|
|
@@ -196,6 +227,7 @@ interface Product {
|
|
|
196
227
|
options: ProductOption[];
|
|
197
228
|
productVariants: ProductVariant[];
|
|
198
229
|
}
|
|
230
|
+
|
|
199
231
|
declare class Products {
|
|
200
232
|
private apiClient;
|
|
201
233
|
constructor(apiKey: string);
|
|
@@ -203,23 +235,13 @@ declare class Products {
|
|
|
203
235
|
retrieve(productId: string): Promise<Product>;
|
|
204
236
|
}
|
|
205
237
|
|
|
206
|
-
|
|
207
|
-
apiKey
|
|
208
|
-
|
|
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
|
-
|
|
218
|
-
declare class BetterStore {
|
|
238
|
+
declare function betterStore(config: {
|
|
239
|
+
apiKey: string;
|
|
240
|
+
}): {
|
|
219
241
|
checkout: Checkout;
|
|
220
242
|
products: Products;
|
|
221
243
|
customer: Customer;
|
|
222
|
-
|
|
223
|
-
|
|
244
|
+
};
|
|
245
|
+
declare function createStoreClient(): Client;
|
|
224
246
|
|
|
225
|
-
export {
|
|
247
|
+
export { type Address, type CheckoutCreateParams, type CheckoutSession, type CheckoutUpdateParams, type Customer$1 as Customer, type CustomerAddress, type CustomerCreateParams, type CustomerUpdateParams, type LineItem, type Product, type ProductOption, ProductStatus, type ProductVariant, type ShippingRate, type VariantOption, createStoreClient, betterStore as default };
|
package/dist/index.js
CHANGED
|
@@ -50,8 +50,9 @@ var __async = (__this, __arguments, generator) => {
|
|
|
50
50
|
// src/index.ts
|
|
51
51
|
var index_exports = {};
|
|
52
52
|
__export(index_exports, {
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
ProductStatus: () => ProductStatus,
|
|
54
|
+
createStoreClient: () => createStoreClient,
|
|
55
|
+
default: () => betterStore
|
|
55
56
|
});
|
|
56
57
|
module.exports = __toCommonJS(index_exports);
|
|
57
58
|
|
|
@@ -99,7 +100,7 @@ var createApiClient = (apiKey) => {
|
|
|
99
100
|
return client;
|
|
100
101
|
};
|
|
101
102
|
|
|
102
|
-
// src/checkout.ts
|
|
103
|
+
// src/checkout/index.ts
|
|
103
104
|
var Checkout = class {
|
|
104
105
|
constructor(apiKey) {
|
|
105
106
|
this.apiClient = createApiClient(apiKey);
|
|
@@ -164,7 +165,97 @@ var Checkout = class {
|
|
|
164
165
|
};
|
|
165
166
|
var checkout_default = Checkout;
|
|
166
167
|
|
|
167
|
-
// src/
|
|
168
|
+
// src/client/index.ts
|
|
169
|
+
var Client = class {
|
|
170
|
+
/**
|
|
171
|
+
* Retrieve a checkout session by ID
|
|
172
|
+
*/
|
|
173
|
+
retrieveCheckout(clientSecret, checkoutId) {
|
|
174
|
+
return __async(this, null, function* () {
|
|
175
|
+
const apiClient = createApiClient(clientSecret);
|
|
176
|
+
const data = yield apiClient.get(
|
|
177
|
+
`/checkout/${checkoutId}`
|
|
178
|
+
);
|
|
179
|
+
return data;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Update a checkout session
|
|
184
|
+
*/
|
|
185
|
+
updateCheckout(clientSecret, checkoutId, params) {
|
|
186
|
+
return __async(this, null, function* () {
|
|
187
|
+
const apiClient = createApiClient(clientSecret);
|
|
188
|
+
const data = yield apiClient.put(
|
|
189
|
+
`/checkout/${checkoutId}`,
|
|
190
|
+
params
|
|
191
|
+
);
|
|
192
|
+
return data;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Get shipping rates for a checkout session
|
|
197
|
+
*/
|
|
198
|
+
getCheckoutShippingRates(clientSecret, checkoutId) {
|
|
199
|
+
return __async(this, null, function* () {
|
|
200
|
+
const apiClient = createApiClient(clientSecret);
|
|
201
|
+
const data = yield apiClient.get(
|
|
202
|
+
`/checkout/shipping/${checkoutId}`
|
|
203
|
+
);
|
|
204
|
+
return data;
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Generate payment secret for a checkout session
|
|
209
|
+
*/
|
|
210
|
+
generateCheckoutsPaymentSecret(clientSecret, checkoutId) {
|
|
211
|
+
return __async(this, null, function* () {
|
|
212
|
+
const apiClient = createApiClient(clientSecret);
|
|
213
|
+
const data = yield apiClient.post(
|
|
214
|
+
`/checkout/payment/${checkoutId}`
|
|
215
|
+
);
|
|
216
|
+
return data;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Create a new customer
|
|
221
|
+
*/
|
|
222
|
+
createCustomer(clientSecret, params) {
|
|
223
|
+
return __async(this, null, function* () {
|
|
224
|
+
const apiClient = createApiClient(clientSecret);
|
|
225
|
+
const data = yield apiClient.post("/customers", params);
|
|
226
|
+
return data;
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Retrieve a customer by ID or email
|
|
231
|
+
*/
|
|
232
|
+
retrieveCustomer(clientSecret, idOrEmail) {
|
|
233
|
+
return __async(this, null, function* () {
|
|
234
|
+
const apiClient = createApiClient(clientSecret);
|
|
235
|
+
const data = yield apiClient.get(`/customers/${idOrEmail}`);
|
|
236
|
+
if (!data) {
|
|
237
|
+
throw new Error("Customer not found");
|
|
238
|
+
}
|
|
239
|
+
return data;
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Update a customer
|
|
244
|
+
*/
|
|
245
|
+
updateCustomer(clientSecret, customerId, params) {
|
|
246
|
+
return __async(this, null, function* () {
|
|
247
|
+
const apiClient = createApiClient(clientSecret);
|
|
248
|
+
const data = yield apiClient.put(
|
|
249
|
+
`/customers/${customerId}`,
|
|
250
|
+
params
|
|
251
|
+
);
|
|
252
|
+
return data;
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
var client_default = Client;
|
|
257
|
+
|
|
258
|
+
// src/customer/index.ts
|
|
168
259
|
var Customer = class {
|
|
169
260
|
constructor(apiKey) {
|
|
170
261
|
this.apiClient = createApiClient(apiKey);
|
|
@@ -183,7 +274,9 @@ var Customer = class {
|
|
|
183
274
|
*/
|
|
184
275
|
retrieve(idOrEmail) {
|
|
185
276
|
return __async(this, null, function* () {
|
|
186
|
-
const data = yield this.apiClient.get(
|
|
277
|
+
const data = yield this.apiClient.get(
|
|
278
|
+
`/customers/${idOrEmail}`
|
|
279
|
+
);
|
|
187
280
|
if (!data) {
|
|
188
281
|
throw new Error("Customer not found");
|
|
189
282
|
}
|
|
@@ -213,7 +306,7 @@ var Customer = class {
|
|
|
213
306
|
};
|
|
214
307
|
var customer_default = Customer;
|
|
215
308
|
|
|
216
|
-
// src/products.ts
|
|
309
|
+
// src/products/index.ts
|
|
217
310
|
var Products = class {
|
|
218
311
|
constructor(apiKey) {
|
|
219
312
|
this.apiClient = createApiClient(apiKey);
|
|
@@ -236,318 +329,30 @@ var Products = class {
|
|
|
236
329
|
};
|
|
237
330
|
var products_default = Products;
|
|
238
331
|
|
|
239
|
-
// src/
|
|
240
|
-
var
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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;
|
|
332
|
+
// src/products/types.ts
|
|
333
|
+
var ProductStatus = /* @__PURE__ */ ((ProductStatus2) => {
|
|
334
|
+
ProductStatus2["DRAFT"] = "DRAFT";
|
|
335
|
+
ProductStatus2["ACTIVE"] = "ACTIVE";
|
|
336
|
+
ProductStatus2["ARCHIVED"] = "ARCHIVED";
|
|
337
|
+
return ProductStatus2;
|
|
338
|
+
})(ProductStatus || {});
|
|
339
|
+
|
|
340
|
+
// src/index.ts
|
|
341
|
+
function betterStore(config) {
|
|
342
|
+
if (!config.apiKey) {
|
|
343
|
+
throw new Error("API key is required.");
|
|
424
344
|
}
|
|
425
345
|
return {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
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
|
-
}
|
|
346
|
+
checkout: new checkout_default(config.apiKey),
|
|
347
|
+
products: new products_default(config.apiKey),
|
|
348
|
+
customer: new customer_default(config.apiKey)
|
|
534
349
|
};
|
|
535
350
|
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
// private apiKey: string;
|
|
540
|
-
constructor(apiKey) {
|
|
541
|
-
if (!apiKey) {
|
|
542
|
-
throw new Error("API key is required.");
|
|
543
|
-
}
|
|
544
|
-
this.checkout = new checkout_default(apiKey);
|
|
545
|
-
this.products = new products_default(apiKey);
|
|
546
|
-
this.customer = new customer_default(apiKey);
|
|
547
|
-
}
|
|
548
|
-
};
|
|
549
|
-
var index_default = BetterStore;
|
|
351
|
+
function createStoreClient() {
|
|
352
|
+
return new client_default();
|
|
353
|
+
}
|
|
550
354
|
// Annotate the CommonJS export names for ESM import in node:
|
|
551
355
|
0 && (module.exports = {
|
|
552
|
-
|
|
356
|
+
ProductStatus,
|
|
357
|
+
createStoreClient
|
|
553
358
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -63,7 +63,7 @@ var createApiClient = (apiKey) => {
|
|
|
63
63
|
return client;
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
// src/checkout.ts
|
|
66
|
+
// src/checkout/index.ts
|
|
67
67
|
var Checkout = class {
|
|
68
68
|
constructor(apiKey) {
|
|
69
69
|
this.apiClient = createApiClient(apiKey);
|
|
@@ -128,7 +128,97 @@ var Checkout = class {
|
|
|
128
128
|
};
|
|
129
129
|
var checkout_default = Checkout;
|
|
130
130
|
|
|
131
|
-
// src/
|
|
131
|
+
// src/client/index.ts
|
|
132
|
+
var Client = class {
|
|
133
|
+
/**
|
|
134
|
+
* Retrieve a checkout session by ID
|
|
135
|
+
*/
|
|
136
|
+
retrieveCheckout(clientSecret, checkoutId) {
|
|
137
|
+
return __async(this, null, function* () {
|
|
138
|
+
const apiClient = createApiClient(clientSecret);
|
|
139
|
+
const data = yield apiClient.get(
|
|
140
|
+
`/checkout/${checkoutId}`
|
|
141
|
+
);
|
|
142
|
+
return data;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Update a checkout session
|
|
147
|
+
*/
|
|
148
|
+
updateCheckout(clientSecret, checkoutId, params) {
|
|
149
|
+
return __async(this, null, function* () {
|
|
150
|
+
const apiClient = createApiClient(clientSecret);
|
|
151
|
+
const data = yield apiClient.put(
|
|
152
|
+
`/checkout/${checkoutId}`,
|
|
153
|
+
params
|
|
154
|
+
);
|
|
155
|
+
return data;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get shipping rates for a checkout session
|
|
160
|
+
*/
|
|
161
|
+
getCheckoutShippingRates(clientSecret, checkoutId) {
|
|
162
|
+
return __async(this, null, function* () {
|
|
163
|
+
const apiClient = createApiClient(clientSecret);
|
|
164
|
+
const data = yield apiClient.get(
|
|
165
|
+
`/checkout/shipping/${checkoutId}`
|
|
166
|
+
);
|
|
167
|
+
return data;
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Generate payment secret for a checkout session
|
|
172
|
+
*/
|
|
173
|
+
generateCheckoutsPaymentSecret(clientSecret, checkoutId) {
|
|
174
|
+
return __async(this, null, function* () {
|
|
175
|
+
const apiClient = createApiClient(clientSecret);
|
|
176
|
+
const data = yield apiClient.post(
|
|
177
|
+
`/checkout/payment/${checkoutId}`
|
|
178
|
+
);
|
|
179
|
+
return data;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Create a new customer
|
|
184
|
+
*/
|
|
185
|
+
createCustomer(clientSecret, params) {
|
|
186
|
+
return __async(this, null, function* () {
|
|
187
|
+
const apiClient = createApiClient(clientSecret);
|
|
188
|
+
const data = yield apiClient.post("/customers", params);
|
|
189
|
+
return data;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Retrieve a customer by ID or email
|
|
194
|
+
*/
|
|
195
|
+
retrieveCustomer(clientSecret, idOrEmail) {
|
|
196
|
+
return __async(this, null, function* () {
|
|
197
|
+
const apiClient = createApiClient(clientSecret);
|
|
198
|
+
const data = yield apiClient.get(`/customers/${idOrEmail}`);
|
|
199
|
+
if (!data) {
|
|
200
|
+
throw new Error("Customer not found");
|
|
201
|
+
}
|
|
202
|
+
return data;
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Update a customer
|
|
207
|
+
*/
|
|
208
|
+
updateCustomer(clientSecret, customerId, params) {
|
|
209
|
+
return __async(this, null, function* () {
|
|
210
|
+
const apiClient = createApiClient(clientSecret);
|
|
211
|
+
const data = yield apiClient.put(
|
|
212
|
+
`/customers/${customerId}`,
|
|
213
|
+
params
|
|
214
|
+
);
|
|
215
|
+
return data;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
var client_default = Client;
|
|
220
|
+
|
|
221
|
+
// src/customer/index.ts
|
|
132
222
|
var Customer = class {
|
|
133
223
|
constructor(apiKey) {
|
|
134
224
|
this.apiClient = createApiClient(apiKey);
|
|
@@ -147,7 +237,9 @@ var Customer = class {
|
|
|
147
237
|
*/
|
|
148
238
|
retrieve(idOrEmail) {
|
|
149
239
|
return __async(this, null, function* () {
|
|
150
|
-
const data = yield this.apiClient.get(
|
|
240
|
+
const data = yield this.apiClient.get(
|
|
241
|
+
`/customers/${idOrEmail}`
|
|
242
|
+
);
|
|
151
243
|
if (!data) {
|
|
152
244
|
throw new Error("Customer not found");
|
|
153
245
|
}
|
|
@@ -177,7 +269,7 @@ var Customer = class {
|
|
|
177
269
|
};
|
|
178
270
|
var customer_default = Customer;
|
|
179
271
|
|
|
180
|
-
// src/products.ts
|
|
272
|
+
// src/products/index.ts
|
|
181
273
|
var Products = class {
|
|
182
274
|
constructor(apiKey) {
|
|
183
275
|
this.apiClient = createApiClient(apiKey);
|
|
@@ -200,318 +292,30 @@ var Products = class {
|
|
|
200
292
|
};
|
|
201
293
|
var products_default = Products;
|
|
202
294
|
|
|
203
|
-
// src/
|
|
204
|
-
var
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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;
|
|
295
|
+
// src/products/types.ts
|
|
296
|
+
var ProductStatus = /* @__PURE__ */ ((ProductStatus2) => {
|
|
297
|
+
ProductStatus2["DRAFT"] = "DRAFT";
|
|
298
|
+
ProductStatus2["ACTIVE"] = "ACTIVE";
|
|
299
|
+
ProductStatus2["ARCHIVED"] = "ARCHIVED";
|
|
300
|
+
return ProductStatus2;
|
|
301
|
+
})(ProductStatus || {});
|
|
302
|
+
|
|
303
|
+
// src/index.ts
|
|
304
|
+
function betterStore(config) {
|
|
305
|
+
if (!config.apiKey) {
|
|
306
|
+
throw new Error("API key is required.");
|
|
388
307
|
}
|
|
389
308
|
return {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
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
|
-
}
|
|
309
|
+
checkout: new checkout_default(config.apiKey),
|
|
310
|
+
products: new products_default(config.apiKey),
|
|
311
|
+
customer: new customer_default(config.apiKey)
|
|
498
312
|
};
|
|
499
313
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
// private apiKey: string;
|
|
504
|
-
constructor(apiKey) {
|
|
505
|
-
if (!apiKey) {
|
|
506
|
-
throw new Error("API key is required.");
|
|
507
|
-
}
|
|
508
|
-
this.checkout = new checkout_default(apiKey);
|
|
509
|
-
this.products = new products_default(apiKey);
|
|
510
|
-
this.customer = new customer_default(apiKey);
|
|
511
|
-
}
|
|
512
|
-
};
|
|
513
|
-
var index_default = BetterStore;
|
|
314
|
+
function createStoreClient() {
|
|
315
|
+
return new client_default();
|
|
316
|
+
}
|
|
514
317
|
export {
|
|
515
|
-
|
|
516
|
-
|
|
318
|
+
ProductStatus,
|
|
319
|
+
createStoreClient,
|
|
320
|
+
betterStore as default
|
|
517
321
|
};
|