Skip to content

Commit ea3efbc

Browse files
committed
Merge branch 'feature/forward-axios-config-in-error-object'
2 parents ea6ac28 + 9e8c3ac commit ea3efbc

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-api-middleman",
3-
"version": "3.2.2",
3+
"version": "3.2.2-canary.0",
44
"description": "A Redux middleware making sending request a breeze",
55
"main": "lib/index.js",
66
"jest": {
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"scripts": {
21-
"test": "jest",
21+
"test": "jest --watch --verbose false",
2222
"test:ci": "yarn lint && jest",
2323
"lint": "standard --fix --verbose | snazzy",
2424
"size": "size-limit",

spec/createRequestPromise.test.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Promise from 'es6-promise'
2-
import { CALL_API, CHAIN_API } from '../src'
2+
import { CALL_API } from '../src'
33
import createRequestPromise from '../src/createRequestPromise'
44
import axios from 'axios'
55
import MockDate from 'mockdate'
@@ -8,16 +8,17 @@ import * as utils from '../src/utils'
88
jest.mock('axios')
99
jest.mock('../src/log')
1010

11-
const getMockAxiosPromise = ({ error } = {}) => {
11+
const getMockAxiosPromise = ({ error, config } = {}) => {
1212
return new Promise((resolve, reject) => {
1313
if (error) {
14-
process.nextTick(() => reject(new Error({
15-
response: {
16-
data: {
17-
key_1: 'val_1'
18-
}
14+
const _error = new Error('Mock error')
15+
_error.config = config
16+
_error.response = {
17+
data: {
18+
key_1: 'val_1'
1919
}
20-
})))
20+
}
21+
process.nextTick(() => reject(_error))
2122
} else {
2223
process.nextTick(() => resolve({
2324
data: {
@@ -160,14 +161,17 @@ describe('createRequestPromise', () => {
160161
})
161162

162163
describe('when axios catches error', () => {
164+
let config
163165
beforeEach(() => {
164-
axios.mockReturnValue(getMockAxiosPromise({ error: true }))
166+
config = { key: 'value' }
167+
axios.mockReturnValue(getMockAxiosPromise({ error: true, config }))
165168
})
166-
it('should call errorInterceptor', () => {
169+
170+
it('should call errorInterceptor', async () => {
167171
const errorInterceptor = jest.fn(({ proceedError }) => {
168172
proceedError()
169173
})
170-
createRequestPromise({
174+
await createRequestPromise({
171175
timeout,
172176
generateDefaultParams,
173177
createCallApiAction,
@@ -179,22 +183,24 @@ describe('createRequestPromise', () => {
179183
})(mockPrevBody)
180184
.catch(() => {
181185
expect(errorInterceptor).toHaveBeenCalledTimes(1)
182-
expect(errorInterceptor.mock.calls[0][0]).toMatchObject({
186+
expect(errorInterceptor.mock.calls[0][0]).toEqual({
183187
err: {
188+
config,
184189
data: {
185190
key1: 'val_1'
186191
}
187192
},
188-
dispatch,
189-
getState
193+
getState,
194+
proceedError: expect.any(Function),
195+
replay: expect.any(Function)
190196
})
191197
})
192198
})
193199
})
194200

195201
describe('revalidate behavior', () => {
196202
const currentime = 1579508700000
197-
let path, testSetCount = 0
203+
let path; let testSetCount = 0
198204

199205
beforeEach(() => {
200206
testSetCount++
@@ -210,8 +216,8 @@ describe('createRequestPromise', () => {
210216
jest.clearAllMocks()
211217
})
212218

213-
function createRequest({ revalidate, revalidateDisabled } = {}){
214-
extractParams = jest.fn().mockReturnValue({ ...mockParams, revalidate})
219+
function createRequest ({ revalidate, revalidateDisabled } = {}) {
220+
extractParams = jest.fn().mockReturnValue({ ...mockParams, revalidate })
215221
createRequestPromise({
216222
revalidateDisabled,
217223
timeout,

spec/index.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import createApiMiddleware, {
77
CHAIN_API
88
} from '../src'
99

10-
const createRequestPromise = require('../src/createRequestPromise')
11-
1210
import * as utils from '../src/utils'
1311

12+
const createRequestPromise = require('../src/createRequestPromise')
13+
1414
jest.mock('../src/log', () => ({
1515
error: jest.fn()
1616
}))
@@ -558,7 +558,7 @@ describe('Middleware::Api', () => {
558558
it('dispatches CHAIN_API with revalidateDisabled = true if action type is CALL_API', async () => {
559559
const action = { [CHAIN_API]: [
560560
() => ({ [CALL_API]: {} })
561-
]}
561+
] }
562562
await apiMiddleware({ dispatch, getState })(next)(action)
563563
expect(createRequestPromise.default.mock.calls[0][0].revalidateDisabled).toBe(true)
564564
})

src/createRequestPromise.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export default function ({
117117

118118
function prepareErrorPayload ({ error, camelize }) {
119119
const res = error.response || {}
120+
res.config = error.config
120121
if (camelize) {
121122
res.data = camelizeKeys(res.data)
122123
}
@@ -164,19 +165,19 @@ export default function ({
164165
}
165166
}
166167

167-
function _getRevalidationKey(actionObj) {
168+
function _getRevalidationKey (actionObj) {
168169
const {
169170
method,
170171
path,
171172
url,
172173
params,
173-
data,
174+
data
174175
} = actionObj
175176
return JSON.stringify({
176177
method,
177178
path,
178179
url,
179180
params,
180-
data,
181+
data
181182
})
182183
}

src/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,3 @@ export function paramsExtractor ({ baseUrl }) {
7070
const _window = typeof window === 'undefined' ? null : window
7171

7272
export { _window as window }
73-

0 commit comments

Comments
 (0)