npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fake-api-mocker

v1.0.6

Published

**fake-api-mocker** is Schema based fake API mocker with `TypeScript` ready. With this you can create any nested `complex schema` and generate `tons of fake data` as per as `specified schema` and can be used at `client side` as well as `server side`.

Downloads

5

Readme

fake-api-mocker

fake-api-mocker is Schema based fake API mocker with TypeScript ready. With this you can create any nested complex schema and generate tons of fake data as per as specified schema and can be used at client side as well as server side.

The declared Schema are just like Yup lib Schema.

Supported Schema as of now.

  • isString() for random strings.
  • isEmail() for random emails.
  • isImage({ width: 200, height: 400 }) for random images urls.
  • isDate({ interval: 100000 }) for timestamps.
  • isUUID() for random UUID's.
  • isObject({ /* pass new schema*/ }) for random nested objects.
  • isBoolean() for random boolean values.
  • rows(count = 10, chunks = 0) for generating number of rows in chunks. default count value is 10 and chunks value is 0.
  • done() Indicates schema termination. It's necessary to terminate each individual schema with .done() in the end otherwise it'll throw an error.

To be added.

  • isText() for random large text like Lorem ipsum.
  • isGeo() for random geo coordinates lat-long tuples.
  • isPhone({ code:'+91' }) for random phone numbers.
  • isIP({ type:'v4' }) for random ipv4 and ipv6.
  • isSlug() for random Slugs. .. Above are just vaugely at the top of my head :-/ Yet lot more to be added :-D

Install

Install with npm:

npm i fake-api-mocker --save

Install with yarn:

yarn add fake-api-mocker

Imports with CommonJS or ES5

const { schema, mockFakeAPI } = require("fake-api-mocker");
or 
import { schema, mockFakeAPI } from "fake-api-mocker";

Basic Example

index.js
const { schema, mockFakeAPI } = require("fake-api-mocker");

// Remember to terminate each individual schema with .done() in the end otherwise it'll throw an error.
const fakeSchema = {
  id: schema.isUUID().done(),
  name: schema.isObject({
    firstName: schema.isString().done(),
    lastName: schema.isString().done(),
  }).done(),
  email: schema.isEmail().done(),
  avatar: schema.isImage({ height: 320, width: 240 }).done(),
  isActive: schema.isBoolean().done(),
  joinedOn: schema.isDate().done()
};

(async () => {
  try {
    const data = await mockFakeAPI(fakeSchema, 1000);
    console.log(data)
  } catch (e) {
    console.log(e.message)
  }
})();
index.js (output)
{
  "id": "be43687f-f2de-4427-b87b-ac78013b61c6",
  "name": {
    "firstName": "rtanumnpzz",
    "lastName": "npqfvpcsp"
  },
  "email": "[email protected]",
  "avatar": "https://picsum.photos/240/320",
  "isActive": true,
  "joinedOn": 1632231989950
}

Complex Nested Type.

  1. For nested object you can simply nest it by chaining .isObject() which will accept a object schema as a argument for exapmle consider name has nested keys named firstName and lastName.
  • For below JSON:-
{
  "name": {
    "firstName": "rhzophn",
    "lastName": "huoljh"
  }
}

Schema will look like:-

index.js
const { schema, mockFakeAPI } = require("fake-api-mocker");

// Remember to terminate each individual schema with .done() in the end otherwise it'll throw an error.
const fakeSchema = {
  name: schema.isObject({
    firstName: schema.isString().done(),
    lastName: schema.isString().done(),
  }).done(),
};
(async () => {
  try {
    const data = await mockFakeAPI(fakeSchema, 1000);
    console.log(data)
  } catch (e) {
    console.log(e.message)
  }
})()
  1. Consider the above same example but insted of having a single object you want array of objects. This is where the .rows() shines. .rows(count = 10, chunks = 0) it takes count as it's 1st argument to populate that amount of objects inside an array. And an optional chunks as 2nd argument to populate data in chunks. For instance:-
  • schema.isBoolean().rows(5) will generate data as [ true, false, true, false, false ]
  • schema.isBoolean().rows(5, 2) will generate data as [ [ false, false ], [ true, true ], [ false ] ]

For below JSON:-

{
  "name": [
    {
      "firstName": "yuiywbl",
      "lastName": "pobgq"
    },
    {
      "firstName": "zpoiqifn",
      "lastName": "dassl"
    }
  ]
}

Schema will look like:-

index.js
const { schema, mockFakeAPI } = require("fake-api-mocker");

// Remember to terminate each individual schema with .done() in the end otherwise it'll throw an error.
const fakeSchema = {
  name: schema.isObject({
    firstName: schema.isString().done(),
    lastName: schema.isString().done(),
  }).rows(2).done(), // Added .rows(2) will generate 2 object in an array
};

(async () => {
  const data = await mockFakeAPI(fakeSchema, 1000);
  console.log(data);
})()
  1. For primitive types i.e String, Boolean, Number. Let's say you want array of emails or timestamps or uuids

For below JSON:-

{
  "id_array": [
    "a2c3909b-5292-45cc-8a08-e0352b912955",
    "012659c5-0ae9-450c-b5b0-2bc9bd299271",
    "78e34b9c-0d14-4114-903d-6ebae1f168d3"
  ],
  "email_array": [
    "[email protected]"
  ],
  "timestamp_array": [
    1631479167348,
    1631479267348,
    1631479367348,
    1631479467348,
    1631479567348
  ]
}

Schema will look like:-

index.js
const { schema, mockFakeAPI } = require("fake-api-mocker");

// Remember to terminate each individual schema with .done() in the end otherwise it'll throw an error.
const fakeSchema = {
  id_array: schema.isUUID().rows(3).done(),
  email_array: schema.isEmail({ isYopMail: true }).rows(1).done(),
  timestamp_array: schema.isDate({ interval: 100000 }).rows(5).done()
};

(async () => {
  const data = await mockFakeAPI(fakeSchema, 1000);
  console.log(JSON.stringify(data, null, 2))
})()