commerce-kit 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/README.md +150 -59
- package/dist/index.d.ts +5 -1173
- package/dist/index.js +13 -22
- package/dist/provider-CvZe8Dqo.d.ts +140 -0
- package/dist/stripe.d.ts +77 -0
- package/dist/stripe.js +1 -0
- package/dist/yns-context.d.ts +22 -0
- package/dist/yns-context.js +1 -0
- package/dist/yns.d.ts +95 -18
- package/dist/yns.js +238 -1
- package/package.json +16 -8
package/README.md
CHANGED
|
@@ -7,10 +7,11 @@ Built by [Your Next Store](https://yournextstore.com).
|
|
|
7
7
|
## Features
|
|
8
8
|
|
|
9
9
|
- **Multi-provider**: Switch between Stripe and YNS without changing code
|
|
10
|
-
- **
|
|
10
|
+
- **Class-based**: Industry standard pattern like Stripe, OpenAI, AWS SDK
|
|
11
|
+
- **Zero config**: Works out of the box with environment variables
|
|
11
12
|
- **Type-safe**: Full TypeScript with provider-specific types
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
13
|
+
- **GraphQL support**: Field selection for YNS, REST for Stripe
|
|
14
|
+
- **Multi-instance**: Perfect for testing and multi-tenant apps
|
|
14
15
|
|
|
15
16
|
## Install
|
|
16
17
|
|
|
@@ -20,19 +21,18 @@ npm install commerce-kit
|
|
|
20
21
|
|
|
21
22
|
## Quick start
|
|
22
23
|
|
|
24
|
+
**Zero config** - works automatically with environment variables:
|
|
25
|
+
|
|
23
26
|
```tsx
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
// Set environment variables:
|
|
28
|
+
// STRIPE_SECRET_KEY=sk_test_...
|
|
29
|
+
// or YNS_ENDPOINT=https://api.yournextstore.com and YNS_TOKEN=token_...
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
provider: "stripe", // or "yns"
|
|
30
|
-
stripe: { secretKey: process.env.STRIPE_SECRET_KEY }
|
|
31
|
-
});
|
|
31
|
+
import commerce from "commerce-kit";
|
|
32
|
+
import { formatMoney } from "commerce-kit/currencies";
|
|
32
33
|
|
|
33
|
-
// Use everywhere
|
|
34
34
|
export async function ProductList() {
|
|
35
|
-
const products = await
|
|
35
|
+
const products = await commerce.product.browse({ first: 6 });
|
|
36
36
|
|
|
37
37
|
return (
|
|
38
38
|
<div>
|
|
@@ -47,97 +47,188 @@ export async function ProductList() {
|
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
**Explicit configuration**:
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import { Commerce } from "commerce-kit";
|
|
54
|
+
|
|
55
|
+
// Create your own instance with explicit config
|
|
56
|
+
const commerce = new Commerce({
|
|
57
|
+
provider: "stripe",
|
|
58
|
+
stripe: { secretKey: "sk_test_..." }
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const products = await commerce.product.browse({ first: 6 });
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Usage Patterns
|
|
65
|
+
|
|
66
|
+
### 1. Default Instance (Recommended)
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import commerce from "commerce-kit";
|
|
70
|
+
|
|
71
|
+
// Auto-detects from STRIPE_SECRET_KEY or YNS_ENDPOINT/YNS_TOKEN
|
|
72
|
+
const products = await commerce.product.browse({ first: 10 });
|
|
73
|
+
const cart = await commerce.cart.add({ variantId: "var_123", quantity: 1 });
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 2. Commerce Class
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
import { Commerce } from "commerce-kit";
|
|
80
|
+
|
|
81
|
+
// Zero-config constructor
|
|
82
|
+
const commerce = new Commerce();
|
|
83
|
+
|
|
84
|
+
// Or explicit configuration
|
|
85
|
+
const commerce = new Commerce({
|
|
86
|
+
provider: "stripe",
|
|
87
|
+
stripe: { secretKey: "sk_test_..." }
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const products = await commerce.product.browse({ first: 10 });
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 3. Provider-Specific Classes
|
|
94
|
+
|
|
95
|
+
```tsx
|
|
96
|
+
import { StripeCommerce, YNSCommerce } from "commerce-kit";
|
|
97
|
+
|
|
98
|
+
// Stripe-specific client
|
|
99
|
+
const stripe = new StripeCommerce({
|
|
100
|
+
secretKey: "sk_test_..."
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// YNS-specific client with GraphQL support
|
|
104
|
+
const yns = new YNSCommerce({
|
|
105
|
+
endpoint: "https://api.yournextstore.com",
|
|
106
|
+
token: "token_..."
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const stripeProducts = await stripe.product.browse({ first: 10 });
|
|
110
|
+
const ynsProducts = await yns.product.browse({
|
|
111
|
+
first: 10,
|
|
112
|
+
fields: ["id", "name", "price"] // GraphQL field selection
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## API Reference
|
|
51
117
|
|
|
52
118
|
### Products
|
|
119
|
+
|
|
53
120
|
```tsx
|
|
121
|
+
import { Commerce } from "commerce-kit";
|
|
122
|
+
const commerce = new Commerce();
|
|
123
|
+
|
|
54
124
|
// Browse with filters
|
|
55
|
-
const products = await
|
|
125
|
+
const products = await commerce.product.browse({
|
|
56
126
|
first: 10,
|
|
57
127
|
category: "electronics",
|
|
58
128
|
fields: ["id", "name", "price"] // GraphQL field selection (YNS only)
|
|
59
129
|
});
|
|
60
130
|
|
|
61
131
|
// Get single product
|
|
62
|
-
const product = await
|
|
132
|
+
const product = await commerce.product.get({ slug: "awesome-laptop" });
|
|
63
133
|
|
|
64
|
-
// Search
|
|
65
|
-
const results = await
|
|
134
|
+
// Search (YNS only)
|
|
135
|
+
const results = await commerce.product.search({ query: "macbook" });
|
|
66
136
|
```
|
|
67
137
|
|
|
68
138
|
### Cart
|
|
139
|
+
|
|
69
140
|
```tsx
|
|
141
|
+
import { Commerce } from "commerce-kit";
|
|
142
|
+
const commerce = new Commerce();
|
|
143
|
+
|
|
70
144
|
// Add to cart
|
|
71
|
-
const
|
|
145
|
+
const result = await commerce.cart.add({
|
|
72
146
|
variantId: "variant_123",
|
|
73
147
|
quantity: 2
|
|
74
148
|
});
|
|
75
149
|
|
|
76
150
|
// Update quantity
|
|
77
|
-
await
|
|
78
|
-
cartId:
|
|
151
|
+
await commerce.cart.update({
|
|
152
|
+
cartId: result.cartId,
|
|
79
153
|
variantId: "variant_123",
|
|
80
154
|
quantity: 3
|
|
81
155
|
});
|
|
82
156
|
|
|
83
157
|
// Get cart
|
|
84
|
-
const cartData = await
|
|
158
|
+
const cartData = await commerce.cart.get({ cartId: result.cartId });
|
|
85
159
|
```
|
|
86
160
|
|
|
87
|
-
###
|
|
161
|
+
### Orders (YNS only)
|
|
162
|
+
|
|
88
163
|
```tsx
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
164
|
+
import { YNSCommerce } from "commerce-kit";
|
|
165
|
+
|
|
166
|
+
const yns = new YNSCommerce({
|
|
167
|
+
endpoint: process.env.YNS_ENDPOINT,
|
|
168
|
+
token: process.env.YNS_TOKEN
|
|
93
169
|
});
|
|
94
170
|
|
|
95
|
-
//
|
|
96
|
-
const
|
|
97
|
-
const stripe = Commerce.withProvider("stripe");
|
|
171
|
+
// List orders
|
|
172
|
+
const orders = await yns.order.list({ first: 10 });
|
|
98
173
|
|
|
99
|
-
|
|
100
|
-
const
|
|
174
|
+
// Get single order
|
|
175
|
+
const order = await yns.order.get({ id: "order_123" });
|
|
101
176
|
```
|
|
102
177
|
|
|
103
|
-
##
|
|
178
|
+
## Environment Variables
|
|
104
179
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
180
|
+
Set these environment variables for automatic configuration:
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
# For Stripe
|
|
184
|
+
STRIPE_SECRET_KEY=sk_test_...
|
|
185
|
+
STRIPE_TAG_PREFIX=my-store # optional
|
|
186
|
+
|
|
187
|
+
# For YNS
|
|
188
|
+
YNS_ENDPOINT=https://api.yournextstore.com
|
|
189
|
+
YNS_TOKEN=token_...
|
|
190
|
+
|
|
191
|
+
# Optional: explicitly choose provider
|
|
192
|
+
COMMERCE_PROVIDER=stripe # or "yns"
|
|
114
193
|
```
|
|
115
194
|
|
|
116
|
-
|
|
195
|
+
## Multi-Tenant & Testing
|
|
196
|
+
|
|
197
|
+
Perfect for multi-tenant applications and testing:
|
|
198
|
+
|
|
117
199
|
```tsx
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
200
|
+
// Multi-tenant
|
|
201
|
+
class TenantService {
|
|
202
|
+
getCommerce(tenantId: string) {
|
|
203
|
+
const config = this.getTenantConfig(tenantId);
|
|
204
|
+
return new Commerce(config);
|
|
123
205
|
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Testing
|
|
209
|
+
describe("Product Service", () => {
|
|
210
|
+
it("should fetch products", async () => {
|
|
211
|
+
const commerce = new Commerce({
|
|
212
|
+
provider: "stripe",
|
|
213
|
+
stripe: { secretKey: "sk_test_mock" }
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const products = await commerce.product.browse();
|
|
217
|
+
expect(products).toBeDefined();
|
|
218
|
+
});
|
|
124
219
|
});
|
|
125
220
|
```
|
|
126
221
|
|
|
127
|
-
##
|
|
222
|
+
## Why Class-Based?
|
|
128
223
|
|
|
129
|
-
|
|
130
|
-
```tsx
|
|
131
|
-
const products = await Commerce.productBrowse({ first: 6 });
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
New way (recommended):
|
|
135
|
-
```tsx
|
|
136
|
-
const products = await Commerce.product.browse({ first: 6 });
|
|
137
|
-
```
|
|
224
|
+
Following industry standards from Stripe, OpenAI, and AWS SDK:
|
|
138
225
|
|
|
139
|
-
|
|
226
|
+
- ✅ **Explicit initialization** - Clear where configuration happens
|
|
227
|
+
- ✅ **Multiple instances** - Multi-tenant and testing support
|
|
228
|
+
- ✅ **Type safety** - Better TypeScript integration
|
|
229
|
+
- ✅ **No global state** - Each instance is isolated
|
|
230
|
+
- ✅ **Familiar pattern** - Same as `new Stripe()`, `new OpenAI()`
|
|
140
231
|
|
|
141
232
|
## License
|
|
142
233
|
|
|
143
|
-
AGPL-3.0 – see LICENSE.md
|
|
234
|
+
AGPL-3.0 – see LICENSE.md
|