Giter Site home page Giter Site logo

fakeless's Introduction

!Fakeless

Fake REST API with no code required.

Setup

In order to run the project locally and/or develop on it clone the repository to your machine and install the node dependencies. Make sure you have node and npm installed.

yarn add fakeless -g

Quick start

Create a fakeless.json file:

// fakeless.json

{
  "variables": {
    "host": "http://localhost:3000",
    "rootDir": "."
  },
  "templates": [
    {
      "variables": {
          "hero": "spider"
      },
      "method": "get",
      "route": "/heroes/<%= hero %>",
      "response": {
        "body": {
            "name": "<%= hero %>",
            "photos": "<%= host %>/<%= hero %>/photos", // http://localhost:3000/spider/photos
        },
        "status": 200
      }
    }
  ]
}

run

fakeless -f fakeless.json -p 3000

Templates

You can define global and local variables by put them into the variables property.

{
  "variables": {
    "rootDir": "I'm a global variable",
  },
  "templates": [
    {
      "variables": {
          "scope": "I only exist in this scope"
      },
      "route": "/heroes/<%= hero %>"
    }
  ]
}

Success Response

The body response can be defined explicity on your json file or it can be exported from a different path.

Directly on your json file:

{
  "variables": {
    "host": "http://localhost:3000"
  },
  "templates": [
    {
      "variables": {
          "hero": "spider"
      },
      "method": "get",
      "route": "/heroes/<%= hero %>",
      "response": {
        "body": {
            "name": "<%= hero %>",
            "photos": "<%= host %>/<%= hero %>/photos",
        },
        "status": 200
      }
    }
  ]
}

Load external template

// ./fakeless.json
{
  "variables": {
    "host": "http://localhost:3000",
    "rootDir": "."
  },
  "templates": [
    {
      "variables": {
          "hero": "spider"
      },
      "method": "get",
      "route": "/heroes/<%= hero %>",
      "response": {
        "body": "<%= rootDir %>/hero.json",
        "status": 200
      },
      "validate": {
          "body": '<%= rootDir %>/body_validation.json',
          "headers": '<%= rootDir %>/headers_validation.json',
      },
    }
  ]
}

// ./hero.json
{
    "name": "<%= hero %>",
    "photos": "<%= host %>/<%= hero %>/photos",
}

Error Response

// ./fakeless.json
{
  "variables": {
    "host": "http://localhost:3000",
    "rootDir": "."
  },
  "templates": [
    {
      "variables": {
          "hero": "spider"
      },
      "method": "post",
      "route": "/heroes/<%= hero %>",
      "response": {
        "body": {
          "message": "Out of webs"
        },
        "status": 400
      }
    }
  ]
}

Validations

It is possible to apply validations to the request body and headers.

Header Validations

Fakeless will check if headers' values matches with those one specified on the configuration file.

// ./fakeless.json
{
  "variables": {
    "host": "http://localhost:3000",
    "rootDir": "."
  },
  "templates": [
    {
      "variables": {
          "hero": "spider"
      },
      "method": "post",
      "route": "/heroes/<%= hero %>",
      "response": {
        "body": {},
        "status": 200
      },
      "validate": {
        "headers": {
          "content-type": {
            "toBe": "application/json",
            "error": {
              "message": "Out of webs"
            },
            "status": 400
          }
        }
      }
    }
  ]
}

Body Validations

It is also possible to set which field is required:

// ./fakeless.json
{
  "variables": {
    "host": "http://localhost:3000",
    "rootDir": "."
  },
  "templates": [
    {
      "variables": {
          "hero": "spider"
      },
      "method": "post",
      "route": "/heroes/<%= hero %>",
      "response": {
        "body": {},
        "error": {
            "message": "Out of webs"
        },
        "status": 400
      },
      "validate": {
        "body": {
          "reload": {
            "toBe": "required",
            "error": {
              "message": "Out of webs"
            },
            "status": 400
          }
        }
      }
    }
  ]
}

Simulations

You can also change the response behaviour:

// ./fakeless.json
{
  "variables": {
    "host": "http://localhost:3000",
    "rootDir": "."
  },
  "templates": [
    {
      "variables": {
          "hero": "spider"
      },
      "method": "post",
      "route": "/heroes/<%= hero %>",
      "response": {
        "body": {},
        "error": {},
        "status": 400
      },
      "simulate": {
        "timeout": 100
      }
    }
  ]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.