• Stars
    star
    690
  • Rank 65,522 (Top 2 %)
  • Language
    HTML
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Swagger Codegen for typescript, nodejs & angularjs

Swagger to JS & Typescript Codegen

Circle CI NPM version

We are looking for a new maintainer

This project is no longer actively maintained by its creator. Please let us know if you would like to become a maintainer. At the time we wrote this package, the swagger didn't have generators for JavaScript nor TypeScript. Now there are great alternatives of this package available.

This package generates a nodejs, reactjs or angularjs class from a swagger specification file. The code is generated using mustache templates and is quality checked by jshint and beautified by js-beautify.

The typescript generator is based on superagent and can be used for both nodejs and the browser via browserify/webpack.

Installation

npm install swagger-js-codegen

Example

var fs = require('fs');
var CodeGen = require('swagger-js-codegen').CodeGen;

var file = 'swagger/spec.json';
var swagger = JSON.parse(fs.readFileSync(file, 'UTF-8'));
var nodejsSourceCode = CodeGen.getNodeCode({ className: 'Test', swagger: swagger });
var angularjsSourceCode = CodeGen.getAngularCode({ className: 'Test', swagger: swagger });
var reactjsSourceCode = CodeGen.getReactCode({ className: 'Test', swagger: swagger });
var tsSourceCode = CodeGen.getTypescriptCode({ className: 'Test', swagger: swagger, imports: ['../../typings/tsd.d.ts'] });
console.log(nodejsSourceCode);
console.log(angularjsSourceCode);
console.log(reactjsSourceCode);
console.log(tsSourceCode);

Custom template

var source = CodeGen.getCustomCode({
    moduleName: 'Test',
    className: 'Test',
    swagger: swaggerSpec,
    template: {
        class: fs.readFileSync('my-class.mustache', 'utf-8'),
        method: fs.readFileSync('my-method.mustache', 'utf-8'),
        type: fs.readFileSync('my-type.mustache', 'utf-8')
    }
});

Options

In addition to the common options listed below, getCustomCode() requires a template field:

template: { class: "...", method: "..." }

getAngularCode(), getNodeCode(), and getCustomCode() each support the following options:

  moduleName:
    type: string
    description: Your AngularJS module name
  className:
    type: string
  lint:
    type: boolean
    description: whether or not to run jslint on the generated code
  esnext:
    type: boolean
    description: passed through to jslint
  beautify:
    type: boolean
    description: whether or not to beautify the generated code
  mustache:
    type: object
    description: See the 'Custom Mustache Variables' section below
  imports:
    type: array
    description: Typescript definition files to be imported.
  swagger:
    type: object
    required: true
    description: swagger object

Template Variables

The following data are passed to the mustache templates:

isNode:
  type: boolean
isES6:
  type: boolean
description:
  type: string
  description: Provided by your options field: 'swagger.info.description'
isSecure:
  type: boolean
  description: false unless 'swagger.securityDefinitions' is defined
moduleName:
  type: string
  description: Your AngularJS module name - provided by your options field
className:
  type: string
  description: Provided by your options field
domain:
  type: string
  description: If all options defined: swagger.schemes[0] + '://' + swagger.host + swagger.basePath
methods:
  type: array
  items:
    type: object
    properties:
      path:
        type: string
      className:
        type: string
        description: Provided by your options field
      methodName:
        type: string
        description: Generated from the HTTP method and path elements or 'x-swagger-js-method-name' field
      method:
        type: string
        description: 'GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'COPY', 'HEAD', 'OPTIONS', 'LINK', 'UNLIK', 'PURGE', 'LOCK', 'UNLOCK', 'PROPFIND'
        enum:
        - GET
        - POST
        - PUT
        - DELETE
        - PATCH
        - COPY
        - HEAD
        - OPTIONS
        - LINK
        - UNLIK
        - PURGE
        - LOCK
        - UNLOCK
        - PROPFIND
      isGET:
        type: string
        description: true if method === 'GET'
      summary:
        type: string
        description: Provided by the 'description' or 'summary' field in the schema
      externalDocs:
        type: object
        properties:
          url:
            type: string
            description: The URL for the target documentation. Value MUST be in the format of a URL.
            required: true
          description:
            type: string
            description: A short description of the target documentation. GitHub-Markdown syntax can be used for rich text representation.
      isSecure:
        type: boolean
        description: true if the 'security' is defined for the method in the schema
      parameters:
        type: array
        description: Includes all of the properties defined for the parameter in the schema plus:
        items:
          camelCaseName:
            type: string
          isSingleton:
            type: boolean
            description: true if there was only one 'enum' defined for the parameter
          singleton:
            type: string
            description: the one and only 'enum' defined for the parameter (if there is only one)
          isBodyParameter:
            type: boolean
          isPathParameter:
            type: boolean
          isQueryParameter:
            type: boolean
          isPatternType:
            type: boolean
            description: true if *in* is 'query', and 'pattern' is defined
          isHeaderParameter:
            type: boolean
          isFormParameter:
            type: boolean

