An open API service indexing awesome lists of open source software.

https://github.com/bharath-s-j/cfapi

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.
https://github.com/bharath-s-j/cfapi

automation cfapi express json-schema mongoose nodejs npm openapi3 restful-api

Last synced: 7 months ago
JSON representation

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.

Awesome Lists containing this project

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

```bash
cfapi --type --schema --output --engine
```

### πŸ”¨ Generate Full API

```bash
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)

```bash
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
```bash
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](https://github.com/Bharath-S-J/cfapi/blob/main/images/mock.png?raw=true) | ![Mongo Output](https://github.com/Bharath-S-J/cfapi/blob/main/images/mongo.png?raw=true) |

- πŸ”— [`sample-schema1-mock`](https://github.com/Bharath-S-J/cfapi/tree/main/example-outputs/sample-schema1-mock)
- πŸ”— [`sample-schema1-mongo`](https://github.com/Bharath-S-J/cfapi/tree/main/example-outputs/sample-schema1-mongo)

---

### 🏒 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`](https://github.com/Bharath-S-J/cfapi/tree/main/example-outputs/sample-schema2-mock)
- πŸ”— [`sample-schema2-mongo`](https://github.com/Bharath-S-J/cfapi/tree/main/example-outputs/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

```json
{
"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)

```bash
npm install -g @bharathsj/cfapi
```

Once installed globally, generate your API from a schema:

```bash
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:

```bash
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:

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

---

## πŸ›  Example: Complex Schema

```json
{
"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

```bash
cfapi --help
```

```
Usage:
cfapi --type --schema --output --engine

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

- πŸ”— GitHub: [https://github.com/Bharath-S-J/cfapi.git](https://github.com/Bharath-S-J/cfapi.git)
- πŸ“¦ npm: [https://npmjs.com/package/@bharathsj/cfapi](https://npmjs.com/package/@bharathsj/cfapi)