import type { ActionFromMatcher , Matcher , UnionToIntersection , } from './tsHelpers' ; import { hasMatchFunction } from './tsHelpers' ; import type { AsyncThunk , AsyncThunkFulfilledActionCreator , AsyncThunkPendingActionCreator , AsyncThunkRejectedActionCreator , } from './createAsyncThunk' ; public export type ActionMatchingAnyOf < Matchers extends [ Matcher < any > , ... Matcher < any > [ ] ] > = ActionFromMatcher < Matchers [ number ] > ; public export type ActionMatchingAllOf < Matchers extends [ Matcher < any > , ... Matcher < any > [ ] ] > = UnionToIntersection < ActionMatchingAnyOf < Matchers > > ; const matches = ( matcher , action ) => { if ( hasMatchFunction ( matcher ) ) { return matcher . match ( action ) ; } else { return matcher ( action ) ; } } ; param matchers public export function isAnyOf < Matchers extends [ Matcher < any > , ... Matcher < any > [ ] ] > ( ... matchers ) { return ( action ) : action is ActionMatchingAnyOf < Matchers > => { return matchers . some ( ( matcher ) => matches ( matcher , action ) ) ; } ; } param matchers public export function isAllOf < Matchers extends [ Matcher < any > , ... Matcher < any > [ ] ] > ( ... matchers ) { return ( action ) : action is ActionMatchingAllOf < Matchers > => { return matchers . every ( ( matcher ) => matches ( matcher , action ) ) ; } ; } param action param validStatus internal export function hasExpectedRequestMetadata ( action , validStatus ) { if ( ! action || ! action . meta ) return false ; const hasValidRequestId = typeof action . meta . requestId === 'string' ; const hasValidRequestStatus = validStatus . indexOf ( action . meta . requestStatus ) > - 1 ; return hasValidRequestId && hasValidRequestStatus ; } function isAsyncThunkArray ( a ) { return ( typeof a [ 0 ] === 'function' && 'pending' in a [ 0 ] && 'fulfilled' in a [ 0 ] && 'rejected' in a [ 0 ] ) ; } export type UnknownAsyncThunkPendingAction = ReturnType < AsyncThunkPendingActionCreator < unknown > > ; export type PendingActionFromAsyncThunk < T extends AnyAsyncThunk > = ActionFromMatcher < T [ 'pending' ] > ; public export function isPending ( ) ; param asyncThunks public export function isPending < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) ; public export function isPending ( action ) ; export function isPending < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) { if ( asyncThunks . length === 0 ) { return ( action ) => hasExpectedRequestMetadata ( action , [ 'pending' ] ) ; } if ( ! isAsyncThunkArray ( asyncThunks ) ) { return isPending ( ) ( asyncThunks [ 0 ] ) ; } return ( action ) : action is PendingActionFromAsyncThunk < AsyncThunks [ number ] > => { const matchers = asyncThunks . map ( ( asyncThunk ) => asyncThunk . pending ) as any ; const combinedMatcher = isAnyOf ( ... matchers ) ; return combinedMatcher ( action ) ; } ; } export type UnknownAsyncThunkRejectedAction = ReturnType < AsyncThunkRejectedActionCreator < unknown , unknown > > ; export type RejectedActionFromAsyncThunk < T extends AnyAsyncThunk > = ActionFromMatcher < T [ 'rejected' ] > ; public export function isRejected ( ) ; param asyncThunks public export function isRejected < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) ; public export function isRejected ( action ) ; export function isRejected < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) { if ( asyncThunks . length === 0 ) { return ( action ) => hasExpectedRequestMetadata ( action , [ 'rejected' ] ) ; } if ( ! isAsyncThunkArray ( asyncThunks ) ) { return isRejected ( ) ( asyncThunks [ 0 ] ) ; } return ( action ) : action is RejectedActionFromAsyncThunk < AsyncThunks [ number ] > => { const matchers = asyncThunks . map ( ( asyncThunk ) => asyncThunk . rejected ) as any ; const combinedMatcher = isAnyOf ( ... matchers ) ; return combinedMatcher ( action ) ; } ; } export type UnknownAsyncThunkRejectedWithValueAction = ReturnType < AsyncThunkRejectedActionCreator < unknown , unknown > > ; export type RejectedWithValueActionFromAsyncThunk < T extends AnyAsyncThunk > = ActionFromMatcher < T [ 'rejected' ] > & ( T extends AsyncThunk < any , any , { rejectValue : infer RejectedValue ; } > ? { payload : RejectedValue ; } : unknown ) ; public export function isRejectedWithValue ( ) ; param asyncThunks public export function isRejectedWithValue < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) ; public export function isRejectedWithValue ( action ) ; export function isRejectedWithValue < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) { const hasFlag = ( action ) : action is any => { return action && action . meta && action . meta . rejectedWithValue ; } ; if ( asyncThunks . length === 0 ) { return ( action ) => { const combinedMatcher = isAllOf ( isRejected ( ... asyncThunks ) , hasFlag ) ; return combinedMatcher ( action ) ; } ; } if ( ! isAsyncThunkArray ( asyncThunks ) ) { return isRejectedWithValue ( ) ( asyncThunks [ 0 ] ) ; } return ( action ) : action is RejectedActionFromAsyncThunk < AsyncThunks [ number ] > => { const combinedMatcher = isAllOf ( isRejected ( ... asyncThunks ) , hasFlag ) ; return combinedMatcher ( action ) ; } ; } export type UnknownAsyncThunkFulfilledAction = ReturnType < AsyncThunkFulfilledActionCreator < unknown , unknown > > ; export type FulfilledActionFromAsyncThunk < T extends AnyAsyncThunk > = ActionFromMatcher < T [ 'fulfilled' ] > ; public export function isFulfilled ( ) ; param asyncThunks public export function isFulfilled < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) ; public export function isFulfilled ( action ) ; export function isFulfilled < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) { if ( asyncThunks . length === 0 ) { return ( action ) => hasExpectedRequestMetadata ( action , [ 'fulfilled' ] ) ; } if ( ! isAsyncThunkArray ( asyncThunks ) ) { return isFulfilled ( ) ( asyncThunks [ 0 ] ) ; } return ( action ) : action is FulfilledActionFromAsyncThunk < AsyncThunks [ number ] > => { const matchers = asyncThunks . map ( ( asyncThunk ) => asyncThunk . fulfilled ) as any ; const combinedMatcher = isAnyOf ( ... matchers ) ; return combinedMatcher ( action ) ; } ; } export type UnknownAsyncThunkAction = UnknownAsyncThunkPendingAction | UnknownAsyncThunkRejectedAction | UnknownAsyncThunkFulfilledAction ; export type AnyAsyncThunk = { pending : { match : ( action : any ) => action is any ; } ; fulfilled : { match : ( action : any ) => action is any ; } ; rejected : { match : ( action : any ) => action is any ; } ; } ; export type ActionsFromAsyncThunk < T extends AnyAsyncThunk > = ActionFromMatcher < T [ 'pending' ] > | ActionFromMatcher < T [ 'fulfilled' ] > | ActionFromMatcher < T [ 'rejected' ] > ; public export function isAsyncThunkAction ( ) ; param asyncThunks public export function isAsyncThunkAction < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) ; public export function isAsyncThunkAction ( action ) ; export function isAsyncThunkAction < AsyncThunks extends [ AnyAsyncThunk , ... AnyAsyncThunk [ ] ] > ( ... asyncThunks ) { if ( asyncThunks . length === 0 ) { return ( action ) => hasExpectedRequestMetadata ( action , [ 'pending' , 'fulfilled' , 'rejected' ] ) ; } if ( ! isAsyncThunkArray ( asyncThunks ) ) { return isAsyncThunkAction ( ) ( asyncThunks [ 0 ] ) ; } return ( action ) : action is ActionsFromAsyncThunk < AsyncThunks [ number ] > => { const matchers = [ ] as any ; for ( const asyncThunk of asyncThunks ) { matchers . push ( asyncThunk . pending , asyncThunk . rejected , asyncThunk . fulfilled ) ; } const combinedMatcher = isAnyOf ( ... matchers ) ; return combinedMatcher ( action ) ; } ; }