11export const CALL_API = Symbol ( 'Call API' ) ;
2+ export const CALL_API_PHASE = Symbol ( 'Call API phase' ) ;
3+
4+ const REQUEST = 'REQUEST' ;
5+ const SUCCESS = 'SUCCESS' ;
6+ const FAILURE = 'FAILURE' ;
7+
8+ export const callApiPhases = {
9+ REQUEST ,
10+ SUCCESS ,
11+ FAILURE ,
12+ } ;
213
314export const actionWith = ( actionType , args , payload ) => {
415 let nextAction ;
@@ -16,10 +27,21 @@ export const actionWith = (actionType, args, payload) => {
1627 nextAction . payload = payload ;
1728 }
1829 }
30+ // backward compatibility
1931 if ( payload instanceof Error ) {
2032 nextAction . error = true ;
2133 }
2234
35+ let phase ;
36+ if ( payload === undefined ) {
37+ phase = REQUEST ;
38+ } else if ( payload instanceof Error ) {
39+ phase = FAILURE ;
40+ } else if ( payload !== undefined ) {
41+ phase = SUCCESS ;
42+ }
43+ nextAction [ CALL_API_PHASE ] = phase ;
44+
2345 return nextAction ;
2446} ;
2547
@@ -40,7 +62,7 @@ export const createMiddleware = ({
4062
4163 const apiAction = action [ CALL_API ] ;
4264 let { batch } = apiAction ;
43- const { endpoint, options, types } = apiAction ;
65+ const { endpoint, options } = apiAction ;
4466 const batchMode = Array . isArray ( batch ) ;
4567
4668 if ( ! batchMode ) {
@@ -53,6 +75,10 @@ export const createMiddleware = ({
5375 } ) ) ;
5476
5577 // action types
78+ const types = apiAction . type
79+ ? Array ( 3 ) . fill ( apiAction . type )
80+ : apiAction . types ;
81+
5682 const [ requestType , successType , failureType ] = types ;
5783
5884 // dispatch request type
0 commit comments