1.0.4 ⢠Published 4 months ago
auto-typegen v1.0.4
auto-typegen š
Automatically generate TypeScript interfaces from JSON data, API responses, or database schemas. Perfect for Mongoose, Sequelize, and raw data. Simplify your workflow and ensure type safety with just one function call!
Features āØ
- Instant Type Generation: Convert JSON/API responses to TypeScript interfaces in one call.
- ORM Support: Works seamlessly with Mongoose and Sequelize.
- Universal: Runs in Node.js (writes files) and browsers (triggers downloads).
- Zero Config: Simple API with smart defaults.
- Nested Objects: Handles complex data structures effortlessly.
Installation š»
npm install auto-typegen
# or
yarn add auto-typegen
Usage š
1. In React āļø
import { createTypedFetch } from 'auto-typegen';
const fetchTodo = async () => {
const res = await fetch('https://api.example.com/todos/1');
const todo = await res.json();
await createTypedFetch({
interfaceName: 'Todo',
data: todo,
outputPath: 'TodoType.ts',
});
};
Browser Behavior: Triggers a download of TodoType.ts
with:
interface Todo {
userId: number;
id: number;
title: string;
completed: boolean;
}
export { Todo };
2. Basic Example (Node.js)
import { createTypedFetch } from 'auto-typegen';
const user = {
id: 1,
name: 'Alice',
email: 'alice@example.com',
roles: ['admin', 'user'],
};
createTypedFetch({
interfaceName: 'User',
data: user,
outputPath: './types/user-types.ts',
});
Generated File (user-types.ts):
interface User {
id: number;
name: string;
email: string;
roles: string[];
}
export { User };
3. With Mongoose š
import { createTypedFetch } from 'auto-typegen';
import UserModel from './models/User';
const user = await UserModel.findOne({ email: 'test@example.com' });
createTypedFetch({
interfaceName: 'User',
data: user,
outputPath: './types/mongoose-user.ts',
});
Output:
interface User {
_id: string;
email: string;
createdAt: Date;
updatedAt: Date;
__v: number;
}
export { User };
4. With Sequelize šļø
import { createTypedFetch } from 'auto-typegen';
import Product from './models/Product';
const product = await Product.findByPk(123);
createTypedFetch({
interfaceName: 'Product',
data: product,
outputPath: './types/sequelize-product.ts',
});
Output:
interface Product {
id: number;
name: string;
price: number;
createdAt: Date;
updatedAt: Date;
}
export { Product };
5. With Raw SQL Data š¬
import { createTypedFetch } from 'auto-typegen';
import { executeQuery } from './database';
const results = await executeQuery('SELECT * FROM orders WHERE user_id = 456');
createTypedFetch({
interfaceName: 'Order',
data: results[0],
outputPath: './types/sql-order.ts',
});
Output:
interface Order {
id: number;
user_id: number;
total: number;
created_at: Date;
}
export { Order };
API š
createTypedFetch(options)
Generates and saves TypeScript interfaces.
Options
Parameter | Type | Required | Description |
---|---|---|---|
interfaceName | string | Yes | Name for the root interface (e.g., "User") |
data | any | Yes | Data object to analyze |
outputPath | string | Yes | File path to save interfaces (e.g., "./types/user.ts") |
FAQ ā
Q: How do I handle circular references?
A: The library automatically detects and handles circular references by using any
for the offending fields.
Q: Can I customize date handling?
A: Dates are always typed as Date
. For custom formatting, transform your data first.
Q: Does it work with Next.js/Nuxt.js? A: Yes! Works in any Node.js or browser environment.
Contributing š¤
Found a bug? Want a feature?
- Fork the repo ā
git clone your-fork-url
- Create a branch ā
git checkout -b cool-feature
- Commit changes ā
git commit -m "Add cool feature"
- Push ā
git push origin cool-feature
- Open a PR!
License š
MIT Ā© GAZI ASSADUJJAMAN MAMUN
ā Star this repo if you love type-safe coding! š