Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
axios-mock-shim
Advanced tools
A plugin build for easily using axios-mock-adapter with axios.
Be sure to install axios
& axios-mock-adapter
before using this plugin.
npm i axios-mock-shim
// or
yarn add axios-mock-shim
Import the plugin with two methods createAxios
, createAPIHandler
import { createAxios, createAPIHandler } from './axios-mock-shim';
Create Axios instance by given config.(default config or your custom config)
const instance = createAxios();
Create an AxiosRequest
object, which you can then use to define & call the api with specific mock data.
Note: Here must provide the
shimOptions
with at leastuseMock
property to tell the plugin whether using the mock-adapter.
// Create an AxiosRequest with mock-adapter feature
const api = createAPIHandler(instance, { useMock: true });
// If you would like to use pure axios for production
// You can set the option as below
const api = createAPIHandler(instance, {
useMock: process.env.NODE_ENV !== 'production'
});
Define your api logic & mock data with your AxiosRequest
object
method list: use
, with
, run
Define the api setting
method: http method
required
- type: string
svc: url path
required
- type: string
(note => without baseURL in axios instance)
data: params or data
optional
- type: object
Define the mock reply data.
If your set
useMock
tofalse
, then no need to call this method.
Even you call this method with
useMock
set byfalse
, it will still skip the mock feature automatically.
reply
method
required
- type: function(resolve, reject, config)
| array[statusCode: number, data]
> function will get three arguments, when using function type reply, please wrap your data into the first resolve
method in order to tell the plugin fullfill the Promise.Execute the AxiosRequest
export default {
// array type reply
getProfile() {
return api.use('get', 'solo/config').with([200, {
data: {
number: 10,
}
}]).run();
},
// function type reply
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();
},
// If no need for mock
getNoMock() {
return api.use('get', 'nomock').run();
},
};
Required to set whether using mock-adapter. no default value.
You can specify what to do before getting the response, eg.
const api = createAPIHandler(instance, {
useMock: true,
beforeResponse(res) {
return camelizeKeys(res.data);
},
});
If you need to mock every request which without using with
to set its mock data, this property could that you define a fallback reply which will be used for all unhandled request.
This could only used with
useMock
set bytrue
;
const api = createAPIHandler(instance, {
useMock: process.env.NODE_ENV !== 'production',
anyReply: [200, {
error: 'Unhandled request',
}],
// or
anyReply(resolve, reject, config) {
if (Math.random() > 0.1) return reject([404]);
return resolve([200, {
error: 'Unhandled request'
}]);
},
});
const mockDefaultConfig = {
delayResponse: 500,
onNoMatch: "passthrough",
}
const axiosDefaultConfig = {
baseURL: '/api/',
headers: {
'Content-Type': 'application/json',
},
timeout: 5000,
withCredentials: true,
}
Copyright (c) 2020-present, Johnny Wang
FAQs
A plugin build for easily using axios-mock-adapter with axios
We found that axios-mock-shim demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.