Supple mock server with random fake data using Faker.js
- Using Bun
- Written in Typescript
- Optional get random fake data using Faker.js
- Request body validation using JSON Schema
- Flexible on-demand response (can modify body, status, headers, and delayed response)
- No data storage needed
Setup Bum (Bun Version Manager):
$ curl -fsSL https://github.com/owenizedd/bum/raw/main/install.sh | bash
$ source ~/.zshrc
$ bum use
Bum is supported on Linux x86_64 and Darwin x86_64 (Mac OS),
You can enter uname -ms
command in your terminal to see yours.
Install dependencies:
$ bun install
Run in local:
$ PORT=8080 \
SECRET_KEY="" \
ALLOWED_ORIGIN="*" \
ALLOWED_METHODS="GET, HEAD, PUT, PATCH, POST, DELETE" \
ALLOWED_HEADERS="*" \
bun server.ts
When you define SECRET_KEY
as a parameter and is not empty string, the client request must be include X-Smockr-Secret
header with the same value
Specify a search body param to retrieve a response with that body.
GET http://localhost:8080/?mock[response][body]={"ping":"pong"}
HTTP/1.1 200 OK
content-type: application/json
content-length: 18
{
"ping": "pong"
}
Specify a search body param with random fake data using faker.js api helpers fake.
GET http://localhost:8080/?mock[response][body]={"id":{{number.int({"min":100000,"max":900000})}},"name":"{{person.fullName}}","avatar":"{{image.avatar}}"}
HTTP/1.1 200 OK
content-type: application/json
content-length: 107
{
"id": 429178,
"name": "Allen Brown",
"avatar": "https://avatars.githubusercontent.com/u/97165289"
}
Specify a search status param get back that code status. The status must be inside the range 200 to 599.
GET http://localhost:8080/?mock[response][status]=301
HTTP/1.1 301 Moved Permanently
content-type: application/json
content-length: 0
Specify a search headers param as json string to get them back.
GET http://localhost:8080/?mock[response][headers]={"x-hello":"world"}
HTTP/1.1 200 OK
content-type: application/json
content-length: 0
x-hello: world
Specify a search delay param in milliseconds in order to delay the response.
GET http://localhost:8080/?mock[response][delay]=3000
HTTP/1.1 200 OK
content-type: application/json
content-length: 0
Specify a search schema validation in json schema (stringify) to set request body validation.
POST http://localhost:8080/?mock[request][body][schema]={"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer","minimum":17}},"required":["name"]}
{
"age": 20
}
HTTP/1.1 400 Bad Request
content-type: application/json
content-length: 84
{
"code": 400,
"message": "Requires property name",
"type": "SchemaValidationException"
}
Predefined health check route.
GET http://localhost:8080/health-check
HTTP/1.1 200 OK
content-type: application/json
content-length: 24
{
"health_check": "up"
}