Custom Mustache Variables

You can also pass in your own variables for the mustache templates by adding a mustache object:

var source = CodeGen.getCustomCode({
    ...
    mustache: {
      foo: 'bar',
      app_build_id: env.BUILD_ID,
      app_version: pkg.version
    }
});

Swagger Extensions

x-proxy-header

Some proxies and application servers inject HTTP headers into the requests. Server-side code may use these fields, but they are not required in the client API.

eg: https://cloud.google.com/appengine/docs/go/requests#Go_Request_headers

  /locations:
    get:
      parameters:
      - name: X-AppEngine-Country
        in: header
        x-proxy-header: true
        type: string
        description: Provided by AppEngine eg - US, AU, GB
      - name: country
        in: query
        type: string
        description: |
          2 character country code.
          If not specified, will default to the country provided in the X-AppEngine-Country header
      ...

Grunt task

There is a grunt task that enables you to integrate the code generation in your development pipeline. This is extremely convenient if your application is using APIs which are documented/specified in the swagger format.

Who is using it?

28.io is using this project to generate their nodejs and angularjs language bindings.

More Repositories

1

can-it-be-done-in-react-native

โš›๏ธ ๐Ÿ“บ Projects from the โ€œCan it be done in React Native?โ€ YouTube series
TypeScript
4,045
star
2

react-native-redash

The React Native Reanimated and Gesture Handler Toolbelt
TypeScript
1,982
star
3

react-native-img-cache

Image Cache for React Native
TypeScript
731
star
4

react-native-expo-image-cache

React Native Image Cache and Progressive Loading based on Expo
TypeScript
672
star
5

react-native-webgpu

React Native implementation of WebGPU using Dawn
TypeScript
407
star
6

remotion-fireship

Fireship video made with React
TypeScript
366
star
7

eslint-config-react-native-wcandillon

ESLint and TypeScript configuration for React Native
JavaScript
251
star
8

react-progressive-image-loading

Progressively load images using a blur effect.
TypeScript
120
star
9

react-native-gestures-and-animations-workshop

TypeScript
117
star
10

react-native-shader

Java
107
star
11

eslint-plugin-reanimated

TypeScript
76
star
12

canvaskit-js

A polyfill of CanvasKit that uses browser APIs
TypeScript
76
star
13

love-death-react

Love, Death & React
TypeScript
73
star
14

eslint-plugin-ts-exports

ESLint wrapper for ts-unused-export
TypeScript
68
star
15

jsoniq

JSONiq Implementation that compiles to JavaScript
TypeScript
63
star
16

xqlint

JSONiq & XQuery Quality Checker
JavaScript
49
star
17

remotion-skia

TypeScript
46
star
18

remotion-skia-tutorial

TypeScript
21
star
19

react-native-static-images

Generate a file for static images for react native projects
JavaScript
21
star
20

react-native-redash-docs

19
star
21

react-skia-experiment

Ruby
16
star
22

grunt-swagger-js-codegen

Grunt Task for Swagger JS Codegen
JavaScript
16
star
23

firebase-bolt-compiler

Compiles Firebase Bolt files to TypeScript, Flow, and more
TypeScript
14
star
24

mkbhd-intro

TypeScript
11
star
25

material-ui-flow

Flow Types for Material UI
JavaScript
9
star
26

react-native-redash-v1-docs

9
star
27

expo-tutorial

TypeScript
8
star
28

react-native-image-viewer

An experiment
TypeScript
7
star
29

courrier

Postman runner that can run requests in parallel
JavaScript
6
star
30

xquery.js

Deprecated use XQLint instead
XQuery
5
star
31

language-jsoniq

JSONiq & XQuery Package for Atom
JavaScript
5
star
32

appjs-workshop-2020

5
star
33

react-native-do-firebase-documentation

4
star
34

react-native-eslint-rules

My ESLint configuration for React Native Projects
JavaScript
4
star
35

react-native-do-documentation

React Native Do Documentation
4
star
36

react-native-init

3
star
37

jsoniq-mongodb-examples

JSONiq Query Examples for MongoDB
JSONiq
3
star
38

grunt-xqlint

Grunt task for XQLint
JavaScript
2
star
39

zorba.js

Zorba Compiled to JavaScript
C++
2
star
40

xqdoc

XQuery Documentation
2
star
41

react-native-do-purple-sky-documentation

React Native Do Purple Sky Documentation
2
star
42

dropbox.xq

Dropbox client library written in XQuery
XQuery
2
star
43

cloud9-xquery

XQuery extension for cloud9 IDE
JavaScript
2
star
44

jsoniq-demo

JSONiq Demo
JSONiq
1
star
45

cms

JSONiq
1
star
46

intro-html

A robot powered training repository ๐Ÿค–
HTML
1
star
47

react-native-ting-documentation

React Native Ting Documentation
1
star
48

cloud9.xq

XQuery Backend for Cloud9
JavaScript
1
star
49

gist-to-png

JavaScript
1
star
50

xbrl-crawler

JavaScript
1
star