import type { Denormalize, EndpointInterface, EntityPath, Queryable, ResolveType, UnknownError, } from '@data-client/normalizr'; import type { SET, RESET, FETCH, SUBSCRIBE, UNSUBSCRIBE, INVALIDATE, GC, OPTIMISTIC, INVALIDATEALL, EXPIREALL, SET_RESPONSE, } from './actionTypes.js'; import type { EndpointUpdateFunction } from './controller/types.js'; type EndpointAndUpdate = EndpointInterface & { update?: EndpointUpdateFunction; }; type EndpointDefault = EndpointInterface & { update?: EndpointUpdateFunction; }; /** General meta-data for operators */ export interface ActionMeta { readonly fetchedAt: number; readonly date: number; readonly expiresAt: number; } /** Action for Controller.set() */ export interface SetAction { type: typeof SET; schema: S; args: readonly any[]; meta: ActionMeta; value: {} | ((previousValue: Denormalize) => {}); } /* setResponse */ export interface SetResponseActionBase< E extends EndpointAndUpdate = EndpointDefault, > { type: typeof SET_RESPONSE; endpoint: E; args: readonly any[]; key: string; meta: ActionMeta; } export interface SetResponseActionSuccess< E extends EndpointAndUpdate = EndpointDefault, > extends SetResponseActionBase { response: ResolveType; error?: false; } export interface SetResponseActionError< E extends EndpointAndUpdate = EndpointDefault, > extends SetResponseActionBase { response: UnknownError; error: true; } /** Action for Controller.setResponse() */ export type SetResponseAction< E extends EndpointAndUpdate = EndpointDefault, > = SetResponseActionSuccess | SetResponseActionError; /* FETCH */ export interface FetchMeta { fetchedAt: number; resolve: (value?: any | PromiseLike) => void; reject: (reason?: any) => void; promise: PromiseLike; } /** Action for Controller.fetch() */ export interface FetchAction = EndpointDefault> { type: typeof FETCH; endpoint: E; args: readonly [...Parameters]; key: string; meta: FetchMeta; } /* OPTIMISTIC */ /** Action for Endpoint.getOptimisticResponse() */ export interface OptimisticAction< E extends EndpointAndUpdate = EndpointDefault, > { type: typeof OPTIMISTIC; endpoint: E; args: readonly any[]; key: string; meta: ActionMeta; error?: false; } /* SUBSCRIBE */ /** Action for Controller.subscribe() */ export interface SubscribeAction< E extends EndpointAndUpdate = EndpointDefault, > { type: typeof SUBSCRIBE; endpoint: E; args: readonly any[]; key: string; } /** Action for Controller.unsubscribe() */ export interface UnsubscribeAction< E extends EndpointAndUpdate = EndpointDefault, > { type: typeof UNSUBSCRIBE; endpoint: E; args: readonly any[]; key: string; } /* EXPIRY */ export interface ExpireAllAction { type: typeof EXPIREALL; testKey: (key: string) => boolean; } /* INVALIDATE */ export interface InvalidateAllAction { type: typeof INVALIDATEALL; testKey: (key: string) => boolean; } export interface InvalidateAction { type: typeof INVALIDATE; key: string; } /* RESET */ export interface ResetAction { type: typeof RESET; date: number; } /* GC */ export interface GCAction { type: typeof GC; entities: EntityPath[]; endpoints: string[]; } /** @see https://dataclient.io/docs/api/Actions */ export type ActionTypes = | FetchAction | OptimisticAction | SetAction | SetResponseAction | SubscribeAction | UnsubscribeAction | InvalidateAction | InvalidateAllAction | ExpireAllAction | ResetAction | GCAction;