axios-mock-shim
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -5,2 +5,3 @@ /* eslint-disable */ | ||
import { mockDefaultConfig, httpMethodList } from './config'; | ||
// Cache for whole page | ||
const RequestCache = new Set(); | ||
@@ -48,7 +49,8 @@ /** | ||
this.$adapter = null; | ||
this.mockReply = null; | ||
this.run = () => { }; | ||
this.ReplyCache = new Map(); | ||
this.runBuilder = () => { }; | ||
this.init(); | ||
} | ||
AxiosRequest.prototype = { | ||
use(...args) { | ||
init() { | ||
const { $instance } = this; | ||
@@ -59,17 +61,25 @@ const { useMock } = this.$options; | ||
} | ||
// run will return a [Promise] | ||
this.run = useMock | ||
? () => this.useMockRequest(...args) | ||
: () => this.normalRequest(...args); | ||
return this; | ||
// runBuilder return the correct method for run | ||
this.runBuilder = useMock | ||
? (...args) => () => this.useMockRequest(...args) | ||
: (...args) => () => this.normalRequest(...args); | ||
}, | ||
with(fn) { | ||
const invalid = !isFn(fn) && !isArray(fn); | ||
if (invalid) | ||
return warn('reply invalid, should be type Function or Array', fn); | ||
this.mockReply = fn; | ||
return this; | ||
use(method, svc, data = {}) { | ||
const parent = this; | ||
const { ReplyCache } = this; | ||
const cacheToken = stringify(method, svc, data); | ||
// Return an object to define mock data & calling | ||
return { | ||
with(fn) { | ||
const invalid = !isFn(fn) && !isArray(fn); | ||
if (invalid) | ||
return warn('reply invalid, should be type Function or Array', fn); | ||
ReplyCache.set(cacheToken, fn); | ||
return this; | ||
}, | ||
run: parent.runBuilder.apply(parent, [method, svc, data]), | ||
}; | ||
}, | ||
useMockRequest(method, svc, data = {}) { | ||
const { normalRequest, mockReply, $adapter } = this; | ||
const { normalRequest, $adapter, ReplyCache } = this; | ||
const cacheToken = stringify(method, svc, data); | ||
@@ -80,4 +90,5 @@ // with mockReply defined & not yet cached | ||
RequestCache.add(cacheToken); | ||
if (mockReply) { | ||
mockHandler.call($adapter, { method, svc, data }, mockReply); | ||
// check if cache has the mock data | ||
if (ReplyCache.has(cacheToken)) { | ||
mockHandler.call($adapter, { method, svc, data }, ReplyCache.get(cacheToken)); | ||
} | ||
@@ -84,0 +95,0 @@ else { |
{ | ||
"name": "axios-mock-shim", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A plugin build for easily using axios-mock-adapter with axios", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
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
16394
341