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 🙏

© 2026 – Pkg Stats / Ryan Hefner

create-mock-api-from-types

v1.0.0

Published

Generate a mock REST API server from TypeScript interfaces

Readme

Mock API

Generate a mock REST API server from TypeScript interfaces and object type aliases. Perfect for frontend development, testing, and prototyping—define your data shapes in TypeScript and get a fully functional API with realistic fake data powered by Faker.

Features

  • TypeScript-first: Define schemas as interfaces or object type aliases
  • Smart mock data: Property names are matched to appropriate Faker generators (e.g. email → realistic emails, price → commerce prices)
  • Full REST API: GET, POST, PUT, PATCH, DELETE with pagination support
  • Nested types: References to other interfaces/types are resolved recursively
  • Multiple schemas: Point to one or more schema files to build a complete API

Installation

pnpm add mock-api
# or
npm install mock-api

Quick Start

  1. Create a schema file (e.g. schema.ts):
interface User {
    id: string;
    name: string;
    email: string;
    phone: string;
    isActive: boolean;
}

type Product = {
    id: string;
    name: string;
    price: number;
    inStock: boolean;
};
  1. Start the mock API:
npx mock-api schema.ts
  1. Visit http://localhost:3000 to see all available routes and collections.

CLI Usage

mock-api <schemas...> [options]

Arguments

| Argument | Description | | ---------- | ------------------------------------ | | <schemas...> | One or more TypeScript schema file paths (required) |

Options

| Option | Short | Default | Description | | ------ | ----- | ------- | ------------------------------------ | | --port | -p | 3000 | Port to run the server on | | --count | -c | 10 | Number of mock items to generate per collection |

Examples

# Basic usage
mock-api src/schema.ts

# Custom port and item count
mock-api schemas/*.ts -p 8080 -c 25

# Multiple schema files
mock-api src/models/user.ts src/models/product.ts

Schema Definition

Interfaces

interface User {
    id: string;
    name: string;
    email: string;
}

Object Type Aliases

type Product = {
    id: string;
    name: string;
    price: number;
};

Both interfaces and object type aliases (type X = { ... }) are supported. Only object-shaped types are used—primitives and union aliases (e.g. type ID = string) are ignored.

Supported Types

  • Primitives: string, number, boolean
  • Arrays: T[] or Array<T>
  • Unions: 'a' | 'b' (picks a random option)
  • Nested types: References to other interfaces or type aliases in the same project
  • Optional: | undefined and | null are stripped when generating mocks

API Endpoints

Each schema becomes a collection with the following REST endpoints:

| Method | Path | Description | | ------ | ---- | ----------- | | GET | / | List all collections and their endpoints | | GET | /:collection | List items (supports ?page and ?limit for pagination) | | GET | /:collection/:id | Get a single item by ID | | POST | /:collection | Create a new item | | PUT | /:collection/:id | Replace an item | | PATCH | /:collection/:id | Partially update an item | | DELETE | /:collection/:id | Delete an item |

Route Naming

Interface/type names are converted to kebab-case and pluralized:

  • User/users
  • Product/products
  • OrderItem/order-items

Response Format

List (GET /:collection):

{
  "data": [...],
  "total": 10,
  "page": 1,
  "limit": 10
}

Single item (GET /:collection/:id):

{
  "id": "uuid-here",
  "name": "John Doe",
  "email": "[email protected]"
}

Smart Property Generation

Property names are matched to Faker generators for realistic data:

| Pattern | Example | | ------- | ------- | | id, *Id | UUID | | email | [email protected] | | name, firstName, lastName | Person names | | phone, mobile | Phone numbers | | password, secret | Passwords | | address, city, state, zip, country | Location data | | price, cost, amount | Commerce prices | | createdAt, updatedAt, date | ISO date strings | | description, content, body | Lorem paragraphs | | url, image, avatar | URLs | | isActive, isEnabled, verified | Booleans | | ...and more | See propGenerator.ts for full list |

Unmatched properties fall back to type-based generation (string → word, number → integer, etc.).

Programmatic Usage

Use the library in your own Node.js application:

import { createServer, startServer } from "mock-api";
import type { MockApiOptions } from "mock-api";

const options: MockApiOptions = {
    schemaPaths: ["src/schema.ts"],
    port: 3000,
    count: 10,
};

// Start a server (blocks)
startServer(options);

// Or create an Express app for custom use (e.g. testing)
const app = createServer(options);
// app is a standard Express application

Development

# Install dependencies
pnpm install

# Build
pnpm run build

# Run with tsx (no build step)
pnpm run dev src/schema.ts

# Type check
pnpm run typecheck

License

ISC