diff --git a/types/redux-api-middleware/index.d.ts b/types/redux-api-middleware/index.d.ts index d8af85f240..5bf397bf69 100644 --- a/types/redux-api-middleware/index.d.ts +++ b/types/redux-api-middleware/index.d.ts @@ -2,16 +2,11 @@ // Project: https://github.com/agraboso/redux-api-middleware // Definitions by: Andrew Luca // Craig S +// Arturs Vonda // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.0 -import { - Middleware, - MiddlewareAPI, - Dispatch, - AnyAction, - applyMiddleware -} from 'redux'; +import { Dispatch, Middleware, MiddlewareAPI } from 'redux'; type TypeOrResolver = Type | ((arg: Arg) => Type); @@ -121,32 +116,36 @@ export interface RSAAAction { [RSAA]: RSAACall; } +type ValidAction = + { type: string | symbol; error?: false } + // The `[Payload] extends [never]` is required to check if generic type is never. + // Can't do it with just `Payload extends never`. + & ([Payload] extends [never] ? {} : { payload: Payload }) + & ([Meta] extends [never] ? {} : { meta: Meta }); + +interface InvalidAction { + type: string | symbol; + payload: Payload; + error: true; +} + /** * `Promise` is not returned from dispatch like other actions * Is only dispatched through redux */ -export interface RSAARequestAction { - type: string | symbol; - payload?: Payload | InvalidRSAA; - meta?: Meta; - error?: true; -} +export type RSAARequestAction = ValidAction | InvalidAction; -export interface RSAASuccessAction { - type: string | symbol; - payload: Payload | InternalError; - meta?: Meta; - error?: true; -} +// @deprecated Use RSAAResultAction +export type RSAASuccessAction = RSAAResultAction; -export interface RSAAFailureAction { - type: string | symbol; - payload: InternalError | RequestError | ApiError; - meta?: Meta; - error: true; -} +// @deprecated Use RSAAResultAction +export type RSAAFailureAction = RSAAResultAction; -export type RSAAActions = RSAARequestAction | RSAASuccessAction | RSAAFailureAction; +export type RSAAResultAction = + | ValidAction + | InvalidAction>; + +export type RSAAActions = RSAARequestAction | RSAAResultAction; /** * Redux behaviour changed by middleware, so overloads here @@ -157,8 +156,7 @@ declare module 'redux' { * Useful for react-redux or any other library which could use this type. */ interface Dispatch { - (action: RSAAAction): Promise; - (action: RSAAAction): Promise; + (action: RSAAAction): Promise>; // `Promise is returned in case of RSAA validation errors or user bails out (action: RSAAAction): Promise; } diff --git a/types/redux-api-middleware/redux-api-middleware-tests.ts b/types/redux-api-middleware/redux-api-middleware-tests.ts index 38ed451b08..35cc2776ad 100644 --- a/types/redux-api-middleware/redux-api-middleware-tests.ts +++ b/types/redux-api-middleware/redux-api-middleware-tests.ts @@ -17,8 +17,7 @@ import { RSAASuccessTypeDescriptor, RSAAFailureTypeDescriptor, RSAARequestAction, - RSAASuccessAction, - RSAAFailureAction, + RSAAResultAction, } from 'redux-api-middleware'; { @@ -156,7 +155,7 @@ import { { const store: Store = createStore(() => undefined); - const action: RSAAAction = { + const action: RSAAAction = { [RSAA]: { endpoint: '/test/endpoint', method: 'GET', @@ -165,10 +164,21 @@ import { }; store.dispatch(action); - store.dispatch(action).then((action: RSAASuccessAction) => Promise.resolve()); + store.dispatch(action).then((action: RSAAResultAction) => Promise.resolve()); store.dispatch(action).then(() => Promise.resolve()); + store + .dispatch(action) + .then(action => (action.error ? Promise.reject() : Promise.resolve(action.payload))) + .then((payload: string) => payload); + store + .dispatch(action) + .then(action => (action.error ? Promise.reject() : Promise.resolve(action.meta))) + .then((payload: number) => Promise.resolve()); + store + .dispatch(action) + .then(action => (action.error ? Promise.reject() : Promise.resolve(action.payload))) + .then((payload: number) => Promise.resolve()); // $ExpectError store.dispatch(action).then((action: string) => Promise.resolve()); // $ExpectError - store.dispatch(action).catch((action: RSAAFailureAction) => Promise.reject()); } { @@ -222,18 +232,93 @@ import { } { - const requestAction0: RSAARequestAction = { - type: '' + const requestActionSimple: RSAARequestAction = { + type: '', }; - const successAction0: RSAASuccessAction = { + const requestActionWithPayload: RSAARequestAction = { type: '', - payload: 6 + payload: 1, }; - const failureAction0: RSAAFailureAction = { + const requestActionWithIncorrectPayload: RSAARequestAction = { type: '', + payload: '', // $ExpectError + }; + + const requestActionWithMeta: RSAARequestAction = { + type: '', + meta: 1, + }; + + // $ExpectError + const requestActionWithMissingPayload: RSAARequestAction = { + type: '', + }; + + const requestActionWithExtraneousMeta: RSAARequestAction = { + type: '', + payload: 1, + meta: 1, // $ExpectError + }; + + const requestActionWithError: RSAARequestAction = { + type: '', + error: true, + payload: new InvalidRSAA(['']), + }; + + const resultActionSimple: RSAAResultAction = { + type: '', + }; + + const resultActionWithPayload: RSAAResultAction = { + type: '', + payload: 1, + }; + + const resultActionWithIncorrectPayload: RSAAResultAction = { + type: '', + payload: '', // $ExpectError + }; + + const resultActionWithMeta: RSAAResultAction = { + type: '', + meta: 1, + }; + + // $ExpectError + const resultActionWithMissingPayload: RSAAResultAction = { + type: '', + }; + + const resultActionWithExtraneousMeta: RSAAResultAction = { + type: '', + payload: 1, + meta: 1, // $ExpectError + }; + + const resultActionWithApiError: RSAAResultAction = { + type: '', + error: true, payload: new ApiError(500, '', 1), - error: true + }; + + const resultActionWithInternalError: RSAAResultAction = { + type: '', + error: true, + payload: new InternalError(''), + }; + + const resultActionWithRequestError: RSAAResultAction = { + type: '', + error: true, + payload: new RequestError(''), + }; + + // $ExpectError + const resultActionWithMissingErrorPayload: RSAAResultAction = { + type: '', + error: true, }; }