
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Interfake is a tool which allows developers of client-side applications to easily create dummy APIs to develop against. Let's get started with a simple example.
Install Interfake globally:
npm install interfake -g
Create a file called adventuretime.json
:
[
{
"request": {
"url": "/whattimeisit",
"method": "get"
},
"response": {
"code": 200,
"body": {
"theTime": "Adventure Time!",
"starring": [
"Finn",
"Jake"
],
"location": "ooo"
}
}
}
]
Then run Interfake against it:
interfake --file ./adventuretime.json
Then using curl
:
curl http://localhost:3000/whattimeisit --verbose
Or go to http://localhost:3000/whattimeisit in your web browser.
The above example will create a endpoint at http://localhost:3000/whattimeisit
which returns a 200
and the body specified in the response
object.
Run interfake -?
for a full list of command-line options.
Interfake allows for complex API structures, dynamic response endpoints and has three interfaces: the JavaScript API (useful for tests), the command line (like above), or on-the-fly using Interfake's HTTP meta-API.
Make sure you've install Interfake as a local module using npm install interfake --save
. Then, you can start doing things like this:
var Interfake = require('interfake');
var request = require('request'); // Let's use request for this example
var interfake = new Interfake();
interfake.createRoute({
request: {
url: '/whats-next',
method: 'get'
},
response: {
code: 200,
body: {
next:'more stuff'
}
}
});
interfake.listen(3030); // The server will listen on port 3030
request('http://localhost:3030/whats-next', function (error, response, body) {
console.log(response.statusCode); // prints 200
console.log(body); // prints { next: "more stuff" }
});
new Interfake(options)
: creates an Interfake object. Options are:
debug
: If true
, outputs lots of annoying but helpful log messages. Default is false
.#createRoute(route)
: Takes a JSON object with request
, response
and optionally afterResponse
properties#listen(port)
: Takes a port and starts the server#stop()
: Stops the server if it's been startedCreate a file from this template:
[
{
"request": {
"url": "",
"method": ""
},
"response": {
"code": 200,
"body": {}
}
}
]
The top-level array should contain a list of endpoints (represented by request/response objects). The request
object contains a URL and HTTP Method (GET/POST/PUT/DELETE/etc) to match against, and the response
object contains an HTTP Status Code (code
) and body
object, which is in itself a JSON object, and optional. This body
is what will be returned in the body of the response for the request you're creating.
You can create as many HTTP request/response pairs as you like. I've put some simple examples below for your copy & paste pleasure, or you can look in /examples-command-line
for some more complex examples.
Then, run the server like so:
interfake ./path/to/file.json
For situations where the API needs to react to mutated data, such as after a POST, PUT or DELETE request, there is an afterResponse
property available for any existing endpoint. In this object, create another array of endpoints to be created after the original one has been created, like so:
[
{
"request": {
"url": "",
"method": ""
},
"response": {
"code": 200,
"body": {}
},
"afterResponse": {
"endpoints": [
{
"request": {
"url": "",
"method": ""
},
"response": {
"code": 200,
"body": {}
}
}
]
}
}
]
The afterResponse
property can be used as deep as you like in the endpoint hierarchy. For a complex example of the use of post-response endpoints, see the /example-apis/crud.json
file in this repository.
While the server is running, you can create new endpoints on-the-fly. You can make a POST request to /_request
with a string containing the same JSON structure as above. If you were using curl
, this is an example (smaller, for brevity).
While Interfake is running, make this request using curl
.
curl -X POST -d '{ "request":{"url":"/whattimeisit", "method":"get"}, "response":{"code":200,"body":{ "theTime" : "Adventure Time!" } } }' http://localhost:3000/_request --header "Content-Type:application/json"
If you'd like the response to come back as JSONP (so, for example you are trying to make a cross-origin request without using CORS) then specify a callback
query parameter, like so:
curl http://localhost:3000/whattimeisit?callback=handleSomeJson --verbose
If you inject this code into your webpage the handleSomeJson
method will be called with the data.
If you'd like to develop an API-driven mobile application you might not yet have a finished API available. This is a perfect example of where Interfake is useful. You can quickly mock up some dummy APIs and work on the mobile application. In parallel, perhaps another developer will be creating the real API, or you could create it later.
You can use Interfake to create dummy APIs which use data from your test setup with the HTTP method above, or by using a static set of test data. If you're writing your test suite using a NodeJS library, you can use the JavaScript API.
The HTTP API is particularly useful for developing iOS Applications which uses Automated tests written in JavaScript, or developing Node.js applications which rely on external APIs.
If you have a website or mobile application which only needs static data, deploy Interfake to a server somewhere with a JSON file serving up the data, and point your application at it.
I tested this on my Mac. If you have trouble on Windows or any other platform, raise an issue.
Interfake
instanceInterfake is a labour of love, created for front-end and mobile developers to increase their prototyping and development speeds. If you can contribute by getting through some issues, I would be very grateful. <3 Open Source!
FAQs
A simple way to create dummy APIs
The npm package interfake receives a total of 75 weekly downloads. As such, interfake popularity was classified as not popular.
We found that interfake 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.