@reactionary/provider-commercetools 0.0.42 → 0.0.51

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.
Files changed (38) hide show
  1. package/README.md +11 -0
  2. package/core/client.js +130 -72
  3. package/core/initialize.js +4 -4
  4. package/package.json +4 -3
  5. package/providers/cart.provider.js +76 -96
  6. package/providers/category.provider.js +29 -32
  7. package/providers/checkout.provider.js +457 -0
  8. package/providers/identity.provider.js +11 -64
  9. package/providers/index.js +4 -0
  10. package/providers/inventory.provider.js +23 -22
  11. package/providers/order.provider.js +118 -0
  12. package/providers/price.provider.js +48 -36
  13. package/providers/product.provider.js +28 -16
  14. package/providers/profile.provider.js +30 -0
  15. package/providers/search.provider.js +9 -12
  16. package/providers/store.provider.js +42 -0
  17. package/schema/capabilities.schema.js +4 -2
  18. package/schema/commercetools.schema.js +4 -4
  19. package/schema/configuration.schema.js +3 -1
  20. package/src/core/client.d.ts +70 -10
  21. package/src/core/initialize.d.ts +9 -3
  22. package/src/providers/cart.provider.d.ts +34 -23
  23. package/src/providers/category.provider.d.ts +13 -13
  24. package/src/providers/checkout.provider.d.ts +67 -0
  25. package/src/providers/identity.provider.d.ts +7 -7
  26. package/src/providers/index.d.ts +4 -0
  27. package/src/providers/inventory.provider.d.ts +8 -6
  28. package/src/providers/order.provider.d.ts +11 -0
  29. package/src/providers/price.provider.d.ts +12 -8
  30. package/src/providers/product.provider.d.ts +10 -8
  31. package/src/providers/profile.provider.d.ts +14 -0
  32. package/src/providers/search.provider.d.ts +6 -6
  33. package/src/providers/store.provider.d.ts +13 -0
  34. package/src/schema/capabilities.schema.d.ts +6 -4
  35. package/src/schema/commercetools.schema.d.ts +3 -3
  36. package/src/schema/configuration.schema.d.ts +22 -0
  37. package/providers/cart-payment.provider.js +0 -152
  38. package/src/providers/cart-payment.provider.d.ts +0 -16
