@empathyco/x-components
Version:
Empathy X Components
72 lines • 3.3 kB
TypeScript
import type { XActionContext } from '../actions.types';
import type { StatusMutations, StatusState } from './status-store.utils';
/**
* Utility to create an action that requests and save some data asynchronously, with the
* option to cancel the request at any moment. This factory provides with the standard flow
* for requesting, cancelling, handling errors for a module, while also taking care of its status.
*
* @param hooks - The {@link FetchAndSaveHooks} hooks to create the action.
* @public
* @returns An action to fetch and save some data, and an action to cancel the last request.
*/
export declare function createFetchAndSaveActions<Context extends XActionContext<StatusState, object, StatusMutations, object>, Request, Response>({ fetch, onSuccess, onError, onCancel, }: FetchAndSaveHooks<Context, Request, Response>): FetchAndSaveActions<Context, Request>;
/**
* Options to use with the {@link createFetchAndSaveActions} factory.
*
* @public
*/
export interface FetchAndSaveHooks<Context extends XActionContext<StatusState, object, StatusMutations, object>, Request, Response> {
/**
* Retrieves and returns asynchronously some data.
*
* @param context - The {@link https://vuex.vuejs.org/guide/actions.html | context} of the
* actions, provided by Vuex.
* @param request - The request object used for fetching.
* @returns A Promise resolved with the response of the fetch request.
*/
fetch: (context: Context, request: Request) => Promise<Response>;
/**
* Asynchronous callback executed when the {@link FetchAndSaveHooks.fetch} is
* performed successfully.
*
* @param context - The {@link https://vuex.vuejs.org/guide/actions.html | context} of the
* actions, provided by Vuex.
* @param response - The data returned by {@link FetchAndSaveHooks.fetch}.
*/
onSuccess: (context: Context, response: Response) => void;
/**
* Asynchronous callback executed when either the {@link FetchAndSaveHooks.fetch}
* or {@link FetchAndSaveHooks.onSuccess} methods fail.
*
* @param error - The error that triggered this callback.
*/
onError?: (error: unknown) => void;
/**
* Synchronous callback executed when the request is cancelled. This can happen mainly for two
* reasons:
* - The {@link FetchAndSaveActions.cancelPrevious} action is dispatched.
* - A new {@link FetchAndSaveActions.fetchAndSave} is dispatched before the previous one was
* resolved.
*/
onCancel?: () => void;
}
/**
* Actions returned from the {@link createFetchAndSaveActions}.
*
* @public
*/
export interface FetchAndSaveActions<Context extends XActionContext<StatusState, object, StatusMutations, object>, Request> {
/**
* Action that requests and saves the response.
*
* @param context - The {@link https://vuex.vuejs.org/guide/actions.html | context} of the
* actions, provided by Vuex.
* @returns A promise that resolves after saving the response.
*/
fetchAndSave: (context: Context, request: Request) => void | Promise<void>;
/**
* Action that cancels the previous request call if it stills in progress.
*/
cancelPrevious: () => void;
}
//# sourceMappingURL=fetch-and-save-action.utils.d.ts.map