UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

56 lines (55 loc) 1.62 kB
import { ErrorResponse } from "../../errors/index.mjs"; import { HttpStatusCode, HttpStatusMessage } from "../../constants/index.mjs"; import { defineRpcHandler } from "../../utils/index.mjs"; export const fetchAllNavigationTrees = defineRpcHandler( async ({ params }, context) => { const { sapiClient: { navigation }, cached } = context; return await cached(navigation.getAll, { cacheKeyPrefix: "getAll-navigation-trees" })(params); }, { method: "GET" } ); export const fetchNavigationTreeById = defineRpcHandler( async ({ treeId, params }, context) => { const { sapiClient: { navigation }, cached } = context; return await cached(navigation.getById, { cacheKeyPrefix: `getById-navigation-trees-${treeId}` })(treeId, params); }, { method: "GET" } ); export const fetchNavigationTreeByName = defineRpcHandler( async ({ treeName, params }, context) => { const { sapiClient: { navigation }, cached } = context; return await cached( async (name, params2) => { const navigationTree = (await navigation.getAll(params2)).find( (nav) => nav.name === treeName ); if (!navigationTree) { return new ErrorResponse( HttpStatusCode.NOT_FOUND, `No navigation tree with name "${treeName}" found.`, HttpStatusMessage.NOT_FOUND, { name } ); } return navigationTree; }, { cacheKeyPrefix: `getByName-navigation-trees-${treeName}` } )(treeName, params); }, { method: "GET" } );