0.1.0 • Published 10 months ago

kodgen v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Kodgen

npm license

Under development until 1.0.0. Be cautious of potential breaking changes, even in minor updates.

Kodgen is a TypeScript-based code generation library that parses OpenAPI definitions into models and services.

Installation

npm install kodgen kodgen-cli --save-dev

where

  • kodgen is the main codegen library, and
  • kodgen-cli is a command-line interface for it.

Features

  • Supported Swagger/OpenAPI versions: 2.0, 3.0.x, 3.1
  • JSON/YAML spec format
  • External definitions (via $ref)
  • <%= EJS %> templating
  • Custom generators

Usage

kodgen generate - run code generation

OptionAliasDescription
--configConfiguration file with all other possible options (json, js)
--package-pGenerator package name
--generator-gGenerator name
--generatorConfigFileGenerator configuration file (json, js)
--input-iInput spec (http, https, file path -- json, yaml)
--insecureInsecure HTTPS connection
--skipValidationSkip schema validation process
--output-oOutput path
--cleanClean the output path before generation
--templateDir-tCustom template directory (overrides default templates)
--templateDataFileAdditional template data file provided to all ejs templates (json, js)
--skipTemplatesSkip specific templates when generating
--includePathsInclude specific url patterns (regex strings)
--excludePathsExclude specific url patterns (regex strings)
--hooksFileHooks file. Overrides default generator functions (js)
--baseUrlOverrides default base url
--silentSuppress all informational messages
--verboseDetailed information about the process
--eolGenerated file newlines (CR, LF or CRLF)

JSON Schema (generate command)

Note: CLI arguments take precedence over the options in the configuration file.

kodgen validate - run schema validation only

OptionAliasDescription
--configConfiguration file with all other possible options (json, js)
--input-iInput spec (http, https, file path -- json, yaml)
--insecureInsecure HTTPS connection
--silentSuppress all informational messages

JSON Schema (validate command)

Note: CLI arguments take precedence over the options in the configuration file.

You can also use --help or -h on any command.

Templates

All templates are driven by EJS.

Most of the OpenAPI definition data, including vendor extensions, is accessible within templates.

All templates can be overridden. If you specify the templateDir option, the generator will look for templates in the specified directory. If no template is found in the user's directory, the default template will be used.

Additionally, you can include arbitrary data, such as functions or constants, in each template by providing a templateDataFile, which can be either a JSON or JS file. The data contained within this file will be accessible through the d key.

// create an example_template_data_file.js and specify it in the templateDataFile option

module.exports = {
    fn: () => 'hello!',
    myConstant: 1,
};

// Now, in all templates, you have access to d.fn() and d.myConstant

Hooks

A Hook is a function in the Kodgen library (or generator) that can be overridden. You can provide a custom implementation for these functions in the file specified by the hooksFile option.

// Example. Assume we are generating a name for a model
// The type of function is
type GenerateModelName = (name: string) => string;

// ...
const fn = Hooks.getOrDefault<GenerateModelName>(
    'generateModelName',        // hook name
    name => toPascalCase(name), // default implementation
);

// The Hooks service returned a default function or an overridden function
// (default in this case)
const name = fn('order'); // -> 'Order'

// Now the override. It's a generic hook type
// - T is a type of function to override
// - the default implementation always comes first argument
type HookFn<T extends Function> = (defaultFn: T, ...args: Parameters<T>) => ReturnType<T>;

// For instance, we may want to add an 'I' prefix in addition to the default implementation
// Create example_hook_file.js and specify it in the config (the hooksFile option)
module.exports = {
    generateModelName: (defaultFn, name) => `I${defaultFn(name)}`,
};

// Now the function call will produce 'IOrder', and all the models will be renamed accordingly

Kodgen exports the types, allowing you to manually compile a JavaScript file from TypeScript.

// example_hook_file.ts (based on kodgen-typescript generators hook)
import { HookFn } from 'kodgen';
import { TsGenGenerateModelName } from 'kodgen-typescript';

// For example, rename all models to Model, Model1, Model2...
export const generateModelName: HookFn<TsGenGenerateModelName> =
    (_: TsGenGenerateModelName, name: string, modifier?: number, type?: string) =>
        `Model${modifier ?? ''}`;

Generators

All generators are third-party packages. Like plugins.

Available generators

PackageGeneratorDescription
kodgen-typescriptng-typescriptAngular services generator
axios-typescriptAxios-based generator
fetch-typescriptNative Fetch API generator

Custom generators

Kodgen can transform OpenAPI definitions into any desired format. It offers access to all parsed entities from the OpenAPI specification, allowing you to use it in your own generator.

While there are no clear instructions on how to interact with the API, you can look at how the kodgen-typescript works from the inside.

Examples

You can find basic generation examples in this repository.