@scayle/storefront-core
Version:
Collection of essential utilities to work with the Storefront API
59 lines (58 loc) • 1.64 kB
JavaScript
import {
PROMOTION_PAGE_DEFAULT,
PROMOTION_PER_PAGE_DEFAULT
} from "../../constants/index.mjs";
import { mapSAPIFetchErrorToResponse } from "../../utils/sapi.mjs";
import { defineRpcHandler } from "../../utils/index.mjs";
const setPaginationDefault = (pagination) => {
return {
page: pagination?.page || PROMOTION_PAGE_DEFAULT,
perPage: pagination?.perPage || PROMOTION_PER_PAGE_DEFAULT
};
};
export const getPromotions = defineRpcHandler(
async (params = {}, context) => {
const { sapiClient, cached } = context;
return await cached(
mapSAPIFetchErrorToResponse(sapiClient.promotions.get),
{
cacheKeyPrefix: "getPromotions"
}
)({
...params,
pagination: setPaginationDefault(params.pagination)
});
},
{ method: "GET" }
);
export const getCurrentPromotions = defineRpcHandler(
async (params = {}, context) => {
const { sapiClient, cached } = context;
const now = /* @__PURE__ */ new Date();
now.setSeconds(0, 0);
const activeAt = now.toISOString();
return await cached(
mapSAPIFetchErrorToResponse(sapiClient.promotions.get),
{
cacheKeyPrefix: "getPromotions"
}
)({
...params,
activeAt,
pagination: setPaginationDefault(params.pagination)
});
},
{ method: "GET" }
);
export const getPromotionsByIds = defineRpcHandler(
async (ids, context) => {
const { sapiClient, cached } = context;
return await cached(
mapSAPIFetchErrorToResponse(sapiClient.promotions.getByIds),
{
cacheKeyPrefix: "getByIds-promotions"
}
)(ids);
},
{ method: "GET" }
);