Skip to content

Commit 7db197c

Browse files
feat: fix of make @asyncapi/react component with vs asyncapi preview (#1082)
1 parent d87fd60 commit 7db197c

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

library/src/helpers/parser.ts

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Parser as AsyncapiParser, fromURL } from '@asyncapi/parser';
1+
import {
2+
Parser as AsyncapiParser,
3+
Diagnostic,
4+
DiagnosticSeverity,
5+
fromURL,
6+
} from '@asyncapi/parser';
27
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
38
import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser';
49
import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';
@@ -26,41 +31,18 @@ export class Parser {
2631
parserOptions?: any,
2732
): Promise<ParserReturn> {
2833
try {
29-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
30-
const parseResult = await asyncapiParser.parse(content, parserOptions);
31-
32-
const error: {
33-
title: string | undefined;
34-
validationErrors: ValidationError[] | undefined;
35-
} = {
36-
title: 'There are errors in your Asyncapi document',
37-
validationErrors: [],
38-
};
34+
const { document, diagnostics } = await asyncapiParser.parse(
35+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
36+
content,
37+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
38+
parserOptions,
39+
);
3940

40-
if (parseResult.document === undefined) {
41-
parseResult.diagnostics.forEach((diagnostic) => {
42-
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
43-
if (diagnostic.severity == 0) {
44-
const tempObj: ValidationError = {
45-
title: diagnostic.message,
46-
location: {
47-
jsonPointer: '/' + diagnostic.path.join('/'),
48-
startLine: diagnostic.range.start.line,
49-
startColumn: diagnostic.range.start.character,
50-
// as of @asyncapi/parser 3.3.0 offset of 1 correctly shows the error line
51-
startOffset: 1,
52-
endLine: diagnostic.range.end.line,
53-
endColumn: diagnostic.range.end.character,
54-
endOffset: 0,
55-
},
56-
};
57-
error.validationErrors?.push(tempObj);
58-
}
59-
});
60-
throw error;
41+
if (document === undefined) {
42+
throw this.convertDiagnosticToErrorObject(diagnostics, [0]);
6143
}
6244

63-
return { asyncapi: parseResult.document };
45+
return { asyncapi: document };
6446
} catch (err) {
6547
return this.handleError(err as ErrorObject);
6648
}
@@ -79,13 +61,51 @@ export class Parser {
7961
arg.requestOptions as any,
8062
);
8163
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
82-
const { document } = await fromResult.parse(parserOptions);
83-
return { asyncapi: document };
64+
const { document, diagnostics } = await fromResult.parse(parserOptions);
65+
66+
if (document == undefined) {
67+
// this means there are errors in the document.
68+
// so we gather all the severity 0 diagnostics and throw them as errors
69+
throw this.convertDiagnosticToErrorObject(diagnostics, [0]);
70+
}
71+
72+
return { asyncapi: document, error: undefined };
8473
} catch (err) {
8574
return this.handleError(err as ErrorObject);
8675
}
8776
}
8877

78+
static readonly convertDiagnosticToErrorObject = (
79+
diagnostics: Diagnostic[],
80+
severities: DiagnosticSeverity[],
81+
): ErrorObject => {
82+
const error: ErrorObject = {
83+
title: 'There are errors in your Asyncapi document',
84+
type: 'VALIDATION_ERRORS_TYPE',
85+
validationErrors: [],
86+
};
87+
diagnostics.forEach((diagnostic) => {
88+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
89+
if (severities.includes(diagnostic.severity)) {
90+
const tempObj: ValidationError = {
91+
title: diagnostic.message,
92+
location: {
93+
jsonPointer: '/' + diagnostic.path.join('/'),
94+
startLine: diagnostic.range.start.line,
95+
startColumn: diagnostic.range.start.character,
96+
// as of @asyncapi/parser 3.3.0 offset of 1 correctly shows the error line
97+
startOffset: 1,
98+
endLine: diagnostic.range.end.line,
99+
endColumn: diagnostic.range.end.character,
100+
endOffset: 0,
101+
},
102+
};
103+
error.validationErrors?.push(tempObj);
104+
}
105+
});
106+
return error;
107+
};
108+
89109
private static handleError = (err: ErrorObject): ParserReturn => {
90110
if (err.type === VALIDATION_ERRORS_TYPE) {
91111
return {

0 commit comments

Comments
 (0)