axios-mock-shim
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -71,2 +71,5 @@ /* eslint-disable */ | ||
with(fn) { | ||
const { useMock } = parent.$options; | ||
if (!useMock) | ||
return this; | ||
const invalid = !isFn(fn) && !isArray(fn); | ||
@@ -82,4 +85,7 @@ if (invalid) | ||
useMockRequest(method, svc, data = {}) { | ||
const { normalRequest, $adapter, ReplyCache } = this; | ||
const { normalRequest, $adapter, ReplyCache, $options } = this; | ||
const { snakifyData, anyReply } = $options; | ||
const cacheToken = stringify(method, svc, data); | ||
data = snakifyData ? snakifyKeys(data) : data; | ||
let config = { method, svc, data }; | ||
// with mockReply defined & not yet cached | ||
@@ -91,7 +97,6 @@ const hasCache = RequestCache.has(cacheToken); | ||
if (ReplyCache.has(cacheToken)) { | ||
mockHandler.call($adapter, { method, svc, data }, ReplyCache.get(cacheToken)); | ||
mockHandler.call($adapter, config, ReplyCache.get(cacheToken)); | ||
} | ||
else { | ||
const { anyReply } = this.$options; | ||
anyReply && mockHandler.call($adapter, { method, svc, data }, anyReply); | ||
anyReply && mockHandler.call($adapter, config, anyReply); | ||
} | ||
@@ -105,14 +110,7 @@ } | ||
const { $instance, $options } = this; | ||
const { beforeRequest, beforeResponse, snakifyData } = $options; | ||
const { beforeRequest, beforeResponse } = $options; | ||
if (!httpMethodList.has(method.toUpperCase())) | ||
return warn('Invalid http method', method); | ||
const params = { | ||
method, | ||
url: svc, | ||
[method.toUpperCase() === 'GET' | ||
? 'params' | ||
: 'data']: snakifyData ? snakifyKeys(data) : data, | ||
}; | ||
return $instance(beforeRequest ? beforeRequest(params) : params).then(beforeResponse ? beforeResponse : (res) => res); | ||
return $instance[method](svc, beforeRequest ? beforeRequest(data) : data).then(beforeResponse ? beforeResponse : (res) => res); | ||
}, | ||
}; |
{ | ||
"name": "axios-mock-shim", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "A plugin build for easily using axios-mock-adapter with axios", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -103,3 +103,3 @@ # Axios Mock Shim | ||
getProfile() { | ||
return api.use('get', 'solo/config').with([200, { | ||
return api.use('get', 'solo/config', { params: { token: 1 } }).with([200, { | ||
data: { | ||
@@ -113,14 +113,15 @@ number: 10, | ||
getSoloConfig() { | ||
return api.use('get', 'profile').with((resolve, reject, config) => { | ||
console.log(config); // mock-adapter's config | ||
res([ | ||
200, | ||
{ | ||
data: { | ||
name: 'Johnny', | ||
money: 1000, | ||
}, | ||
} | ||
]); | ||
}).run(); | ||
return api.use('get', 'profile', { id: 100 }) | ||
.with((resolve, reject, config) => { | ||
console.log(config); // mock-adapter's config | ||
res([ | ||
200, | ||
{ | ||
data: { | ||
name: 'Johnny', | ||
money: 1000, | ||
}, | ||
} | ||
]); | ||
}).run(); | ||
}, | ||
@@ -148,3 +149,3 @@ | ||
Whether snakify keys in params or data in request. | ||
Whether snakify keys in params or data in request. SnakifyKeys will run before the `beforeRequest`, those config added by `beforeRequest` will not auto apply the `snakifyKeys` method. | ||
@@ -162,9 +163,10 @@ | ||
> Be careful not to overwrite the `method`, `data` or `params` property here, or you may send a wrong request every request. | ||
> The axios instance will call such as `axios.post()` but not `axios()` to send the request in order to match the mock-adapter's setting, using `axios()` with sending data will cause 404 error!! | ||
See: [Mock failed with 404 Error](https://github.com/ctimmerm/axios-mock-adapter/issues/116) | ||
```js | ||
const api = createAPIHandler(instance, { | ||
beforeRequest(params) { | ||
beforeRequest(config) { | ||
const TOKEN = localStorage.getItem('TOKEN'); | ||
return Object.assign(params, { | ||
return Object.assign(config, { | ||
headers: { | ||
@@ -171,0 +173,0 @@ 'Content-Type': 'application/json', |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18524
246
366