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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@gordela/api-generator

v1.2.0

Published

TypeScript API generator from Swagger/OpenAPI specifications with environment variable support

Readme

@gordela/api-generator

A powerful TypeScript API client generator that creates type-safe interfaces and API methods from Swagger/OpenAPI specifications with full environment variable support.

Features

  • 🚀 TypeScript-first: Generates fully typed interfaces and API methods
  • 🌍 Environment Variables: Full support for configuration via .env files
  • 🔧 Flexible Configuration: Multiple ways to configure (env vars, config files, CLI args)
  • 📦 Production Ready: Proper error handling, logging, and robust code generation
  • 🎯 Smart Naming: Automatic conversion to PascalCase interfaces and camelCase methods
  • Fast & Reliable: Efficient parsing and generation with axios and prettier
  • 🛠 CLI & Programmatic: Use as CLI tool or import as a library

Installation

npm install -g @gordela/api-generator
# or
npm install --save-dev @gordela/api-generator

Quick Start

1. Environment Variables (Recommended)

Create a .env file in your project root:

# Required
API_URL=https://api.sprtverse.com
SWAGGER_PATH=/docs?api-docs.json

# Optional
API_NAMESPACE=MyApi
OUTPUT_DIR=./src/api/generated

Then run:

gordela-api-gen
# or
npx gordela-api-gen

2. CLI Arguments

gordela-api-gen --api-url https://api.example.com --output-dir ./api

3. Configuration File

Create api-generator.config.js:

module.exports = {
  apiUrl: 'https://api.sprtverse.com',
  swaggerPath: '/docs?api-docs.json',
  outputDir: './src/api/generated',
  namespace: 'SportverseApi',
};

4. Programmatic Usage

import { ApiGenerator } from '@gordela/api-generator';

const generator = new ApiGenerator({
  apiUrl: 'https://api.example.com',
  swaggerPath: '/openapi.json',
  outputDir: './generated',
  namespace: 'MyApi',
});

await generator.generate();

Configuration Options

Environment Variables

| Variable | Description | Default | |----------|-------------|---------| | API_URL | Base URL of your API | https://api.sprtverse.com | | SWAGGER_PATH | Path to OpenAPI/Swagger JSON | /docs?api-docs.json | | OUTPUT_DIR | Directory for generated files | api/generated | | API_NAMESPACE | Namespace for generated code | ApiClient |

CLI Options

gordela-api-gen [options]

Options:
  -u, --api-url <url>        API base URL
  -s, --swagger-path <path>  Swagger/OpenAPI path  
  -o, --output-dir <dir>     Output directory
  -n, --namespace <name>     API namespace
  -c, --config <file>        Config file path
  -h, --help                 Show help
  -v, --version              Show version

Configuration File

Create api-generator.config.js in your project root:

module.exports = {
  apiUrl: 'https://api.example.com',
  swaggerPath: '/openapi.json',
  outputDir: './src/api/generated',
  namespace: 'MyApi',
};

Generated Files

The generator creates three files in your output directory:

interfaces.ts

Type-safe TypeScript interfaces for all API models:

export interface Player {
  id: number;
  name: string;
  position?: string;
}

export interface Team {
  id: number;
  name: string;
  players: Player[];
}

api.ts

API methods with proper typing:

export async function getPlayers(): Promise<Player[]> {
  const response = await axios.get('/players');
  return response.data;
}

export async function createPlayer(data: Player): Promise<Player> {
  const response = await axios.post('/players', data);
  return response.data;
}

index.ts

Convenient re-exports:

export * from './interfaces';
export * from './api';

Usage Example

After generation, use your API client:

import { getPlayers, createPlayer, Player } from './api/generated';

// Fetch players
const players = await getPlayers();

// Create a new player
const newPlayer: Player = {
  id: 1,
  name: 'John Doe',
  position: 'Forward'
};

const createdPlayer = await createPlayer(newPlayer);

Integration with Axios

The generated API methods use a relative import to ../axios, so create your axios configuration:

// api/axios.ts
import axios from 'axios';

const instance = axios.create({
  baseURL: process.env.API_URL || 'https://api.sprtverse.com',
  timeout: 10000,
});

// Add interceptors for auth, error handling, etc.
instance.interceptors.request.use((config) => {
  const token = getAuthToken();
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

export default instance;

Advanced Configuration

Custom Headers for Swagger Fetching

If your OpenAPI spec requires authentication:

# .env
SWAGGER_AUTH_HEADER=Bearer your-token-here
SWAGGER_API_KEY=your-api-key-here

Multiple APIs

Generate clients for multiple APIs:

// multi-api.config.js
module.exports = [
  {
    apiUrl: 'https://api.users.com',
    swaggerPath: '/openapi.json',
    outputDir: './src/api/users',
    namespace: 'UsersApi',
  },
  {
    apiUrl: 'https://api.orders.com', 
    swaggerPath: '/docs/json',
    outputDir: './src/api/orders',
    namespace: 'OrdersApi',
  }
];

NPM Scripts Integration

Add to your package.json:

{
  "scripts": {
    "generate-api": "sportverse-api-gen",
    "build": "npm run generate-api && tsc",
    "dev": "npm run generate-api && npm run start:dev"
  }
}

Error Handling

The generator provides detailed error messages:

  • ✅ Successful operations with clear feedback
  • ⚠️ Warnings for skipped or invalid types
  • ❌ Clear error messages with response details
  • 🔧 Configuration validation and helpful hints

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License - see the LICENSE file for details.

Support


Made with ❤️ by the Sportverse Team