Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@devexperts/swagger-codegen-ts

Package Overview
Dependencies
Maintainers
11
Versions
52
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@devexperts/swagger-codegen-ts

TS generator for swagger spec

  • 2.0.0-alpha.29
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7K
decreased by-0.18%
Maintainers
11
Weekly downloads
 
Created
Source

Build Status

Typesafe OpenAPI generator for TypeScript

Features

  • Generates client code from OpenAPI 3.0, 2.0 (aka Swagger) and AsyncAPI specs
  • Pluggable HTTP clients: can use fetch, Axios or any other library
  • Flexible response types: works with Promises and reactive streams like RxJS
  • Runtime type checks: validates server responses against the spec
  • Written in pure TypeScript using fp-ts and io-ts libraries

Demo code

The examples below refer to the Pet Store OpenAPI 3.0 schema.

After running the codegen, interacting with a REST API may be as simple as this:

import { petController as createPetController } from "./src/generated/petstore.json/paths/PetController";
import { Pet } from "./src/generated/petstore.json/components/schemas/Pet";

// Creating a controller, see the "HTTP Clients" wiki page for more details
const petController = createPetController({ httpClient: fetchHttpClient });

// The returned object is guaranteed to be a valid `Pet`
const createdPet: Promise<Pet> = petController.addPet({
  body: {
    // The parameters are statically typed, IntelliSense works, too
    name: "Spotty",
    photoUrls: [],
  },
});

More usage scenarios are supported - check the usage page for more detail.

Installation

  1. Make sure the peer dependencies are installed, then install the codegen itself:

    yarn add typescript fp-ts io-ts io-ts-types
    yarn add -D @devexperts/swagger-codegen-ts
    
  2. Create a console script that would invoke the generate function, passing the options such as path to the schema file and the output directory. See the Generators page for the API reference, and examples/generate for sample scripts.

  3. In most cases, you might want to include the code generation step into the build and local launch scripts. Example:

    /* package.json */
    
      "scripts": {
    +   "generate:api": "ts-node scripts/generate-api.ts",
    -   "start": "react-scripts start",
    +   "start": "yarn generate:api && react-scripts start",
    -   "build": "react-scripts build"
    +   "build": "yarn generate:api && react-scripts build"
      }
    

Contributing

Please read the Contributors Guide for more information.

FAQs

Package last updated on 17 Feb 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc