Socket
Socket
Sign inDemoInstall

@asyncapi/parser

Package Overview
Dependencies
Maintainers
3
Versions
169
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@asyncapi/parser

JavaScript AsyncAPI parser.


Version published
Maintainers
3
Created

What is @asyncapi/parser?

@asyncapi/parser is a powerful npm package designed to parse AsyncAPI documents. It allows developers to read, validate, and manipulate AsyncAPI specifications programmatically. This is particularly useful for generating documentation, creating mock servers, and integrating with other tools in the API development lifecycle.

What are @asyncapi/parser's main functionalities?

Parsing AsyncAPI Documents

This feature allows you to parse an AsyncAPI document from a JSON string. The parsed document can then be used for further processing or validation.

const { Parser } = require('@asyncapi/parser');
const parser = new Parser();
const asyncapiDocument = `{
  "asyncapi": "2.0.0",
  "info": {
    "title": "Streetlights API",
    "version": "1.0.0"
  },
  "channels": {
    "light/measured": {
      "description": "Channel for light measurement events",
      "subscribe": {
        "summary": "Receive light measurement events",
        "message": {
          "$ref": "#/components/messages/LightMeasured"
        }
      }
    }
  },
  "components": {
    "messages": {
      "LightMeasured": {
        "payload": {
          "type": "object",
          "properties": {
            "lumens": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}`;
parser.parse(asyncapiDocument)
  .then(doc => console.log('Parsed document:', doc))
  .catch(err => console.error('Error parsing document:', err));

Validating AsyncAPI Documents

This feature allows you to validate an AsyncAPI document after parsing it. It checks for any schema violations or inconsistencies in the document.

const { Parser } = require('@asyncapi/parser');
const parser = new Parser();
const asyncapiDocument = `{
  "asyncapi": "2.0.0",
  "info": {
    "title": "Streetlights API",
    "version": "1.0.0"
  },
  "channels": {
    "light/measured": {
      "description": "Channel for light measurement events",
      "subscribe": {
        "summary": "Receive light measurement events",
        "message": {
          "$ref": "#/components/messages/LightMeasured"
        }
      }
    }
  },
  "components": {
    "messages": {
      "LightMeasured": {
        "payload": {
          "type": "object",
          "properties": {
            "lumens": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}`;
parser.parse(asyncapiDocument)
  .then(doc => {
    const validationErrors = doc.validate();
    if (validationErrors.length) {
      console.error('Validation errors:', validationErrors);
    } else {
      console.log('Document is valid');
    }
  })
  .catch(err => console.error('Error parsing document:', err));

Accessing Document Components

This feature allows you to access various components of the parsed AsyncAPI document, such as channels and messages. This is useful for generating documentation or creating mock servers.

const { Parser } = require('@asyncapi/parser');
const parser = new Parser();
const asyncapiDocument = `{
  "asyncapi": "2.0.0",
  "info": {
    "title": "Streetlights API",
    "version": "1.0.0"
  },
  "channels": {
    "light/measured": {
      "description": "Channel for light measurement events",
      "subscribe": {
        "summary": "Receive light measurement events",
        "message": {
          "$ref": "#/components/messages/LightMeasured"
        }
      }
    }
  },
  "components": {
    "messages": {
      "LightMeasured": {
        "payload": {
          "type": "object",
          "properties": {
            "lumens": {
              "type": "integer"
            }
          }
        }
      }
    }
  }
}`;
parser.parse(asyncapiDocument)
  .then(doc => {
    const channels = doc.channels();
    console.log('Channels:', channels);
    const messages = doc.components().messages();
    console.log('Messages:', messages);
  })
  .catch(err => console.error('Error parsing document:', err));

Other packages similar to @asyncapi/parser

FAQs

Package last updated on 10 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc