axios-mock-shim
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -22,1 +22,6 @@ export const mockDefaultConfig = { | ||
]); | ||
export const useDataMethodList = new Set([ | ||
'POST', | ||
'PUT', | ||
'PATCH', | ||
]); |
/* eslint-disable */ | ||
const MockAdapter = require('axios-mock-adapter'); | ||
import { snakifyKeys, firstUp, isArray, isFn, stringify, warn } from './utils'; | ||
import { mockDefaultConfig, httpMethodList } from './config'; | ||
import { mockDefaultConfig, httpMethodList, useDataMethodList } from './config'; | ||
// Cache for whole page | ||
const RequestCache = new Set(); | ||
/** | ||
* Create config for real axios request | ||
* | ||
* @param instance | ||
* @param config | ||
*/ | ||
function configHandler({ methodUp, beforeRequest, data }) { | ||
const config = []; | ||
if (useDataMethodList.has(methodUp)) { | ||
config[0] = data; | ||
beforeRequest | ||
? config[1] = beforeRequest({}) | ||
: false; | ||
} | ||
else { | ||
config[0] = beforeRequest ? beforeRequest(data) : data; | ||
} | ||
return config; | ||
} | ||
/** | ||
* Create mock adapter instance | ||
@@ -23,8 +42,9 @@ * | ||
*/ | ||
function mockHandler({ method, svc, data }, mockReply) { | ||
function mockHandler({ method, svc, config }, mockReply) { | ||
const mock = this; | ||
// handler choose | ||
let handler; | ||
if (isFn(mockReply)) { | ||
handler = function mockReplyHandler(config) { | ||
return new Promise((resolve, reject) => mockReply(resolve, reject, config)); | ||
handler = function mockReplyHandler(mockConfig) { | ||
return new Promise((resolve, reject) => mockReply(resolve, reject, mockConfig)); | ||
}; | ||
@@ -35,3 +55,4 @@ } | ||
} | ||
mock[`on${firstUp(method)}`](svc, data) | ||
// config handling | ||
mock[`on${firstUp(method)}`](svc, ...config) | ||
.reply.apply(mock, isFn(handler) ? [handler] : handler); | ||
@@ -87,6 +108,11 @@ } | ||
const { normalRequest, $adapter, ReplyCache, $options } = this; | ||
const { snakifyData, anyReply } = $options; | ||
const { snakifyData, anyReply, beforeRequest } = $options; | ||
const methodUp = method.toUpperCase(); | ||
const cacheToken = stringify(method, svc, data); | ||
data = snakifyData ? snakifyKeys(data) : data; | ||
let config = { method, svc, data }; | ||
let configs = { | ||
method, | ||
svc, | ||
config: configHandler({ methodUp, beforeRequest, data }), | ||
}; | ||
// with mockReply defined & not yet cached | ||
@@ -98,6 +124,6 @@ const hasCache = RequestCache.has(cacheToken); | ||
if (ReplyCache.has(cacheToken)) { | ||
mockHandler.call($adapter, config, ReplyCache.get(cacheToken)); | ||
mockHandler.call($adapter, configs, ReplyCache.get(cacheToken)); | ||
} | ||
else { | ||
anyReply && mockHandler.call($adapter, config, anyReply); | ||
anyReply && mockHandler.call($adapter, configs, anyReply); | ||
} | ||
@@ -107,11 +133,12 @@ } | ||
// This return stays for Promise mechanism | ||
return normalRequest.call(this, method, svc, data); | ||
return normalRequest.call(this, configs); | ||
}, | ||
normalRequest(method, svc, data) { | ||
normalRequest({ method, svc, config }) { | ||
const { $instance, $options } = this; | ||
const { beforeRequest, beforeResponse } = $options; | ||
if (!httpMethodList.has(method.toUpperCase())) | ||
const { beforeResponse } = $options; | ||
const methodUp = method.toUpperCase(); | ||
if (!httpMethodList.has(methodUp)) | ||
return warn('Invalid http method', method); | ||
return $instance[method.toLowerCase()](svc, beforeRequest ? beforeRequest(data) : data).then(beforeResponse ? beforeResponse : (res) => res); | ||
return $instance[method.toLowerCase()](svc, ...config).then(beforeResponse ? beforeResponse : (res) => res); | ||
}, | ||
}; |
{ | ||
"name": "axios-mock-shim", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "A plugin build for easily using axios-mock-adapter with axios", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -9,3 +9,3 @@ export interface MockAdapterOptions { | ||
svc: string; | ||
data?: Object; | ||
config?: Array<any>; | ||
} | ||
@@ -12,0 +12,0 @@ |
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
19353
402