@towns-protocol/react-sdk
Version:
React Hooks for Towns Protocol SDK
38 lines • 1.74 kB
TypeScript
/**
* Configuration options for an action.
* It can be used to configure the behavior of the useAction hook.
*/
export type ActionConfig<Action> = {
/** Callback function to be called when an error occurs while executing the action. */
onError?: (err: Error) => void;
/** Callback function to be called when the action is successful. */
onSuccess?: (data: ReturnOf<Action>) => void;
};
type MultipleParams<T> = T extends unknown[] ? T : [T];
type ActionFn<T> = T extends (...args: infer Args) => Promise<infer Return> ? (...args: Args) => Promise<Return> : never;
type ParamsOf<T> = Parameters<ActionFn<T>>;
type ReturnOf<T> = Awaited<ReturnType<ActionFn<T>>>;
/**
* Hook to create an action from a namespace.
* @internal
* @param namespace - The namespace to create the action from.
* @param fnName - The name of the action to create. Example: `Namespace.fnName`
* @param config - Configuration options for the action.
* @returns The action and its loading state.
*/
export declare const useAction: <Namespace, Key extends keyof Namespace, Fn extends Namespace[Key]>(namespace: Namespace | undefined, fnName: Key & string, config?: ActionConfig<Fn>) => {
/** The action to execute. */
action: (...args: MultipleParams<ParamsOf<Fn>>) => Promise<ReturnOf<Fn>>;
/** The data returned by the action. */
data: Awaited<ReturnType<ActionFn<Fn>>> | undefined;
/** The error that occurred while executing the action. */
error: Error | undefined;
/** Whether the action is pending. */
isPending: boolean;
/** Whether the action is successful. */
isSuccess: boolean;
/** Whether the action is in error. */
isError: boolean;
};
export {};
//# sourceMappingURL=useAction.d.ts.map