feat(redux-api-middleware): overload redux/Dispatch signature (#38175)

This commit is contained in:
Andrew Luca
2019-09-09 20:16:16 -07:00
committed by Mine Starks
parent e341115317
commit e53c3246bc
2 changed files with 27 additions and 2 deletions
+15 -2
View File
@@ -74,13 +74,13 @@ export interface RSAARequestTypeDescriptor<State = any, Payload = any, Meta = an
export interface RSAASuccessTypeDescriptor<State = any, Payload = any, Meta = any> {
type: string | symbol;
payload?: ((action: RSAAAction, state: State, res: any) => Payload) | Payload;
payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload;
meta?: ((action: RSAAAction, state: State, res: any) => Meta) | Meta;
}
export interface RSAAFailureTypeDescriptor<State = any, Payload = any, Meta = any> {
type: string | symbol;
payload?: ((action: RSAAAction, state: State) => Payload) | Payload;
payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload;
meta?: ((action: RSAAAction, state: State, res: any) => Meta) | Meta;
}
@@ -105,4 +105,17 @@ export interface RSAAAction<State = any, Payload = any, Meta = any> {
[RSAA]: RSAACall<State, Payload, Meta>;
}
/**
* Redux behaviour changed by middleware, so overloads here
*/
declare module 'redux' {
/*
* Overload to add api middleware support to Redux's dispatch() function.
* Useful for react-redux or any other library which could use this type.
*/
interface Dispatch {
(action: RSAAAction): Promise<any>;
}
}
export {};
@@ -1,3 +1,4 @@
import { createStore, Store } from 'redux';
import {
RSAA,
isRSAA,
@@ -134,3 +135,14 @@ import {
types: ['REQ_TYPE', 'SUCCESS_TYPE', 'FAILURE_TYPE'];
}
}
{
const store: Store = createStore(() => undefined);
store.dispatch({
[RSAA]: {
endpoint: '/test/endpoint',
method: 'GET',
types: ['REQ_TYPE', 'SUCCESS_TYPE', 'FAILURE_TYPE'],
}
});
}