UNPKG

@scayle/storefront-core

Version:

Collection of essential utilities to work with the Storefront API

30 lines (29 loc) 909 B
import { mapSAPIFetchErrorToResponse } from "../../utils/sapi.mjs"; import { defineRpcHandler } from "../../utils/index.mjs"; const MAX_PER_PAGE = 100; export const getBrands = defineRpcHandler( async ({ pagination }, context) => { const { sapiClient, cached } = context; return await cached(mapSAPIFetchErrorToResponse(sapiClient.brands.get), { cacheKeyPrefix: "getBrands" })({ pagination: { page: pagination?.page, perPage: pagination?.perPage ? Math.min(pagination.perPage, MAX_PER_PAGE) : void 0 } }); }, { method: "GET" } ); export const getBrandById = defineRpcHandler( async ({ brandId }, context) => { const { sapiClient, cached } = context; return await cached( mapSAPIFetchErrorToResponse(sapiClient.brands.getById), { cacheKeyPrefix: `getBrandById-${brandId}` } )(brandId); }, { method: "GET" } );