Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@asyncapi/parser

Package Overview
Dependencies
Maintainers
3
Versions
170
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@asyncapi/parser - npm Package Compare versions

Comparing version 0.21.0 to 0.22.0

21

lib/index.js
const parser = require('./parser');
const noop = () => {}; // No operation
const noop = {
parse: () => {}, // No operation
getMimeTypes: () => [
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/schema;version=draft-07',
'application/schema+json;version=draft-07',
'application/schema+yaml;version=draft-07',
]
};
parser.registerSchemaParser([
'application/vnd.aai.asyncapi;version=2.0.0',
'application/vnd.aai.asyncapi+json;version=2.0.0',
'application/vnd.aai.asyncapi+yaml;version=2.0.0',
'application/schema;version=draft-07',
'application/schema+json;version=draft-07',
'application/schema+yaml;version=draft-07',
], noop);
parser.registerSchemaParser(noop);
module.exports = parser;

@@ -186,9 +186,15 @@ const path = require('path');

* @param {string[]} schemaFormats An array of schema formats the given schema parser is able to recognize and transform.
* @param {Function} parserFunction The schema parser function.
* @param {Object} parserModule The schema parser module containing parse() and getMimeTypes() functions.
*/
function registerSchemaParser(schemaFormats, parserFunction) {
if (!Array.isArray(schemaFormats)) throw new ParserError(`schemaFormats must be an array of strings but found ${typeof schemaFormats}.`);
if (typeof parserFunction !== 'function') throw new ParserError(`parserFunction must be a function but found ${typeof parserFunction}.`);
schemaFormats.forEach((schemaFormat) => {
PARSERS[schemaFormat] = parserFunction;
function registerSchemaParser(parserModule) {
if (typeof parserModule !== 'object'
|| typeof parserModule.parse !== 'function'
|| typeof parserModule.getMimeTypes !== 'function')
throw new ParserError({
type: 'impossible-to-register-parser',
title: 'parserModule must have parse() and getMimeTypes() functions.'
});
parserModule.getMimeTypes().forEach((schemaFormat) => {
PARSERS[schemaFormat] = parserModule.parse;
});

@@ -195,0 +201,0 @@ }

{
"name": "@asyncapi/parser",
"version": "0.21.0",
"version": "0.22.0",
"description": "JavaScript AsyncAPI parser.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -82,2 +82,24 @@ <h5 align="center">

### Custom message parsers
AsyncAPI doesn't enforce one schema format for messages. You can have payload of your messages described with OpenAPI, Avro, etc. This parser by default parses only AsyncAPI schema format. You can extend it by creating a custom parser and registering it withing the parser:
1. Create custom parser module that exports two functions:
```js
module.exports = {
parse: ({ message, defaultSchemaFormat }) => { //custom parsing logic},
getMimeTypes: () => [
'//mime types that will be used as the `schemaFormat` property of the message to specify its mime type',
'application/vnd.custom.type;version=1.0.0',
'application/vnd.custom.type+json;version=1.0.0',
]
}
```
2. Before parsing an AsyncAPI document with a parser, register the additional custom schema parser:
```
const myCustomParser = require('mycustomParser');
parser.registerSchemaParser(myCustomParser);
```
### Error types

@@ -99,2 +121,3 @@

|`validation-errors`|`parsedJSON`, `validationErrors`|The AsyncAPI document contains errors. See `validationErrors` for more information.
|`impossible-to-register-parser`| None | Registration of custom message parser failed.

@@ -101,0 +124,0 @@ For more information about the `ParserError` class, [check out the documentation](./API.md#new_ParserError_new).

@@ -388,1 +388,25 @@ /* eslint-disable sonarjs/no-duplicate-string */

});
describe('registerSchemaParser()', function() {
it('no errors can be thrown', function() {
const parserModule = {
parse: () => {},
getMimeTypes: () => ['schemaFormat1', 'schemaFormat2']
};
expect(() => parser.registerSchemaParser(parserModule)).to.not.throw();
});
it('should throw error that required functions are missing', function() {
const parserModule = {
parse: () => {}
};
try {
parser.registerSchemaParser(parserModule);
} catch (e) {
expect(e.type).to.equal('https://github.com/asyncapi/parser-js/impossible-to-register-parser');
expect(e.title).to.equal('parserModule must have parse() and getMimeTypes() functions.');
}
});
});

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

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