UNPKG

@scayle/storefront-nuxt

Version:

Nuxt integration for the SCAYLE Commerce Engine and Storefront API

42 lines (41 loc) 1.21 kB
import { unwrap, DEFAULT_RPC_HTTP_METHOD } from "@scayle/storefront-core"; import { rpcHttpMethods } from "#virtual/rpcHttpMethods"; const buildRequest = (method, params, apiBasePath, shopId, rpcHttpMethods2) => { const httpMethod = rpcHttpMethods2[method] ?? DEFAULT_RPC_HTTP_METHOD; const headers = { "x-shop-id": shopId.toString() }; if (httpMethod === "GET") { return { path: `${apiBasePath}/rpc/${method}`, options: { method: httpMethod, headers, params: params !== void 0 && params !== null ? { payload: JSON.stringify(params) } : void 0 } }; } return { path: `${apiBasePath}/rpc/${method}`, options: { method: httpMethod, body: { payload: params }, headers } }; }; export const rpcCall = (nuxtApp, method, shop) => { const fetch = nuxtApp.ssrContext?.event.context.$fetchWithContext ?? $fetch; return async (params = void 0) => { const apiBasePath = shop.apiBasePath ?? "/api"; const { path, options } = buildRequest( method, params, apiBasePath, shop.shopId, rpcHttpMethods ); const data = await fetch(path, options); return await unwrap(data); }; };