Skip to content

Transform Swagger JSON Schema into TypeScript interfaces

License

Notifications You must be signed in to change notification settings

danielbonifacio/s2n

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

s2n

The Swagger to Node provides a simple way to create TypeScript interfaces based on swagger documentation JSON.

You can use it to populate some BFF's SDK or just to make API calls simpler, by using it on your front-end project.

The problem

In large scale projects, it is very common the usage of various programming languages/frameworks to expose some service, and the most common way to do the communication of all, it's by expose some REST API with swagger documentation, (which is great!). But swagger just provides us UI or JSON schema validation. No IntelliSense at all. And that's where s2n becomes useful. It will read the Swagger JSON and create a TypeScript interface for you. In 2 seconds

How can be this possible?

Swagger provides us a very nice structured documentation of all of our routes and methods. s2n only parses it and generate TypeScript code based on it. It will use Axios to make the request, so it's nice to know how Axios works.

Limitations

If you're a Windows user, ensure that you're using POSIX directory navigation by using the CLI.

# this will cause some bugs with path
s2n [...args] --definitions-path do\\not\\do\\this

# this is how you should do it
s2n [...args] --defnitions-path do/this/instead

Usage

First, you'll need to install s2n globally on your machine. You can do it with the following command:

npm i -g s2n

After that, you must be able to use the CLI with s2n

TIP: Try to type s2n --help on your terminal, to see all the available documented commands.

Example of usage:

s2n --name Petstore --json-url https://petstore.swagger.io/v2/swagger.json

Required arguments

You must pass two arguments to the CLI, otherwise, it will throw an error. They're:

  • --name: name of the service that will be parsed (will be rendered with this);
  • --json-url: the URL of Swagger documentation JSON. This must be over the HTTP protocol.

Available arguments

  • --name: name of the service that will be parsed (will be rendered with this);
  • --json-url: the URL of Swagger documentation JSON. This must be over the HTTP protocol.
  • --definitions-path -DP: path where all definitions will be rendered (relative to process.cwd());
  • --service-path -SP: path where the service will be rendered (relative to process.cwd()).

Credits

This package was entirely designed and developed by @danielbonifacio.

Thanks to Axios team to provide the HTTP library.

Thanks to pag! for giving me the opportunity to explore this architecture and apply it on a large project.

License

MIT

About

Transform Swagger JSON Schema into TypeScript interfaces

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published