Skip to content

Commit 562599e

Browse files
authored
Merge pull request #343 from amplitude/AMP-KW-override
Add `customQueryMiddlewareConfig` to requests/mutations
2 parents 6510b11 + 096d3dc commit 562599e

File tree

5 files changed

+34
-14
lines changed

5 files changed

+34
-14
lines changed

packages/redux-query-react/flow-test/hooks/use-request.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import useRequest from '../../src/hooks/use-request';
77
const Card = () => {
88
const [{ isPending }] = useRequest({
99
url: '/api',
10+
customQueryMiddlewareConfig: {
11+
retryableStatusCodes: [504],
12+
},
1013
});
1114

1215
return <div>{isPending ? 'loading…' : 'loaded'}</div>;

packages/redux-query/index.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ declare module 'redux-query' {
3939
export type RollbackStrategy<T> = (initialValue: T, currentValue: T) => T;
4040

4141
export type Update<TEntities = Entities> = {
42-
[K in keyof TEntities]?: UpdateStrategy<TEntities[K]>
42+
[K in keyof TEntities]?: UpdateStrategy<TEntities[K]>;
4343
};
4444

4545
export type OptimisticUpdate<TEntities = Entities> = {
46-
[K in keyof TEntities]?: OptimisticUpdateStrategy<TEntities[K]>
46+
[K in keyof TEntities]?: OptimisticUpdateStrategy<TEntities[K]>;
4747
};
4848

4949
export type Rollback<TEntities = Entities> = {
50-
[K in keyof TEntities]?: RollbackStrategy<TEntities[K]>
50+
[K in keyof TEntities]?: RollbackStrategy<TEntities[K]>;
5151
};
5252

5353
export interface WithTime {
@@ -158,6 +158,15 @@ declare module 'redux-query' {
158158
queryCount: number;
159159
};
160160

161+
export type QueryMiddlewareConfig = {
162+
backoff: {
163+
maxAttempts: number;
164+
minDuration: number;
165+
maxDuration: number;
166+
};
167+
retryableStatusCodes: Array<Status>;
168+
};
169+
161170
export interface QueryConfig<TEntities = Entities> {
162171
body?: RequestBody;
163172
force?: boolean;
@@ -171,6 +180,7 @@ declare module 'redux-query' {
171180
rollback?: Rollback<TEntities>;
172181
unstable_preDispatchCallback?: () => void;
173182
url: Url;
183+
customQueryMiddlewareConfig?: QueryMiddlewareConfig;
174184
}
175185

176186
export interface QueriesState {

packages/redux-query/src/actions/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ export const requestAsync = ({
276276
url,
277277
/* eslint-disable-next-line camelcase */
278278
unstable_preDispatchCallback,
279+
customQueryMiddlewareConfig,
279280
}: QueryConfig): RequestAsyncAction => {
280281
return {
281282
type: actionTypes.REQUEST_ASYNC,
@@ -289,6 +290,7 @@ export const requestAsync = ({
289290
update,
290291
url,
291292
unstable_preDispatchCallback,
293+
customQueryMiddlewareConfig,
292294
};
293295
};
294296

@@ -307,6 +309,7 @@ export const mutateAsync = ({
307309
transform,
308310
update,
309311
url,
312+
customQueryMiddlewareConfig,
310313
}: QueryConfig): MutateAsyncAction => {
311314
return {
312315
type: actionTypes.MUTATE_ASYNC,
@@ -319,6 +322,7 @@ export const mutateAsync = ({
319322
transform,
320323
update,
321324
url,
325+
customQueryMiddlewareConfig,
322326
};
323327
};
324328

packages/redux-query/src/middleware/query.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,17 @@ import type {
2929
ResponseBody,
3030
Status,
3131
Transform,
32+
QueryMiddlewareConfig,
3233
} from '../types';
3334

34-
type Config = {|
35-
backoff: {|
36-
maxAttempts: number,
37-
minDuration: number,
38-
maxDuration: number,
39-
|},
40-
retryableStatusCodes: Array<Status>,
41-
|};
42-
4335
type ReduxStore = {|
4436
dispatch: (action: Action) => any,
4537
getState: () => any,
4638
|};
4739

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

50-
const defaultConfig: Config = {
42+
const defaultConfig: QueryMiddlewareConfig = {
5143
backoff: {
5244
maxAttempts: 5,
5345
minDuration: 300,
@@ -105,7 +97,8 @@ const queryMiddleware = (
10597

10698
return ({ dispatch, getState }: ReduxStore) => (next: Next) => (action: PublicAction) => {
10799
let returnValue;
108-
const config = { ...defaultConfig, ...customConfig };
100+
const customQueryMiddlewareConfigFromAction = action.customQueryMiddlewareConfig || {};
101+
const config = { ...defaultConfig, ...customConfig, ...customQueryMiddlewareConfigFromAction };
109102

110103
switch (action.type) {
111104
case actionTypes.REQUEST_ASYNC: {

packages/redux-query/src/types.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ type QueryOptions = {
1313
headers?: { [key: string]: any },
1414
};
1515

16+
export type QueryMiddlewareConfig = {|
17+
backoff: {|
18+
maxAttempts: number,
19+
minDuration: number,
20+
maxDuration: number,
21+
|},
22+
retryableStatusCodes: Array<Status>,
23+
|};
24+
1625
export type QueryConfig = {|
1726
body?: RequestBody,
1827
force?: boolean,
@@ -26,6 +35,7 @@ export type QueryConfig = {|
2635
rollback?: { [key: string]: (initialValue: any, currentValue: any) => any },
2736
unstable_preDispatchCallback?: () => void,
2837
url: Url,
38+
customQueryMiddlewareConfig?: QueryMiddlewareConfig,
2939
|};
3040

3141
export type QueryDetails = {

0 commit comments

Comments
 (0)