0.3.11 • Published 10 months ago

@microloop/apispec-optimizer v0.3.11

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

@microloop/apispec-optimizer

An OpenAPI specification optimizer designed for LoopBack 4 applications, which removes the Controller suffix from controller and tag names, and the prefix ...Controller. from operationId.

Installation

npm i @microloop/apispec-optimizer --save

Usage

The component should be loaded in the constructor of your custom Application class.

Start by importing the component class:

import {ApiSpecOptimizerComponent} from '@microloop/apispec-optimizer';

In the constructor, add the component to your application:

this.component(ApiSpecOptimizerComponent);

This component provides a configuration for customizing the controller name and operationId of the OpenAPI spec.

import {CONTROLLER_NAME_KEY} from '@microloop/apispec-optimizer';

const apiSpecOptimizerOptions: ApiSpecOptimizerOptions = {
  customizeControllerName: (name, op, spec) => name + 'Api',
  customizeOperationId: (name, op, spec) => op[CONTROLLER_NAME_KEY] + '.' + name,
};
app.configure(ApiSpecOptimizerBindings.API_SPEC_OPTIMIZER).to(apiSpecOptimizerOptions);

Optimization example

The OpenAPI spec generated by LoopBack applications is like:

{
  "openapi": "3.0.0",
  "info": {
    "title": "LoopBack Application",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {
    "/test": {
      "get": {
        "x-controller-name": "TestController",
        "tags": ["TestController"],
        "operationId": "TestController.test",
        "responses": {
          "200": {
            "description": "test"
          }
        }
      }
    }
  }
}

After optimization with the configuration blow.

import {CONTROLLER_NAME_KEY} from '@microloop/apispec-optimizer';

const apiSpecOptimizerOptions: ApiSpecOptimizerOptions = {
  customizeControllerName: (name, op, spec) => name + 'Api',
  customizeOperationId: (name, op, spec) => op[CONTROLLER_NAME_KEY] + '.' + name,
};
app.configure(ApiSpecOptimizerBindings.API_SPEC_OPTIMIZER).to(apiSpecOptimizerOptions);

The OpenAPI spec is like:

{
  "openapi": "3.0.0",
  "info": {
    "title": "LoopBack Application",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "paths": {
    "/test": {
      "get": {
        "x-controller-name": "TestApi",
        "tags": ["TestApi"],
        "operationId": "TestApi.test",
        "responses": {
          "200": {
            "description": "test"
          }
        }
      }
    }
  }
}

Tests

Run npm test from the root folder.

License

MIT

0.3.9

10 months ago

0.3.11

10 months ago

0.3.10

10 months ago

0.3.8

11 months ago

0.3.0

12 months ago

0.3.6

11 months ago

0.2.7

1 year ago

0.3.5

11 months ago

0.2.6

1 year ago

0.2.9

12 months ago

0.3.7

11 months ago

0.2.8

12 months ago

0.3.2

11 months ago

0.3.1

11 months ago

0.3.4

11 months ago

0.3.3

11 months ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago