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

chatgpt-dto

v0.1.0

Published

A lightweight wrapper for OpenAI's GPT API with DTO support via class-validator and class-transformer. Designed for structured JSON output in TypeScript backends.

Readme

🧠 chatgpt-dto - DTO-first GPT Integration

A lightweight wrapper around the OpenAI GPT API with first-class support for DTO-based JSON output validation using class-validator and class-transformer. Ideal for TypeScript-based server applications (e.g., NestJS, routing-controllers) that require structured and type-safe responses from ChatGPT.


✨ Features

  • ✅ Seamless integration with class-validator & class-transformer
  • 🧩 Converts DTOs to JSON Schema using class-validator-jsonschema
  • 🧠 Utilizes OpenAI's JSON Schema output format (response_format: json_schema)
  • 🔄 Automatically parses and validates GPT responses into typed DTO instances
  • ⚙️ Minimal setup, with lazy singleton provider initialization
  • 🪄 Supports $ref schema expansion for complex/nested DTOs

📦 Installation

npm install chatgpt-dto

🚀 Quick Start

import { getGPTProvider } from "chatgpt-dto";
import { IsString, IsEmail } from "class-validator";
import { Expose } from "class-transformer";

class UserDTO {
  @IsString()
  @Expose()
  name: string;

  @IsEmail()
  @Expose()
  email: string;
}

async function run() {
  const gpt = getGPTProvider({
    apiKey: "your-openai-api-key", // Required
    model: "gpt-4o-mini",          // Optional (default: gpt-4o-mini)
  });

  const response = await gpt.jsonDTO("Generate a random user", UserDTO);
  console.log(response?.result); // typed UserDTO instance
}

🧩 API

getGPTProvider(options?: PluginOptions): GPTProvider

Initializes (once) and returns a GPTProvider instance.

Options

| Option | Type | Required | Description | |----------|----------|----------|--------------------------------------| | apiKey | string | ✅ | Your OpenAI API key | | model | string | ❌ | Model name (default: gpt-4o-mini) |


gpt.jsonDTO<T>(prompt: string, dtoClass: new () => T, model?: string): Promise<GptResponse<T> | null>

Sends a prompt and expects a structured JSON response that matches the given DTO class.

  • Automatically generates JSON Schema from the DTO
  • Expands $ref properties for compatibility with OpenAI

Returns:

type GptResponse<T> = {
  result: T; // typed DTO instance
  usage: OpenAI.Completions.CompletionUsage;
  message: OpenAI.Chat.Completions.ChatCompletionMessage;
}

gpt.chat(prompt: string, model?: string): Promise<GptResponse<string> | null>

Standard ChatGPT interaction that returns a raw string message.


gpt.chatString(prompt: string, model?: string): Promise<string>

Convenience wrapper for simple string responses.


gpt.JsonString<T>(prompt: string, jsonSchema: any, model?: string): Promise<T | null>

Advanced method for validating GPT output against a manually provided JSON Schema.


📘 Use Case: Schema Expansion with $ref

This plugin automatically expands $ref references in generated schemas. That means you can safely use nested DTOs, and the schema sent to OpenAI will be fully inlined and self-contained.


📌 Requirements

  • Node.js >= 18
  • OpenAI API Key
  • DTOs must use class-validator decorators for schema generation
  • reflect-metadata must be imported globally
import "reflect-metadata";

📄 License

MIT © unbywyd