Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
instant-mock
Advanced tools
Readme
instant-mock is a quick and easy web API mock server.
Installation via npm:
npm install -g instant-mock
mkdir mymock
cd mymock
instant-mock init
instant-mock
You can open http://localhost:3000 to view the instant-mock web console.
All mock API is mounted on http://localhost:3000/mock.
Please try GET to http://localhost:3000/mock/users by curl or web browser. It is sample mock API created by instant-mock init
.
Servce configuration is wrote on server.yml
http:
host: localhost
port: 3000
socket:
host: localhost
port: 3010
You can create mock definition file to mock
directory.
Mock API URL is auto generated by directory path.
mock/api-name/@METHOD
is mapped to METHOD: http://localhost:3000/mock/api-name
METHOD
can use get/post/put/patch/delete.
If you need route parameters, can use PARAM
directory.
mock/api-name/$id/@get
is mapped to GET: http://localhost:3000/mock/api-name/:id
Request for mock is parsed by user definition parser file. User definition parser file name start with "parser-" and format is yaml or js.
Try create a parser file below, and access to http://localhost:3000/mock/books
after restart the instant-mock.
/mock/books/@get/parser-default.yml:
status: 200 # Response status code.
headers: # Response headers.
Content-Type: application/text
rawBody: 'test body' # Response body.
You can define response body to a any file.
./mock/books/@get/body.json:
{
"key": "value"
}
./mock/books/@get/parser-default.yml:
status: 200
headers:
Content-Type: application/json
body: 'body.json' # Response body file.
Parser file can define multiple resonse for switing by request. Define request parsing rule to if
, and response to then
.
Try create a parser file below, and access to http://localhost:3000/mock/books/:id
after restart the instant-mock.
If :id
is "1" then response body is "user 1", and if it is "3" then response is 404.
./mock/books/$id/@get/parser-default.yml:
- if:
params:
id: 1
then:
rawBody: 'book 1'
- if:
params:
id: 2
then:
rawBody: 'book 2'
- then:
status: 404
if
is can use params/query/body, and it is "and" condition.
YAML format parser is support simple rule only. Use a js parser if you need more advanced rules.
./mock/shelves/:id/@get/parser-default.js:
exports.default = function (req) {
return {
status: 200,
headers: {
'content-type': 'application/text',
},
rawBody: 'your book is ' + req.params.id,
};
};
You can open http://localhost:3000 to view the instant-mock web console. Web console can change parser, and show mock api access logs.
FAQs
Quick and Easy web API mock server.
The npm package instant-mock receives a total of 30 weekly downloads. As such, instant-mock popularity was classified as not popular.
We found that instant-mock 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.