UNPKG

@scayle/storefront-nuxt

Version:

Nuxt integration for the SCAYLE Commerce Engine and Storefront API

43 lines (42 loc) 2.08 kB
import type { RpcContext, RpcMethodName, RpcMethodParameters, RpcMethodReturnType } from '@scayle/storefront-core'; /** * Provides a function for calling a specified RPC method. * * This function acts as a factory, returning a function that can be used to * call the given RPC method. It's designed for calling RPCs in response to * events or user interactions, such as clicking a button or submitting a form. * `useRpcCall` accepts the RPC method name and returns a callback function. * When invoked, this callback receives the RPC parameters, makes the request, * and returns a promise that resolves with the RPC's response. * * @template N The RPC Method Name. * @template P The RPC Method Parameters. * @template TResult The result type of the RPC method, excluding its`Response` object. * @template TakesParameters A boolean indicating whether the RPC method takes parameters. * It's `undefined` if the parameters extend `RpcContext`. * * @param method The name of the RPC method. * * @returns A function to call the specified RPC method. This returned function * will accept the RPC method parameters (if any) and return a Promise * that resolves with the result of the RPC call. * * @example * ```html * <!-- Example of a `subscribeToNewsletter` RPC method, triggered by a form submission --> * <script setup lang="ts"> * import { useRpcCall } from '#storefront/composables' * import { ref } from 'vue'; * * const subscribeToNewsletter = useRpcCall('subscribeToNewsletter') * const email = ref('hello@scayle.com') * * const onSubmit = async () => { * const res = await subscribeToNewsletter({ email: email.value }) * * console.log(res) * } * </script> * ``` */ export declare function useRpcCall<N extends RpcMethodName, P extends RpcMethodParameters<N>, TResult extends Exclude<Awaited<RpcMethodReturnType<N>>, Response>, TakesParameters = P extends RpcContext ? undefined : boolean>(method: N): TakesParameters extends undefined ? () => Promise<TResult> : (params: P) => Promise<TResult>;