npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@asyncapi/optimizer

v1.0.4

Published

This library will optimize the AsyncAPI specification file.

Downloads

76,053

Readme

AsyncAPI 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 clone 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: 3.0.0
info:
  title: Example Service
  version: 1.0.0
  description: Example Service.
servers:
  production:
    host: 'test.mosquitto.org:{port}'
    protocol: mqtt
    description: Test broker
    variables:
      port:
        description: Secure connection (TLS) is available through port 8883.
        default: '1883'
        enum:
          - '1883'
          - '8883'
operations:
  user/deleteAccount.subscribe:
    action: send
    channel:
      $ref: '#/channels/commentLikedChannel'
channels:
  commentLikedChannel:
    address: comment/liked
    messages:
      commentLikedMessage:
        description: Message that is being sent when a comment has been liked by someone.
        payload:
          type: object
          title: commentLikedPayload
          properties:
            commentId:
              type: string
              description: an id object
              x-origin: ./schemas.yaml#/schemas/idSchema
          x-origin: ./schemas.yaml#/schemas/commentLikedSchema
        x-origin: ./messages.yaml#/messages/commentLikedMessage
    x-origin: ./channels.yaml#/channels/commentLikedChannel`

const optimizer = new Optimizer(yaml)

Generating report

const report: Report = await optimizer.getReport()
/*
the report value will be:
{
  removeComponents: [],
  reuseComponents: [],
  moveAllToComponents: [
    {
      path: 'channels.commentLikedChannel.messages.commentLikedMessage.payload.properties.commentId',
      action: 'move',
      target: 'components.schemas.idSchema'
    },
    {
      path: 'channels.commentLikedChannel.messages.commentLikedMessage.payload',
      action: 'move',
      target: 'components.schemas.commentLikedSchema'
    },
    {
      path: 'channels.commentLikedChannel.messages.commentLikedMessage',
      action: 'move',
      target: 'components.messages.commentLikedMessage'
    },
    {
      path: 'operations.user/deleteAccount.subscribe',
      action: 'move',
      target: 'components.operations.subscribe'
    },
    {
      path: 'channels.commentLikedChannel',
      action: 'move',
      target: 'components.channels.commentLikedChannel'
    },
    {
      path: 'servers.production',
      action: 'move',
      target: 'components.servers.production'
    }
  ],
  moveDuplicatesToComponents: []
}
 */

Applying the suggested changes

const optimizedDocument = optimizer.getOptimizedDocument({
  output: 'YAML',
  rules: {
    reuseComponents: true,
    removeComponents: true,
    moveAllToComponents: true,
    moveDuplicatesToComponents: false,
  },
  disableOptimizationFor: {
    schema: false,
  },
})
/*
the optimizedDocument value will be:

asyncapi: 3.0.0
info:
  title: Example Service
  version: 1.0.0
  description: Example Service.
servers:
  production:
    $ref: '#/components/servers/production'
operations:
  user/deleteAccount.subscribe:
    action: send
    channel:
      $ref: '#/channels/commentLikedChannel'
  user/deleteAccount:
    subscribe:
      $ref: '#/components/operations/subscribe'
channels:
  commentLikedChannel:
    $ref: '#/components/channels/commentLikedChannel'
components:
  schemas:
    idSchema:
      type: string
      description: an id object
      x-origin: ./schemas.yaml#/schemas/idSchema
    commentLikedSchema:
      type: object
      title: commentLikedPayload
      properties:
        commentId:
          $ref: '#/components/schemas/idSchema'
      x-origin: ./schemas.yaml#/schemas/commentLikedSchema
  messages:
    commentLikedMessage:
      description: Message that is being sent when a comment has been liked by someone.
      payload:
        $ref: '#/components/schemas/commentLikedSchema'
      x-origin: ./messages.yaml#/messages/commentLikedMessage
  operations: {}
  channels:
    commentLikedChannel:
      address: comment/liked
      messages:
        commentLikedMessage:
          $ref: '#/components/messages/commentLikedMessage'
      x-origin: ./channels.yaml#/channels/commentLikedChannel
  servers:
    production:
      host: test.mosquitto.org:{port}
      protocol: mqtt
      description: Test broker
      variables:
        port:
          description: Secure connection (TLS) is available through port 8883.
          default: '1883'
          enum:
            - '1883'
            - '8883'
 */

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.