fauxapi
v1.1.0
Published
A schema-based mock JSON generator with CLI and API mocking capabilities
Maintainers
Readme
FauxAPI
A schema-based mock JSON generator with CLI and API mocking capabilities.
Features
- 🎯 Schema-driven mock data generation
- 🔄 Deterministic output with seed support
- 🖥️ Powerful CLI tool
- 🌐 Mock API server (coming soon)
- 📦 TypeScript-first with full type inference
- 🚀 ESM and CommonJS support
Installation
npm install -g fauxapiUsage
Programmatic API
import { defineMock, generate, string, number, email } from 'fauxapi';
const userSchema = defineMock({
id: number(),
name: string(),
email: email(),
});
const user = generate(userSchema);
// { id: 42, name: "lorem", email: "[email protected]" }
const users = generate(userSchema, { count: 5, seed: 123 });
// Array of 5 users with deterministic dataCLI
Generate from a schema file:
fauxapi generate path/to/schema.js
fauxapi generate path/to/schema.mjs
fauxapi generate path/to/schema.json
fauxapi generate path/to/schema.ts # requires esbuild installedExample schema.mjs:
import { defineMock, string, number, email } from './dist/index.js';
export default defineMock({
id: number,
name: string,
email: email,
});Generate and print one object:
fauxapi generate ./schema.mjsGenerate five deterministic objects:
fauxapi generate ./schema.mjs --count 5 --seed 42Options:
-o, --out <file>: Output to file-c, --count <number>: Number objects (default: 1)-s, --seed <number>: Seed for deterministic data
Built-in Generators
string(): Random stringnumber(): Random numberboolean(): Random booleanemail(): Random emailuuid(): Random UUIDdate(): Random ISO date string
Nested schema support
generate now supports nested object schemas and nested arrays within schema values.
import { defineMock, generate, string, number, boolean } from 'fauxapi';
const schema = defineMock({
user: {
name: string,
age: number,
},
settings: {
theme: 'dark',
notifications: boolean,
},
});
const result = generate(schema);
// {
// user: { name: '...', age: ... },
// settings: { theme: 'dark', notifications: true }
// }Nested arrays are also supported in schema values:
const schemaWithList = defineMock({
tags: ['a', 'b', 'c'],
entries: [
{ id: number, value: string },
],
});Custom Generators
const schema = defineMock({
custom: () => 'always this value',
fakerName: () => faker.person.fullName(),
});API
defineMock(schema)
Defines a mock schema.
generate(schema, options?)
Generates mock data from schema.
Options:
seed?: number- Seed for deterministic outputcount?: number- Number of items to generate (returns array if > 1)
Development
npm install
npm run build
npm testLicense
MIT
