Skip to content

Commit

Permalink
feat: add spec v3 support (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushmau5 authored Dec 15, 2024
1 parent ba23fd9 commit 6fcfb89
Show file tree
Hide file tree
Showing 14 changed files with 2,216 additions and 759 deletions.
2 changes: 1 addition & 1 deletion .sonarcloud.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Disable specific duplicate code since it would introduce more complexity to reduce it.
sonar.cpd.exclusions=src/standard.ts
sonar.cpd.exclusions=src/standards/v2.ts, src/standards/v3.ts, test/fixtures/main.fixtures.ts
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# For more details, read the following article on GitHub: https://help.github.com/articles/about-codeowners/.

# The default owners are automatically added as reviewers when you open a pull request unless different owners are specified in the file.
* @aayushmau5 @vinitshahdeo @onbit-uchenik @magicmatatjahu @derberg @asyncapi-bot-eve
* @aayushmau5 @magicmatatjahu @derberg @asyncapi-bot-eve
17 changes: 17 additions & 0 deletions src/helpers/DiffHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,20 @@ export function formatDiffOutput(
}
return output;
}

export function getDocumentMajorVersion(document: any): string {
const asyncapiVersion: string = document.asyncapi;
return asyncapiVersion.split('.')[0];
}

export function incompatibleDocuments(
firstDocument: any,
secondDocument: any
): boolean {
const firstDocumentMajorVersion = getDocumentMajorVersion(firstDocument);
const secondDocumentMajorVersion = getDocumentMajorVersion(secondDocument);
if (firstDocumentMajorVersion !== secondDocumentMajorVersion) {
return true;
}
return false;
}
13 changes: 10 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Config, OverrideStandard } from './types';
import generateDiff from './generateDiff';
import { standard } from './standard';
import { getStandardFromVersion } from './standard';
import categorizeChanges from './categorizeChanges';
import AsyncAPIDiff from './asyncapidiff';
import { mergeStandard } from './mergeStandard';
import { incompatibleDocuments } from './helpers/DiffHelpers';

/**
* Generates diff between two AsyncAPI documents
Expand Down Expand Up @@ -33,6 +34,12 @@ export function diff(
secondDocument: any,
config: Config = {}
): AsyncAPIDiff {
if (incompatibleDocuments(firstDocument, secondDocument)) {
throw new TypeError('diff between different AsyncAPI version is not allowed');
}

const standard = getStandardFromVersion(firstDocument);

if (config.override) {
if (typeof config.override !== 'object') {
throw new TypeError('Override data must be an object');
Expand All @@ -43,7 +50,7 @@ export function diff(
const diffOutput = generateDiff(firstDocument, secondDocument);
const output = categorizeChanges(standard as OverrideStandard, diffOutput);
return new AsyncAPIDiff(JSON.stringify(output), {
outputType: config.outputType || 'json',
markdownSubtype: config.markdownSubtype || 'json'
outputType: config.outputType ?? 'json',
markdownSubtype: config.markdownSubtype ?? 'json',
});
}
Loading

0 comments on commit 6fcfb89

Please sign in to comment.