@reactionary/provider-commercetools 0.0.83 → 0.0.84
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/core/client.js +20 -3
- package/core/token-cache.js +0 -2
- package/package.json +2 -2
- package/providers/cart.provider.js +19 -7
- package/providers/category.provider.js +1 -1
- package/providers/checkout.provider.js +70 -33
- package/providers/product.provider.js +27 -18
- package/src/core/client.d.ts +3 -6
package/core/client.js
CHANGED
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
import { randomUUID } from "crypto";
|
|
6
6
|
import {
|
|
7
7
|
AnonymousIdentitySchema,
|
|
8
|
-
GuestIdentitySchema,
|
|
9
8
|
RegisteredIdentitySchema
|
|
10
9
|
} from "@reactionary/core";
|
|
11
10
|
import * as crypto from "crypto";
|
|
@@ -113,7 +112,16 @@ class CommercetoolsClient {
|
|
|
113
112
|
}
|
|
114
113
|
async logout() {
|
|
115
114
|
await this.cache.set({ token: "", refreshToken: "", expirationTime: 0 });
|
|
116
|
-
return
|
|
115
|
+
return {
|
|
116
|
+
type: "Anonymous",
|
|
117
|
+
meta: {
|
|
118
|
+
cache: {
|
|
119
|
+
hit: false,
|
|
120
|
+
key: ""
|
|
121
|
+
},
|
|
122
|
+
placeholder: false
|
|
123
|
+
}
|
|
124
|
+
};
|
|
117
125
|
}
|
|
118
126
|
// FIXME: This can fail if the short-lived access token has expired. In other words, probably missing a token refresh.
|
|
119
127
|
async introspect() {
|
|
@@ -188,7 +196,16 @@ class CommercetoolsClient {
|
|
|
188
196
|
};
|
|
189
197
|
return identity;
|
|
190
198
|
}
|
|
191
|
-
return
|
|
199
|
+
return {
|
|
200
|
+
type: "Anonymous",
|
|
201
|
+
meta: {
|
|
202
|
+
cache: {
|
|
203
|
+
hit: false,
|
|
204
|
+
key: ""
|
|
205
|
+
},
|
|
206
|
+
placeholder: false
|
|
207
|
+
}
|
|
208
|
+
};
|
|
192
209
|
}
|
|
193
210
|
async becomeGuest() {
|
|
194
211
|
const credentials = Buffer.from(
|
package/core/token-cache.js
CHANGED
|
@@ -21,7 +21,6 @@ class RequestContextTokenCache {
|
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
async set(cache, tokenCacheOptions) {
|
|
24
|
-
console.log("TokenCache set session:", this.context.session["PROVIDER_COMMERCETOOLS"], "with token:", cache);
|
|
25
24
|
const session = CommercetoolsSessionSchema.parse(
|
|
26
25
|
this.context.session["PROVIDER_COMMERCETOOLS"] || {}
|
|
27
26
|
);
|
|
@@ -29,7 +28,6 @@ class RequestContextTokenCache {
|
|
|
29
28
|
session.refreshToken = cache.refreshToken;
|
|
30
29
|
session.token = cache.token;
|
|
31
30
|
session.expirationTime = cache.expirationTime;
|
|
32
|
-
console.log("TokenCache updated session:", this.context.session["PROVIDER_COMMERCETOOLS"]);
|
|
33
31
|
}
|
|
34
32
|
}
|
|
35
33
|
export {
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactionary/provider-commercetools",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.84",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "src/index.d.ts",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@reactionary/core": "0.0.
|
|
7
|
+
"@reactionary/core": "0.0.84",
|
|
8
8
|
"debug": "^4.4.3",
|
|
9
9
|
"zod": "4.1.9",
|
|
10
10
|
"@commercetools/ts-client": "^4.2.1",
|
|
@@ -252,7 +252,7 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
252
252
|
},
|
|
253
253
|
totalShipping: {
|
|
254
254
|
value: shippingTotal / 100,
|
|
255
|
-
currency
|
|
255
|
+
currency
|
|
256
256
|
},
|
|
257
257
|
totalProductPrice: {
|
|
258
258
|
value: productTotal / 100,
|
|
@@ -265,16 +265,21 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
265
265
|
};
|
|
266
266
|
const items = new Array();
|
|
267
267
|
for (const remoteItem of remote.lineItems) {
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
268
|
+
const identifier2 = {
|
|
269
|
+
key: remoteItem.id
|
|
270
|
+
};
|
|
271
|
+
const product = {
|
|
272
|
+
key: remoteItem.productId
|
|
273
|
+
};
|
|
274
|
+
const variant = {
|
|
275
|
+
sku: remoteItem.variant.sku || ""
|
|
276
|
+
};
|
|
277
|
+
const quantity = remoteItem.quantity;
|
|
273
278
|
const unitPrice = remoteItem.price.value.centAmount;
|
|
274
279
|
const totalPrice = remoteItem.totalPrice.centAmount || 0;
|
|
275
280
|
const totalDiscount = remoteItem.price.discounted?.value.centAmount || 0;
|
|
276
281
|
const unitDiscount = totalDiscount / remoteItem.quantity;
|
|
277
|
-
|
|
282
|
+
const price2 = {
|
|
278
283
|
unitPrice: {
|
|
279
284
|
value: unitPrice / 100,
|
|
280
285
|
currency
|
|
@@ -292,6 +297,13 @@ class CommercetoolsCartProvider extends CartProvider {
|
|
|
292
297
|
currency
|
|
293
298
|
}
|
|
294
299
|
};
|
|
300
|
+
const item = {
|
|
301
|
+
identifier: identifier2,
|
|
302
|
+
price: price2,
|
|
303
|
+
product,
|
|
304
|
+
quantity,
|
|
305
|
+
variant
|
|
306
|
+
};
|
|
295
307
|
items.push(item);
|
|
296
308
|
}
|
|
297
309
|
const cart = {
|
|
@@ -170,17 +170,21 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
170
170
|
const result = [];
|
|
171
171
|
const inputShippingMethods = shippingMethodsResponse.body.results;
|
|
172
172
|
for (const sm of inputShippingMethods) {
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
173
|
+
const identifier = {
|
|
174
|
+
key: sm.key
|
|
175
|
+
};
|
|
176
|
+
const name = sm.name;
|
|
177
|
+
const description = sm.localizedDescription?.[this.context.languageContext.locale] || "";
|
|
178
|
+
const shippingMethod = {
|
|
179
|
+
deliveryTime: "",
|
|
180
|
+
description,
|
|
181
|
+
identifier,
|
|
182
|
+
name,
|
|
179
183
|
price: sm.zoneRates[0].shippingRates[0].price ? {
|
|
180
184
|
value: (sm.zoneRates[0].shippingRates[0].price.centAmount || 0) / 100,
|
|
181
185
|
currency: sm.zoneRates[0].shippingRates[0].price.currencyCode || this.context.languageContext.currencyCode
|
|
182
186
|
} : { value: 0, currency: this.context.languageContext.currencyCode }
|
|
183
|
-
}
|
|
187
|
+
};
|
|
184
188
|
result.push(shippingMethod);
|
|
185
189
|
}
|
|
186
190
|
return result;
|
|
@@ -361,7 +365,7 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
361
365
|
},
|
|
362
366
|
totalShipping: {
|
|
363
367
|
value: shippingTotal / 100,
|
|
364
|
-
currency
|
|
368
|
+
currency
|
|
365
369
|
},
|
|
366
370
|
totalProductPrice: {
|
|
367
371
|
value: productTotal / 100,
|
|
@@ -374,15 +378,18 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
374
378
|
};
|
|
375
379
|
const items = new Array();
|
|
376
380
|
for (const remoteItem of remote.lineItems) {
|
|
377
|
-
const
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
+
const identifier2 = {
|
|
382
|
+
key: remoteItem.id
|
|
383
|
+
};
|
|
384
|
+
const variant = {
|
|
385
|
+
sku: remoteItem.variant.sku
|
|
386
|
+
};
|
|
387
|
+
const quantity = remoteItem.quantity;
|
|
381
388
|
const unitPrice = remoteItem.price.value.centAmount;
|
|
382
389
|
const totalPrice = remoteItem.totalPrice.centAmount || 0;
|
|
383
390
|
const totalDiscount = remoteItem.price.discounted?.value.centAmount || 0;
|
|
384
391
|
const unitDiscount = totalDiscount / remoteItem.quantity;
|
|
385
|
-
|
|
392
|
+
const price2 = {
|
|
386
393
|
unitPrice: {
|
|
387
394
|
value: unitPrice / 100,
|
|
388
395
|
currency
|
|
@@ -400,10 +407,22 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
400
407
|
currency
|
|
401
408
|
}
|
|
402
409
|
};
|
|
410
|
+
const item = {
|
|
411
|
+
identifier: identifier2,
|
|
412
|
+
price: price2,
|
|
413
|
+
quantity,
|
|
414
|
+
variant
|
|
415
|
+
};
|
|
403
416
|
items.push(item);
|
|
404
417
|
}
|
|
405
418
|
const shippingInstruction = this.parseShippingInstruction(remote);
|
|
406
|
-
const readyForFinalization = this.isReadyForFinalization(
|
|
419
|
+
const readyForFinalization = this.isReadyForFinalization(
|
|
420
|
+
price,
|
|
421
|
+
paymentInstructions,
|
|
422
|
+
billingAddress,
|
|
423
|
+
shippingAddress,
|
|
424
|
+
shippingInstruction
|
|
425
|
+
);
|
|
407
426
|
const result = {
|
|
408
427
|
identifier,
|
|
409
428
|
originalCartReference,
|
|
@@ -441,35 +460,50 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
441
460
|
return true;
|
|
442
461
|
}
|
|
443
462
|
parsePaymentInstruction(remote) {
|
|
444
|
-
const
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
newModel.amount = {
|
|
463
|
+
const identifier = {
|
|
464
|
+
key: remote.id
|
|
465
|
+
};
|
|
466
|
+
const amount = {
|
|
449
467
|
value: remote.amountPlanned.centAmount / 100,
|
|
450
468
|
currency: remote.amountPlanned.currencyCode
|
|
451
469
|
};
|
|
452
470
|
const method = remote.paymentMethodInfo?.method || "unknown";
|
|
453
471
|
const paymentProcessor = remote.paymentMethodInfo?.paymentInterface || method;
|
|
454
472
|
const paymentName = remote.paymentMethodInfo.name[this.context.languageContext.locale];
|
|
455
|
-
|
|
473
|
+
const paymentMethod = {
|
|
456
474
|
method,
|
|
457
475
|
paymentProcessor,
|
|
458
476
|
name: paymentName || method || "Unknown"
|
|
459
|
-
}
|
|
477
|
+
};
|
|
460
478
|
const customData = remote.custom?.fields || {};
|
|
461
|
-
|
|
479
|
+
const protocolData = Object.keys(customData).map((x) => ({ key: x, value: customData[x] })) || [];
|
|
480
|
+
let status = "pending";
|
|
462
481
|
if (remote.transactions && remote.transactions.length > 0) {
|
|
463
482
|
const lastTransaction = remote.transactions[remote.transactions.length - 1];
|
|
464
483
|
if (lastTransaction.type === "Authorization" && lastTransaction.state === "Pending") {
|
|
465
|
-
|
|
484
|
+
status = "pending";
|
|
466
485
|
} else if (lastTransaction.type === "Authorization" && lastTransaction.state === "Success") {
|
|
467
|
-
|
|
486
|
+
status = "authorized";
|
|
468
487
|
}
|
|
469
488
|
} else {
|
|
470
|
-
|
|
489
|
+
status = "pending";
|
|
471
490
|
}
|
|
472
|
-
|
|
491
|
+
const meta = {
|
|
492
|
+
cache: {
|
|
493
|
+
hit: false,
|
|
494
|
+
key: ""
|
|
495
|
+
},
|
|
496
|
+
placeholder: false
|
|
497
|
+
};
|
|
498
|
+
const result = {
|
|
499
|
+
amount,
|
|
500
|
+
identifier,
|
|
501
|
+
meta,
|
|
502
|
+
paymentMethod,
|
|
503
|
+
protocolData,
|
|
504
|
+
status
|
|
505
|
+
};
|
|
506
|
+
return result;
|
|
473
507
|
}
|
|
474
508
|
parseAddress(remote) {
|
|
475
509
|
return {
|
|
@@ -499,18 +533,21 @@ class CommercetoolsCheckoutProvider extends CheckoutProvider {
|
|
|
499
533
|
const instructions = remote.custom?.fields["shippingInstruction"] || "";
|
|
500
534
|
const consentForUnattendedDelivery = remote.custom?.fields["consentForUnattendedDelivery"] === "true" || false;
|
|
501
535
|
const pickupPoint = remote.custom?.fields["pickupPointId"] || "";
|
|
502
|
-
const shippingInstruction =
|
|
503
|
-
amount: {
|
|
504
|
-
value: (remote.shippingInfo.price.centAmount || 0) / 100,
|
|
505
|
-
currency: remote.shippingInfo.price.currencyCode
|
|
506
|
-
},
|
|
536
|
+
const shippingInstruction = {
|
|
507
537
|
shippingMethod: {
|
|
508
538
|
key: remote.shippingInfo.shippingMethod?.obj?.key || ""
|
|
509
539
|
},
|
|
510
540
|
pickupPoint: pickupPoint || "",
|
|
511
541
|
instructions: instructions || "",
|
|
512
|
-
consentForUnattendedDelivery: consentForUnattendedDelivery || false
|
|
513
|
-
|
|
542
|
+
consentForUnattendedDelivery: consentForUnattendedDelivery || false,
|
|
543
|
+
meta: {
|
|
544
|
+
cache: {
|
|
545
|
+
hit: false,
|
|
546
|
+
key: ""
|
|
547
|
+
},
|
|
548
|
+
placeholder: false
|
|
549
|
+
}
|
|
550
|
+
};
|
|
514
551
|
return shippingInstruction;
|
|
515
552
|
}
|
|
516
553
|
}
|
|
@@ -20,8 +20,6 @@ import {
|
|
|
20
20
|
ProductQueryBySKUSchema,
|
|
21
21
|
ProductQueryBySlugSchema,
|
|
22
22
|
ProductSchema,
|
|
23
|
-
ProductVariantIdentifierSchema,
|
|
24
|
-
ProductVariantSchema,
|
|
25
23
|
Reactionary
|
|
26
24
|
} from "@reactionary/core";
|
|
27
25
|
class CommercetoolsProductProvider extends ProductProvider {
|
|
@@ -79,7 +77,10 @@ class CommercetoolsProductProvider extends ProductProvider {
|
|
|
79
77
|
}
|
|
80
78
|
const variantLevelAttributes = data.masterVariant.attributes?.map((x) => this.parseAttribute(x)) || [];
|
|
81
79
|
const productLevelAttributes = data.attributes.map((x) => this.parseAttribute(x)) || [];
|
|
82
|
-
const sharedAttributes = [
|
|
80
|
+
const sharedAttributes = [
|
|
81
|
+
...productLevelAttributes,
|
|
82
|
+
...variantLevelAttributes
|
|
83
|
+
];
|
|
83
84
|
const mainVariant = this.parseVariant(data.masterVariant, data);
|
|
84
85
|
const meta = {
|
|
85
86
|
cache: { hit: false, key: this.generateCacheKeySingle(identifier) },
|
|
@@ -103,21 +104,29 @@ class CommercetoolsProductProvider extends ProductProvider {
|
|
|
103
104
|
return result;
|
|
104
105
|
}
|
|
105
106
|
parseVariant(variant, product) {
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
images
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
107
|
+
const identifier = {
|
|
108
|
+
sku: variant.sku
|
|
109
|
+
};
|
|
110
|
+
const images = [
|
|
111
|
+
...(variant.images || []).map(
|
|
112
|
+
(img) => ImageSchema.parse({
|
|
113
|
+
sourceUrl: img.url,
|
|
114
|
+
altText: img.label || "",
|
|
115
|
+
width: img.dimensions?.w,
|
|
116
|
+
height: img.dimensions?.h
|
|
117
|
+
})
|
|
118
|
+
)
|
|
119
|
+
];
|
|
120
|
+
const result = {
|
|
121
|
+
identifier,
|
|
122
|
+
images,
|
|
123
|
+
barcode: "",
|
|
124
|
+
ean: "",
|
|
125
|
+
gtin: "",
|
|
126
|
+
name: "",
|
|
127
|
+
options: [],
|
|
128
|
+
upc: ""
|
|
129
|
+
};
|
|
121
130
|
return result;
|
|
122
131
|
}
|
|
123
132
|
parseAttribute(attr) {
|
package/src/core/client.d.ts
CHANGED
|
@@ -49,17 +49,14 @@ export declare class CommercetoolsClient {
|
|
|
49
49
|
type: "Registered";
|
|
50
50
|
}>;
|
|
51
51
|
logout(): Promise<{
|
|
52
|
-
|
|
52
|
+
type: "Anonymous";
|
|
53
53
|
meta: {
|
|
54
|
-
[x: string]: unknown;
|
|
55
54
|
cache: {
|
|
56
|
-
|
|
57
|
-
hit: boolean;
|
|
55
|
+
hit: false;
|
|
58
56
|
key: string;
|
|
59
57
|
};
|
|
60
|
-
placeholder:
|
|
58
|
+
placeholder: false;
|
|
61
59
|
};
|
|
62
|
-
type: "Anonymous";
|
|
63
60
|
}>;
|
|
64
61
|
introspect(): Promise<AnonymousIdentity | GuestIdentity | RegisteredIdentity>;
|
|
65
62
|
protected becomeGuest(): Promise<void>;
|