REST API in local environment

In <30 seconds, we can use json-server to mock a full REST API over a simple JSON file

Spin-up the API

npm install -g json-server
json-server --watch rest-api.json-server.data.json --port 5001
Input

Write API

curl -X POST 'http://localhost:5001/activities?user=a' \
  -H "Content-type: application/json" \
  -d "@/tmp/a.json"
Input
{ "user": "a", "action": "update", "date": "2012-08-01" }

Read API

curl -X GET 'http://localhost:5001/activities?user=a&action=update&date=2012-08-01'

Output
[ { "user": "a", "action": "update", "date": "2012-08-01", "id": 7 } ]

Routing (optional)

Spin-up the API with the routes

json-server --port 8081 --delay 750   \
  --watch   hlr.json-server.data.json \
  --routes  hlr.json-server.route.json
Inputs


Sample Query 1 - default result

curl -X GET 'http://localhost:8081/helm-releases/foo/bar'
Output
[ { "namespace": "default", "name": "xxx", "chart": "xxx", "version": "1.0.0", "status": { "code": 0, "desc": "failed" } } ]


Sample Query 2 - custom result

curl -X GET 'http://localhost:8081/helm-releases/cluster-1/ns-x'
Output
[
  {
    "namespace": "ns-x", "name": "foo", "chart": "foo", "version": "1.0.1", "status": { "code": 1, "desc": "deployed" },
    "resources": { "workloads": [
      { "kind": "Deployment", "namespace": "ns-x", "name": "foo-api" },
      { "kind": "Deployment", "namespace": "ns-x", "name": "foo-web" }] }
  }
]