Code Monkey home page Code Monkey logo

swag-ts's Introduction

swag-ts

Go Report Card

Simply provide a OpenAPI Specification and swag-ts will generate typescript types for you. You can provide json or yaml definitions on your local filesystem or a remote url.

Motivation

Why another type generator for OpenAPI (Swagger)? Well it's because I could not find a generator that only generates typescript types. Most generators also generate runtime code which I don't necessarily need. I just want to have the typescript types to use them in my frontend application.

If thats something for you, feel free to use it. If you need more functionality, feel free to open an issue or a pull request.

Installation

go install github.com/ditschedev/swag-ts@latest

Usage

Usage:
  swag-ts [flags]

Flags:
  -f, --file string     file path or url to the OpenAPI Specification
  -h, --help            help for swag-ts
  -o, --output string   output file for generated definitions (default "./types/swagger.ts")
  -v, --version         shows the version of the cli

Format

This library aims to only provide typescript type definitions from a given OpenAPI Specification. It does not provide any runtime functionality. All types are exported as interface.

For example, the following Schema:

LoginResponse:
  required:
    - token
  type: object
  properties:
    token:
      minLength: 1
      type: string
  additionalProperties: false

LoginResponseWrapper:
  required:
    - data
  type: object
  properties:
    data:
      $ref: '#/components/schemas/LoginResponse'
    message:
      type: string
      nullable: true
  additionalProperties: false

will be converted to the following typescript definitions:

export interface LoginResponse {
  token: string;
}

export interface LoginResponseWrapper {
  data: LoginResponse;
  message?: string | null;
}

Enums

Enums are converted to typescript enums. See the example below:

Car:
  required:
    - manufacturer
  type: object
  properties:
    manufacturer:
      $ref: "#/components/schemas/CarManufacturer"
CarManufacturer:
  type: string
  enum:
    - BMW
    - Mercedes
    - Audi  

will be converted to the following typescript definitions:

export interface Car {
    manufacturer: CarManufacturer;
}

export enum CarManufacturer {
    BMW = "BMW",
    Mercedes = "Mercedes",
    Audi = "Audi",
}

FormData Requests

If you have a request with multipart/form-data content type the cli will generate a type definition as well. As for most OpenAPI Specs the schema of the form data will not be added to the schemas section of the definition itself, rather than in the requestBody section of the path.

The generated type will be named after the operation id with a suffix of FormData. For converting this type to a FormData object you can use the convertToFormData function from the generated file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.