- Notifications
You must be signed in to change notification settings - Fork52
A Swagger Codegenerator tailored for typescript.
License
mtennoe/swagger-typescript-codegen
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This package generates a TypeScript class from aswagger specification file. The code is generated usingmustache templates and is quality checked byjshint and beautified byjs-beautify.
The typescript generator is based onsuperagent and can be used for both nodejs and the browser via browserify/webpack.
This fork was made to simplify some parts, add some more features, and tailor it more to specific use cases.
npm install swagger-typescript-codegen
varfs=require("fs");varCodeGen=require("swagger-typescript-codegen").CodeGen;varfile="swagger/spec.json";varswagger=JSON.parse(fs.readFileSync(file,"UTF-8"));vartsSourceCode=CodeGen.getTypescriptCode({className:"Test",swagger:swagger,imports:["../../typings/tsd.d.ts"]});console.log(tsSourceCode);
varsource=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")}});
In addition to the common options listed below,getCustomCode()
requires atemplate
field:
template: { class: "...", method: "..." }
getTypescriptCode()
,getCustomCode()
each support the following options:
moduleName:type:stringdescription:Your module nameclassName:type:stringbeautify:type:booleandescription:whether or not to beautify the generated codebeautifyOptions:type:objectdescription:Options to be passed to the beautify command. See js-beautify for all available options.mustache:type:objectdescription:See the 'Custom Mustache Variables' section belowimports:type:arraydescription:Typescript definition files to be imported.convertQueryParamsToFormDataInPOST:type:booleandescription:whether or not to convert query parameters in a POST request to form data.swagger:type:objectrequired:truedescription:swagger object
The following data are passed to themustache templates:
isES6:type:booleandescription:type:stringdescription:Provided by your options field: 'swagger.info.description'isSecure:type:booleandescription:false unless 'swagger.securityDefinitions' is definedmoduleName:type:stringdescription:Your 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:stringpathFormatString: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', 'UNLINK', 'PURGE', 'LOCK', 'UNLOCK', 'PROPFIND'enum: -GET -POST -PUT -DELETE -PATCH -COPY -HEAD -OPTIONS -LINK -UNLINK -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 schemaversion:type:stringdescription:Version part of the path, if the path starts with the prefix '/api/vXXX/'.intVersion:type:integerdescription:Integer part of the version string.isLatestVersion:type:booleandescription:True iff this is the latest version of the method.parameters: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:booleancollectionFormat:type:stringsuccessfulResponseType:type:stringdescription:The type of a successful response. Defaults to any for non-parsable types or Swagger 1.0 spec filessuccessfulResponseTypeIsRef:type:booleandescription:True iff the successful response type is the name of a type defined in the Swagger schema.convertQueryParamsToFormDataInPOST:type:booleandescription:Provided by your options field
You can also pass in your own variables for the mustache templates by adding amustache
object:
varsource=CodeGen.getCustomCode({ ...mustache:{foo:'bar',app_build_id:env.BUILD_ID,app_version:pkg.version}});
Some proxies and application servers inject HTTP headers into the requests. Server-side codemay 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-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...
To run the typescript compiler on the source files run. This will start a watch process on the sources and build them into thelib
folder.
npm run build:watch
In addition you can run the test watcher in a separate tab to run the tests in watch mode on the files in thelib
folder.
npm run test:watch
About
A Swagger Codegenerator tailored for typescript.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- TypeScript82.5%
- Mustache12.2%
- JavaScript5.3%