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

ts-mock-api

v1.0.13

Published

A TypeScript utility for generating dynamic mock data based on TypeScript types.

Downloads

549

Readme

ts-mock-api

ts-mock-api is a TypeScript utility for generating dynamic mock data based on TypeScript types. This package helps developers quickly create mock APIs and test data during development, especially when working with complex TypeScript interfaces.

Installation

To use the package you first need to install it as dev dependency:

npm i -D ts-mock-api

Usage

Basic Example

To get started, define your TypeScript interfaces and a corresponding schema that describes the data types:

import { generateMockData } from "ts-mock-api";

interface User {
  name: string;
  age: number;
  isActive: boolean;
}

const userSchema = {
  name: "string",
  age: "number",
  isActive: "boolean",
};

const mockUser = generateMockData(userSchema);

console.log(mockUser);
// Output { name: 'random string', age: 25, isActive: true }

Generating Arrays

You can easily generate arrays of mock data by specifying the type as an array in the schema:

const schema = {
  names: "string[]",
};

const mockData = generateMockData(schema, { arrayLength: 5 });

console.log(mockData);
// Output might be: { names: ['random string', 'another string', ...] }

Customizing Mock Data

You can customize the mock data generation by passing an options object:

const options = {
  stringLength: 10, // Custom length for strings
  arrayLength: 5, // Number of elements in arrays
};

const mockUser = generateMockData(userSchema, options);

console.log(mockUser);
// Output might be: { name: 'long random string', age: 42, isActive: false }

Handling Nested Objects

The package also supports nested objects. Define the schema for the nested object as well:

interface UserProfile {
  name: string;
  age: number;
  contact: {
    email: string;
    phone: string;
  };
}

const profileSchema = {
  name: "string",
  age: "number",
  contact: {
    email: "string",
    phone: "string",
  },
};

const mockProfile = generateMockData(profileSchema);

console.log(mockProfile);
// Output might be: { name: 'John Doe', age: 30, contact: { email: '[email protected]', phone: '123-456-7890' } }

API

generateMockData<T>(typeSchema: T, options?: MockOptions): T

Generates mock data based on the provided TypeScript schema.

  • typeSchema: An object representing the schema of the data to generate. The keys should match the structure of the desired output, and the values should be string representations of the types ('string', 'number', 'boolean', 'string[]', etc.).
  • options (optional): An object that allows you to customize the generated data.
    • stringLength: Specifies the length of generated strings.
    • arrayLength: Specifies the number of elements in generated arrays.

MockOptions

An interface to customize mock data generation:

interface MockOptions {
  stringLength?: number; // Default: 5
  arrayLength?: number; // Default: 3
}

Authors

License

MIT