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

create-swagger-client

v0.1.7

Published

Generate fully type-safe REST API clients from OpenAPI/Swagger specifications

Readme

Create Swagger Client

A TypeScript tool that generates a fully type-safe REST API client from OpenAPI/Swagger specifications. Built with openapi-typescript and ts-morph, it creates a strongly-typed client class with autocomplete and compile-time type checking for all API endpoints.

Features

  • Full Type Safety: All endpoints, parameters, request bodies, and responses are type-checked
  • 🚀 Auto-completion: IDE autocomplete for paths, methods, and payloads
  • 🔄 Multiple HTTP Methods: Support for GET, POST, PUT, DELETE, and PATCH
  • 📝 OpenAPI Spec Support: Works with OpenAPI 3.x specifications (JSON or YAML)
  • 🌐 URL or File Input: Generate from remote URLs or local files
  • 🎯 Type Inference: Automatic extraction of path params, query params, headers, and request/response types
  • ⏱️ Built-in Timeout: Default 30s timeout per request (configurable)

Installation

npm install -g create-swagger-client
# or
npx create-swagger-client

Usage

Generate API Client

Run the generator with your OpenAPI specification:

# From a URL
npx create-swagger-client https://api.example.com/openapi.json

# From a local file
npx create-swagger-client ./swagger.json

# Specify custom output file
npx create-swagger-client https://api.example.com/openapi.json my-api-client.ts

Arguments:

  • source (required): URL or file path to your OpenAPI/Swagger specification
  • output (optional): Output file name (default: swagger-client.ts)

This will generate a file with:

  • All TypeScript types from your OpenAPI spec
  • A RestApiClient class with type-safe methods
  • Helper types for extracting parameters and responses

Using the Generated Client

After generation, import and use the client:

import { RestApiClient } from './swagger-client';

// Initialize the client
const api = new RestApiClient('https://api.example.com', {
  headers: {
    'Authorization': 'Bearer YOUR_TOKEN'
  }
});

// Make type-safe requests
// GET request with query parameters
const users = await api.get('/users', {
  query: { page: 1, limit: 10 }
});

// GET request with path parameters
const user = await api.get('/users/{id}', {
  path: { id: '123' }
});

// POST request with body
const newUser = await api.post('/users', {
  body: {
    name: 'John Doe',
    email: '[email protected]'
  }
});

// PUT request
const updatedUser = await api.put('/users/{id}', {
  path: { id: '123' },
  body: {
    name: 'Jane Doe',
    email: '[email protected]'
  }
});

// DELETE request
await api.delete('/users/{id}', {
  path: { id: '123' }
});

// PATCH request
const patchedUser = await api.patch('/users/{id}', {
  path: { id: '123' },
  body: {
    email: '[email protected]'
  }
});

Advanced Usage

Custom Headers per Request

const data = await api.get('/protected-endpoint', {
  headers: {
    'X-Custom-Header': 'value'
  }
});

Request with Multiple Parameter Types

const result = await api.post('/projects/{projectId}/tasks', {
  path: { projectId: 'proj-123' },
  query: { notify: true },
  headers: { 'X-Request-ID': 'req-456' },
  body: {
    title: 'New Task',
    description: 'Task description'
  }
});

Custom Fetch Options

Each method accepts an optional RequestInit as the third argument:

const users = await api.get('/users', { query: { page: 1 } }, {
  credentials: 'include',
  mode: 'cors'
});

Timeout

The client uses a default timeout of 30 seconds. You can override it in the constructor:

const api = new RestApiClient('https://api.example.com', {}, 10_000);

Generated Types

The generator creates several useful type utilities:

  • RestMethod: Union of HTTP methods ("get" | "post" | "put" | "delete" | "patch")
  • KeyPaths: All available API paths
  • PathsForMethod<M>: Paths that support method M
  • ExtractPathParams<Path, Method>: Extract path parameters for an endpoint
  • ExtractQueryParams<Path, Method>: Extract query parameters for an endpoint
  • ExtractHeaderParams<Path, Method>: Extract header parameters for an endpoint
  • ExtractBody<Path, Method>: Extract request body type for an endpoint
  • APIResponse<Path, Method>: Extract response type for an endpoint
  • ApiPayload<Path, Method>: Combined payload type for a request
  • ApiClientType: Type definition for the entire client

Example: Using Generated Types

import { 
  RestApiClient, 
  ExtractBody, 
  APIResponse 
} from './swagger-client';

// Use types in your code
type CreateUserBody = ExtractBody<'/users', 'post'>;
type UserResponse = APIResponse<'/users/{id}', 'get'>;

const createUser = (data: CreateUserBody): Promise<UserResponse> => {
  return api.post('/users', { body: data });
};

Error Handling

The client throws an ApiError for failed requests. The error includes status, statusText, and a parsed JSON body when available:

try {
  const user = await api.get('/users/{id}', {
    path: { id: '123' }
  });
} catch (error) {
  console.error('API request failed:', error.message);
  // error.status, error.statusText, error.body
}

Requirements

  • Node.js 16+ (for running the CLI)
  • TypeScript 5.x (peer dependency for generated types)

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.