GithubHelp home page GithubHelp logo

mcturco / optimizer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from asyncapi/optimizer

0.0 0.0 0.0 2.97 MB

AsyncAPI offers many different ways to reuse certain parts of the document like messages or schemas definitions or references to external files, not to even mention the traits. There is a need for a tool that can be plugged into any workflows and optimize documents that are generated from code, but not only.

License: Apache License 2.0

TypeScript 98.30% JavaScript 1.70%

optimizer's Introduction


AsyncAPI logo
Optimizer

AsyncAPI offers many ways to reuse certain parts of the document like messages or schemas definitions or references to external files, not to even mention the traits. Purpose of **AsyncAPI Optimizer** is to enable different ways to optimize AsyncAPI files. It is a library that can be used in UIs and CLIs.

npm npm

Testing

  1. Clone the project git https://github.com/asyncapi/optimizer.git
  2. Install the dependencies npm i
  3. for a quick check you can run npm run example. You can open examples/index.js modify it or add your own AsyncAPI document for optimization.

Usage

Node.js

import { Optimizer } from '@asyncapi/optimizer';
import type { Report } from '@asyncapi/optimizer';

const yaml =`
asyncapi: 2.0.0
info:
  title: Streetlights API
  version: '1.0.0'

channels:

  smartylighting/event/{streetlightId}/lighting/measured:
    parameters:
      #this parameter is duplicated. it can be moved to components and ref-ed from here.
      streetlightId:
        schema:
          type: string
    subscribe:
      operationId: receiveLightMeasurement
      traits:
        - bindings:
            kafka:
              clientId: my-app-id
      message:
        name: lightMeasured
        title: Light measured
        contentType: application/json
        traits:
          - headers:
              type: object
              properties:
                my-app-header:
                  type: integer
                  minimum: 0
                  maximum: 100
        payload:
          type: object
          properties:
            lumens:
              type: integer
              minimum: 0
            #full form is used, we can ref it to: #/components/schemas/sentAt
            sentAt:
              type: string
              format: date-time

  smartylighting/action/{streetlightId}/turn/on:
    parameters:
      streetlightId:
        schema:
          type: string
    publish:
      operationId: turnOn
      traits:
        - bindings:
            kafka:
              clientId: my-app-id
      message:
        name: turnOnOff
        title: Turn on/off
        traits:
          - headers:
              type: object
              properties:
                my-app-header:
                  type: integer
                  minimum: 0
                  maximum: 100
        payload:
          type: object
          properties:
            sentAt:
              $ref: "#/components/schemas/sentAt"

components:
  messages:
    #libarary should be able to find and delete this message, because it is not used anywhere.
    unusedMessage:
      name: unusedMessage
      title: This message is not used in any channel.
      
  schemas:
    #this schema is ref-ed in one channel and used full form in another. library should be able to identify and ref the second channel as well.
    sentAt:
      type: string
      format: date-time`;

const optimizer = new Optimizer(yaml);

Generating report

const report: Report = await optimizer.getReport();
/*
the report value will be:
{
  reuseComponents: [
    {
      path: 'channels.smartylighting/event/{streetlightId}/lighting/measured.message.payload.properties.sentAt',
      action: 'reuse',
      target: 'components.schemas.sentAt'
    }
  ],
  removeComponents: [
    {
      path: 'components.messages.unusedMessage',
      action: 'remove',
    }
  ],
  moveToComponents: [
    {
      //move will ref the current path to the moved component as well.
      path: 'channels.smartylighting/event/{streetlightId}/lighting/measured.parameters.streetlightId',
      action: 'move',
      target: 'components.parameters.streetlightId'
    },
    {
      path: 'channels.smartylighting/action/{streetlightId}/turn/on.parameters.streetlightId',
      action: 'reuse',
      target: 'components.parameters.streetlightId'
    }
  ]
}
 */

Applying the suggested changes

const optimizedDocument = optimizer.getOptimizedDocument({
  rules: {
    reuseComponents: true,
    removeComponents: true,
    moveToComponents: true 
  }
});
/*
the optimizedDocument value will be:

asyncapi: 2.0.0
info:
  title: Streetlights API
  version: 1.0.0
channels:
  "smartylighting/event/{streetlightId}/lighting/measured":
    parameters:
      streetlightId:
        $ref: "#/components/parameters/parameter-1"
    subscribe:
      operationId: receiveLightMeasurement
      traits:
        - bindings:
            kafka:
              clientId: my-app-id
      message:
        name: lightMeasured
        title: Light measured
        contentType: application/json
        traits:
          - headers:
              $ref: "#/components/schemas/schema-1"
        payload:
          type: object
          properties:
            lumens:
              type: integer
              minimum: 0
            sentAt:
              $ref: "#/components/schemas/sentAt"
  "smartylighting/action/{streetlightId}/turn/on":
    parameters:
      streetlightId:
        $ref: "#/components/parameters/parameter-1"
    publish:
      operationId: turnOn
      traits:
        - bindings:
            kafka:
              clientId: my-app-id
      message:
        name: turnOnOff
        title: Turn on/off
        traits:
          - headers:
              $ref: "#/components/schemas/schema-1"
        payload:
          type: object
          properties:
            sentAt:
              $ref: "#/components/schemas/sentAt"
components:
  schemas:
    sentAt:
      type: string
      format: date-time
    schema-1:
      type: object
      properties:
        my-app-header:
          type: integer
          minimum: 0
          maximum: 100
  parameters:
    parameter-1:
      schema:
        type: string`
 */

API documentation

For using the optimizer to optimize file you just need to import the Optimizer class. Use its two methods to get the report (getReport()) and get the optimized document (getOptimizedDocument()).

See API documentation for more example and full API reference information.

optimizer's People

Contributors

asyncapi-bot avatar derberg avatar khudadad414 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.