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

@bharathsj/cfapi

v1.0.1

Published

⚡ Code-Free API generator from JSON schema. Supports mock data or MongoDB (Mongoose).

Downloads

31

Readme

⚡ cfapi – Code-Free API Generator (Prototype)

cfapi is a powerful command-line tool that generates full REST APIs from a simple JSON schema — in seconds. Whether you're prototyping or building real apps, cfapi handles everything from input validation to route generation, database setup, and OpenAPI documentation — with zero boilerplate.

🔰 This is my first prototype. I'm still learning backend and open source patterns — feedback is welcome and appreciated!


✨ Features

✅ Generate a complete API with one command
✅ Choose between:

  • mock engine – JSON file storage with auto-persistence
  • mongo engine – Mongoose ODM with full validation

✅ Auto-generates:

  • Controllers
  • Routes
  • Middleware validation based on constraints provided in the schema
  • Mongoose/mock models
  • OpenAPI model schemas

✅ Schema-aware:

  • Deeply nested objects and arrays
  • References with ref fields
  • Validation: required, enum, pattern, minLength, unique, etc.

✅ Built-in support for:

  • Unique constraints in both engines
  • addModel to extend existing APIs
  • .cfapi.config.json created on first run

✅ Mock engine:

  • Auto-generates 10 fake records per model on creation

✅ MongoDB engine:

  • Uses Mongoose schemas with timestamps, ref, and full validation logic

📦 CLI Usage

cfapi --type <generate|add> --schema <path> --output <dir> --engine <mock|mongo>

🔨 Generate Full API

cfapi -t generate -s ./schema.json -o ./my-api -e mock

✅ When using the mock engine, 10 sample mock records are created automatically.

➕ Add Model(s)

cfapi -t add -s ./new-models.json -o ./my-api -e mongo

⚠️ You cannot add a model twice — existing models will be skipped unless --force is used.

Help

cfapi --help

🧠 PATCH Support: Explained

PATCH route exists ❌ Only supports top-level fields (e.g. email, not profile.bio) 🛠️ Nested patching is not supported due to complexity and business logic ambiguity.


⚠ Limitations

  • PATCH only supports flat fields (nested updates are not allowed).
  • No built-in support for authentication, pagination, or advanced filtering.
  • Malformed schemas may result in validation errors or undefined behavior.
  • The id field is handled internally – ensure your schema follows strict rules as demonstrated in the examples.
  • Validation is extremely strict – it will reject any input that does not exactly match the schema definition. No extra fields are allowed.

📦 Example Outputs

cfapi generates complete, ready-to-run REST API scaffolding based on your schema and selected engine. Below are four full examples demonstrating both the mock and mongo engines across two different schemas:

🧪 Sample Schema 1: User API

  • ✨ Demonstrates a basic schema with string, number, boolean, date, array, enum, and nested object types.
  • ✅ Both mock and MongoDB engines.
  • 📘 Includes auto-generated OpenAPI spec and route/controller/model/middleware files.

| Mock Engine Output | MongoDB Engine Output | | --------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | | Mock Output | Mongo Output |


🏢 Sample Schema 2: User + Company (Nested + Ref)

  • 🔗 Shows a complex schema with multiple models and references (ref), nested object fields, and validations.
  • 🌱 Highlights mock vs mongo behavior with relational handling.
  • 📘 Each version includes OpenAPI schema, working routes, and validation middleware.
  • 🔗 sample-schema2-mock
  • 🔗 sample-schema2-mongo

📁 Output Structure

my-api/
├── config/
│   └── cfapi.config.json         # Project config
│   └── db.json                   # MongoDB connection details
├── controller/                   # Controller functions
├── middleware/                   # Validation logic
├── models/                       # Mongoose or mock schemas
├── openapi-models/               # OpenAPI model schemas
├── routes/                       # REST routes per model
├── data/                         # Mock engine only: .json files
├── .env
├── package.json
└── server.js

🧪 Example Schema

