Skip to content

Command-line tool for generating fake API responses based on a JSON configuration file

License

Notifications You must be signed in to change notification settings

paqstd-team/fake-cli

Repository files navigation

Fake API (CLI)

This is a command-line tool for generating fake API responses based on a JSON configuration file. The tool is written in Go and uses the gofakeit library to generate random data.

Installation

To install the fakeapi command-line tool, you must have Go installed on your system. You can download and install Go from the official Go website.

Once you have Go installed, you can install the fakeapi tool by running the following command:

go install -v github.com/paqstd-team/fake-cli@latest

Also you can install the binary by downloading it from one of the latest releases.

Local development

Use commands:

make        # builds the binary (default command)
make build  # builds the binary
make clean  # removes the binary
make run    # runs the program

Usage

To generate fake API responses, you must create a configuration file in JSON format that defines the endpoints and fields for each endpoint. We recommended use .json type for files. Here's an example configuration file config.json:

{
  "cache": 5,
  "endpoints": [
    {
      "url": "/users",
      "fields": {
        "id": "uuid",
        "name": "name",
        "email": "email"
      },
      "response": "list"
    },
    {
      "url": "/products",
      "fields": {
        "id": "uuid",
        "name": "word",
        "price": "price"
      }
    },
    {
      "url": "/products/{id}",
      "fields": {
        "id": "uuid",
        "tags": [{
          "id": "uuid",
          "name": "name"
        }],
        "details": {
          "about": {
            "author": "name",
            "customValue": "my-custom-value"
          }
        }
      }
    },
    {
      "url": "/list",
      "fields": {
        "names": ["name", "name", "name"]
      }
    }
  ]
}

This configuration file defines two endpoints: /users and /products. The /users endpoint returns a list of users with pagination (by default page = 1 and per_page = 10), while the /products endpoint returns a single product.

Cache is not required, but if existed it is count of request for caching. By default cache settings is 0 and every request will generate new data. If you specify -1, this will turn off further generation and all data will be used from the cache.

You can also specify the type Array, Object as a value. Each of these types can have nested values.

List of types:

  • uuid
  • city
  • state
  • country
  • latitude
  • longitude
  • name
  • name_prefix
  • name_suffix
  • first_name
  • last_name
  • gender
  • ssn
  • hobby
  • email
  • phone
  • username
  • password
  • paragraph
  • sentence
  • phrase
  • quote
  • word
  • date
  • second
  • minute
  • hour
  • month
  • day
  • year
  • url
  • domain
  • ip
  • int
  • float

To generate fake API responses using this configuration file, you can run the fake command:

fake-cli -c path/to/config.json

Replace path/to/config.json with the path to your configuration file.

By default, the fake command starts a web server on port 8000 that responds to requests for the endpoints defined in your configuration file. You can specify a different port by adding the --p flag followed by the desired port number:

fake-cli -c path/to/config.json -p 8080

Customization

You can customize the types of fake data generated by editing the handler/handler.go file. The MakeHandler function generates fake data based on the fields and response type defined in the configuration file.

You can also add new types of fake data by modifying the switch statement in the MakeHandler function. The gofakeit library provides many built-in functions for generating fake data, and you can use these functions to generate custom data types.

Docker

Local

Pull image to local:

docker pull ghcr.io/paqstd-team/fake-cli

Run with docker:

docker run --name fake-cli -it -v ${PWD}/config.json:/app/config.json -p 8080:8080 -e CONFIG_PATH=config.json -e PORT=8080 ghcr.io/paqstd-team/fake-cli

Docker Compose

Here is an example of usage fake-cli with docker-compose and other containers:

services:
  # ...other services
  fake-cli:
    # pull from github container registry
    image: ghcr.io/paqstd-team/fake-cli:latest
    environment:
      # default config path is "config.json"
      - CONFIG_PATH=config.json
      # default port is 8080
      - PORT=8080
    ports:
      # link port inside container to real world
      - 8080:8080
    volumes:
      # copy config file to container
      - ./config.json:/app/config.json

Contributing

If you find a bug or would like to suggest a new feature, you can create an issue on the GitHub repository for this project. If you'd like to contribute code, you can fork the repository and submit a pull request with your changes.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

About

Command-line tool for generating fake API responses based on a JSON configuration file

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 2

  •  
  •