Installation & Configuration
Installation
npm install mockae
-- or clone repo
git clone https://github.com/cyrilbois/mockae.git
npm install
Configuration
Create a db.json
file
{
"products": [
{
"id": 1, "name": "T-shirt", "price": 19.99
},
{
"id": 2, "name": "Jeans", "price": 49.99
}
],
"users": [
{
"id": 1, "username": "johndoe", "email": "johndoe@example.com"
},
{
"id": 2, "username": "janedoe", "email": "janedoe@example.com"
}
]
}
In this example, you have created 2 resources: "products" and "users" (2 objects for each resource).
Create a rules.lua
file
if request.method() == "POST" and request.pathname() == "/users" then response.status(400) response.send('{' .. '"error":"Bad Request",\n' .. '"message":"Missing required field: email"' .. '}') return response.exit() end
With these rules, when calling the creation of a "users" a 400 error is returned.
Start the REST API service
$ npm start
Here you go, you now have a REST API accessible locally:
curl http://localhost:3000/products/1
Custom rules
Custom rules are Lua code that allow you to modify the behavior of the fake REST API. With custom rules, you can set conditions based on the request (such as HTTP method, headers, and payload) and define the response (including HTTP status code and payload). This enables you to tailor the API's behavior to suit specific testing and development scenarios.
The Request and Response objects are provided to define the rules.
Request
The Request
object is used to represent the request data.
Methods
Method | Description |
request.header(name) | Returns the header `name` |
request.method() | Returns the HTTP method ('GET', 'POST', 'PUT', 'PATCH', 'DELETE') |
request.url() | Returns the pathname of the URL (e.g., /users/23). |
request.id() | Returns the resource ID |
request.resource() | Returns the resource |
request.payload(name) | Returns the attribute of the payload object with the name specified in `name` |
Response
The Response
object is used to update the response, including the HTTP status, headers, and payload.
Methods
Method | Description |
response.status(status) | Sets the HTTP status (e.g., 200, 404, etc.) |
response.send(payload) | Sets the response payload |
response.header(name, value) | Sets the header name to value . |
response.exit() | Stops the standard execution of the API (No action or resource loading will be performed) |
API
The REST API handles different HTTP methods for creating, retrieving, updating, and deleting resources.
Routes
GET | /products | Returns all products |
GET | /products/23 | Returns the product with ID 23 |
POST | /products | Create a new product |
GET | /products/23 | Returns the product with ID 23 |
PUT | /products/23 | Update the product with ID 23 |
PATCH | /products/23 | Update partially the product with ID 23 |
DELETE | /products/23 | Delete the product with ID 23 |
Public API: No updates will be performed for POST / PUT / PATCH and DELETE