0.5.0 • Published 4 months ago

genum-openapi v0.5.0

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

gEnum OpenAPI

ci npm npm

Tech used

About

genum-openapi is a simple Node.js CLI tool for creating TypeScript enums from OpenAPI document. This package is inspired by another tool named openapi-typescript. Toghether with mentioned package, developers should be able to generate TypeScript interfaces (using openapi-typescript) and enums (using genum-openapi) within their projects with the same YAML file as an input.

Features

  • Supports OpenAPI 3.x
  • Generate TypeScript enums
  • Load schemas from local YAML or JSON file
  • Native Node.js code used to write this tool

Usage

Generate local file with Typescript enums by running npx genum-openapi:

npx genum-openapi ./path/to/the/schema.yaml -o ./path/to/generated/enums.ts

:warning: Make sure that all your schemas are validated before using them with this tool.

Then you can import the enums from the generated file:

import { StatusEnum } from "./path/to/generated/enums";

export const App = () => {
  if (status === StatusEnum.SUCCESS) {
    console.log("Enum successfully generated and used");
  } else {
    console.log("Enum failed to generate");
  }
};

:gear: Options

Following flags are available for the CLI tool.

OptionAliasDefaultDescription
--help-hDisplay help for command
--version-vOutput the current version
--output-o(stdout)Path of the output file
--exclude-eNames of enums from OpenAPI document to exclude
--prefix-pPut prefix at the beggining of the enum name
--suffix-sEnumPut suffix at the end of the enum name if it does not already exists
--prenumPut specified prefix before the enum key name that starts with a number (underscore by default when not provided)
--normalize-nNormalize enum keys that can be invalid (replace . with __ and - or / with _)
--uppercase-uParse all enum keys to be uppercase (commonly used with --parse option)

:book: Examples

Let's say that our OpenAPI document looks like this:

openapi: 3.0.3
info:
  title: Example OpenApi description
  version: 0.0.0
components:
  schemas:
    Status:
      enum:
        - ACTIVE
        - INACTIVE
      type: string
    ColorsEnum:
      enum:
        - GREEN
        - RED
      type: string
    InvalidCase:
      enum:
        - SOME/WEIRD-Strings
        - another.weird.string
        - just(another)weird string
      type: string
    StartWithNumber:
      enum:
        - 250x330
        - 70x40
        - OTHER
      type: string
  1. By providing --exclude Status InvalidCase StartWithNumber option, generated file should contain only ColorsEnum:
export enum ColorsEnum {
  GREEN = "GREEN",
  RED = "RED",
}
  1. --prefix I option should add provided string at the beggining of the enums names:
export enum IStatus {
  [...]
}

export enum IColorsEnum {
  [...]
}

export enum IInvalidCase {
  [...]
}

export enum IStartWithNumber {
  [...]
}
  1. --suffix option should add provided string at the end of the enums names (when not provided Enum is used by default):
export enum StatusEnum {
  [...]
}

export enum ColorsEnum {
  [...]
}

export enum InvalidCaseEnum {
  [...]
}

export enum StartWithNumberEnum {
  [...]
}
  1. With --parse and --exclude options, generated file should contain parsed values of enum keys:
[...]
export enum InvalidCase {
  SOME_WEIRD_STRINGS = "SOME/WEIRD-Strings",
  ANOTHER__WEIRD__STRING = "another.weird.string"
  JUST_ANOTHER_WEIRD_STRING = "just(another)weird string"
}
[...]
  1. With --prenum $ option, keys of enums should be prefixed with a $ sign (by default enum keys that starts with a number are prefixed with an underscore):
[...]
export enum StartWithNumber {
  $250x330 = "250x330",
  $70x40 = "70x40",
  OTHER = "OTHER"
}
[...]

:mega: Goals

  1. Fetching schema from remote resource (with Auth header if needed)
  2. Parsing multiple schemas at once (with globbing)

:couple_with_heart: Contributing

PRs are welcome!

0.5.0

4 months ago

0.4.0

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.0

11 months ago

0.1.6

11 months ago

0.1.5

11 months ago

0.1.3

11 months ago

0.1.4

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago