@massive/swagger-client-gen

Swagger code generator for Haxe and BrightScript

Usage no npm install needed!

<script type="module">
  import massiveSwaggerClientGen from 'https://cdn.skypack.dev/@massive/swagger-client-gen';
</script>

README

Haxe Swagger generator

Prerequisite

This tool supports swagger 2.0 only.

Running or building the tool requires NodeJS 10+.

Running the tool

From npm:

npm install @massive/swagger-client-gen
npx haxe-swagger-gen example/config_file.json
npx brightscript-swagger-gen example/config_file.json

From source:

node bin/haxe-swagger-generator.js example/config_file.json
node bin/brightscript-swagger-generator.js example/config_file.json

Configuration file

{
    "srcPath": "../src", // the destination directory
    "apiUrls": [ // urls used to generate classes
        ["https://api-server.com/spec"]
    ],
    "packages": { // classes packages
        "definitions": "swagger.gen.model",
        "paths": "swagger.gen.api", // apis
        "parameters": "swagger.gen.parameters"
    }
}

How to use the API

Haxe

Generated classes

The API is generating static methods which returns a Promise<T>.

Example:

ListsAPI.getList(list.id, {
    page: 2,
    page_size: 12,
    device: "web_browser"
});

This method takes all required parameters, and an object of the optional ones. If you don't want to specify any optional parameters, you can just do:

ListsAPI.getList(list.id);

As the call returns a typed Promise, you will receive a typed object:

ConfigAPI.getAppConfig().then(function(config: AppConfig) {
    // Now you have the config, enjoy :)
});

Use the actions with a Redux middleware

If you want to use the generated actions in a custom middleware, you can map the middleware with the generated actions:

// During store creation:
mapMiddleware(PageAction, new PageMiddleware())

// Your middleware should look like:
class PageMiddleware implements IMiddleware<PageAction, AppState> {

    public function new() {}

    public function middleware(action: PageAction, next: Void->Dynamic):Dynamic {
        return switch action {
            case GetList(...):
            ...
        }
    }
}

Brightscript

The generator creates API files for each Swagger section.

options = ConfigAPI_getAppConfig("roku")
'options = {
'    method: "GET",
'    params: { device: "roku" },
'    uri: "/config"
'}

service.load(options)

Building the tool

npm install
npm run test
npm run build

The JS script will be generated into bin/. If you update this tool, please also commit the .js so it can be used from git without compilation.

Haxe tooling

The tool is written in Haxe, targeting NodeJS. No need to install Haxe.

The Haxe toolchain and libraries are automatically installed by lix, a Haxe SDK and package manager. Lix is installed by npm as a package.json dependency.

If you're using VSCode you should install the following extensions:

  • VSHaxe extension (nadako.vshaxe)
  • Lix package manager (lix.lix)

Updating a dependency

lix switch <lib-name> <version>