@spree/sdk 0.1.8 → 0.2.1
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 +164 -95
- package/dist/index.cjs +131 -51
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -23
- package/dist/index.d.ts +61 -23
- package/dist/index.js +130 -52
- package/dist/index.js.map +1 -1
- package/dist/types/index.d.cts +32 -1
- package/dist/types/index.d.ts +32 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,72 +20,84 @@ import { createSpreeClient } from '@spree/sdk';
|
|
|
20
20
|
// Initialize the client
|
|
21
21
|
const client = createSpreeClient({
|
|
22
22
|
baseUrl: 'https://api.mystore.com',
|
|
23
|
-
|
|
23
|
+
publishableKey: 'spree_pk_xxx', // Store API
|
|
24
|
+
secretKey: 'spree_sk_xxx', // Admin API (optional)
|
|
24
25
|
});
|
|
25
26
|
|
|
26
|
-
// Browse products
|
|
27
|
-
const products = await client.products.list({
|
|
27
|
+
// Browse products (Store API)
|
|
28
|
+
const products = await client.store.products.list({
|
|
28
29
|
per_page: 10,
|
|
29
30
|
includes: 'variants,images',
|
|
30
31
|
});
|
|
31
32
|
|
|
32
33
|
// Get a single product
|
|
33
|
-
const product = await client.products.get('ruby-on-rails-tote');
|
|
34
|
+
const product = await client.store.products.get('ruby-on-rails-tote');
|
|
34
35
|
|
|
35
36
|
// Authentication
|
|
36
|
-
const { token, user } = await client.auth.login({
|
|
37
|
+
const { token, user } = await client.store.auth.login({
|
|
37
38
|
email: 'customer@example.com',
|
|
38
39
|
password: 'password123',
|
|
39
40
|
});
|
|
40
41
|
|
|
41
42
|
// Create a cart and add items
|
|
42
|
-
const cart = await client.cart.create();
|
|
43
|
-
await client.orders.lineItems.create(cart.id, {
|
|
43
|
+
const cart = await client.store.cart.create();
|
|
44
|
+
await client.store.orders.lineItems.create(cart.id, {
|
|
44
45
|
variant_id: 'var_abc123',
|
|
45
46
|
quantity: 2,
|
|
46
47
|
}, { orderToken: cart.token });
|
|
47
48
|
|
|
48
49
|
// Checkout flow
|
|
49
|
-
await client.orders.next(cart.id, { orderToken: cart.token });
|
|
50
|
-
await client.orders.complete(cart.id, { orderToken: cart.token });
|
|
50
|
+
await client.store.orders.next(cart.id, { orderToken: cart.token });
|
|
51
|
+
await client.store.orders.complete(cart.id, { orderToken: cart.token });
|
|
51
52
|
```
|
|
52
53
|
|
|
54
|
+
## Client Architecture
|
|
55
|
+
|
|
56
|
+
The SDK exposes two API namespaces:
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
client.store // Store API — customer-facing (products, cart, checkout, account)
|
|
60
|
+
client.admin // Admin API — administrative (coming soon)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
All Store API endpoints live under `client.store.*`. The Admin API namespace is ready for future endpoints.
|
|
64
|
+
|
|
53
65
|
## Authentication
|
|
54
66
|
|
|
55
67
|
The SDK supports multiple authentication modes:
|
|
56
68
|
|
|
57
|
-
### 1.
|
|
69
|
+
### 1. Publishable Key Only (Guest/Public Access)
|
|
58
70
|
|
|
59
71
|
```typescript
|
|
60
72
|
const client = createSpreeClient({
|
|
61
73
|
baseUrl: 'https://api.mystore.com',
|
|
62
|
-
|
|
74
|
+
publishableKey: 'spree_pk_xxx',
|
|
63
75
|
});
|
|
64
76
|
|
|
65
77
|
// Public endpoints work without user authentication
|
|
66
|
-
const products = await client.products.list();
|
|
78
|
+
const products = await client.store.products.list();
|
|
67
79
|
```
|
|
68
80
|
|
|
69
|
-
### 2.
|
|
81
|
+
### 2. Publishable Key + JWT (Authenticated Customer)
|
|
70
82
|
|
|
71
83
|
```typescript
|
|
72
84
|
// Login to get tokens
|
|
73
|
-
const { token, user } = await client.auth.login({
|
|
85
|
+
const { token, user } = await client.store.auth.login({
|
|
74
86
|
email: 'customer@example.com',
|
|
75
87
|
password: 'password123',
|
|
76
88
|
});
|
|
77
89
|
|
|
78
90
|
// Use token for authenticated requests
|
|
79
|
-
const orders = await client.orders.list({}, { token });
|
|
91
|
+
const orders = await client.store.orders.list({}, { token });
|
|
80
92
|
|
|
81
93
|
// Refresh token when needed
|
|
82
|
-
const newTokens = await client.auth.refresh({ token });
|
|
94
|
+
const newTokens = await client.store.auth.refresh({ token });
|
|
83
95
|
```
|
|
84
96
|
|
|
85
97
|
### 3. Register New Customer
|
|
86
98
|
|
|
87
99
|
```typescript
|
|
88
|
-
const { token, user } = await client.auth.register({
|
|
100
|
+
const { token, user } = await client.store.auth.register({
|
|
89
101
|
email: 'new@example.com',
|
|
90
102
|
password: 'password123',
|
|
91
103
|
password_confirmation: 'password123',
|
|
@@ -100,24 +112,24 @@ For guest checkout, use the `token` (or `order_token`) returned when creating a
|
|
|
100
112
|
|
|
101
113
|
```typescript
|
|
102
114
|
// Create a cart (guest)
|
|
103
|
-
const cart = await client.cart.create();
|
|
115
|
+
const cart = await client.store.cart.create();
|
|
104
116
|
|
|
105
117
|
// Use orderToken for all cart operations
|
|
106
118
|
const options = { orderToken: cart.token };
|
|
107
119
|
|
|
108
120
|
// Add items
|
|
109
|
-
await client.orders.lineItems.create(cart.id, {
|
|
121
|
+
await client.store.orders.lineItems.create(cart.id, {
|
|
110
122
|
variant_id: 'var_abc123',
|
|
111
123
|
quantity: 1,
|
|
112
124
|
}, options);
|
|
113
125
|
|
|
114
126
|
// Update order with email
|
|
115
|
-
await client.orders.update(cart.id, {
|
|
127
|
+
await client.store.orders.update(cart.id, {
|
|
116
128
|
email: 'guest@example.com',
|
|
117
129
|
}, options);
|
|
118
130
|
|
|
119
131
|
// Complete checkout
|
|
120
|
-
await client.orders.complete(cart.id, options);
|
|
132
|
+
await client.store.orders.complete(cart.id, options);
|
|
121
133
|
```
|
|
122
134
|
|
|
123
135
|
## API Reference
|
|
@@ -126,14 +138,14 @@ await client.orders.complete(cart.id, options);
|
|
|
126
138
|
|
|
127
139
|
```typescript
|
|
128
140
|
// Get current store information
|
|
129
|
-
const store = await client.store.get();
|
|
141
|
+
const store = await client.store.store.get();
|
|
130
142
|
```
|
|
131
143
|
|
|
132
144
|
### Products
|
|
133
145
|
|
|
134
146
|
```typescript
|
|
135
147
|
// List products with filtering
|
|
136
|
-
const products = await client.products.list({
|
|
148
|
+
const products = await client.store.products.list({
|
|
137
149
|
page: 1,
|
|
138
150
|
per_page: 25,
|
|
139
151
|
'q[name_cont]': 'shirt',
|
|
@@ -141,12 +153,12 @@ const products = await client.products.list({
|
|
|
141
153
|
});
|
|
142
154
|
|
|
143
155
|
// Get single product by ID or slug
|
|
144
|
-
const product = await client.products.get('ruby-on-rails-tote', {
|
|
156
|
+
const product = await client.store.products.get('ruby-on-rails-tote', {
|
|
145
157
|
includes: 'variants,images',
|
|
146
158
|
});
|
|
147
159
|
|
|
148
160
|
// Get available filters (price range, availability, options, taxons)
|
|
149
|
-
const filters = await client.products.filters({
|
|
161
|
+
const filters = await client.store.products.filters({
|
|
150
162
|
taxon_id: 'txn_abc123', // Optional: scope filters to a taxon
|
|
151
163
|
});
|
|
152
164
|
```
|
|
@@ -155,28 +167,28 @@ const filters = await client.products.filters({
|
|
|
155
167
|
|
|
156
168
|
```typescript
|
|
157
169
|
// List taxonomies
|
|
158
|
-
const taxonomies = await client.taxonomies.list({
|
|
170
|
+
const taxonomies = await client.store.taxonomies.list({
|
|
159
171
|
includes: 'taxons',
|
|
160
172
|
});
|
|
161
173
|
|
|
162
174
|
// Get taxonomy with taxons
|
|
163
|
-
const categories = await client.taxonomies.get('tax_123', {
|
|
175
|
+
const categories = await client.store.taxonomies.get('tax_123', {
|
|
164
176
|
includes: 'root,taxons',
|
|
165
177
|
});
|
|
166
178
|
|
|
167
179
|
// List taxons with filtering
|
|
168
|
-
const taxons = await client.taxons.list({
|
|
180
|
+
const taxons = await client.store.taxons.list({
|
|
169
181
|
'q[depth_eq]': 1, // Top-level categories only
|
|
170
182
|
'q[taxonomy_id_eq]': '123', // Filter by taxonomy
|
|
171
183
|
});
|
|
172
184
|
|
|
173
185
|
// Get single taxon by ID or permalink
|
|
174
|
-
const taxon = await client.taxons.get('categories/clothing', {
|
|
186
|
+
const taxon = await client.store.taxons.get('categories/clothing', {
|
|
175
187
|
includes: 'ancestors,children', // For breadcrumbs and subcategories
|
|
176
188
|
});
|
|
177
189
|
|
|
178
190
|
// List products in a category
|
|
179
|
-
const categoryProducts = await client.taxons.products.list('categories/clothing', {
|
|
191
|
+
const categoryProducts = await client.store.taxons.products.list('categories/clothing', {
|
|
180
192
|
page: 1,
|
|
181
193
|
per_page: 12,
|
|
182
194
|
includes: 'images,default_variant',
|
|
@@ -187,14 +199,14 @@ const categoryProducts = await client.taxons.products.list('categories/clothing'
|
|
|
187
199
|
|
|
188
200
|
```typescript
|
|
189
201
|
// Get current cart
|
|
190
|
-
const cart = await client.cart.get({ orderToken: 'xxx' });
|
|
202
|
+
const cart = await client.store.cart.get({ orderToken: 'xxx' });
|
|
191
203
|
|
|
192
204
|
// Create a new cart
|
|
193
|
-
const newCart = await client.cart.create();
|
|
205
|
+
const newCart = await client.store.cart.create();
|
|
194
206
|
|
|
195
207
|
// Associate guest cart with authenticated user
|
|
196
208
|
// (after user logs in, merge their guest cart with their account)
|
|
197
|
-
await client.cart.associate({
|
|
209
|
+
await client.store.cart.associate({
|
|
198
210
|
token: jwtToken, // User's JWT token
|
|
199
211
|
orderToken: cart.token, // Guest cart token
|
|
200
212
|
});
|
|
@@ -204,19 +216,19 @@ await client.cart.associate({
|
|
|
204
216
|
|
|
205
217
|
```typescript
|
|
206
218
|
// List orders for authenticated customer
|
|
207
|
-
const orders = await client.orders.list({}, { token });
|
|
219
|
+
const orders = await client.store.orders.list({}, { token });
|
|
208
220
|
|
|
209
221
|
// Create a new order (cart)
|
|
210
|
-
const cart = await client.orders.create();
|
|
222
|
+
const cart = await client.store.orders.create();
|
|
211
223
|
const options = { orderToken: cart.order_token };
|
|
212
224
|
|
|
213
225
|
// Get order by ID or number
|
|
214
|
-
const order = await client.orders.get('R123456789', {
|
|
226
|
+
const order = await client.store.orders.get('R123456789', {
|
|
215
227
|
includes: 'line_items,shipments',
|
|
216
228
|
}, options);
|
|
217
229
|
|
|
218
230
|
// Update order (email, addresses)
|
|
219
|
-
await client.orders.update(cart.id, {
|
|
231
|
+
await client.store.orders.update(cart.id, {
|
|
220
232
|
email: 'customer@example.com',
|
|
221
233
|
ship_address: {
|
|
222
234
|
firstname: 'John',
|
|
@@ -232,9 +244,9 @@ await client.orders.update(cart.id, {
|
|
|
232
244
|
}, options);
|
|
233
245
|
|
|
234
246
|
// Checkout flow
|
|
235
|
-
await client.orders.next(cart.id, options); // Move to next step
|
|
236
|
-
await client.orders.advance(cart.id, options); // Advance through all steps
|
|
237
|
-
await client.orders.complete(cart.id, options); // Complete the order
|
|
247
|
+
await client.store.orders.next(cart.id, options); // Move to next step
|
|
248
|
+
await client.store.orders.advance(cart.id, options); // Advance through all steps
|
|
249
|
+
await client.store.orders.complete(cart.id, options); // Complete the order
|
|
238
250
|
```
|
|
239
251
|
|
|
240
252
|
### Line Items
|
|
@@ -243,18 +255,18 @@ await client.orders.complete(cart.id, options); // Complete the order
|
|
|
243
255
|
const options = { orderToken: cart.token };
|
|
244
256
|
|
|
245
257
|
// Add item
|
|
246
|
-
await client.orders.lineItems.create(cart.id, {
|
|
258
|
+
await client.store.orders.lineItems.create(cart.id, {
|
|
247
259
|
variant_id: 'var_123',
|
|
248
260
|
quantity: 2,
|
|
249
261
|
}, options);
|
|
250
262
|
|
|
251
263
|
// Update item quantity
|
|
252
|
-
await client.orders.lineItems.update(cart.id, lineItemId, {
|
|
264
|
+
await client.store.orders.lineItems.update(cart.id, lineItemId, {
|
|
253
265
|
quantity: 3,
|
|
254
266
|
}, options);
|
|
255
267
|
|
|
256
268
|
// Remove item
|
|
257
|
-
await client.orders.lineItems.delete(cart.id, lineItemId, options);
|
|
269
|
+
await client.store.orders.lineItems.delete(cart.id, lineItemId, options);
|
|
258
270
|
```
|
|
259
271
|
|
|
260
272
|
### Coupon Codes
|
|
@@ -263,10 +275,10 @@ await client.orders.lineItems.delete(cart.id, lineItemId, options);
|
|
|
263
275
|
const options = { orderToken: cart.token };
|
|
264
276
|
|
|
265
277
|
// Apply a coupon code
|
|
266
|
-
await client.orders.couponCodes.apply(cart.id, 'SAVE20', options);
|
|
278
|
+
await client.store.orders.couponCodes.apply(cart.id, 'SAVE20', options);
|
|
267
279
|
|
|
268
280
|
// Remove a coupon code
|
|
269
|
-
await client.orders.couponCodes.remove(cart.id, 'promo_xxx', options);
|
|
281
|
+
await client.store.orders.couponCodes.remove(cart.id, 'promo_xxx', options);
|
|
270
282
|
```
|
|
271
283
|
|
|
272
284
|
### Store Credits
|
|
@@ -275,13 +287,13 @@ await client.orders.couponCodes.remove(cart.id, 'promo_xxx', options);
|
|
|
275
287
|
const options = { orderToken: cart.token };
|
|
276
288
|
|
|
277
289
|
// Apply store credit to order (applies maximum available by default)
|
|
278
|
-
await client.orders.addStoreCredit(cart.id, undefined, options);
|
|
290
|
+
await client.store.orders.addStoreCredit(cart.id, undefined, options);
|
|
279
291
|
|
|
280
292
|
// Apply specific amount of store credit
|
|
281
|
-
await client.orders.addStoreCredit(cart.id, 25.00, options);
|
|
293
|
+
await client.store.orders.addStoreCredit(cart.id, 25.00, options);
|
|
282
294
|
|
|
283
295
|
// Remove store credit from order
|
|
284
|
-
await client.orders.removeStoreCredit(cart.id, options);
|
|
296
|
+
await client.store.orders.removeStoreCredit(cart.id, options);
|
|
285
297
|
```
|
|
286
298
|
|
|
287
299
|
### Shipments
|
|
@@ -290,10 +302,10 @@ await client.orders.removeStoreCredit(cart.id, options);
|
|
|
290
302
|
const options = { orderToken: cart.token };
|
|
291
303
|
|
|
292
304
|
// List shipments for an order
|
|
293
|
-
const shipments = await client.orders.shipments.list(cart.id, options);
|
|
305
|
+
const shipments = await client.store.orders.shipments.list(cart.id, options);
|
|
294
306
|
|
|
295
307
|
// Select a shipping rate
|
|
296
|
-
await client.orders.shipments.update(cart.id, shipmentId, {
|
|
308
|
+
await client.store.orders.shipments.update(cart.id, shipmentId, {
|
|
297
309
|
selected_shipping_rate_id: 'rate_xxx',
|
|
298
310
|
}, options);
|
|
299
311
|
```
|
|
@@ -304,23 +316,61 @@ await client.orders.shipments.update(cart.id, shipmentId, {
|
|
|
304
316
|
const options = { orderToken: cart.token };
|
|
305
317
|
|
|
306
318
|
// Get available payment methods for an order
|
|
307
|
-
const methods = await client.orders.paymentMethods.list(cart.id, options);
|
|
319
|
+
const methods = await client.store.orders.paymentMethods.list(cart.id, options);
|
|
308
320
|
|
|
309
321
|
// List payments on an order
|
|
310
|
-
const payments = await client.orders.payments.list(cart.id, options);
|
|
322
|
+
const payments = await client.store.orders.payments.list(cart.id, options);
|
|
311
323
|
|
|
312
324
|
// Get a specific payment
|
|
313
|
-
const payment = await client.orders.payments.get(cart.id, paymentId, options);
|
|
325
|
+
const payment = await client.store.orders.payments.get(cart.id, paymentId, options);
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Payment Sessions
|
|
329
|
+
|
|
330
|
+
Payment sessions provide a unified, provider-agnostic interface for payment processing. They work with any payment gateway (Stripe, Adyen, PayPal, etc.) through a single API.
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
const options = { orderToken: cart.token };
|
|
334
|
+
|
|
335
|
+
// Create a payment session (initializes a session with the payment gateway)
|
|
336
|
+
const session = await client.store.orders.paymentSessions.create(cart.id, {
|
|
337
|
+
payment_method_id: 'pm_xxx',
|
|
338
|
+
amount: '99.99', // Optional, defaults to order total
|
|
339
|
+
external_data: { // Optional, provider-specific data
|
|
340
|
+
return_url: 'https://mystore.com/checkout/complete',
|
|
341
|
+
},
|
|
342
|
+
}, options);
|
|
343
|
+
|
|
344
|
+
// The session contains provider-specific data (e.g., Stripe client_secret)
|
|
345
|
+
console.log(session.external_data.client_secret);
|
|
346
|
+
|
|
347
|
+
// Get a payment session
|
|
348
|
+
const existing = await client.store.orders.paymentSessions.get(
|
|
349
|
+
cart.id, session.id, options
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
// Update a payment session (e.g., after order total changes)
|
|
353
|
+
await client.store.orders.paymentSessions.update(cart.id, session.id, {
|
|
354
|
+
amount: '149.99',
|
|
355
|
+
}, options);
|
|
356
|
+
|
|
357
|
+
// Complete the payment session (after customer confirms payment on the frontend)
|
|
358
|
+
const completed = await client.store.orders.paymentSessions.complete(
|
|
359
|
+
cart.id, session.id,
|
|
360
|
+
{ session_result: 'success' },
|
|
361
|
+
options
|
|
362
|
+
);
|
|
363
|
+
console.log(completed.status); // 'completed'
|
|
314
364
|
```
|
|
315
365
|
|
|
316
366
|
### Geography
|
|
317
367
|
|
|
318
368
|
```typescript
|
|
319
369
|
// List countries available for checkout
|
|
320
|
-
const { data: countries } = await client.countries.list();
|
|
370
|
+
const { data: countries } = await client.store.countries.list();
|
|
321
371
|
|
|
322
372
|
// Get country by ISO code (includes states)
|
|
323
|
-
const usa = await client.countries.get('US');
|
|
373
|
+
const usa = await client.store.countries.get('US');
|
|
324
374
|
console.log(usa.states); // Array of states
|
|
325
375
|
```
|
|
326
376
|
|
|
@@ -330,10 +380,10 @@ console.log(usa.states); // Array of states
|
|
|
330
380
|
const options = { token: jwtToken };
|
|
331
381
|
|
|
332
382
|
// Get profile
|
|
333
|
-
const profile = await client.customer.get(options);
|
|
383
|
+
const profile = await client.store.customer.get(options);
|
|
334
384
|
|
|
335
385
|
// Update profile
|
|
336
|
-
await client.customer.update({
|
|
386
|
+
await client.store.customer.update({
|
|
337
387
|
first_name: 'John',
|
|
338
388
|
last_name: 'Doe',
|
|
339
389
|
}, options);
|
|
@@ -345,13 +395,13 @@ await client.customer.update({
|
|
|
345
395
|
const options = { token: jwtToken };
|
|
346
396
|
|
|
347
397
|
// List addresses
|
|
348
|
-
const { data: addresses } = await client.customer.addresses.list({}, options);
|
|
398
|
+
const { data: addresses } = await client.store.customer.addresses.list({}, options);
|
|
349
399
|
|
|
350
400
|
// Get address by ID
|
|
351
|
-
const address = await client.customer.addresses.get('addr_xxx', options);
|
|
401
|
+
const address = await client.store.customer.addresses.get('addr_xxx', options);
|
|
352
402
|
|
|
353
403
|
// Create address
|
|
354
|
-
await client.customer.addresses.create({
|
|
404
|
+
await client.store.customer.addresses.create({
|
|
355
405
|
firstname: 'John',
|
|
356
406
|
lastname: 'Doe',
|
|
357
407
|
address1: '123 Main St',
|
|
@@ -362,10 +412,14 @@ await client.customer.addresses.create({
|
|
|
362
412
|
}, options);
|
|
363
413
|
|
|
364
414
|
// Update address
|
|
365
|
-
await client.customer.addresses.update('addr_xxx', { city: 'Brooklyn' }, options);
|
|
415
|
+
await client.store.customer.addresses.update('addr_xxx', { city: 'Brooklyn' }, options);
|
|
366
416
|
|
|
367
417
|
// Delete address
|
|
368
|
-
await client.customer.addresses.delete('addr_xxx', options);
|
|
418
|
+
await client.store.customer.addresses.delete('addr_xxx', options);
|
|
419
|
+
|
|
420
|
+
// Mark as default billing or shipping address
|
|
421
|
+
await client.store.customer.addresses.markAsDefault('addr_xxx', 'billing', options);
|
|
422
|
+
await client.store.customer.addresses.markAsDefault('addr_xxx', 'shipping', options);
|
|
369
423
|
```
|
|
370
424
|
|
|
371
425
|
### Customer Credit Cards
|
|
@@ -374,13 +428,13 @@ await client.customer.addresses.delete('addr_xxx', options);
|
|
|
374
428
|
const options = { token: jwtToken };
|
|
375
429
|
|
|
376
430
|
// List saved credit cards
|
|
377
|
-
const { data: cards } = await client.customer.creditCards.list({}, options);
|
|
431
|
+
const { data: cards } = await client.store.customer.creditCards.list({}, options);
|
|
378
432
|
|
|
379
433
|
// Get credit card by ID
|
|
380
|
-
const card = await client.customer.creditCards.get('cc_xxx', options);
|
|
434
|
+
const card = await client.store.customer.creditCards.get('cc_xxx', options);
|
|
381
435
|
|
|
382
436
|
// Delete credit card
|
|
383
|
-
await client.customer.creditCards.delete('cc_xxx', options);
|
|
437
|
+
await client.store.customer.creditCards.delete('cc_xxx', options);
|
|
384
438
|
```
|
|
385
439
|
|
|
386
440
|
### Wishlists
|
|
@@ -389,26 +443,26 @@ await client.customer.creditCards.delete('cc_xxx', options);
|
|
|
389
443
|
const options = { token: jwtToken };
|
|
390
444
|
|
|
391
445
|
// List wishlists
|
|
392
|
-
const { data: wishlists } = await client.wishlists.list({}, options);
|
|
446
|
+
const { data: wishlists } = await client.store.wishlists.list({}, options);
|
|
393
447
|
|
|
394
448
|
// Get wishlist by ID
|
|
395
|
-
const wishlist = await client.wishlists.get('wl_xxx', {
|
|
449
|
+
const wishlist = await client.store.wishlists.get('wl_xxx', {
|
|
396
450
|
includes: 'wished_items',
|
|
397
451
|
}, options);
|
|
398
452
|
|
|
399
453
|
// Create wishlist
|
|
400
|
-
const newWishlist = await client.wishlists.create({
|
|
454
|
+
const newWishlist = await client.store.wishlists.create({
|
|
401
455
|
name: 'Birthday Ideas',
|
|
402
456
|
is_private: true,
|
|
403
457
|
}, options);
|
|
404
458
|
|
|
405
459
|
// Update wishlist
|
|
406
|
-
await client.wishlists.update('wl_xxx', {
|
|
460
|
+
await client.store.wishlists.update('wl_xxx', {
|
|
407
461
|
name: 'Updated Name',
|
|
408
462
|
}, options);
|
|
409
463
|
|
|
410
464
|
// Delete wishlist
|
|
411
|
-
await client.wishlists.delete('wl_xxx', options);
|
|
465
|
+
await client.store.wishlists.delete('wl_xxx', options);
|
|
412
466
|
```
|
|
413
467
|
|
|
414
468
|
### Wishlist Items
|
|
@@ -417,18 +471,18 @@ await client.wishlists.delete('wl_xxx', options);
|
|
|
417
471
|
const options = { token: jwtToken };
|
|
418
472
|
|
|
419
473
|
// Add item to wishlist
|
|
420
|
-
await client.wishlists.items.create('wl_xxx', {
|
|
474
|
+
await client.store.wishlists.items.create('wl_xxx', {
|
|
421
475
|
variant_id: 'var_123',
|
|
422
476
|
quantity: 1,
|
|
423
477
|
}, options);
|
|
424
478
|
|
|
425
479
|
// Update item quantity
|
|
426
|
-
await client.wishlists.items.update('wl_xxx', 'wi_xxx', {
|
|
480
|
+
await client.store.wishlists.items.update('wl_xxx', 'wi_xxx', {
|
|
427
481
|
quantity: 2,
|
|
428
482
|
}, options);
|
|
429
483
|
|
|
430
484
|
// Remove item from wishlist
|
|
431
|
-
await client.wishlists.items.delete('wl_xxx', 'wi_xxx', options);
|
|
485
|
+
await client.store.wishlists.items.delete('wl_xxx', 'wi_xxx', options);
|
|
432
486
|
```
|
|
433
487
|
|
|
434
488
|
## Nested Resources
|
|
@@ -437,25 +491,27 @@ The SDK uses a resource builder pattern for nested resources:
|
|
|
437
491
|
|
|
438
492
|
| Parent Resource | Nested Resource | Available Methods |
|
|
439
493
|
|-----------------|-----------------|-------------------|
|
|
440
|
-
| `orders` | `lineItems` | `create`, `update`, `delete` |
|
|
441
|
-
| `orders` | `payments` | `list`, `get` |
|
|
442
|
-
| `orders` | `paymentMethods` | `list` |
|
|
443
|
-
| `orders` | `
|
|
444
|
-
| `orders` | `
|
|
445
|
-
| `
|
|
446
|
-
| `customer` | `
|
|
447
|
-
| `
|
|
448
|
-
| `
|
|
494
|
+
| `store.orders` | `lineItems` | `create`, `update`, `delete` |
|
|
495
|
+
| `store.orders` | `payments` | `list`, `get` |
|
|
496
|
+
| `store.orders` | `paymentMethods` | `list` |
|
|
497
|
+
| `store.orders` | `paymentSessions` | `create`, `get`, `update`, `complete` |
|
|
498
|
+
| `store.orders` | `shipments` | `list`, `update` |
|
|
499
|
+
| `store.orders` | `couponCodes` | `apply`, `remove` |
|
|
500
|
+
| `store.customer` | `addresses` | `list`, `get`, `create`, `update`, `delete`, `markAsDefault` |
|
|
501
|
+
| `store.customer` | `creditCards` | `list`, `get`, `delete` |
|
|
502
|
+
| `store.customer` | `giftCards` | `list`, `get` |
|
|
503
|
+
| `store.taxons` | `products` | `list` |
|
|
504
|
+
| `store.wishlists` | `items` | `create`, `update`, `delete` |
|
|
449
505
|
|
|
450
506
|
Example:
|
|
451
507
|
```typescript
|
|
452
|
-
// Nested resources follow the pattern: client.parent.nested.method(parentId, ...)
|
|
453
|
-
await client.orders.lineItems.create(orderId, params, options);
|
|
454
|
-
await client.orders.payments.list(orderId, options);
|
|
455
|
-
await client.orders.shipments.update(orderId, shipmentId, params, options);
|
|
456
|
-
await client.customer.addresses.list({}, options);
|
|
457
|
-
await client.taxons.products.list(taxonId, params, options);
|
|
458
|
-
await client.wishlists.items.create(wishlistId, params, options);
|
|
508
|
+
// Nested resources follow the pattern: client.store.parent.nested.method(parentId, ...)
|
|
509
|
+
await client.store.orders.lineItems.create(orderId, params, options);
|
|
510
|
+
await client.store.orders.payments.list(orderId, options);
|
|
511
|
+
await client.store.orders.shipments.update(orderId, shipmentId, params, options);
|
|
512
|
+
await client.store.customer.addresses.list({}, options);
|
|
513
|
+
await client.store.taxons.products.list(taxonId, params, options);
|
|
514
|
+
await client.store.wishlists.items.create(wishlistId, params, options);
|
|
459
515
|
```
|
|
460
516
|
|
|
461
517
|
## Localization & Currency
|
|
@@ -464,13 +520,13 @@ Pass locale and currency headers with any request:
|
|
|
464
520
|
|
|
465
521
|
```typescript
|
|
466
522
|
// Set locale and currency per request
|
|
467
|
-
const products = await client.products.list({}, {
|
|
523
|
+
const products = await client.store.products.list({}, {
|
|
468
524
|
locale: 'fr',
|
|
469
525
|
currency: 'EUR',
|
|
470
526
|
});
|
|
471
527
|
|
|
472
528
|
// Works with all endpoints
|
|
473
|
-
const taxon = await client.taxons.get('categories/clothing', {
|
|
529
|
+
const taxon = await client.store.taxons.get('categories/clothing', {
|
|
474
530
|
includes: 'ancestors',
|
|
475
531
|
}, {
|
|
476
532
|
locale: 'de',
|
|
@@ -484,7 +540,7 @@ const taxon = await client.taxons.get('categories/clothing', {
|
|
|
484
540
|
import { SpreeError } from '@spree/sdk';
|
|
485
541
|
|
|
486
542
|
try {
|
|
487
|
-
await client.products.get('non-existent');
|
|
543
|
+
await client.store.products.get('non-existent');
|
|
488
544
|
} catch (error) {
|
|
489
545
|
if (error instanceof SpreeError) {
|
|
490
546
|
console.log(error.code); // 'record_not_found'
|
|
@@ -513,8 +569,8 @@ import type {
|
|
|
513
569
|
} from '@spree/sdk';
|
|
514
570
|
|
|
515
571
|
// All responses are fully typed
|
|
516
|
-
const products: PaginatedResponse<StoreProduct> = await client.products.list();
|
|
517
|
-
const taxon: StoreTaxon = await client.taxons.get('clothing');
|
|
572
|
+
const products: PaginatedResponse<StoreProduct> = await client.store.products.list();
|
|
573
|
+
const taxon: StoreTaxon = await client.store.taxons.get('clothing');
|
|
518
574
|
```
|
|
519
575
|
|
|
520
576
|
## Available Types
|
|
@@ -537,10 +593,12 @@ The SDK exports all Store API types:
|
|
|
537
593
|
### Commerce Types
|
|
538
594
|
- `StorePayment` - Payment record
|
|
539
595
|
- `StorePaymentMethod` - Payment method
|
|
596
|
+
- `StorePaymentSession` - Provider-agnostic payment session
|
|
540
597
|
- `StoreShipment` - Shipment record
|
|
541
598
|
- `StoreShippingRate` - Shipping rate option
|
|
542
599
|
- `StoreShippingMethod` - Shipping method
|
|
543
600
|
- `StoreCreditCard` - Saved credit card
|
|
601
|
+
- `StoreGiftCard` - Gift card
|
|
544
602
|
|
|
545
603
|
### Product Types
|
|
546
604
|
- `StoreImage` - Product image
|
|
@@ -553,10 +611,21 @@ The SDK exports all Store API types:
|
|
|
553
611
|
- `StoreWishlist` - Wishlist
|
|
554
612
|
- `StoreWishedItem` - Wishlist item
|
|
555
613
|
|
|
614
|
+
### Client Types
|
|
615
|
+
- `SpreeClient` - Main client class
|
|
616
|
+
- `StoreClient` - Store API client
|
|
617
|
+
- `AdminClient` - Admin API client
|
|
618
|
+
- `SpreeClientConfig` - Client configuration
|
|
619
|
+
- `RequestOptions` - Per-request options
|
|
620
|
+
- `RetryConfig` - Retry behavior configuration
|
|
621
|
+
|
|
556
622
|
### Utility Types
|
|
557
623
|
- `PaginatedResponse<T>` - Paginated API response
|
|
558
624
|
- `AuthTokens` - JWT tokens from login
|
|
559
625
|
- `AddressParams` - Address input parameters
|
|
626
|
+
- `CreatePaymentSessionParams` - Payment session creation parameters
|
|
627
|
+
- `UpdatePaymentSessionParams` - Payment session update parameters
|
|
628
|
+
- `CompletePaymentSessionParams` - Payment session completion parameters
|
|
560
629
|
- `ProductFiltersResponse` - Product filters response
|
|
561
630
|
|
|
562
631
|
## Custom Fetch
|
|
@@ -568,7 +637,7 @@ import { createSpreeClient } from '@spree/sdk';
|
|
|
568
637
|
|
|
569
638
|
const client = createSpreeClient({
|
|
570
639
|
baseUrl: 'https://api.mystore.com',
|
|
571
|
-
|
|
640
|
+
publishableKey: 'spree_pk_xxx',
|
|
572
641
|
fetch: customFetchImplementation,
|
|
573
642
|
});
|
|
574
643
|
```
|