0.0.3 • Published 4 years ago

@edwardmx/mockserver v0.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Mock server

To build full fake REST API with json-server for Front-end developer who need a quick Back-end for prototyping and mocking service responses.

Table of Contents

Instalation

You need to first install it via npm:

$ npm install @edwardmx/mockserver --save-dev

Next, create javascript file and include library:

const server = require('@edwardmx/mockserver');

server();

Finally, create mockserver.config.json file in root folder.

{
  "description": "RestFul Mock API",
  "port": 8000,
  "prefix": "/public.api.mx",
  "folder": {
    "root": "/api/",
    "public": "public/",
    "protected": "protected/",
    "users": "protected/users.json"
  },
  "security": {
    "prefix": "/private.api.mx",
    "enabled": true,
    "endpoint": "/login",
    "schemma": "Bearer",
    "jwt": {
      "secret": "hibhturIei47eoaWbACns7nl17o8k0YP",
      "claims": {
        "issuer": "sso.localhost.mx",
        "expiresIn": "10m",
        "audience": "aud.localhost.mx",
        "subject": "mock:hn7LT49LG93oioVq"
      },
      "data": [
        "name",
        "lastName",
        "email",
        "phone",
        "website",
        "company"
      ]
    },
    "lastLogin": {
      "value": 2,
      "type": "days"
    }
  },
  "writeFile": false
}

Start the server with the following command:

node mockserver.js

Database

The database files saved into folder api in root folder. Save some data in a JSON file. Eg.

  • api
    • protected
      • users.json
      • actors.json
    • private
      • movies.json

users.json

{
    "users": [
        {
            "id": 1,
            "name": "Susie",
            "lastName": "Martínez",
            "email": "susie.martinez@gmail.com",
            "password": "Abcd1234$",
            "phone": "1122334455",
            "website": "susiemartinez.mx"
        },
        {
            "id": 2,
            "name": "Tonya",
            "lastName": "Keller",
            "password": "Abcd1234$",
            "email": "tonya.keller@gmail.com",
            "phone": "9922776633",
            "website": "tonyakeller.io"
        },
        {
            "id": 3,
            "name": "Greg",
            "lastName": "Boyd",
            "password": "Abcd1234$",
            "email": "greg.boyd@gmail.com",
            "phone": "6644993300",
            "website": "gregboyd.net"
        }
    ]
}

actors.json

{
  "actors": [
        {
          "name": "Catherine Missal",
          "rating": 4875,
          "image_path": "/g3fsRgEoMxaqPayIMtGDWERqJ6A.jpg",
          "alternative_name": null,
          "objectID": "551486300"
        },
        {
          "name": "Monica Bellucci",
          "rating": 3956,
          "image_path": "/z3sLuRKP7hQVrvSTsqdLjGSldwG.jpg",
          "alternative_name": "Monica Anna Maria Bellucci",
          "objectID": "551486310"
        },
        {
          "name": "Tom Cruise",
          "rating": 2237,
          "image_path": "/3oWEuo0e8Nx8JvkqYCDec2iMY6K.jpg",
          "alternative_name": null,
          "objectID": "551486340"
        },
        {
          "name": "Scarlett Johansson",
          "rating": 2128,
          "image_path": "/f3c1rwcOoeU0v6Ak5loUvMyifR0.jpg",
          "alternative_name": "Scarlett Johanssen",
          "objectID": "551486350"
        }
    ]
}

Configuration

The mockserver.config.json file defines all configuration for create mock server.

ItemDescription
descriptionDescription for mock server
portPort number for running server
prefixDefine prefix for all endpoints public
folder.rootPath for root folder
folder.usersFile name contains users data
folder.endpointsFile name contains proxy endpoints (middleware)
folder.protectedFolder that contains all the JSON files that make up the protected endpoints
folder.publicFolder that contains all the JSON files that make up the public endpoints
security.prefixDefine prefix for all endpoints protected
security.enabledtrue for indicates load security configuration
security.endpointEndpoint name for login service
security.schemmaSchemma for authorization header (Basic, Bearer)
security.jwt.secretString security for JWT
security.jwt.claims.issuerString or array of strings of valid values for the iss field
security.jwt.claims.expiresInExpires time for JWT
security.jwt.claims.audienceThe audience can be checked against a string, a regular expression or a list of strings and/or regular expressions
security.jwt.claims.subjectIf you want to check subject (sub), provide a value here
security.jwt.dataArray list with field data user
lastLogin.valueNumber for defined last login
lastLogin.typeString define last login type (seconds, minutes, hours days, etc)
writeFiletrue for indicates write file with all JSON files

Endpoints

Create the endpoints.json file inside the api folder. This file will contain all routes that will be rewritten with a new route. Each path must have its own JSON file with the response structure, all routes will be treated with the GET method.

The structure for the routes is:

{
  "/public.api.mx/movies/genres": {
    "to": "/public.api.mx/genres"
  },
  "/private.api.mx/movies/directors": {
    "to": "/private.api.mx/directors"
  }
}