Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate javascript class/objects from a YAML

Is there any tool to generate JavaScript classes from a YAML ?

I have a swagger YAML and I want to generate javascript models that look like this :

'use strict';

function Product(name,description){
    this.name = name;
    this.description = description;
    this.toString = function(){
        return this.name+" : "+this.description;
}

}

using the swagger YAML :

.
.
.
definitions:
  product:
properties:
  name:
    type: string
  description:
    type: string
required:
  - name
  - description
.
.
.
like image 618
walox Avatar asked Nov 09 '22 05:11

walox


1 Answers

You can use Swagger Codegen to generate JS API clients (with models), clients in other langauges (e.g. Ruby, Typescript, etc), server stubs (e.g. NodeJS, Java Spring, etc) and API documentation.

Given that you already have the OpenAPI/Swagger spec, you can do the following:

  1. Go to https://editor.swagger.io
  2. Select "File" in the top menu, then "Import File" to load your spec
  3. Then select "Generate Client" in the top menu and click on "Javascript"
like image 87
William Cheng Avatar answered Nov 14 '22 22:11

William Cheng