1.0.4 • Published 4 months ago

auto-typegen v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

auto-typegen šŸš€

npm version License Downloads

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

ParameterTypeRequiredDescription
interfaceNamestringYesName for the root interface (e.g., "User")
dataanyYesData object to analyze
outputPathstringYesFile 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?

  1. Fork the repo → git clone your-fork-url
  2. Create a branch → git checkout -b cool-feature
  3. Commit changes → git commit -m "Add cool feature"
  4. Push → git push origin cool-feature
  5. Open a PR!

GitHub Repository

License šŸ“œ

MIT Ā© GAZI ASSADUJJAMAN MAMUN

⭐ Star this repo if you love type-safe coding! šŸš€