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 🙏

© 2024 – Pkg Stats / Ryan Hefner

api-typescript-generator

v2.4.24

Published

Generates OpenAPI TypeScript client. Extremely fast and flexible.

Downloads

90

Readme

OpenAPI Typescript Client Generator

Generates OpenAPI client for TypeScript. Extremely configurable.

Features

  1. Generates TypeScript models for all the schemas in the OpenAPI document in a form of interfaces and type aliases.
  2. Generates TypeScript services for all the operations in the OpenAPI document.
  3. Generates a client class that combines all the services.
  4. Uses fetch API for making HTTP requests by default, but can be configured to use any other HTTP client.
  5. May generate validation for the API responses if configured.

Setup

Install using npm

npm add --save-dev api-typescript-generator 

Or using yarn

yarn add --dev api-typescript-generator

Configuring

Create a api-typescript-generator.config.ts file in the root of your project.

import path from 'path';
import {ApiTypescriptGeneratorConfig} from 'api-typescript-generator';

const configuration: ApiTypescriptGeneratorConfig = {
    generates: [
        {
            type: 'openapiClient',
            document: {
                // The source of the OpenAPI document.
                // Can also be { type: 'file', path: 'path/to/file.yaml' }
                // Both YAML and JSON formats are supported.
                source: {
                    type: 'url',
                    url: 'https://raw.githubusercontent.com/readmeio/oas-examples/main/3.1/yaml/petstore.yaml'
                }
            },
            // The output directory for the generated client.
            outputDirPath: path.join(__dirname, 'petstore-api-client'),
            client: {
                // The name of the generated client class.
                name: 'PetStoreApiClient',
                // Overrides the default base URL for the API.
                baseUrl: 'https://petstore.swagger.io/v2'
            }
        }
    ]
};

export default configuration;

Add the following script to your package.json:

{
  // ...
  "scripts": {
    // ...
    "openapi-codegen": "api-typescript-generator generate api-typescript-generator.config.ts"
  }
}

Run the script:

npm run openapi-codegen

Or using yarn:

yarn openapi-codegen

By default it generates:

  1. Models for all the schemas in the OpenAPI document. Stored at models directory by default.
  2. Services for all the operations in the OpenAPI document. Stored at services directory by default.
  3. A client class that combines all the services. Stored at the root of the output directory by default.
  4. Core classes for handling HTTP requests and responses. Stored at core directory by default.

Usage

The generated client can be used as follows:

import {PetStoreApiClient} from './petstore-api-client';

async function testApiClient() {
    const apiClient = new PetStoreApiClient();
    console.log(await client.store.getInventory());
}

testApiClient().catch(console.error);

What is configurable?

  1. Validation of the API responses. See validation.
  2. Default base URL for the API. See client.baseUrl.
  3. Leading and trailing comments for the files. See comments.
  4. File naming conventions. I.e. models.filenameFormat.
  5. Output directory structure. I.e. models.relativeDirPath.
  6. JSDoc generation. I.e. models.generateJsDoc.
  7. Many more. See the documentation below.

Documentation

The most important interface for now would be the OpenApiClientGeneratorConfig interface. It contains all the configuration options for the OpenAPI Client Generator.

Types are exported as part of three modules, depending on the area of application:

  1. api-typescript-generator - The main module that exports the common API Generator Configuration types.
  2. api-typescript-generator/openapi-client - The module that exports the OpenAPI Client Generator Configuration types.
  3. api-typescript-generator/openapi - The module that exports the OpenAPI Document types.

At the moment only types are exported to be used with CLI. Callable API is planned for the future.

Collaborators

References

  1. OpenAPI Specification
  2. JSON Schema