axios-mock-shim
Advanced tools
Comparing version 1.0.5 to 1.0.6
/* eslint-disable */ | ||
const MockAdapter = require('axios-mock-adapter'); | ||
import { firstUp, isArray, isFn, stringify, warn } from './utils'; | ||
import { snakifyKeys, firstUp, isArray, isFn, stringify, warn } from './utils'; | ||
import { mockDefaultConfig, httpMethodList } from './config'; | ||
@@ -102,3 +102,3 @@ // Cache for whole page | ||
const { $instance, $options } = this; | ||
const { beforeResponse } = $options; | ||
const { beforeResponse, snakifyData } = $options; | ||
if (!httpMethodList.has(method.toUpperCase())) | ||
@@ -111,5 +111,5 @@ return warn('Invalid http method', method); | ||
? 'params' | ||
: 'data']: data, | ||
: 'data']: snakifyData ? snakifyKeys(data) : data, | ||
}).then(beforeResponse ? beforeResponse : (res) => res); | ||
}, | ||
}; |
@@ -0,1 +1,25 @@ | ||
function camelizeStr(str) { | ||
return str.replace(/[_.-](\w|$)/g, (_, x) => x.toUpperCase()); | ||
} | ||
function snakifyStr(str) { | ||
return str.replace(/(?:^|\.?)([A-Z])/g, (_, x) => `_${x.toLowerCase()}`); | ||
} | ||
function convertCase(convertFunc) { | ||
function converter(thing) { | ||
if (thing instanceof Array) { | ||
return thing.map((i) => converter(i)); | ||
} | ||
if (thing instanceof Object) { | ||
const newObj = {}; | ||
Object.keys(thing).forEach((k) => { | ||
newObj[convertFunc(k)] = converter(thing[k]); | ||
}); | ||
return newObj; | ||
} | ||
return thing; | ||
} | ||
return converter; | ||
} | ||
export const camelizeKeys = convertCase(camelizeStr); | ||
export const snakifyKeys = convertCase(snakifyStr); | ||
export function isFn(v) { | ||
@@ -2,0 +26,0 @@ return v !== null && typeof v === 'function'; |
{ | ||
"name": "axios-mock-shim", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "A plugin build for easily using axios-mock-adapter with axios", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -140,2 +140,7 @@ # Axios Mock Shim | ||
### snakifyData | ||
Whether snakify keys in params or data in request. | ||
### beforeResponse | ||
@@ -142,0 +147,0 @@ |
@@ -16,2 +16,3 @@ export interface MockAdapterOptions { | ||
beforeResponse?: Function; | ||
snakifyData?: boolean; | ||
} | ||
@@ -18,0 +19,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
17311
366
211