swagger-js-codegen, 用于打字,nodejs & angularjs的Swagger样式

分享于 

8分钟阅读

GitHub

  繁體 雙語
A Swagger Codegen for nodejs & angularjs
  • 源代码名称:swagger-js-codegen
  • 源代码网址:http://www.github.com/wcandillon/swagger-js-codegen
  • swagger-js-codegen源代码文档
  • swagger-js-codegen源代码下载
  • Git URL:
    git://www.github.com/wcandillon/swagger-js-codegen.git
    Git Clone代码到本地:
    git clone http://www.github.com/wcandillon/swagger-js-codegen
    Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/wcandillon/swagger-js-codegen
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
    Swagger & Codegen

    Circle CINPM version

    这个包生成一个 nodejs,reactjs或者angularjs类,从 swagger规范文件。 使用 mustache 模板生成代码,并通过jshintjs美化来检查 quality。

    app生成器基于 superagent,可以通过 browserify/web pack同时用于nodejs和浏览器。

    安装

    npm install swagger-js-codegen

    示例

    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);

    自定义模板

    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')
     }
    });

    命令行选项

    除了下面的常用选项列表,getCustomCode() 还需要 template 字段:

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

    getAngularCode()getNodeCode()getCustomCode() 都支持以下选项:

    moduleName:
     type: stringdescription: Your AngularJS module nameclassName:
     type: stringlint:
     type: booleandescription: whether or not to run jslint on the generated codeesnext:
     type: booleandescription: passed through to jslintbeautify:
     type: booleandescription: whether or not to beautify the generated codemustache:
     type: objectdescription: See the 'Custom Mustache Variables' section belowimports:
     type: arraydescription: Typescript definition files to be imported.swagger:
     type: objectrequired: truedescription: swagger object

    模板变量

    下面的数据传递给 mustache 模板插件:

    isNode:
     type: booleanisES6:
     type: booleandescription:
     type: stringdescription: Provided by your options field: 'swagger.info.description'isSecure:
     type: booleandescription: false unless 'swagger.securityDefinitions' is definedmoduleName:
     type: stringdescription: Your AngularJS module name - provided by your options fieldclassName:
     type: stringdescription: Provided by your options fielddomain:
     type: stringdescription: If all options defined: swagger.schemes[0] + '://' + swagger.host + swagger.basePathmethods:
     type: arrayitems:
     type: objectproperties:
     path:
     type: stringclassName:
     type: stringdescription: Provided by your options fieldmethodName:
     type: stringdescription: Generated from the HTTP method and path elements or 'x-swagger-js-method-name' fieldmethod:
     type: stringdescription: '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 - PROPFINDisGET:
     type: stringdescription: true if method === 'GET'summary:
     type: stringdescription: Provided by the 'description' or 'summary' field in the schemaexternalDocs:
     type: objectproperties:
     url:
     type: stringdescription: The URL for the target documentation. Value MUST be in the format of a URL.required: truedescription:
     type: stringdescription: A short description of the target documentation. GitHub-Markdown syntax can be used for rich text representation.isSecure:
     type: booleandescription: true if the 'security' is defined for the method in the schemaparameters:
     type: arraydescription: Includes all of the properties defined for the parameter in the schema plus:items:
     camelCaseName:
     type: stringisSingleton:
     type: booleandescription: true if there was only one 'enum' defined for the parametersingleton:
     type: stringdescription: the one and only 'enum' defined for the parameter (if there is only one)isBodyParameter:
     type: booleanisPathParameter:
     type: booleanisQueryParameter:
     type: booleanisPatternType:
     type: booleandescription: true if *in* is 'query', and 'pattern' is definedisHeaderParameter:
     type: booleanisFormParameter:
     type: boolean
    自定义 mustache-变量

    你还可以通过添加 mustache 对象,为 mustache 模板传递自己的变量:

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

    Swagger扩展

    x-proxy-header

    一些代理和应用服务器将HTTP头注入到请求中。 服务器端代码可以使用这些字段,但在客户端API中不需要这些字段。

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

    /locations:
     get:
     parameters:
     - name: X-AppEngine-Countryin: headerx-proxy-header: truetype: stringdescription: Provided by AppEngine eg - US, AU, GB - name: countryin: querytype: stringdescription: | 2 character country code. If not specified, will default to the country provided in the X-AppEngine-Country header...

    Grunt任务

    有一个grunt任务使你能够在开发管道中集成代码生成。 如果应用程序使用的api是swagger格式的文档,那么这是非常方便的。

    谁使用它?

    使用这个项目来生成他们的nodejs和angularjs语言绑定。


    cod  angular  type  类型  nodejs  Typescript  
    相关文章