All Projects → grahamsutton → factory

grahamsutton / factory

Licence: MIT license
Generate lots of mock API data with ease.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to factory

interface-forge
Graceful mock-data and fixtures generation using TypeScript
Stars: ✭ 58 (+241.18%)
Mutual labels:  mock, mock-data
mock-data
Mock data in PostgreSQL/Greenplum databases
Stars: ✭ 115 (+576.47%)
Mutual labels:  mock, mock-data
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+20182.35%)
Mutual labels:  mock, mock-data
miz
🎯 Generate fake data, Just like a person.
Stars: ✭ 24 (+41.18%)
Mutual labels:  mock, mock-data
better-mock
Forked from Mockjs, Generate random data & Intercept ajax request. Support miniprogram.
Stars: ✭ 140 (+723.53%)
Mutual labels:  mock, mock-data
ng-apimock
Node plugin that provides the ability to use scenario based api mocking: for local development for protractor testing
Stars: ✭ 102 (+500%)
Mutual labels:  mock, mock-data
fe-dev-server
FE Dev Server target to help frontend web developers create view template, styles and js easily.
Stars: ✭ 30 (+76.47%)
Mutual labels:  mock, mock-data
chip
📦 🐳 🚀 - Smart "dummy" mock for cloud native tests
Stars: ✭ 19 (+11.76%)
Mutual labels:  mock
Paw-FakerDynamicValue
A dynamic value extension for Paw using Faker to generate data
Stars: ✭ 16 (-5.88%)
Mutual labels:  mock
admin-base-tmpl
⚡️基于vite2构建的vue2+typescript+elementUI 的后台基础套件,预览地址
Stars: ✭ 52 (+205.88%)
Mutual labels:  mock
dextool
Suite of C/C++ tooling built on LLVM/Clang
Stars: ✭ 81 (+376.47%)
Mutual labels:  mock
axios-mock-server
RESTful mock server using axios.
Stars: ✭ 33 (+94.12%)
Mutual labels:  mock
joke
Typesafe mock utility with minimal boilerplate for jest
Stars: ✭ 16 (-5.88%)
Mutual labels:  mock
entity-framework-mock
Easy Mock wrapper for mocking EF6 DbContext and DbSet using Moq or NSubstitute
Stars: ✭ 45 (+164.71%)
Mutual labels:  mock
wwvue-cli
vue-cli升级版脚手架,应有尽有的开箱即用方法及配置,没有花里胡哨的晦涩难懂的操作,上手成本极低,现已新增simple(极简模式)、vue3和iview-template,是个很不错的垫脚石,来不及解释了赶紧上车😊😘
Stars: ✭ 15 (-11.76%)
Mutual labels:  mock
electron-admin-antd-vue
Electron Vue3.x Ant Design Admin template
Stars: ✭ 21 (+23.53%)
Mutual labels:  mock
firebase-nightlight
An in-memory, JavaScript mock for the Firebase Web API
Stars: ✭ 37 (+117.65%)
Mutual labels:  mock
mockit
A tool that integrates SQL, HTTP,interface,Redis mock
Stars: ✭ 13 (-23.53%)
Mutual labels:  mock
Mokku
Mock API calls seamlessly
Stars: ✭ 109 (+541.18%)
Mutual labels:  mock
kugou
multiple implementations for kugou music
Stars: ✭ 25 (+47.06%)
Mutual labels:  mock

factory

Generate lots of mock data with ease using Marak/faker.js.

CircleCI npm version

Factory is just one simple function that you can use to create vast amounts of mock data in one quick and simple definition. No extra fluff like defining "models". Factory is intended to be straight-to-the-point. Need an array of 50 object literals on the fly to mimic the API response your expecting? Need just one array with 200 elements? No problem, Factory can help you generate that mock data with minimal effort.

Use it in your unit tests, pass it in as a prop to your Vue/React components for quick prototyping, or use it for mocking API responses. Anywhere you need lots of mock data fast, Factory can help.

Check out the examples below to see how easy it is.

Installation

$ npm install --save-dev @grahamsutton/factory

Usage

Function Signature

factory(numRecords: Number, callback: Function): Array

