🤖 Merge PR #45635 Add support for Promises in redux-api-middleware by @AdamCLarsen

* Add support for Promises in redux-api-middleware
fix

* Change promises result type def in redux-api-middleware

Reducing the duplication in the type definitions for the Promises return types in functions.
Change promises result type def in redux-api-middleware
This commit is contained in:
Adam Larsen 2020-06-26 19:12:27 -05:00 committed by GitHub
parent dedd90e20c
commit 5ca1e873b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 7 deletions

View File

@ -9,7 +9,7 @@
import { Dispatch, Middleware, MiddlewareAPI } from 'redux';
type TypeOrResolver<Arg, Type> = Type | ((arg: Arg) => Type);
type TypeOrResolver<Arg, Type> = Type | ((arg: Arg) => Type | Promise<Type>);
/**
* This module is also a UMD module that exposes a global variable 'ReduxApiMiddleware'
@ -64,20 +64,20 @@ export function createMiddleware(options?: CreateMiddlewareOptions): Middleware;
export interface RSAARequestTypeDescriptor<State = any, Payload = any, Meta = any> {
type: string | symbol;
payload?: ((action: RSAAAction, state: State) => Payload) | Payload;
meta?: ((action: RSAAAction, state: State) => Meta) | Meta;
payload?: ((action: RSAAAction, state: State) => Payload | Promise<Payload>) | Payload;
meta?: ((action: RSAAAction, state: State) => Meta | Promise<Meta>) | Meta;
}
export interface RSAASuccessTypeDescriptor<State = any, Payload = any, Meta = any> {
type: string | symbol;
payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload;
meta?: ((action: RSAAAction, state: State, res: Response) => Meta) | Meta;
payload?: ((action: RSAAAction, state: State, res: Response) => Payload | Promise<Payload>) | Payload;
meta?: ((action: RSAAAction, state: State, res: Response) => Meta | Promise<Meta>) | Meta;
}
export interface RSAAFailureTypeDescriptor<State = any, Payload = any, Meta = any> {
type: string | symbol;
payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload;
meta?: ((action: RSAAAction, state: State, res: Response) => Meta) | Meta;
payload?: ((action: RSAAAction, state: State, res: Response) => Payload | Promise<Payload>) | Payload;
meta?: ((action: RSAAAction, state: State, res: Response) => Meta | Promise<Meta>) | Meta;
}
export type RSAARequestType<State = any, Payload = any, Meta = any> =

View File

@ -145,6 +145,26 @@ import {
types: ['REQ_TYPE', 'SUCCESS_TYPE', 'FAILURE_TYPE'];
}
class PromiseStateDrivenRSAACall implements RSAACall<State> {
endpoint(state: State) {
return Promise.resolve(state.path);
}
headers(state: State) {
return Promise.resolve(state.headers);
}
options(state: State) {
return Promise.resolve(state.options);
}
body(state: State) {
return Promise.resolve(state.body);
}
bailout(state: State) {
return Promise.resolve(state.bailout);
}
method: 'GET';
types: ['REQ_TYPE', 'SUCCESS_TYPE', 'FAILURE_TYPE'];
}
class NonStateDrivenRSAACall implements RSAACall<State> {
endpoint: '/test/endpoint';
method: 'GET';
@ -199,6 +219,11 @@ import {
payload: '', // $ExpectError
meta: (action: RSAAAction, state: number) => '', // $ExpectError
};
const requestDescriptor3: RSAARequestTypeDescriptor<number, number, number> = {
type: Symbol(),
payload: (action: RSAAAction, state: number) => Promise.resolve(state),
meta: (action: RSAAAction, state: number) => Promise.resolve(state),
};
const successDescriptor0: RSAASuccessTypeDescriptor<number, number, number> = {
type: '',
@ -215,6 +240,11 @@ import {
payload: '', // $ExpectError
meta: (action: RSAAAction, state: number) => '', // $ExpectError
};
const successDescriptor3: RSAASuccessTypeDescriptor<number, number, number> = {
type: Symbol(),
payload: (action: RSAAAction, state: number, res: Response) => Promise.resolve(state),
meta: (action: RSAAAction, state: number, res: Response) => Promise.resolve(state),
};
const failureDescriptor0: RSAAFailureTypeDescriptor<number, number, number> = {
type: '',
@ -231,6 +261,11 @@ import {
payload: '', // $ExpectError
meta: (action: RSAAAction, state: number) => '', // $ExpectError
};
const failureDescriptor3: RSAAFailureTypeDescriptor<number, number, number> = {
type: Symbol(),
payload: (action: RSAAAction, state: number, res: Response) => Promise.resolve(state),
meta: (action: RSAAAction, state: number, res: Response) => Promise.resolve(state),
};
}
{