Repository URL to install this package:
|
Version:
1.1.16 ▾
|
import isErrorLikeResponse from '../deps/isErrorLikeResponse'
/**
* @param {RequestChain} requestChain
* @return {Object | *}
*/
function respondWithMock(requestChain) {
return requestChain.get('data')
}
/**
* @param {RequestChain} requestChain
* @param {Object} [eventData={}]
* @return {void}
*/
function staticFixtureMiddleware(requestChain, eventData = {}) {
const { type } = eventData
if (type !== 'toRequest') {
return
}
const method = requestChain.get('method')
async function interceptErrorLikeWithFixture(url, params) {
const [error, response] = await method(url, params)
if (error) {
throw error
} else if (isErrorLikeResponse(response)) {
return [undefined, respondWithMock(requestChain)]
} else {
return [error, response]
}
}
// await method(url, { params })
requestChain.setMethod(interceptErrorLikeWithFixture)
}
export { staticFixtureMiddleware }
export default staticFixtureMiddleware