import type { Location, Action, History } from 'history'; import type { AnyAction } from 'redux'; export const CALL_HISTORY_METHOD = '@@router/CALL_HISTORY_METHOD'; export const LOCATION_CHANGE = '@@router/LOCATION_CHANGE'; //support history v4/ v5 export type HistoryMethods = | 'push' | 'replace' | 'go' | 'goBack' | 'goForward' | 'back' | 'forward'; export const locationChangeAction = (location: Location, action: Action) => ({ type: LOCATION_CHANGE as typeof LOCATION_CHANGE, payload: { location, action } as { location: Location; action: Action }, }); export interface LocationActionPayload { method: string; args?: A; } export interface CallHistoryMethodAction extends AnyAction { type: typeof CALL_HISTORY_METHOD; payload: LocationActionPayload; } function updateLocation(method: T) { // @ts-ignore //support history 5.x back/forward return (...args: Parameters): CallHistoryMethodAction> => ({ type: CALL_HISTORY_METHOD as typeof CALL_HISTORY_METHOD, payload: { method, args }, }); } export const push: ( ...args: Parameters ) => CallHistoryMethodAction> = updateLocation('push'); export const replace: ( ...args: Parameters ) => CallHistoryMethodAction> = updateLocation('replace'); export const go: ( ...args: Parameters ) => CallHistoryMethodAction> = updateLocation('go'); export const goBack: () => CallHistoryMethodAction> = updateLocation('goBack'); export const goForward: () => CallHistoryMethodAction> = updateLocation('goForward'); // @ts-ignore //support history 5.x back/forward export const back: () => CallHistoryMethodAction> = updateLocation('back'); // @ts-ignore //support history 5.x back/forward export const forward: () => CallHistoryMethodAction> = updateLocation('forward'); export type RouterActions = | ReturnType | ReturnType | ReturnType | ReturnType | ReturnType | ReturnType | ReturnType | ReturnType;