kjayasa / swagger-ts-client

A tool to generate typescript http clients and types from swagger definitions
MIT License
27 stars 24 forks source link

swagger-ts-client Build Status

This codegenerator is not maintained anymore. There exists much better tools now.

Swagger-ts-client is a tool that generate TypeScript types and http client from Swagger (open api). The code generation is highly configurable through a configuration file. Refer Configuration section for more details.

The generated code can completely controlled by using Handlebar templates.Refer template section for more section. The default template generates http clients based on the SuperAgent library.

Swagger-ts-client can import swagger definition from multiple sources using provider plugins.The default provider imports JSON formated swagger definition file from the file system.There is also an Http provider built in, that can be configured to import swagger from a url.

Some differences form other tools for the same purpose

npm

$ npm install swagger-ts-client --save-dev

Generating Code

when run with out any arguments , swagger-ts-client looks for a config file named ts-client.config.js and loads configuration from it.

$ swagger-ts-client

A minimal all defaults no config file example.Loads swagger from swagger.json generates types to ./generatedTypes/fooApiTypes.ts and generates Http Clients to ./httpProxy/

$ swagger-ts-client -s ./swagger.json -t ./generatedTypes/fooApiTypes.ts -o ./httpProxy/

All generated types are generated into a single file. Operations, default are grouped by tag(swagger spec) and for each group a class is generated and written into separate files.

Configuration

swagger-ts-client looks for a config file named ts-client.config.js and loads settings from it. Some configuration can be overridden by comment line args.

Configuration file

The configuration file needs to export a configuration object. The configuration object has the following schema.

{
    swaggerFile?: string;
    swaggerProvider?: {
        provide:Function
    };
    templateHelpers?:{
        [index: string]:IHandleBarHelper
    };
    type?: {
        typeAliases?: { 
            [index: string]:string
        };
        generatedTypes?: "type"|"interface"|"class";
        membersOptional?: boolean;
        templateFile?: string;
        outPutPath?: string;
        templateTag?: any;
    };
    operations?: {
        operationsGroupNameTransformFn?: Function;
        operationsNameTransformFn?: Function;
        ungroupedOperationsName?: string;
        templateFile?: string;
        outPutPath?: string;
        outFileNameTransformFn?: Function;
        templateTag?: any;
    };
}

For example; a simple config file.

const settings ={
    swaggerFile:"../swagger.json",
    type:{
        outPutPath:"./src/models.ts"
    },
    operations:{       
        outPutPath:"./src/httpClient/"
    }
};
module.exports=settings;

Configuration Options

CLI

Executing swagger-ts-client with out any options, it tries to load settings from ./ts-client.config.js. and generate code.

The recommended way of using swagger-ts-client is by putting all the configuration in the config file, but some options are provided which will the configuration settings from the config file. Using these options it might be possible to run swagger-ts-client with out a config file.

There are some options that can be used to change

Option Explanation
-V --version output the version number
-c --config ./path/to/config.file.js use the configuration file from the path
-s --swaggerFile ./path/to/swagger.doc.json use swagger definition from the path
-t --typesOut ./path/to/generate/types.ts> generate output types at the location
-u --url http://url.to.swaggerDef/swagger/v1/docs use url as swagger source
-o --operationsOut ./path/to/generate/operations/ generate operations at the location
-h --help output usage information

Template

Providers

Build

Clone or download from git hub.

yarn
yarn build