@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
23 lines (22 loc) • 882 B
JavaScript
import { rpcMethods as coreRpcMethods } from "@scayle/storefront-core";
import * as storefrontRpcMethods from "#virtual/customRpcMethods";
import { normalizeRpcHandler } from "./utils/rpc.js";
const rpcMethods = Object.fromEntries(
Object.entries({
...coreRpcMethods,
...storefrontRpcMethods
}).map(([key, value]) => [key, normalizeRpcHandler(value)])
);
export const invokeRpc = async (method, payload, rpcContext) => {
if (!Object.hasOwn(rpcMethods, method)) {
throw new Error(`RPC Method [${method}] was not found`);
}
const log = rpcContext.log.space("sfc").space("rpc");
const handlerFunction = rpcMethods[method];
return await log.time(`Calling RPC method: ${method}`, async () => {
if (handlerFunction.rpcType === "NoParam") {
return await handlerFunction(rpcContext);
}
return await handlerFunction(payload, rpcContext);
});
};