How it works

Quick guide to learn the basics about Mockly

Mockly is all about REST resources (or not, as you will see later) and their HTTP methods to create, retrieve, update and delete its information. Therefore, there should be an easy way to define the resources you need available to fill the requirements of your application.

Defining resources

To discover how to change the pattern used to find files with resources definition see the configuration page.

To define a new resource you only need to create a JSON file and name it using this pattern: {name}.resource.json. We recommend as convention to use a folder named api in the project root folder. Also, if you have multiple resources definitions you probably want to store them in a folder named resources.

.
├── api
|    └── resources
|        └── first.resource.js
|        └── second.resource.js

Inside the file you have to create a JSON object with an unique key representing the resource name and its default values. In the follow example, we are creating the artists resource with a collection of artists:

artists.resource.json
{
    "artists": [
        { id: '1', name: 'Mark Knopfler' },
        { id: '2', name: 'Bob Dylan' },
        { id: '3', name: 'Eric Clapton' }
    ]
}

And that's it! Now you can move to the root folder and type mockly to start the server:

$ mockly

███╗   ███╗  ██████╗   ██████╗ ██╗  ██╗ ██╗      ██╗   ██╗ 
████╗ ████║ ██╔═══██╗ ██╔════╝ ██║ ██╔╝ ██║      ╚██╗ ██╔╝ 
██╔████╔██║ ██║   ██║ ██║      █████╔╝  ██║       ╚████╔╝  
██║╚██╔╝██║ ██║   ██║ ██║      ██╔═██╗  ██║        ╚██╔╝   
██║ ╚═╝ ██║ ╚██████╔╝ ╚██████╗ ██║  ██╗ ███████╗    ██║    
╚═╝     ╚═╝  ╚═════╝   ╚═════╝ ╚═╝  ╚═╝ ╚══════╝    ╚═╝    

[NestFactory] Starting Nest application...
[InstanceLoader] MocklyModule dependencies initialized +22ms
[RoutesResolver] MocklyArtistsController {/artists}: +2ms
[RouterExplorer] Mapped {/, GET} route +2ms
[RouterExplorer] Mapped {/, POST} route +1ms
[RouterExplorer] Mapped {/:id, GET} route +1ms
[RouterExplorer] Mapped {/:id, PUT} route +2ms
[RouterExplorer] Mapped {/:id, DELETE} route +2ms
[NestApplication] Nest application successfully started +4ms

Now the server is up and the resources available in /artists path. You can open http://localhost:3000/artists to see the created resources!

So, what is going on under the hood? Mockly exposes all the routes and HTTP methods to accomplish basic CRUD operations. By default, you will have the following requests available:

Easy peasy! Now you can start making requests to the artists resource and it only takes a couple of minutes!

But, what happen if your application services are not following the RESTful paradigm? What if you only want to define a few endpoints with a mocked response? Let's see it.

Defining data endpoints

Use simple key names and use rewrites to fit your needs

Defining data endpoints it's something trivial as we do with resources. At this time, this kind of data follows the same pattern as resources files so you only need to create a file and put in it a key with a value. For example:

me.resource.json
{
    "me": {
        "name": "Peter"
    }
}

Here we are defining me key data with a JSON response, keep in mind that this key is used to generate the endpoint so when you start the server this data will be available in /me path. With this kind of data Mockly exposes only two HTTP methods: GET and POST.

Last updated