1.0.3 • Published 1 year ago

ts-from-swagger v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

:chopsticks: ts-from-swagger

  • :date:03-15-2023 :pushpin:Beta Version 1.0.2
  • :computer:NajibRachid :purple_circle:ANMOON :office: XHUB

Generate TypeScript interfaces and enums quickly and easily from Swagger definition JSON with our lightweight tool. Improve your development workflow by automatically generating type-safe code that's easy to maintain and understand. Say goodbye to manual coding and hello to streamlined development with our Swagger definition Typescript generator.

keywords: ts-from-swagger, swagger2ts, typescript generator, type-safe, type-safe rest Apis, type safety, TypeScript interfaces generator, Swagger to TypeScript converter, NPM package for generating TypeScript interfaces from Swagger, TypeScript definitions from Swagger, Swagger JSON to TypeScript, TypeScript code from Swagger schema, TypeScript typings from Swagger definition, Swagger interface to TypeScript conversion, TypeScript enum generator from Swagger JSON, TypeScript code generation from Swagger definition., Convert Swagger JSON to TypeScript code, tsgen.


Features

  • Lightweight, & No dependencies.
  • Simple & Easy to use.
  • Fetch directly from Swagger Schema URL.
  • Support interfaces and enums.
  • Support required fields.
  • Respects naming conventions.
  • Authorization for Swagger.
  • Supports both OpenAPI versions 2 & 3.
  • Supports both JSON & YML.

Install

#npm
npm i ts-from-swagger --save-dev

#yarn
yarn add ts-from-swagger --dev

Usage

Specify your swagger JSON url and followed by output directory(default is: ./src/definitions)

npx ts-from-swagger https://petstore.swagger.io/v2/swagger.json ./path/to/output/dir

Or add a script to your package.json file:

  "scripts": {
    "ts-from-swagger": "npx ts-from-swagger https://petstore.swagger.io/v2/swagger.json ./path/to/output/dir"
  }

Then run the script with npm run:

npm run ts-from-swagger

Demo

Write types safe code from your frontend to your backend API, backend agnostic as long as you use Swagger.

import axios from "axios";
import { PetStatus } from "./definitions/enums/PetStatus.enum";
import { Category } from "./definitions/interfaces/Category.interface";
import { Pet } from "./definitions/interfaces/Pet.interface";
import { Tag } from "./definitions/interfaces/Tag.interface";

const cats: Category = {
  id: 1,
  name: "Cats",
};

const friendly: Tag = {
  id: 1,
  name: "friendly",
};

const mellow: Tag = {
  id: 2,
  name: "mellow",
};

const newPet: Pet = {
  id: 123,
  name: "Mischievous",
  category: cats,
  // photoUrls: ["https://picsum.photos/200"], // not required
  tags: [friendly, mellow],
  status: PetStatus.AVAILABLE,
};

axios
  .post("https://petstore.swagger.io/v2/pet", newPet)
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.error(error);
  });

Releases

  - Initial Version 1.0.0 : 11/03/2023
    - Project Setup.
    - Theory & prove of concept.
  - Alpha Version 1.0.1 : 13/03/2023
    - CLI configuration.
    - Generate Enums.
    - Fix Wrong type for arrays.
    - Fix OutputPath make recursive.
  - Beta Version 1.0.2 : 15/03/2023
    - Fix Imports.
    - Make Subfolders.
    - Respect naming conventions.
    - Refactor Code.
  - Beta Version 1.0.3 : 17/03/2023
    - Add simple CLI logs.
    - Fix run npx: node v14.21.1 / npm v6.14.17
  - [Agenda] Beta Version 1.0.x : xx/03/2023
    - Add Authorization.
    - Add support for Swagger version 3.
    - Add support for YML.
    - Fix issue: npx command on later node/npm versions.

Contributing

Contributions are welcome! If you find a bug or have a feature request, please open an issue on the GitHub repository.

Tests included:

   npm test

License

This project is licensed under the MIT License. See the LICENSE file for more information. Feel free to use it in your own projects, or contribute to its development on GitHub.