@scayle/storefront-core 8.2.2 → 8.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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @scayle/storefront-core
2
2
 
3
+ ## 8.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [Basket] Add support to update existing basket items using the `updateBasketItem` RPC.
8
+
3
9
  ## 8.2.2
4
10
 
5
11
  ### Patch Changes
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.removeItemFromBasket = exports.mergeBaskets = exports.getBasket = exports.clearBasket = exports.addItemsToBasket = exports.addItemToBasket = void 0;
6
+ exports.updateBasketItem = exports.removeItemFromBasket = exports.mergeBaskets = exports.getBasket = exports.clearBasket = exports.addItemsToBasket = exports.addItemToBasket = void 0;
7
7
  var _errors = require("../../../errors/index.cjs");
8
8
  var _types = require("../../../types/index.cjs");
9
9
  var _constants = require("../../../constants/index.cjs");
@@ -215,6 +215,45 @@ const mergeBaskets = exports.mergeBaskets = async function mergeBaskets2({
215
215
  orderCustomData
216
216
  }, context);
217
217
  };
218
+ const updateBasketItem = exports.updateBasketItem = async function updateBasketItem2({
219
+ basketItemKey,
220
+ update,
221
+ with: _with
222
+ }, context) {
223
+ if (!(0, _types.hasSession)(context)) {
224
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, _constants.HttpStatusMessage.BAD_REQUEST, "No Session found");
225
+ }
226
+ const {
227
+ basketKey,
228
+ sapiClient
229
+ } = context;
230
+ const resolvedWith = getWithParams({
231
+ with: _with
232
+ }, context);
233
+ const {
234
+ quantity,
235
+ ...updateParams
236
+ } = update;
237
+ const result = await sapiClient.basket.updateItem(basketKey, basketItemKey, quantity, {
238
+ ...updateParams,
239
+ with: resolvedWith
240
+ });
241
+ if (result.type === "success") {
242
+ return {
243
+ basket: result.basket
244
+ };
245
+ } else {
246
+ context.log.error("Updating basket item failed", {
247
+ kind: result.kind,
248
+ basketKey,
249
+ basketItemKey,
250
+ update
251
+ });
252
+ return new _errors.ErrorResponse(_constants.HttpStatusCode.BAD_REQUEST, SAPI_ERROR_NAME, "Updating basket item failed", {
253
+ failureKind: result.kind
254
+ });
255
+ }
256
+ };
218
257
  function parseBasketError(response) {
219
258
  const parsedError = {
220
259
  message: "",
@@ -1,4 +1,4 @@
1
- import type { AddOrUpdateItemError } from '@scayle/storefront-api';
1
+ import type { AddOrUpdateItemError, UpdateBasketItemQuantity } from '@scayle/storefront-api';
2
2
  import { ErrorResponse } from '../../../errors';
3
3
  import type { AddOrUpdateItemType, BasketResponseData, BasketWithOptions, Product, RpcContext, Variant } from '../../../types';
4
4
  import { ExistingItemHandling } from '../../../constants';
@@ -55,3 +55,31 @@ export declare const mergeBaskets: ({ fromBasketKey, toBasketKey, with: _with, o
55
55
  readonly basket: BasketResponseData<Product, Variant>;
56
56
  readonly errors: AddOrUpdateItemError[];
57
57
  } | undefined>;
58
+ /**
59
+ * An object containing properties of a basket item than should be applied to the basket item during the update.
60
+ */
61
+ export type BasketItemUpdateData = Partial<Omit<UpdateBasketItemQuantity, 'basketKey' | 'itemKey' | 'with' | 'quantity'>> & {
62
+ quantity: number;
63
+ };
64
+ export interface UpdateBasketItemRequestParameter {
65
+ /** key of the item to be updated. */
66
+ basketItemKey: string;
67
+ /** The update data that should be applied to the basket item- */
68
+ update: BasketItemUpdateData;
69
+ /** An object describing which data the returned basket should contain. */
70
+ with?: BasketWithOptions;
71
+ }
72
+ /**
73
+ * Applies the provided update data to a specific item in a basket.
74
+ *
75
+ * @param params - The request parameters.
76
+ * @param params.basketItemKey - The key of the item to be updated.
77
+ * @param params.update - The update data that should be applied to the basket item.
78
+ * @param params.with - An object describing which data the returned basket should contain.
79
+ * @param context - The rpc context associated with the request.
80
+ *
81
+ * @returns A promise that resolves with the updated basket. In case of an error the, the failure kind will be resolved.
82
+ */
83
+ export declare const updateBasketItem: ({ basketItemKey, update, with: _with }: UpdateBasketItemRequestParameter, context: RpcContext) => Promise<ErrorResponse | {
84
+ basket: BasketResponseData<Product, Variant>;
85
+ }>;
@@ -223,6 +223,45 @@ export const mergeBaskets = async function mergeBaskets2({ fromBasketKey, toBask
223
223
  context
224
224
  );
225
225
  };
226
+ export const updateBasketItem = async function updateBasketItem2({ basketItemKey, update, with: _with }, context) {
227
+ if (!hasSession(context)) {
228
+ return new ErrorResponse(
229
+ HttpStatusCode.BAD_REQUEST,
230
+ HttpStatusMessage.BAD_REQUEST,
231
+ "No Session found"
232
+ );
233
+ }
234
+ const { basketKey, sapiClient } = context;
235
+ const resolvedWith = getWithParams(
236
+ { with: _with },
237
+ context
238
+ );
239
+ const { quantity, ...updateParams } = update;
240
+ const result = await sapiClient.basket.updateItem(
241
+ basketKey,
242
+ basketItemKey,
243
+ quantity,
244
+ { ...updateParams, with: resolvedWith }
245
+ );
246
+ if (result.type === "success") {
247
+ return { basket: result.basket };
248
+ } else {
249
+ context.log.error("Updating basket item failed", {
250
+ kind: result.kind,
251
+ basketKey,
252
+ basketItemKey,
253
+ update
254
+ });
255
+ return new ErrorResponse(
256
+ HttpStatusCode.BAD_REQUEST,
257
+ SAPI_ERROR_NAME,
258
+ "Updating basket item failed",
259
+ {
260
+ failureKind: result.kind
261
+ }
262
+ );
263
+ }
264
+ };
226
265
  function parseBasketError(response) {
227
266
  const parsedError = {
228
267
  message: "",
@@ -37,7 +37,7 @@ const getCheckoutToken = exports.getCheckoutToken = async function getCheckoutTo
37
37
  carrier,
38
38
  basketId: context.basketKey,
39
39
  campaignKey: context.campaignKey
40
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.2.2"}`).setProtectedHeader({
40
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.3.0"}`).setProtectedHeader({
41
41
  alg: "HS256",
42
42
  typ: "JWT"
43
43
  }).sign(secret);
@@ -35,7 +35,7 @@ export const getCheckoutToken = async function getCheckoutToken2(jwtPayload = {}
35
35
  carrier,
36
36
  basketId: context.basketKey,
37
37
  campaignKey: context.campaignKey
38
- }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.2.2"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
38
+ }).setIssuedAt(now).setNotBefore(now).setExpirationTime("1h").setIssuer(`${"@scayle/storefront-core"}@${"8.3.0"}`).setProtectedHeader({ alg: "HS256", typ: "JWT" }).sign(secret);
39
39
  return {
40
40
  accessToken: refreshedAccessToken,
41
41
  checkoutJwt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scayle/storefront-core",
3
- "version": "8.2.2",
3
+ "version": "8.3.0",
4
4
  "description": "Collection of essential utilities to work with the Storefront API",
5
5
  "author": "SCAYLE Commerce Engine",
6
6
  "license": "MIT",