@codious/ngrx-generator

Generate a NgRx store (for Angular) based on an OpenAPI 3.x configuration (i.e. Swagger).

Usage no npm install needed!

<script type="module">
  import codiousNgrxGenerator from 'https://cdn.skypack.dev/@codious/ngrx-generator';
</script>

README

Codious.NgRx-Generator

Build Status Quality Gate Status

Generate a NgRx store (for Angular) based on an OpenAPI 3.x configuration (i.e. Swagger).

Installation

npm install @codious/ngrx-generator

Usage

[npx] ngrx-gen -i <input path|url> -o <output path>

The NgRx-Generator assumes that you use an RPC-styled REST API that only uses POST and GET HTTP methods. It also assumes that each route contains a tag. Multiple routes can be bundled with the same tag.

An example of a path in the OpenAPI document:

"paths": {
  "/api/v1/auth.login": {
    "post": {
      "parameters": [],
      "requestBody": {
        "required": true,
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/LoginInputModel"
            }
          }
        }
      },
      "responses": {
        "200": {
          "description": "OK (200)",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/LoginOutputModel"
              }
            }
          }
        },
        "401": {
          "description": "Unauthorized (401)"
        },
        "422": {
          "description": "Unprocessable Entity (422)"
        },
        "500": {
          "description": "Internal Server Error (500)"
        }
      },
      "tags": [
        "Authentication"
      ]
    }
  }
},
"components": {
  "schemas": {
    "LoginInputModel": {
      "type": "object",
      "properties": {
        "email": {
          "type": "string"
        },
        "password": {
          "type": "string"
        }
      },
      "required": [
        "email",
        "password"
      ]
    },
    "LoginOutputModel": {
      "type": "object",
      "properties": {
        "access_token": {
          "type": "string"
        },
        "expires_in": {
          "type": "number"
        },
        "refresh_token": {
          "type": "string"
        },
        "token_type": {
          "type": "string"
        }
      },
      "required": [
        "access_token",
        "expires_in",
        "refresh_token",
        "token_type"
      ]
    }
  }
}

This example route /api/v1/auth.login uses a prefix and a version. This wil result in a faulty generated store. Remove it with the option --prefix /api/v1.

Options:

  • [Required] --input, -i: a path to an OpenAPI specification file. This can also be a URL.
  • [Required] --output, -o: a path to a folder where the store will be generated.
  • [Optional] --conversions, -c: conversions from a OpenAPI model to a Typescript model or type (e.a. -c MyObject:any OtherObject:object).
  • [Optional] --lint, -l: ESLint config for linting the generated files. Add --lint false to skip linting.
  • [Optional] --prefix, -p: the route prefix and/or version to remove.
  • [Optional] --server, -s: the server defined in the description of the servers section in the OpenAPI file. This value is used as the API base URL in the generated service. If the option is empty or the server couldn't be found in the servers section, the base URL will be ${environment.apiBaseUrl}. Then, in your tsconfig.json add a path "@environments": ["environments/environment"], and add the property apiBaseUrl to the files in your Angular environments folder.
  • [Optional] --tags, -t: only paths with these tags will be generated.