NOTE: numRecords must be an integer or an error will be thrown.


Basic Example

const factory = require('@grahamsutton/factory')

// Create array containing 3 mock objects
let people = factory(3, faker => {
  return {
    "first_name": faker.name.firstName(),
    "last_name": faker.name.lastName()
  }
})

Example response:

[
  { "first_name": "Andres", "last_name": "Will" }
  { "first_name": "Roma", "last_name": "Welch" }
  { "first_name": "Emmalee", "last_name": "Grant" }
]

Use index Parameter For Uniquely IDing Mock Data

Just add index as the second parameter to the callback function:

const factory = require('@grahamsutton/factory')

// index is zero-based
let people = factory(3, (faker, index) => {
  return {
    "id": index + 1,
    "name": faker.name.firstName() + " " + faker.name.lastName()
  }
})

Example response:

[
  { "id": 1, "name": "Andres Will" }
  { "id": 2, "name": "Roma Welch" }
  { "id": 3, "name": "Emmalee Grant" }
]

Create a Really Long Array of Random Elements

If you just need a really long array of mock data, whatever you return will be placed in the output array:

const factory = require('@grahamsutton/factory')

let randomNumbers = factory(200, faker => {
  return faker.random.number()
})

Example response:

[22892, 12314, 34, 2, 812, 54, 1242, 28532, ...]

Use factory Within Another factory Call To Define Nested Elements

const factory = require('@grahamsutton/factory')

let users = factory(2, (faker, index) => {
  return {
    "id": index + 1,
    "name": faker.name.firstName(),
    "email": faker.internet.email(),
    "departments": factory (4, () => {
      return faker.commerce.department()
    }),
    "addresses": factory(2, () => {
      return {
        "street": faker.address.streetAddress(),
        "city": faker.address.city(),
        "state": faker.address.state()
      }
    })
  }
})

This would return an array with the following structure:

[
  {
    "id": 1,
    "name": "Mike",
    "email": "[email protected]",
    "departments": [
      "Automotive", "Grocery", "Sports", "Electronics"
    ],
    "addresses": [
      {
        "street": "665 Alexander Corner",
        "city": "Doylehaven",
        "state": "Florida"
      },
      {
        "street": "479 Adrain Tracer",
        "city": "Dibbertchester",
        "state": "New York"
      }
    ]
  },
  {
    "id": 2,
    "name": "Sam",
    "email": "[email protected]",
    "departments": [
      "Sports", "Beauty", "Automotive", "Electronics"
    ],
    "addresses": [
      {
        "street": "12780 Juston Neck",
        "city": "Walterville",
        "state": "Alabama"
      },
      {
        "street": "579 Rashad Loop",
        "city": "South Tristinmouth",
        "state": "Idaho"
      }
    ]
  }
]

Mocking Data for Vue/React Components Example

Need some mock data for your component props?

Create a file somewhere in your project, require @grahamsutton/factory, and start building your mock definition, then just export the results from your call to factory so they can be imported in your components.

Here's an example.

Let's say we create a file called UsersMockData.js for our component called Users:

UsersMockData.js

const factory = require('@grahamsutton/factory')

module.exports = factory(10, (faker, index) => {
  return {
    "id": index + 1,
    "name": faker.name.firstName() + " " + faker.name.lastName(),
    "email": faker.internet.email()
  }
})

Then, in your parent component that contains the Users component (Vue example):

<template>
  <users :users="users"></users>
</template>

<script>
  import Users from './Users.vue'
  import UsersMockData from './UsersMockData.js'

  export default {
    components: {
      Users
    },
    data () {
      return {
        users: UsersMockData
      }
    }
  }
</script>

Now, if you want to prototype how your component will look with 20, 50, 100, 200 users or more, you can simple go to your UsersMockData.js file and just increase the amount of records you want factory to produce. Simple as that. Just be sure to remove it once your ready to hook it up to your real API.

Disclaimer: Your mock data could change on every hard refresh or restart (usually soft refreshes or hot reloading won't change it). If you need your data to remain static, I would recommend saving the output from factory to a JSON file and importing that instead.

Contributing

See the CONTRIBUTING.md file for instructions on contributing. Any contributers are welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].