@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
127 lines (126 loc) • 3.81 kB
JavaScript
import { hasSession } from "../../types/index.mjs";
import {
HttpStatusCode,
HttpStatusMessage,
MIN_WITH_PARAMS_WISHLIST
} from "../../constants/index.mjs";
import { unwrap } from "../../utils/response.mjs";
import { ErrorResponse } from "../../errors/index.mjs";
import { mapSAPIFetchErrorToResponse } from "../../utils/sapi.mjs";
import { defineRpcHandler } from "../../utils/index.mjs";
function getWithParams(params, context) {
return params.with ?? context.withParams?.wishlist ?? MIN_WITH_PARAMS_WISHLIST;
}
export const getWishlist = defineRpcHandler(
async (options, context) => {
if (!hasSession(context)) {
return new ErrorResponse(
HttpStatusCode.BAD_REQUEST,
HttpStatusMessage.BAD_REQUEST,
"No Session found"
);
}
const { sapiClient, wishlistKey } = context;
const campaignKey = await context.callRpc?.("getCampaignKey");
const { pricePromotionKey, ...resolvedWith } = getWithParams(
{
with: options
},
context
);
return await mapSAPIFetchErrorToResponse(sapiClient.wishlist.get)(
wishlistKey,
{
with: resolvedWith,
campaignKey,
pricePromotionKey: pricePromotionKey ?? ""
}
);
},
{ method: "POST" }
);
export const addItemToWishlist = defineRpcHandler(
async (options, context) => {
if (!hasSession(context)) {
return new ErrorResponse(
HttpStatusCode.BAD_REQUEST,
HttpStatusMessage.BAD_REQUEST,
"No Session found"
);
}
const { sapiClient, wishlistKey } = context;
const campaignKey = await context.callRpc?.("getCampaignKey");
const { productId, variantId } = options;
const { pricePromotionKey, ...resolvedWith } = getWithParams(
options,
context
);
if (!productId && !variantId) {
return new ErrorResponse(
HttpStatusCode.BAD_REQUEST,
HttpStatusMessage.BAD_REQUEST,
"No productId or variantId given"
);
}
const result = await sapiClient.wishlist.addItem(
wishlistKey,
variantId ? { variantId } : { productId },
{
with: resolvedWith,
campaignKey,
pricePromotionKey: pricePromotionKey ?? ""
}
);
if (result.type === "success") {
return result.wishlist;
} else {
const { statusCode: code, kind, type } = result;
context.log.error("Adding to wishlist failed", result.wishlist);
return new Response(JSON.stringify({ kind, type }), { status: code });
}
},
{ method: "PUT" }
);
export const removeItemFromWishlist = defineRpcHandler(
async (options, context) => {
if (!hasSession(context)) {
return new ErrorResponse(
HttpStatusCode.BAD_REQUEST,
HttpStatusMessage.BAD_REQUEST,
"No Session found"
);
}
const { sapiClient, wishlistKey } = context;
const campaignKey = await context.callRpc?.("getCampaignKey");
const { pricePromotionKey, ...resolvedWith } = getWithParams(
options,
context
);
return await mapSAPIFetchErrorToResponse(sapiClient.wishlist.deleteItem)(
wishlistKey,
options.itemKey,
{
with: resolvedWith,
campaignKey,
pricePromotionKey: pricePromotionKey ?? ""
}
);
},
{ method: "DELETE" }
);
export const clearWishlist = defineRpcHandler(
async (context) => {
const wishlistResponse = await getWishlist({}, context);
if (wishlistResponse instanceof ErrorResponse) {
return wishlistResponse;
}
const wishlist = await unwrap(wishlistResponse);
await Promise.all(
wishlist.items.map(async (item) => {
await removeItemFromWishlist({ itemKey: item.key }, context);
})
);
return await getWishlist({}, context);
},
{ method: "DELETE" }
);