Skip to content
3 changes: 3 additions & 0 deletions packages/redux-query-react/flow-test/hooks/use-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import useRequest from '../../src/hooks/use-request';
const Card = () => {
const [{ isPending }] = useRequest({
url: '/api',
customQueryMiddlewareConfig: {
retryableStatusCodes: [504],
},
});

return <div>{isPending ? 'loading…' : 'loaded'}</div>;
Expand Down
16 changes: 13 additions & 3 deletions packages/redux-query/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ declare module 'redux-query' {
export type RollbackStrategy<T> = (initialValue: T, currentValue: T) => T;

export type Update<TEntities = Entities> = {
[K in keyof TEntities]?: UpdateStrategy<TEntities[K]>
[K in keyof TEntities]?: UpdateStrategy<TEntities[K]>;
};

export type OptimisticUpdate<TEntities = Entities> = {
[K in keyof TEntities]?: OptimisticUpdateStrategy<TEntities[K]>
[K in keyof TEntities]?: OptimisticUpdateStrategy<TEntities[K]>;
};

export type Rollback<TEntities = Entities> = {
[K in keyof TEntities]?: RollbackStrategy<TEntities[K]>
[K in keyof TEntities]?: RollbackStrategy<TEntities[K]>;
};

export interface WithTime {
Expand Down Expand Up @@ -158,6 +158,15 @@ declare module 'redux-query' {
queryCount: number;
};

export type QueryMiddlewareConfig = {
backoff: {
maxAttempts: number;
minDuration: number;
maxDuration: number;
};
retryableStatusCodes: Array<Status>;
};

export interface QueryConfig<TEntities = Entities> {
body?: RequestBody;
force?: boolean;
Expand All @@ -171,6 +180,7 @@ declare module 'redux-query' {
rollback?: Rollback<TEntities>;
unstable_preDispatchCallback?: () => void;
url: Url;
customQueryMiddlewareConfig?: QueryMiddlewareConfig;
}

export interface QueriesState {
Expand Down
4 changes: 4 additions & 0 deletions packages/redux-query/src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export const requestAsync = ({
url,
/* eslint-disable-next-line camelcase */
unstable_preDispatchCallback,
customQueryMiddlewareConfig,
}: QueryConfig): RequestAsyncAction => {
return {
type: actionTypes.REQUEST_ASYNC,
Expand All @@ -289,6 +290,7 @@ export const requestAsync = ({
update,
url,
unstable_preDispatchCallback,
customQueryMiddlewareConfig,
};
};

Expand All @@ -307,6 +309,7 @@ export const mutateAsync = ({
transform,
update,
url,
customQueryMiddlewareConfig,
}: QueryConfig): MutateAsyncAction => {
return {
type: actionTypes.MUTATE_ASYNC,
Expand All @@ -319,6 +322,7 @@ export const mutateAsync = ({
transform,
update,
url,
customQueryMiddlewareConfig,
};
};

Expand Down
15 changes: 4 additions & 11 deletions packages/redux-query/src/middleware/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,17 @@ import type {
ResponseBody,
Status,
Transform,
QueryMiddlewareConfig,
} from '../types';

type Config = {|
backoff: {|
maxAttempts: number,
minDuration: number,
maxDuration: number,
|},
retryableStatusCodes: Array<Status>,
|};

type ReduxStore = {|
dispatch: (action: Action) => any,
getState: () => any,
|};

type Next = (action: PublicAction) => any;

const defaultConfig: Config = {
const defaultConfig: QueryMiddlewareConfig = {
backoff: {
maxAttempts: 5,
minDuration: 300,
Expand Down Expand Up @@ -105,7 +97,8 @@ const queryMiddleware = (

return ({ dispatch, getState }: ReduxStore) => (next: Next) => (action: PublicAction) => {
let returnValue;
const config = { ...defaultConfig, ...customConfig };
const customQueryMiddlewareConfigFromAction = action.customQueryMiddlewareConfig || {};
const config = { ...defaultConfig, ...customConfig, ...customQueryMiddlewareConfigFromAction };

switch (action.type) {
case actionTypes.REQUEST_ASYNC: {
Expand Down
10 changes: 10 additions & 0 deletions packages/redux-query/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ type QueryOptions = {
headers?: { [key: string]: any },
};

export type QueryMiddlewareConfig = {|
backoff: {|
maxAttempts: number,
minDuration: number,
maxDuration: number,
|},
retryableStatusCodes: Array<Status>,
|};

export type QueryConfig = {|
body?: RequestBody,
force?: boolean,
Expand All @@ -26,6 +35,7 @@ export type QueryConfig = {|
rollback?: { [key: string]: (initialValue: any, currentValue: any) => any },
unstable_preDispatchCallback?: () => void,
url: Url,
customQueryMiddlewareConfig?: QueryMiddlewareConfig,
|};

export type QueryDetails = {
Expand Down