customjsonbuilder

当前版本: 1.0.9


customjsonbuilder是什么?

飞翔上带有自定义字段的虚假json响应
Fake json responses with custom fields on the fly

许可协议

ISC

关键词

json , json-schema , generator , fake-json , fake-data , fake-json-generator

安装方式

npm install customjsonbuilder@1.0.9

相关网址


customjsonbuilder使用说明

custom JSONbuilder

Fake json responses with custom fields on the fly

Example (示例)

A simple query like this

http://localhost:6500/{name:string,email:email}

will respond with json like:

{
    "name":"Alexa",
    "email":"Champlin@gmail.com"
}

Installation (安装)

Clone the project or download the zip file

Go to the folder

cd customjsonbuilder-master

Run fake api on Node

Install dependencies

npm install

Launch the server

npm start
// or with a custom port
PORT=4545 npm start

It will prompt

Running on http://0.0.0.0:6500

Open the browser and visit the link http://0.0.0.0:6500

Now query some fake data from your React, Angular, Vue or any external project.

Simple jquery usage example

Example plain jquery project

React usage example
class Posts extends React.Component {
    // my other cool code
    componentDidMount() {
        let pattern = `
            {
                posts: {
                    id: number,
                    title: string,
                    $times: 3
                }
            }
        `
        axios.get(`http://0.0.0.0:6500/${pattern}`)
            .then((res) => {
                this.setState({
                    posts: res.data.posts
                })
            })
    }
    render() {
        return (
              <div>
                  {this.state.posts.map(p => {
                      return </div>p.title<div>
                  })}
              </div>
        );
    }
}

NPM

Install via npm

npm install customjsonbuilder --save-dev
Node example
// index.js
let customjsonbuilder = require('customjsonbuilder');
let fake = customjsonbuilder.build("{name:string}");
console.log(fake)
// run on terminal "node index.js"
Express example

Create your own custom fake server and use customjsobuilder to generate fake data

let express = require('express');
let app = express();
let customjsonbuilder = require('customjsonbuilder');
app.get('/posts', (req, res) => {
    let posts = `
        {
            posts: {
                id: number,
                title: string,
                $times: 3
            }
        }
    `
    let response = customjsonbuilder.build(posts);
    res.json(response);
});
app.listen('8200', '0.0.0.0');
console.log(`Running on http://0.0.0.0:8200`);

Pattern Guide

Pattern

Example: http://localhost:6500/<THE PATTERN GOES HERE>

key: value

It is like writing regular json

{THE_KEY_THAT_I_WANT : THE_DATA_TYPE}
Simple example (简单的示例)

I want an object with a key name and a random word as value

{
    name: string
}

The server will respond with data like

{
    "name": "vm2sgdbmf2e7mmbc8502w8q"
}
Multiple values

Now I need a json response with id, name and email

{
    id: number,
    name: string,
    email: email
}

The server will respond with data like

{
    "id": 49994,
    "name": "Mission",
    "email": "Eula_Deckow@yahoo.com"
}
Nested object

I want an object with a userId, a name and a nested object contact which contains phone and email.

The pattern should be:

{
    userId: number,
    name: firstname,
    contact: {
        phone: number,
        email: email
    }
}

The server will respond with data like

{
    "userId":94781,
    "name":"Maria",
    "contact":{
        "phone":88885,
        "email":"Eladio_Brown44@hotmail.com"
    }
}
Doubly nested object

Lets try something more complex. I want an object with userId, name, a nested object with contact that contains phone and email with nested content personal_email and company_email

{
    userId: number,
    name: firstname,
    contact: {
        phone: number,
        email: {
            personal_email: email,
            company_email: email
        }
    }
}

The server will respond with data like

{
    "userId": 7316,
    "name": "discrete",
    "contact": {
        "phone": 14357,
        "email": {
            "personal_email": "Rowena_Homenick@yahoo.com",
            "company_email": "Caesar52@hotmail.com"
        }
    }
}
Array response

I want 3 posts with id and title

To get an array response just add key $times:NUMBER, were number is the number of elements that i want in the array.

{
    posts: {
        id: number,
        title: string,
        $times: 3
    }
}

And the server will respond with data like

