Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

miraze

1.0.4 • Public • Published

Miraze

  • This is a framework for quickly stubbing a RESTful service.
  • Defining a webserver purely on basis of request/response json contracts.
  • Parsing JSON templates, based on request body/path/query params.

JSO-NG

  • JSO-NG is a templating language for serving json content. \
  • It extends the notion of compiling html templates to json.

So what can you do with this ?

You can use this to quickly setup a web-server that can stub a RESTful remote server.

A sample script :

var miraze     = require("./miraze").create();
miraze.post("/user").sendFile("../sample/create.json");
miraze.get("/user/:id").sendFile("../sample/request-path-param.json");
miraze.get("/user").sendFile("../sample/request-url-param.json");
 
miraze.app.listen(3000, function () {
 console.log('Example app listening on port 3000!');
});

Using expressions in JSO-NG

Given a hello.json is :

{
  "body": {
    "message": "hello",
    "id": "{{0 + 1 }}"
  }
}

And following url mapping is declared

miraze.post("/user").sendFile("../sample/hello.json")

Then making a GET request to '/user' results in :

{
   "message": "hello",
   "id": "1"
}

Using request path parameters

Given we declare following url :

 miraze.get("/user/:id").sendFile("../sample/request-path-param.json");

and request-path-param.json is :

{
  "body": {
    "id"      :"{{request.path.id}}",
    "name"    : "someone"
  }
}

Then if a request is made to url "user/101", above template renders to :

{
  "id": "101",
  "name": "someone"
}

Using request url params in response :

Given we declare following url :

miraze.get("/user").sendFile("../sample/request-url-param.json");

and request-url-param.json is :

{
  "body": {
    "search"  :"{{request.query.search}}",
    "page"   : "{{request.query.page}}",
    "size"   : "{{request.query.size}}"
  }
}

Then if a request is made to url "/user?search=searchme&page=32&size=21"; the above template renders to :

{
    "search": "searchme",
    "page": "32",
    "size": "21"
}

Using request body in response :

Given we declare following url :

miraze.post("/user").sendFile("../sample/create.json");

and create.json is :

{
"body": {
  "id"      :"1",
  "name"    : "{{request.body.name}}",
  "address" : {
    "street" : "{{request.body.address.street}}"
    }
  }
}

And we make a POST request is made to url "/user" with below body :

{
    "name": "Me",
    "address": {
      "street" : "MyPlace"
    }
}

Then we get following reponse :

{
    "id": "1",
    "name": "Me",
    "address": {
        "street": "MyPlace"
    }
}

Coming up...

  • support response status (200, 201, 404..)
  • support for cookies
  • asynchronous actions in controller
  • services and dependency injection

Install

npm i miraze

DownloadsWeekly Downloads

1

Version

1.0.4

License

WTFPL

Last publish

Collaborators

  • avatar