{
  "user": {
    "type": "object",
    "properties": {
      "id": {
        "type": "uuid"
      },
      "username": {
        "type": "string",
        "required": true,
        "unique": true,
        "minLength": 3,
        "maxLength": 20
      },
      "email": {
        "type": "email",
        "required": true,
        "unique": true
      },
      "age": {
        "type": "integer",
        "minimum": 18,
        "maximum": 100
      },
      "isActive": {
        "type": "boolean"
      },
      "role": {
        "type": "string",
        "enum": ["user", "admin", "moderator"]
      },
      "website": {
        "type": "url"
      },
      "bio": {
        "type": "string",
        "maxLength": 500
      },
      "tags": {
        "type": "array",
        "items": {
          "type": "string"
        },
        "minItems": 1,
        "maxItems": 5
      },
      "address": {
        "type": "object",
        "properties": {
          "street": {
            "type": "string"
          },
          "city": {
            "type": "string"
          },
          "postalCode": {
            "type": "string",
            "pattern": "^[0-9]{5}$"
          }
        }
      },
      "createdAt": {
        "type": "date"
      }
    }
  }
}

🔧 Supported Types & Validation Rules

| Feature | Supported | Notes | | ------------------------ | --------- | --------------------------------------------------------------------- | | type | ✅ | string, number, boolean, integer, email, uuid, url, date, object, ref | | required | ✅ | Explicitly mark fields as required | | minLength, maxLength | ✅ | Only for strings | | minimum, maximum | ✅ | Only for numbers | | pattern | ✅ | Use string-form regex (e.g. "^\\d+$") | | enum | ✅ | Array of allowed values | | unique | ✅ | Works in both mock and mongo engines | | default | ✅ | Optional default values | | minItems, maxItems | ✅ | For arrays | | timestamps | ✅ | Adds createdAt and updatedAt automatically |


🚀 Getting Started

Got it! Here’s a clear, step-by-step Usage & Installation guide that explains how users can clone your repo, link locally, or install from npm and generate APIs:


🚀 How to Use cfapi

You can use cfapi either by installing it globally from npm or by cloning and linking the repository locally for development.

Option 1: Install from npm (recommended)

npm install -g @bharathsj/cfapi

Once installed globally, generate your API from a schema:

cfapi -t generate -s ./schema.json -o ./my-api -e mongo
cd my-api
npm install
npm start

Option 2: Clone & link locally (for development)

If you want to use or modify the source code directly:

git clone https://github.com/Bharath-S-J/cfapi.git
cd cfapi
chmod +x bin/index.js
npm install
npm link

This links the cfapi command globally to your local code.

Now you can run the CLI as if installed globally:

cfapi -t generate -s ./schema.json -o ./my-api -e mock
cd my-api
npm install
npm start

🛠 Example: Complex Schema

{
  "user": {
    "type": "object",
    "timestamps": true,
    "properties": {
      "username": { "type": "string", "minLength": 3, "unique": true },
      "email": { "type": "email", "required": true, "unique": true },
      "age": { "type": "integer", "minimum": 18 },
      "status": { "type": "string", "enum": ["active", "inactive", "pending"] },
      "password": { "type": "string", "pattern": "^(?=.*\\d).{8,}$" },
      "profile": {
        "type": "object",
        "properties": {
          "bio": "string",
          "dob": "date",
          "social": {
            "type": "object",
            "properties": {
              "twitter": "string",
              "linkedin": "string"
            }
          }
        }
      },
      "roles": {
        "type": "array",
        "items": { "type": "string" },
        "minItems": 1
      },
      "company": { "type": "ref", "model": "company" }
    }
  },

  "company": {
    "type": "object",
    "name": { "type": "string", "required": true },
    "website": { "type": "url" },
    "location": {
      "type": "object",
      "properties": {
        "city": "string",
        "country": "string"
      }
    }
  }
}

📚 Help Output

cfapi --help
Usage:
  cfapi --type <generate|add> --schema <path> --output <dir> --engine <mock|mongo>

Options:
  --type, -t     Operation type: "generate" or "add"
  --schema, -s   Path to schema JSON file
  --output, -o   Output directory
  --engine, -e   Engine to use: "mock" or "mongo"
  --help, -h     Show help

Examples:
  cfapi -t generate -s ./schema.json -o ./my-api -e mock
  cfapi -t add -s ./posts.json -o ./my-api -e mongo

🙌 Final Note

This is a learning prototype — designed to explore how far you can go with schema-driven API generation. Feedback, PRs, or issues are all welcome!


📦 Links