{
    "posts": [
        {
            "id": 65450,
            "title": "vs4brxz5497yggxg80wvy"
        },
        {
            "id": 11251,
            "title": "v6tfhr591s3isajey067j3l"
        },
        {
            "id": 89704,
            "title": "vs3q84xh8nmdcp87w2c9ax8"
        }
    ]
}

Data types (数据类型)

In the current version we support these data types:

  • string
  • number
  • boolean
  • null
  • undefined
  • empty (empty string "")
  • true (always generate true)
  • false (always generate false)
  • name
  • firstName
  • lastName
  • age (number between 1-99)
  • age18 (number between 18-99)
  • agekid (number between 1-18)
  • email
  • username
  • uuid
  • title
  • text
  • word
  • words
  • paragraph
  • paragraphs
Example (示例)

post title

{
    postTitle: title
}

will generate

{
    "postTitle": "Rerum odio quam."
}

user email

{
    user_email: email
}

will generate

{
    "user_email": "Heber1@hotmail.com"
}

Awesome examples

  • Basic
{
    name: string
}
{
    "name": "Ergonomic Concrete Pants"
}
  • User information
{
    userId: number,
    username: username,
    name: firstname,
    email: email
}
{
    "userId": 89330,
    "username": "Filomena_Bogisich",
    "name": "port",
    "email": "Valentin.Buckridge@hotmail.com"
}
  • User last 3 posts
{
    data: {
        user_id: number,
        posts: {
            post_id: number,
            title: title,
            post_resume: paragraph,
            views_number: number,
            comments_number: number,
            $times: 3
        }
    }
}
{
    "data": {
        "user_id": 26027,
        "posts": [
            {
                "post_id": 7375,
                "title": "Velit enim et quod distinctio.",
                "post_resume": "Quo dolores ...",
                "views_number": 13545,
                "comments_number": 20335
            },
            {
                "post_id": 25141,
                "title": "Consequatur ut illum nobis et.",
                "post_resume": "Temporibus ut ...",
                "views_number": 70492,
                "comments_number": 80291
            },
            {
                "post_id": 18477,
                "title": "Debitis odio sunt laudantium aut eum aut laudantium.",
                "post_resume": "Voluptatem culpa ut dol...",
                "views_number": 23572,
                "comments_number": 80794
            }
        ]
    }
}
  • Shopping card example
{
    data: {
        shopId: number,
        finished: boolean,
        catId: number,
        clientId: number,
        items: {
            id: number,
            name: word,
            quantity: number,
            $times:3
        }
    }
}

Will generate

{
    "data": {
        "shopId": 56079,
        "finished": false,
        "catId": 60197,
        "clientId": 39134,
        "items": [
            {
                "id": 94476,
                "name": "facere",
                "quantity": 85981
            },
            {
                "id": 14435,
                "name": "ut",
                "quantity": 45225
            },
            {
                "id": 52692,
                "name": "rerum",
                "quantity": 99475
            }
        ]
    }
}
  • true, false example
{ 
    valid: true,
    erros: false
}

Will generate

{
    "valid": true,
    "errors": false
}

Beta

This project is in beta so pehaps there are possibly still some some bugs... If you find any bugs, feel free the leave a comment!

API live

Now it only works on localhost but we are working to launch a hosted app and create a public API.

Features (功能)

We are thinking and working on adding more data types like city, uuid, date, time and the capability to add default values to the json response.

Thanks (谢谢)

Especial thanks to faker that is used behind the scenes to generate the fake data. Also thanks to the other opensource projects.

Collaborate

Feel free to collaborate with this project.

Follow me

on twitter @rollrodrig

项目热度

项目信息

分享到

基本信息
开发语言
TypeScript
相关项目
1
相关仓库
0
Watchers
1
Stars
3
Forks
1
版本数量
20
最新发布
2020-09-16
首次发布
2019-06-08
仓库大小
723 KB

版本历史
1.0.9
2020-09-16
1.0.8
2020-09-16
1.0.7
2020-07-11
1.0.5
2020-07-06
1.0.4
2020-07-06
1.0.3
2020-07-06
1.0.2
2020-07-06
0.1.0
2020-07-06
0.0.16
2019-06-11
0.0.15
2019-06-11

贡献者
rollrodrig Michal