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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@innv/ts-typeforge

v0.1.4

Published

Criação automática de mocks de DTOs e tipos complexos em TypeScript usando ts-morph e Faker.

Downloads

17

Readme

@innv/ts-typeforge

NPM Version CI Status Test Coverage License: MIT

Crie mocks de DTOs e tipos complexos em TypeScript sem esforço. ts-typeforge lê seus tipos estaticamente e usa o Faker.js para gerar dados realistas e com tipagem correta.

Cansado de escrever manualmente mocks para seus testes unitários e e2e?

// O jeito difícil...
const mockUser = {
    id: 'uuid-aleatorio',
    name: 'João Silva',
    email: '[email protected]',
    age: 30,
    orders: [
        { sku: 'SKU-123', quantity: 2 },
    ]
};

ts-typeforge automatiza isso.

Features

  • Zero Configuração Aninhada: Cria automaticamente DTOs aninhados (ex: UserDTO tem OrdersDTO[]) sem configuração manual.
  • Inferência Inteligente do Faker: Usa nomes de propriedades (ex: email, uuid, firstName) para escolher o gerador correto do Faker.
  • Suporte a Genéricos: Lida facilmente com tipos como PageableDTO<UserDTO>.
  • Totalmente Configurável: Permite overrides, contagem de arrays (arrayCounts), arrays vazios (emptyArrays) e allowedValues (enums).
  • Seguro para o Desenvolvedor: Lança erros claros se você esquecer de registrar um DTO aninhado.

Instalação

ts-typeforge requer ts-morph, @faker-js/faker e reflect-metadata como peerDependencies.

npm install @innv/ts-typeforge @faker-js/faker ts-morph reflect-metadata

Você também precisará habilitar duas opções no seu tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Uso Rápido (Quick Start)

1. Defina seus DTOs:

// product.dto.ts
export class ProductDTO {
    sku: string;
    productName: string;
}
// user.dto.ts
import { ProductDTO } from './product.dto';

export class UserDTO {
    id: string;
    name: string;
    email: string;
    age: number;
    orders: ProductDTO[]; // Array Aninhado
    favoriteProduct: ProductDTO; // Objeto Aninhado
}

2. Configure a TypeForge (no seu test-setup.ts ou beforeAll):

import { TypeForge, TypeRegistry } from '@innv/ts-typeforge';
import { UserDTO } from './dtos/user.dto';
import { ProductDTO } from './dtos/product.dto';

let forge: TypeForge;
let registry: TypeRegistry;

beforeAll(() => {
    registry = new TypeRegistry();

    // 1. Registre todos os DTOs que a forge pode precisar
    registry.register(UserDTO);
    registry.register(ProductDTO);

    // 2. Crie a forge
    forge = new TypeForge(registry);
});

3. Use nos seus testes:

it('should create a user mock', () => {
    const user = forge.create(UserDTO);

    /*
    user = {
        id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
        name: 'Brenda Smith',
        email: '[email protected]',
        age: 42,
        orders: [
            { sku: 'YDWP21E4', productName: 'dolorem' }
        ],
        favoriteProduct: {
            sku: 'T8S1V9H7',
            productName: 'corporis'
        }
    }
    */

    expect(user.id).toBeString();
    expect(user.orders).toHaveLength(1);
    expect(user.orders[0].sku).toBeString();
});

API Avançada

forge.create(DTO, options)

overrides: Força valores específicos.

const adminUser = forge.create(UserDTO, {
    overrides: {
        name: 'Admin User',
        age: 99,
    },
});

arrayCounts: Controla o tamanho de arrays.

const userWith5Orders = forge.create(UserDTO, {
    arrayCounts: {
        orders: 5, // Cria 5 produtos no array 'orders'
    },
});

emptyArrays: Força um array a ser [].

const userNoOrders = forge.create(UserDTO, {
    emptyArrays: ['orders'],    
});

generics: Resolve tipos genéricos.

// DTO: export class PageableDTO<T> { results: T[] }
import { PageableDTO } from './dtos/pageable.dto';

// NÃO se esqueça de registrar PageableDTO
registry.register(PageableDTO);

const userPage = forge.create<PageableDTO<UserDTO>>(PageableDTO, {
    generics: {
        results: {
            type: UserDTO, // O tipo que 'T' deve assumir
            count: 10,     // Cria 10 usuários
        },
    },
});

// userPage.results terá 10 UserDTOs

registry.register(DTO, options)

allowedValues: Restringe valores (ótimo para Enums).

enum UserStatus { ACTIVE = 'active', INACTIVE = 'inactive' }
// DTO: export class UserDTO { status: UserStatus }

registry.register(UserDTO, {
    allowedValues: {
        status: Object.values(UserStatus),
    },
});

// forge.create(UserDTO).status será 'active' ou 'inactive'

Como Funciona?

ts-typeforge usa ts-morph para ler a estrutura estática do seu DTO (nomes de propriedades e tipos). Quando encontra um tipo aninhado (ex: ProductDTO), ele usa o TypeRegistry para encontrar o construtor ProductDTO real que você registrou no runtime. Isso cria a "ponte" entre a análise estática e a execução em tempo real, permitindo a automação.

Contribuição

Contribuições são muito bem-vindas! Por favor, leia nosso CONTRIBUTING.md para saber como propor correções e novas features.

Licença

Este projeto está licenciado sob a Licença MIT.