Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added types for typescript #116

Merged
merged 8 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
run: npm test
- name: Regenerate docs
run: npm run docs
- name: Regenerate types
run: npm run types
- name: Generate bundle.js for the browser
run: npm run prepublishOnly
- name: Get version from package.json before release step
Expand Down
8 changes: 8 additions & 0 deletions lib/asyncapiSchemaFormatParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module.exports = {
getMimeTypes
};

/**
* @private
*/
async function parse({ message, originalAsyncAPIDocument, fileFormat, parsedAsyncAPIDocument, pathToPayload }) {
const ajv = new Ajv({
jsonPointers: true,
Expand All @@ -27,6 +30,9 @@ async function parse({ message, originalAsyncAPIDocument, fileFormat, parsedAsyn
});
}

/**
* @private
*/
function getMimeTypes() {
return [
'application/vnd.aai.asyncapi;version=2.0.0',
Expand All @@ -42,6 +48,7 @@ function getMimeTypes() {
* To validate schema of the payload we just need a small portion of official AsyncAPI spec JSON Schema, the definition of the schema must be
* a main part of the JSON Schema
*
* @private
* @param {Object} asyncapiSchema AsyncAPI specification JSON Schema
* @returns {Object} valid JSON Schema document describing format of AsyncAPI-valid schema for message payload
*/
Expand All @@ -56,6 +63,7 @@ function preparePayloadSchema(asyncapiSchema) {
* Errors from Ajv contain dataPath information about parameter relative to parsed payload message.
* This function enriches dataPath with additional information on where is the parameter located in AsyncAPI document
*
* @private
* @param {Array<Object>} errors Ajv errors
* @param {String} path Path to location of the payload schema in AsyncAPI Document
* @returns {Array<Object>} same object as received in input but with modified datePath property so it contain full path relative to AsyncAPI document
Expand Down
6 changes: 4 additions & 2 deletions lib/customValidators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const validationError = 'validation-errors';

/**
* Validates if variables provided in the url have corresponding variable object defined
*
* @private
* @param {Object} parsedJSON parsed AsyncAPI document
* @param {String} asyncapiYAMLorJSON AsyncAPI document in string
* @param {String} initialFormat information of the document was oryginally JSON or YAML
Expand Down Expand Up @@ -46,6 +46,7 @@ function validateServerVariables(parsedJSON, asyncapiYAMLorJSON, initialFormat)
/**
* Validates if parameters specified in the channel have corresponding parameters object defined
*
* @private
* @param {Object} parsedJSON parsed AsyncAPI document
* @param {String} asyncapiYAMLorJSON AsyncAPI document in string
* @param {String} initialFormat information of the document was oryginally JSON or YAML
Expand Down Expand Up @@ -88,6 +89,7 @@ function validateChannelParams(parsedJSON, asyncapiYAMLorJSON, initialFormat) {
/**
* Validates if operationIds are duplicated in the document
*
* @private
* @param {Object} parsedJSON parsed AsyncAPI document
* @param {String} asyncapiYAMLorJSON AsyncAPI document in string
* @param {String} initialFormat information of the document was oryginally JSON or YAML
Expand Down Expand Up @@ -137,4 +139,4 @@ module.exports = {
validateChannelParams,
validateServerVariables,
validateOperationId
};
};
2 changes: 2 additions & 0 deletions lib/errors/parser-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const buildError = (from, to) => {

/**
* Represents an error while trying to parse an AsyncAPI document.
* @alias module:@asyncapi/parser#ParserError
* @extends Error
*/
class ParserError extends Error {
/**
Expand Down
37 changes: 26 additions & 11 deletions lib/models/asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const xParserSchemaId = 'x-parser-schema-id';

/**
* Implements functions to deal with the AsyncAPI document.
* @class
* @class
* @alias module:@asyncapi/parser#AsyncAPIDocument
* @extends Base
* @returns {AsyncAPIDocument}
*/
Expand Down Expand Up @@ -145,7 +146,7 @@ class AsyncAPIDocument extends Base {
}

/**
* @returns {Map<Message>}
* @returns {Map<string, Message>}
*/
allMessages() {
const messages = new Map();
Expand Down Expand Up @@ -176,7 +177,7 @@ class AsyncAPIDocument extends Base {
}

/**
* @returns {Map<Schema>}
* @returns {Map<string, Schema>}
*/
allSchemas() {
const schemas = new Map();
Expand All @@ -196,6 +197,12 @@ class AsyncAPIDocument extends Base {
}
}

/**
* Assign message keys as message name to all the component messages.
*
* @private
* @param {AsyncAPIDocument} doc
*/
function assignNameToComponentMessages(doc) {
if (doc.hasComponents()) {
for (const [key, m] of Object.entries(doc.components().messages())) {
Expand All @@ -209,6 +216,7 @@ function assignNameToComponentMessages(doc) {
/**
* Assign parameter keys as uid for the parameter schema.
*
* @private
* @param {AsyncAPIDocument} doc
*/
function assignUidToParameterSchemas(doc) {
Expand All @@ -223,6 +231,7 @@ function assignUidToParameterSchemas(doc) {
/**
* Assign uid to component schemas.
*
* @private
* @param {AsyncAPIDocument} doc
*/
function assignUidToComponentSchemas(doc) {
Expand All @@ -236,6 +245,7 @@ function assignUidToComponentSchemas(doc) {
/**
* Assign anonymous names to nameless messages.
*
* @private
* @param {AsyncAPIDocument} doc
*/
function assignNameToAnonymousMessages(doc) {
Expand All @@ -253,7 +263,8 @@ function assignNameToAnonymousMessages(doc) {
/**
* Add anonymous name to key if no name provided.
*
* @param {messages} map of messages
* @private
* @param {Message} map of messages
*/
function addNameToKey(messages, number) {
messages.forEach(m => {
Expand All @@ -263,13 +274,18 @@ function addNameToKey(messages, number) {
});
}

/**
* Callback that is called foreach schema found
* @private
* @callback FoundSchemaCallback
* @param {Schema} schema found.
*/
/**
* Recursively go through each schema and execute callback.
*
* @private
* @param {Schema} schema found.
* @param {Function} callback(schema)
* the function that is called foreach schema found.
* schema {Schema}: the found schema.
* @param {FoundSchemaCallback} callback
*/
function recursiveSchema(schema, callback) {
if (schema === null) return;
Expand Down Expand Up @@ -324,10 +340,9 @@ function recursiveSchema(schema, callback) {
/**
* Go through each channel and for each parameter, and message payload and headers recursively call the callback for each schema.
*
* @private
* @param {AsyncAPIDocument} doc
* @param {Function} callback(schema)
* the function that is called foreach schema found.
* schema {Schema}: the found schema.
* @param {FoundSchemaCallback} callback
*/
function schemaDocument(doc, callback) {
if (doc.hasChannels()) {
Expand Down Expand Up @@ -356,7 +371,7 @@ function schemaDocument(doc, callback) {

/**
* Gives schemas id to all anonymous schemas.
*
* @private
* @param {AsyncAPIDocument} doc
*/
function assignIdToAnonymousSchemas(doc) {
Expand Down
1 change: 1 addition & 0 deletions lib/models/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ParserError = require('../errors/parser-error');
/**
* Implements common functionality for all the models.
* @class
* @alias module:@asyncapi/parser#Base
* @returns {Base}
*/
class Base {
Expand Down
1 change: 1 addition & 0 deletions lib/models/channel-parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Schema = require('./schema');
/**
* Implements functions to deal with a ChannelParameter object.
* @class
* @alias module:@asyncapi/parser#ChannelParameter
* @extends Base
* @returns {ChannelParameter}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const SubscribeOperation = require('./subscribe-operation');
/**
* Implements functions to deal with a Channel object.
* @class
* @alias module:@asyncapi/parser#Channel
* @extends Base
* @returns {Channel}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const MessageTrait = require('./message-trait');
/**
* Implements functions to deal with a Components object.
* @class
* @alias module:@asyncapi/parser#Components
* @extends Base
* @returns {Components}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Base = require('./base');
/**
* Implements functions to deal with the Contact object.
* @class
* @alias module:@asyncapi/parser#Contact
* @extends Base
* @returns {Contact}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/correlation-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Base = require('./base');
/**
* Implements functions to deal with a CorrelationId object.
* @class
* @alias module:@asyncapi/parser#CorrelationId
* @extends Base
* @returns {CorrelationId}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/external-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Base = require('./base');
/**
* Implements functions to deal with an ExternalDocs object.
* @class
* @alias module:@asyncapi/parser#ExternalDocs
* @extends Base
* @returns {ExternalDocs}
*/
Expand Down
3 changes: 2 additions & 1 deletion lib/models/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const Contact = require('./contact');

/**
* Implements functions to deal with the Info object.
* @class Info
* @class
* @alias module:@asyncapi/parser#Info
* @extends Base
* @returns {Info}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/license.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Base = require('./base');
/**
* Implements functions to deal with the License object.
* @class
* @alias module:@asyncapi/parser#License
* @extends Base
* @returns {License}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/message-trait.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const MessageTraitable = require('./message-traitable');
/**
* Implements functions to deal with a MessageTrait object.
* @class
* @alias module:@asyncapi/parser#MessageTrait
* @extends Base
* @returns {MessageTrait}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/message-traitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const CorrelationId = require('./correlation-id');
/**
* Implements functions to deal with a the common properties that Message and MessageTrait objects have.
* @class
* @alias module:@asyncapi/parser#MessageTraitable
* @extends Base
* @returns {MessageTraitable}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Schema = require('./schema');
/**
* Implements functions to deal with a Message object.
* @class
* @alias module:@asyncapi/parser#Message
* @extends MessageTraitable
* @returns {Message}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/oauth-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Base = require('./base');
/**
* Implements functions to deal with a OAuthFlow object.
* @class
* @alias module:@asyncapi/parser#OAuthFlow
* @extends Base
* @returns {OAuthFlow}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/operation-trait.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const OperationTraitable = require('./operation-traitable');
/**
* Implements functions to deal with a OperationTrait object.
* @class
* @alias module:@asyncapi/parser#OperationTrait
* @extends OperationTraitable
* @returns {OperationTrait}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/operation-traitable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ExternalDocs = require('./external-docs');
/**
* Implements functions to deal with the common properties Operation and OperationTrait object have.
* @class
* @alias module:@asyncapi/parser#OperationTraitable
* @extends Base
* @returns {OperationTraitable}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Message = require('./message');
/**
* Implements functions to deal with an Operation object.
* @class
* @alias module:@asyncapi/parser#Operation
* @extends OperationTraitable
* @returns {Operation}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/publish-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Operation = require('./operation');
/**
* Implements functions to deal with a PublishOperation object.
* @class
* @alias module:@asyncapi/parser#PublishOperation
* @extends Operation
* @returns {PublishOperation}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ExternalDocs = require('./external-docs');
/**
* Implements functions to deal with a Schema object.
* @class
* @alias module:@asyncapi/parser#Schema
* @extends Base
* @returns {Schema}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/security-scheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const OAuthFlow = require('./oauth-flow');
/**
* Implements functions to deal with a SecurityScheme object.
* @class
* @alias module:@asyncapi/parser#SecurityScheme
* @extends Base
* @returns {SecurityScheme}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/server-security-requirement.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Base = require('./base');
/**
* Implements functions to deal with a ServerSecurityRequirement object.
* @class
* @alias module:@asyncapi/parser#ServerSecurityRequirement
* @extends Base
* @returns {ServerSecurityRequirement}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/server-variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Base = require('./base');
/**
* Implements functions to deal with a ServerVariable object.
* @class
* @alias module:@asyncapi/parser#ServerVariable
* @extends Base
* @returns {ServerVariable}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ServerSecurityRequirement = require('./server-security-requirement');
/**
* Implements functions to deal with a Server object.
* @class
* @alias module:@asyncapi/parser#Server
* @extends Base
* @returns {Server}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/subscribe-operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Operation = require('./operation');
/**
* Implements functions to deal with a SubscribeOperation object.
* @class
* @alias module:@asyncapi/parser#SubscribeOperation
* @extends Operation
* @returns {SubscribeOperation}
*/
Expand Down
1 change: 1 addition & 0 deletions lib/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ExternalDocs = require('./external-docs');
/**
* Implements functions to deal with a Tag object.
* @class
* @alias module:@asyncapi/parser#Tag
* @extends Base
* @returns {Tag}
*/
Expand Down
Loading