// Type definitions for redux-api-middleware 3.0 // 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 { Dispatch, Middleware, MiddlewareAPI } from 'redux'; type TypeOrResolver = Type | ((arg: Arg) => Type); /** * This module is also a UMD module that exposes a global variable 'ReduxApiMiddleware' * when loaded outside a module loader environment. */ export as namespace ReduxApiMiddleware; export const RSAA = '@@redux-api-middleware/RSAA'; export function isRSAA(action: object): boolean; export function validateRSAA(action: object): string[]; export function isValidRSAA(action: object): boolean; export class InvalidRSAA extends Error { name: 'InvalidRSAA'; message: 'Invalid RSAA'; validationErrors: string[]; constructor(validationErrors: string[]); } export class InternalError extends Error { name: 'InternalError'; message: string; constructor(message: string); } export class RequestError extends Error { name: 'RequestError'; message: string; constructor(message: string); } export class ApiError extends Error { name: 'ApiError'; status: number; statusText: string; response: T; message: string; constructor(status: number, statusText: string, response: T); } export function getJSON(res: Response): Promise | Promise; export function apiMiddleware(api: MiddlewareAPI): ReturnType; export interface CreateMiddlewareOptions { ok?: (res: Response) => boolean; fetch?: typeof fetch; } export function createMiddleware(options?: CreateMiddlewareOptions): Middleware; export interface RSAARequestTypeDescriptor { type: string | symbol; payload?: ((action: RSAAAction, state: State) => Payload) | Payload; meta?: ((action: RSAAAction, state: State) => Meta) | Meta; } export interface RSAASuccessTypeDescriptor { type: string | symbol; payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload; meta?: ((action: RSAAAction, state: State, res: Response) => Meta) | Meta; } export interface RSAAFailureTypeDescriptor { type: string | symbol; payload?: ((action: RSAAAction, state: State, res: Response) => Payload) | Payload; meta?: ((action: RSAAAction, state: State, res: Response) => Meta) | Meta; } export type RSAARequestType = | string | symbol | RSAARequestTypeDescriptor; export type RSAASuccessType = | string | symbol | RSAASuccessTypeDescriptor; export type RSAAFailureType = | string | symbol | RSAAFailureTypeDescriptor; export interface RSAACall { endpoint: TypeOrResolver; // `redux-api-middleware` strictly allows only this methods method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS'; types: [ RSAARequestType, RSAASuccessType, RSAAFailureType ]; body?: TypeOrResolver; headers?: TypeOrResolver; options?: TypeOrResolver; credentials?: RequestCredentials; bailout?: TypeOrResolver; fetch?: typeof fetch; ok?: (res: Response) => boolean; } 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 type RSAARequestAction = ValidAction | InvalidAction; // @deprecated Use RSAAResultAction export type RSAASuccessAction = RSAAResultAction; // @deprecated Use RSAAResultAction export type RSAAFailureAction = RSAAResultAction; export type RSAAResultAction = | ValidAction | InvalidAction>; export type RSAAActions = RSAARequestAction | RSAAResultAction; /** * 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>; // `Promise is returned in case of RSAA validation errors or user bails out (action: RSAAAction): Promise; } } export { };