package/README.md CHANGED
@@ -11,6 +11,14 @@ Run `nx build provider-commercetools` to build the library.
11
11
  Run `nx test provider-commercetools` to execute the unit tests via [Jest](https://jestjs.io).
12
12
 
13
13
 
14
+ # ASSUMPTIONS for backend config
15
+
16
+ - You will have 2 different channels for prices, one called `Offer Price` and one called `List Price`
17
+ - ProductVariants will all have unique SKU values.
18
+ - Your Supply Channels double as Store Locations
19
+
20
+
21
+
14
22
  ## TODO List
15
23
 
16
24
  ### Core
@@ -25,3 +33,6 @@ Run `nx test provider-commercetools` to execute the unit tests via [Jest](https:
25
33
  ### Inventory
26
34
  - [ ] Be traced and cached
27
35
 
36
+
37
+
38
+
package/core/client.js CHANGED
@@ -1,99 +1,149 @@
1
- import { ClientBuilder } from "@commercetools/ts-client";
1
+ import {
2
+ ClientBuilder
3
+ } from "@commercetools/ts-client";
2
4
  import { createApiBuilderFromCtpClient } from "@commercetools/platform-sdk";
3
5
  import { randomUUID } from "crypto";
4
- class CommercetoolsClient {
5
- constructor(config) {
6
- this.config = config;
7
- }
8
- getClient(token) {
9
- if (token) {
10
- return this.createClientWithToken(token);
11
- }
12
- return this.createAnonymousClient();
6
+ import {
7
+ AnonymousIdentitySchema,
8
+ GuestIdentitySchema,
9
+ RegisteredIdentitySchema
10
+ } from "@reactionary/core";
11
+ import * as crypto from "crypto";
12
+ import createDebug from "debug";
13
+ const debug = createDebug("commercetools:debug");
14
+ class RequestContextTokenCache {
15
+ constructor(context) {
16
+ this.context = context;
13
17
  }
14
- async login(username, password) {
15
- const queryParams = new URLSearchParams({
16
- grant_type: "password",
17
- username,
18
- password
19
- });
20
- const url = `${this.config.authUrl}/oauth/${this.config.projectKey}/customers/token?${queryParams.toString()}`;
21
- const headers = {
22
- Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
18
+ async get(tokenCacheOptions) {
19
+ const identity = this.context.identity;
20
+ return {
21
+ refreshToken: identity.refresh_token,
22
+ token: identity.token || "",
23
+ expirationTime: identity.expiry.getTime()
23
24
  };
24
- const remote = await fetch(url, { method: "POST", headers });
25
- const json = await remote.json();
26
- return json;
27
25
  }
28
- async guest() {
29
- const queryParams = new URLSearchParams({
30
- grant_type: "client_credentials"
31
- });
32
- const url = `${this.config.authUrl}/oauth/${this.config.projectKey}/anonymous/token?${queryParams.toString()}`;
33
- const headers = {
34
- Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
35
- };
36
- const remote = await fetch(url, { method: "POST", headers });
37
- const json = await remote.json();
38
- return json;
26
+ async set(cache, tokenCacheOptions) {
27
+ const identity = this.context.identity;
28
+ identity.refresh_token = cache.refreshToken;
29
+ identity.token = cache.token;
30
+ identity.expiry = new Date(cache.expirationTime);
39
31
  }
40
- async logout(token) {
41
- const queryParams = new URLSearchParams({
42
- token,
43
- token_type_hint: "access_token"
44
- });
45
- const url = `${this.config.authUrl}/oauth/token/revoke?${queryParams.toString()}`;
46
- const headers = {
47
- Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
48
- };
49
- const remote = await fetch(url, { method: "POST", headers });
50
- return remote;
32
+ }
33
+ class CommercetoolsClient {
34
+ constructor(config) {
35
+ this.config = config;
51
36
  }
52
- async introspect(token) {
53
- const queryParams = new URLSearchParams({
54
- token
55
- });
56
- const url = `${this.config.authUrl}/oauth/introspect?` + queryParams;
57
- const headers = {
58
- Authorization: "Basic " + btoa(this.config.clientId + ":" + this.config.clientSecret)
59
- };
60
- const remote = await fetch(url, { method: "POST", headers });
61
- const json = await remote.json();
62
- return json;
37
+ async getClient(reqCtx) {
38
+ return this.createClient(reqCtx);
63
39
  }
64
- createAnonymousClient() {
65
- const scopes = this.config.scopes;
66
- const builder = this.createBaseClientBuilder().withClientCredentialsFlow({
40
+ async register(username, password, reqCtx) {
41
+ const registrationBuilder = this.createBaseClientBuilder().withAnonymousSessionFlow({
67
42
  host: this.config.authUrl,
68
43
  projectKey: this.config.projectKey,
69
44
  credentials: {
70
45
  clientId: this.config.clientId,
71
46
  clientSecret: this.config.clientSecret
72
47
  },
73
- scopes: [...scopes]
48
+ scopes: this.config.scopes
74
49
  });
75
- return createApiBuilderFromCtpClient(builder.build());
76
- }
77
- createClientWithToken(token) {
78
- const builder = this.createBaseClientBuilder().withExistingTokenFlow(
79
- `Bearer ${token}`,
80
- { force: true }
50
+ const registrationClient = createApiBuilderFromCtpClient(
51
+ registrationBuilder.build()
81
52
  );
53
+ const registration = await registrationClient.withProjectKey({ projectKey: this.config.projectKey }).me().signup().post({
54
+ body: {
55
+ email: username,
56
+ password
57
+ }
58
+ }).execute();
59
+ const login = await this.login(username, password, reqCtx);
60
+ return login;
61
+ }
62
+ async login(username, password, reqCtx) {
63
+ const cache = new RequestContextTokenCache(reqCtx);
64
+ const loginBuilder = this.createBaseClientBuilder().withPasswordFlow({
65
+ host: this.config.authUrl,
66
+ projectKey: this.config.projectKey,
67
+ credentials: {
68
+ clientId: this.config.clientId,
69
+ clientSecret: this.config.clientSecret,
70
+ user: { username, password }
71
+ },
72
+ tokenCache: cache,
73
+ scopes: this.config.scopes
74
+ });
75
+ const loginClient = createApiBuilderFromCtpClient(loginBuilder.build());
76
+ const login = await loginClient.withProjectKey({ projectKey: this.config.projectKey }).me().login().post({
77
+ body: {
78
+ email: username,
79
+ password
80
+ }
81
+ }).execute();
82
+ reqCtx.identity = RegisteredIdentitySchema.parse({
83
+ ...reqCtx.identity,
84
+ type: "Registered",
85
+ logonId: username,
86
+ id: {
87
+ userId: login.body.customer.id
88
+ }
89
+ });
90
+ return reqCtx.identity;
91
+ }
92
+ async logout(reqCtx) {
93
+ const cache = new RequestContextTokenCache(reqCtx);
94
+ await cache.set({ token: "", refreshToken: "", expirationTime: 0 });
95
+ reqCtx.identity = AnonymousIdentitySchema.parse({});
96
+ return reqCtx.identity;
97
+ }
98
+ createClient(reqCtx) {
99
+ const cache = new RequestContextTokenCache(reqCtx);
100
+ if (reqCtx.identity.type === "Anonymous") {
101
+ reqCtx.identity = GuestIdentitySchema.parse({
102
+ id: {
103
+ userId: crypto.randomUUID().toString()
104
+ },
105
+ type: "Guest"
106
+ });
107
+ }
108
+ const identity = reqCtx.identity;
109
+ let builder = this.createBaseClientBuilder(reqCtx);
110
+ if (!identity.token || !identity.refresh_token) {
111
+ builder = builder.withAnonymousSessionFlow({
112
+ host: this.config.authUrl,
113
+ projectKey: this.config.projectKey,
114
+ credentials: {
115
+ clientId: this.config.clientId,
116
+ clientSecret: this.config.clientSecret,
117
+ anonymousId: identity.id.userId
118
+ },
119
+ tokenCache: cache
120
+ });
121
+ } else {
122
+ builder = builder.withRefreshTokenFlow({
123
+ credentials: {
124
+ clientId: this.config.clientId,
125
+ clientSecret: this.config.clientSecret
126
+ },
127
+ host: this.config.authUrl,
128
+ projectKey: this.config.projectKey,
129
+ refreshToken: identity.refresh_token || "",
130
+ tokenCache: cache
131
+ });
132
+ }
82
133
  return createApiBuilderFromCtpClient(builder.build());
83
134
  }
84
- createBaseClientBuilder() {
85
- const builder = new ClientBuilder().withProjectKey(this.config.projectKey).withQueueMiddleware({
135
+ createBaseClientBuilder(reqCtx) {
136
+ let builder = new ClientBuilder().withProjectKey(this.config.projectKey).withQueueMiddleware({
86
137
  concurrency: 20
87
138
  }).withConcurrentModificationMiddleware({
88
139
  concurrentModificationHandlerFn: (version, request) => {
89
- console.log(`Concurrent modification error, retry with version ${version}`);
140
+ console.log(
141
+ `Concurrent modification error, retry with version ${version}`
142
+ );
90
143
  const body = request.body;
91
144
  body["version"] = version;
92
145
  return Promise.resolve(body);
93
146
  }
94
- }).withCorrelationIdMiddleware({
95
- // ideally this would be pushed in as part of the session context, so we can trace it end-to-end
96
- generate: () => `REACTIONARY-${randomUUID()}`
97
147
  }).withHttpMiddleware({
98
148
  retryConfig: {
99
149
  backoff: true,
@@ -109,9 +159,17 @@ class CommercetoolsClient {
109
159
  host: this.config.apiUrl,
110
160
  httpClient: fetch
111
161
  });
162
+ const correlationId = reqCtx?.correlationId || "REACTIONARY-" + (typeof crypto !== "undefined" && "randomUUID" in crypto ? crypto.randomUUID() : randomUUID());
163
+ builder = builder.withCorrelationIdMiddleware({
164
+ generate: () => correlationId
165
+ });
166
+ if (debug.enabled) {
167
+ builder = builder.withLoggerMiddleware();
168
+ }
112
169
  return builder;
113
170
  }
114
171
  }
115
172
  export {
116
- CommercetoolsClient
173
+ CommercetoolsClient,
174
+ RequestContextTokenCache
117
175
  };
@@ -6,7 +6,7 @@ import {
6
6
  ProductSchema,
7
7
  SearchResultSchema,
8
8
  CategorySchema,
9
- CartPaymentInstructionSchema
9
+ CheckoutSchema
10
10
  } from "@reactionary/core";
11
11
  import { CommercetoolsSearchProvider } from "../providers/search.provider";
12
12
  import { CommercetoolsProductProvider } from "../providers/product.provider";
@@ -15,7 +15,7 @@ import { CommercetoolsCartProvider } from "../providers/cart.provider";
15
15
  import { CommercetoolsInventoryProvider } from "../providers/inventory.provider";
16
16
  import { CommercetoolsPriceProvider } from "../providers/price.provider";
17
17
  import { CommercetoolsCategoryProvider } from "../providers/category.provider";
18
- import { CommercetoolsCartPaymentProvider } from "../providers/cart-payment.provider";
18
+ import { CommercetoolsCheckoutProvider } from "../providers";
19
19
  function withCommercetoolsCapabilities(configuration, capabilities) {
20
20
  return (cache) => {
21
21
  const client = {};
@@ -40,8 +40,8 @@ function withCommercetoolsCapabilities(configuration, capabilities) {
40
40
  if (capabilities.category) {
41
41
  client.category = new CommercetoolsCategoryProvider(configuration, CategorySchema, cache);
42
42
  }
43
- if (capabilities.cartPayment) {
44
- client.cartPayment = new CommercetoolsCartPaymentProvider(configuration, CartPaymentInstructionSchema, cache);
43
+ if (capabilities.checkout) {
44
+ client.checkout = new CommercetoolsCheckoutProvider(configuration, CheckoutSchema, cache);
45
45
  }
46
46
  return client;
47
47
  };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@reactionary/provider-commercetools",
3
- "version": "0.0.42",
3
+ "version": "0.0.51",
4
4
  "main": "index.js",
5
5
  "types": "src/index.d.ts",
6
6
  "dependencies": {
7
- "@reactionary/core": "0.0.42",
8
- "@reactionary/otel": "0.0.42",
7
+ "@reactionary/core": "0.0.51",
8
+ "@reactionary/otel": "0.0.51",
9
+ "debug": "^4.4.3",
9
10
  "zod": "4.1.9",
10
11
  "@commercetools/ts-client": "^4.2.1",
11
12
  "@commercetools/platform-sdk": "^8.8.0"
@@ -24,21 +24,21 @@ class CommercetoolsCartProvider extends CartProvider {
24
24
  super(schema, cache);
25
25
  this.config = config;
26
26
  }
27
- async getById(payload, session) {
27
+ async getById(payload, reqCtx) {
28
28
  try {
29
- const client = this.getClient(session);
29
+ const client = await this.getClient(reqCtx);
30
30
  const ctId = payload.cart;
31
- const remote = await client.withId({ ID: ctId.key }).get().execute();
32
- return this.parseSingle(remote.body, session);
31
+ const remote = await client.carts.withId({ ID: ctId.key }).get().execute();
32
+ return this.parseSingle(remote.body, reqCtx);
33
33
  } catch (e) {
34
34
  return this.createEmptyCart();
35
35
  }
36
36
  }
37
- async add(payload, session) {
38
- const client = this.getClient(session);
37
+ async add(payload, reqCtx) {
38
+ const client = await this.getClient(reqCtx);
39
39
  let cartIdentifier = payload.cart;
40
40
  if (!cartIdentifier.key) {
41
- cartIdentifier = await this.createCart(session);
41
+ cartIdentifier = await this.createCart(reqCtx);
42
42
  }
43
43
  return this.applyActions(
44
44
  cartIdentifier,
@@ -52,10 +52,10 @@ class CommercetoolsCartProvider extends CartProvider {
52
52
  action: "recalculate"
53
53
  }
54
54
  ],
55
- session
55
+ reqCtx
56
56
  );
57
57
  }
58
- async remove(payload, session) {
58
+ async remove(payload, reqCtx) {
59
59
  return this.applyActions(
60
60
  payload.cart,
61
61
  [
@@ -67,12 +67,12 @@ class CommercetoolsCartProvider extends CartProvider {
67
67
  action: "recalculate"
68
68
  }
69
69
  ],
70
- session
70
+ reqCtx
71
71
  );
72
72
  }
73
- async changeQuantity(payload, session) {
73
+ async changeQuantity(payload, reqCtx) {
74
74
  if (payload.quantity === 0) {
75
- return this.getById({ cart: payload.cart }, session);
75
+ return this.getById({ cart: payload.cart }, reqCtx);
76
76
  }
77
77
  return this.applyActions(
78
78
  payload.cart,
@@ -86,19 +86,13 @@ class CommercetoolsCartProvider extends CartProvider {
86
86
  action: "recalculate"
87
87
  }
88
88
  ],
89
- session
89
+ reqCtx
90
90
  );
91
91
  }
92
- async getActiveCartId(session) {
93
- const client = this.getClient(session);
92
+ async getActiveCartId(reqCtx) {
93
+ const client = await this.getClient(reqCtx);
94
94
  try {
95
- const carts = await client.withCustomerId({ customerId: session.id }).get({
96
- queryArgs: {
97
- limit: 1,
98
- sort: "lastModifiedAt desc",
99
- where: 'cartState="Active"'
100
- }
101
- }).execute();
95
+ const carts = await client.activeCart.get().execute();
102
96
  return CommercetoolsCartIdentifierSchema.parse({
103
97
  key: carts.body.id,
104
98
  version: carts.body.version || 0
@@ -110,22 +104,22 @@ class CommercetoolsCartProvider extends CartProvider {
110
104
  });
111
105
  }
112
106
  }
113
- async deleteCart(payload, session) {
114
- const client = this.getClient(session);
107
+ async deleteCart(payload, reqCtx) {
108
+ const client = await this.getClient(reqCtx);
115
109
  if (payload.cart.key) {
116
110
  const ctId = payload.cart;
117
- await client.withId({ ID: ctId.key }).delete({
111
+ await client.carts.withId({ ID: ctId.key }).delete({
118
112
  queryArgs: {
119
113
  version: ctId.version,
120
114
  dataErasure: false
121
115
  }
122
116
  }).execute();
123
117
  }
124
- const activeCartId = await this.getActiveCartId(session);
125
- return this.getById({ cart: activeCartId }, session);
118
+ const activeCartId = await this.getActiveCartId(reqCtx);
119
+ return this.getById({ cart: activeCartId }, reqCtx);
126
120
  }
127
- setShippingInfo(payload, session) {
128
- const client = this.getClient(session);
121
+ async setShippingInfo(payload, reqCtx) {
122
+ const client = await this.getClient(reqCtx);
129
123
  const ctId = payload.cart;
130
124
  const actions = new Array();
131
125
  if (payload.shippingMethod) {
@@ -141,7 +135,7 @@ class CommercetoolsCartProvider extends CartProvider {
141
135
  actions.push({
142
136
  action: "setShippingAddress",
143
137
  address: {
144
- country: payload.shippingAddress.countryCode || "US",
138
+ country: payload.shippingAddress.countryCode || reqCtx.taxJurisdiction.countryCode || "US",
145
139
  firstName: payload.shippingAddress.firstName,
146
140
  lastName: payload.shippingAddress.lastName,
147
141
  city: payload.shippingAddress.city,
@@ -151,9 +145,9 @@ class CommercetoolsCartProvider extends CartProvider {
151
145
  }
152
146
  });
153
147
  }
154
- return this.applyActions(payload.cart, actions, session);
148
+ return this.applyActions(payload.cart, actions, reqCtx);
155
149
  }
156
- setBillingAddress(payload, session) {
150
+ setBillingAddress(payload, reqCtx) {
157
151
  return this.applyActions(
158
152
  payload.cart,
159
153
  [
@@ -162,7 +156,7 @@ class CommercetoolsCartProvider extends CartProvider {
162
156
  address: {
163
157
  email: payload.notificationEmailAddress,
164
158
  mobile: payload.notificationPhoneNumber,
165
- country: payload.billingAddress.countryCode || "US",
159
+ country: payload.billingAddress.countryCode || reqCtx.taxJurisdiction.countryCode || "US",
166
160
  firstName: payload.billingAddress.firstName,
167
161
  lastName: payload.billingAddress.lastName,
168
162
  city: payload.billingAddress.city,
@@ -177,13 +171,13 @@ class CommercetoolsCartProvider extends CartProvider {
177
171
  },
178
172
  {
179
173
  action: "setCountry",
180
- country: payload.billingAddress.countryCode || "US"
174
+ country: payload.billingAddress.countryCode || reqCtx.taxJurisdiction.countryCode || "US"
181
175
  }
182
176
  ],
183
- session
177
+ reqCtx
184
178
  );
185
179
  }
186
- applyCouponCode(payload, session) {
180
+ applyCouponCode(payload, reqCtx) {
187
181
  return this.applyActions(
188
182
  payload.cart,
189
183
  [
@@ -195,10 +189,10 @@ class CommercetoolsCartProvider extends CartProvider {
195
189
  action: "recalculate"
196
190
  }
197
191
  ],
198
- session
192
+ reqCtx
199
193
  );
200
194
  }
201
- removeCouponCode(payload, session) {
195
+ removeCouponCode(payload, reqCtx) {
202
196
  return this.applyActions(
203
197
  payload.cart,
204
198
  [
@@ -213,19 +207,16 @@ class CommercetoolsCartProvider extends CartProvider {
213
207
  action: "recalculate"
214
208
  }
215
209
  ],
216
- session
210
+ reqCtx
217
211
  );
218
212
  }
219
- async checkout(payload, session) {
220
- const client = this.getOrderClient(session);
213
+ async checkout(payload, reqCtx) {
214
+ const client = await this.getClient(reqCtx);
221
215
  const ctId = payload.cart;
222
- const orderResponse = await client.post({
216
+ const orderResponse = await client.orders.post({
223
217
  body: {
224
218
  version: ctId.version,
225
- cart: {
226
- typeId: "cart",
227
- id: ctId.key
228
- }
219
+ id: ctId.key
229
220
  }
230
221
  }).execute();
231
222
  return CommercetoolsOrderIdentifierSchema.parse({
@@ -233,14 +224,13 @@ class CommercetoolsCartProvider extends CartProvider {
233
224
  version: orderResponse.body.version || 0
234
225
  });
235
226
  }
236
- async changeCurrency(payload, session) {
237
- const client = this.getClient(session);
238
- const currentCart = await client.withId({ ID: payload.cart.key }).get().execute();
239
- const newCart = await client.post({
227
+ async changeCurrency(payload, reqCtx) {
228
+ const client = await this.getClient(reqCtx);
229
+ const currentCart = await client.carts.withId({ ID: payload.cart.key }).get().execute();
230
+ const newCart = await client.carts.post({
240
231
  body: {
241
232
  currency: payload.newCurrency,
242
- country: session.languageContext.countryCode || "US",
243
- locale: session.languageContext.locale
233
+ locale: reqCtx.languageContext.locale
244
234
  }
245
235
  }).execute();
246
236
  const newCartId = CommercetoolsCartIdentifierSchema.parse({
@@ -262,9 +252,9 @@ class CommercetoolsCartProvider extends CartProvider {
262
252
  action: "recalculate"
263
253
  }
264
254
  ],
265
- session
255
+ reqCtx
266
256
  );
267
- await client.withId({ ID: payload.cart.key }).delete({
257
+ await client.carts.withId({ ID: payload.cart.key }).delete({
268
258
  queryArgs: {
269
259
  version: currentCart.body.version || 0,
270
260
  dataErasure: false
@@ -272,13 +262,13 @@ class CommercetoolsCartProvider extends CartProvider {
272
262
  }).execute();
273
263
  return response;
274
264
  }
275
- async createCart(session) {
276
- const client = this.getClient(session);
277
- const response = await client.post({
265
+ async createCart(reqCtx) {
266
+ const client = await this.getClient(reqCtx);
267
+ const response = await client.carts.post({
278
268
  body: {
279
- currency: session.languageContext.currencyCode || "USD",
280
- country: session.languageContext.countryCode || "US",
281
- locale: session.languageContext.locale
269
+ currency: reqCtx.languageContext.currencyCode || "USD",
270
+ country: reqCtx.taxJurisdiction.countryCode || "US",
271
+ locale: reqCtx.languageContext.locale
282
272
  }
283
273
  }).execute();
284
274
  return CommercetoolsCartIdentifierSchema.parse({
@@ -286,42 +276,35 @@ class CommercetoolsCartProvider extends CartProvider {
286
276
  version: response.body.version || 0
287
277
  });
288
278
  }
289
- async applyActions(cart, actions, session) {
290
- const client = this.getClient(session);
279
+ async applyActions(cart, actions, reqCtx) {
280
+ const client = await this.getClient(reqCtx);
291
281
  const ctId = cart;
292
- const response = await client.withId({ ID: ctId.key }).post({
293
- body: {
294
- version: ctId.version,
295
- actions
282
+ try {
283
+ const response = await client.carts.withId({ ID: ctId.key }).post({
284
+ body: {
285
+ version: ctId.version,
286
+ actions
287
+ }
288
+ }).execute();
289
+ if (response.error) {
290
+ console.error(response.error);
296
291
  }
297
- }).execute();
298
- return this.parseSingle(response.body, session);
299
- }
300
- getClient(session) {
301
- const token = session.identity.keyring.find(
302
- (x) => x.service === "commercetools"
303
- )?.token;
304
- const client = new CommercetoolsClient(this.config).getClient(token);
305
- const cartClient = client.withProjectKey({ projectKey: this.config.projectKey }).carts();
306
- return cartClient;
307
- }
308
- getOrderClient(session) {
309
- const token = session.identity.keyring.find(
310
- (x) => x.service === "commercetools"
311
- )?.token;
312
- const client = new CommercetoolsClient(this.config).getClient(token);
313
- const orderClient = client.withProjectKey({ projectKey: this.config.projectKey }).orders();
314
- return orderClient;
292
+ return this.parseSingle(response.body, reqCtx);
293
+ } catch (e) {
294
+ console.error("Error applying actions to cart:", e);
295
+ throw e;
296
+ }
315
297
  }
316
- getPaymentClient(session) {
317
- const token = session.identity.keyring.find(
318
- (x) => x.service === "commercetools"
319
- )?.token;
320
- const client = new CommercetoolsClient(this.config).getClient(token);
321
- const paymentClient = client.withProjectKey({ projectKey: this.config.projectKey }).payments();
322
- return paymentClient;
298
+ async getClient(reqCtx) {
299
+ const client = await new CommercetoolsClient(this.config).getClient(reqCtx);
300
+ const clientWithProject = client.withProjectKey({ projectKey: this.config.projectKey });
301
+ return {
302
+ carts: clientWithProject.me().carts(),
303
+ activeCart: clientWithProject.me().activeCart(),
304
+ orders: clientWithProject.me().orders()
305
+ };
323
306
  }
324
- parseSingle(remote, session) {
307
+ parseSingle(remote, reqCtx) {
325
308
  const result = this.newModel();
326
309
  result.identifier = CommercetoolsCartIdentifierSchema.parse({
327
310
  key: remote.id,
@@ -395,7 +378,7 @@ class CommercetoolsCartProvider extends CartProvider {
395
378
  result.meta = {
396
379
  cache: {
397
380
  hit: false,
398
- key: this.generateCacheKeySingle(result.identifier, session)
381
+ key: this.generateCacheKeySingle(result.identifier, reqCtx)
399
382
  },
400
383
  placeholder: false
401
384
  };
@@ -441,9 +424,6 @@ __decorateClass([
441
424
  __decorateClass([
442
425
  traced()
443
426
  ], CommercetoolsCartProvider.prototype, "getClient", 1);
444
- __decorateClass([
445
- traced()
446
- ], CommercetoolsCartProvider.prototype, "getPaymentClient", 1);
447
427
  export {
448
428
  CommercetoolsCartProvider
449
429
  };