Skip to content

Commit 78c55f4

Browse files
authored
feat: Upgrade AsyncAPI Parser (#112)
* feat: Upgrade AsyncAPI Parser * Upgrade parser to 0.33.0 * Fixes previous commit
1 parent 98c1a15 commit 78c55f4

File tree

11 files changed

+4324
-3940
lines changed

11 files changed

+4324
-3940
lines changed

library/package-lock.json

Lines changed: 645 additions & 1119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@
4343
"build": "tsc && npm run copyCss",
4444
"test": "jest --detectOpenHandles",
4545
"testWatch": "jest --detectOpenHandles --watch",
46-
"copyCss": "cp -r ./src/styles ./lib/styles",
46+
"copyCss": "cp -r ./src/styles ./lib",
4747
"prepare": "npm run build",
4848
"prepack": "cp ../README.md ./README.md && cp ../LICENSE ./LICENSE",
4949
"postpack": "rm -rf ./README.md && rm -rf ./LICENSE",
5050
"get-version": "echo $npm_package_version"
5151
},
5252
"dependencies": {
53-
"@asyncapi/parser": "0.16.2",
53+
"@asyncapi/parser": "^0.33.0",
5454
"constate": "^1.2.0",
5555
"dompurify": "^1.0.11",
56+
"json-schema-ref-parser": "^9.0.6",
5657
"markdown-it": "^9.1.0",
5758
"merge": "^1.2.1",
5859
"openapi-sampler": "^1.0.0-beta.15",

library/src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export const CONTENT_TYPES_SITE =
66
export const COLLAPSE_ALL_TEXT = 'Collapse All';
77
export const EXPAND_ALL_TEXT = 'Expand All';
88

9-
export const UNSUPPORTED_SCHEMA_VERSION =
10-
'AsyncAPI version is unsupported, use version 2.0 or higher';
9+
export const VALIDATION_ERRORS_TYPE =
10+
'https://github.com/asyncapi/parser-js/validation-errors';
1111
export const SERVERS = 'Servers';
1212

1313
export const ONE_OF_PAYLOADS_TEXT = 'One of those payloads:';

library/src/containers/AsyncApi/AsyncApi.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
AsyncAPI,
66
isFetchingSchemaInterface,
77
NullableAsyncApi,
8-
ParserError,
8+
ErrorObject,
99
AsyncApiProps,
1010
PropsSchema,
1111
} from '../../types';
@@ -24,7 +24,7 @@ import { SchemasComponent } from '../Schemas/Schemas';
2424

2525
interface AsyncAPIState {
2626
validatedSchema: NullableAsyncApi;
27-
error?: ParserError;
27+
error?: ErrorObject;
2828
}
2929

3030
const defaultAsyncApi: AsyncAPI = {

library/src/containers/Error/Error.tsx

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,64 @@
11
import React from 'react';
2-
import { ErrorObject } from 'ajv';
32

43
import { bemClasses } from '../../helpers';
5-
import { ParserError } from '../../types';
64
import { Toggle } from '../../components';
75
import { ERROR_TEXT } from '../../constants';
6+
import { ErrorObject, ValidationError } from '../../types';
87

9-
const renderErrors = (
10-
error: ParserError['validationError'],
11-
): React.ReactNode => {
12-
if (!error) {
8+
const renderErrors = (errors: ValidationError[]): React.ReactNode => {
9+
if (!errors) {
1310
return null;
1411
}
1512

16-
return error
17-
.map((singleError: ErrorObject, index: number) => {
18-
const formattedError = formatErrors(singleError);
13+
return errors
14+
.map((singleError: ValidationError, index: number) => {
15+
const formattedError = formatError(singleError);
1916

2017
if (!formattedError) {
2118
return null;
2219
}
2320
return (
24-
<code className={bemClasses.element(`error-content-code`)} key={index}>
25-
{formattedError}
26-
</code>
21+
<div>
22+
<code
23+
className={bemClasses.element(`error-content-code`)}
24+
key={index}
25+
>
26+
{formattedError}
27+
</code>
28+
</div>
2729
);
2830
})
2931
.filter(Boolean);
3032
};
3133

32-
export const formatErrors = (singleError: ErrorObject): string => {
33-
const { message, dataPath, params, keyword } = singleError;
34-
35-
const info = Object.values(params)[0];
36-
return `${dataPath} ${message}${keyword === 'type' ? '' : `: ${info}`}`;
34+
export const formatError = (singleError: ValidationError): string => {
35+
return singleError.title;
3736
};
3837

3938
interface Props {
40-
error: ParserError;
39+
error: ErrorObject;
4140
}
4241

4342
export const ErrorComponent: React.FunctionComponent<Props> = ({ error }) => {
4443
if (!error) {
4544
return null;
4645
}
4746
const className = `error`;
48-
const { message, validationError } = error;
47+
const { title, validationErrors } = error;
4948

5049
const header = (
5150
<h2>
52-
{ERROR_TEXT}: {message}
51+
{ERROR_TEXT}: {title}
5352
</h2>
5453
);
5554

5655
return (
5756
<section className={bemClasses.element(className)}>
5857
<Toggle header={header} className={className}>
59-
{!!validationError && (
58+
{validationErrors && validationErrors.length && (
6059
<div className={bemClasses.element(`${className}-body`)}>
6160
<pre className={bemClasses.element(`${className}-body-pre`)}>
62-
{renderErrors(validationError)}
61+
{renderErrors(validationErrors)}
6362
</pre>
6463
</div>
6564
)}

library/src/containers/Error/__tests__/error.test.ts

Lines changed: 0 additions & 66 deletions
This file was deleted.

library/src/helpers/__tests__/parser.test.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import {
2-
ParserErrorUnsupportedVersion,
3-
ParserErrorNoJS,
4-
} from '@asyncapi/parser';
51
import { Options as ParserOptions } from 'json-schema-ref-parser';
62

73
import { Parser } from '../parser';
@@ -28,28 +24,6 @@ const mockParseURLErr = <T extends Error>(err: T) =>
2824
describe('Parser', () => {
2925
describe('parse', () => {
3026
const parseURL = mockParseURLErr(new Error('not implemented'));
31-
test.each`
32-
error | desc
33-
${new ParserErrorUnsupportedVersion('err')} | ${'ParserErrorUnsupportedVersion is thrown'}
34-
${new ParserErrorNoJS('test error')} | ${'ParserErrorNoJS is thrown'}
35-
${new Error('other error')} | ${'other error'}
36-
${{
37-
message: 'version',
38-
parsedJSON: {
39-
asyncapi: '1',
40-
},
41-
}} | ${'invalid version is returned'}
42-
`(
43-
'should return error when $desc',
44-
async <R extends Error, T extends { error: R; desc: string }>(err: T) => {
45-
const parse = mockParseErr(err.error);
46-
const parser = new Parser(parse, parseURL);
47-
await parser.parse('mocked').then(result => {
48-
expect(result.error).toBeTruthy();
49-
expect(result.data).toBeFalsy();
50-
});
51-
},
52-
);
5327

5428
test('should return no errors and data when doc is valid', async () => {
5529
const doc: AsyncAPI = (validDoc as any) as AsyncAPI;
@@ -64,28 +38,6 @@ describe('Parser', () => {
6438

6539
describe('parseURL', () => {
6640
const parse = mockParseErr(new Error('not implemented'));
67-
test.each`
68-
error | desc
69-
${new ParserErrorUnsupportedVersion('err')} | ${'ParserErrorUnsupportedVersion is thrown'}
70-
${new ParserErrorNoJS('test error')} | ${'ParserErrorNoJS is thrown'}
71-
${new Error('other error')} | ${'other error'}
72-
${{
73-
message: 'version',
74-
parsedJSON: {
75-
asyncapi: '1',
76-
},
77-
}} | ${'invalid version is returned'}
78-
`(
79-
'should return error when $desc',
80-
async <R extends Error, T extends { error: R; desc: string }>(err: T) => {
81-
const parseURL = mockParseURLErr(err.error);
82-
const parser = new Parser(parse, parseURL);
83-
await parser.parseFromUrl({ url: 'mocked' }).then(result => {
84-
expect(result.error).toBeTruthy();
85-
expect(result.data).toBeFalsy();
86-
});
87-
},
88-
);
8941

9042
test('should return no errors and data when doc is valid', async () => {
9143
const doc: AsyncAPI = (validDoc as any) as AsyncAPI;

library/src/helpers/parser.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import {
2-
ParserErrorUnsupportedVersion,
3-
ParserErrorNoJS,
4-
} from '@asyncapi/parser';
51
import { Options as ParserOptions } from 'json-schema-ref-parser';
62

7-
import { ParserReturn, FetchingSchemaInterface } from '../types';
3+
import { ErrorObject, ParserReturn, FetchingSchemaInterface } from '../types';
84

9-
import { UNSUPPORTED_SCHEMA_VERSION } from '../constants';
5+
import { VALIDATION_ERRORS_TYPE } from '../constants';
106

117
type ParseDocument = (
128
content: string | any,
@@ -56,25 +52,15 @@ export class Parser {
5652
}
5753
}
5854

59-
private handleError = (err: any): ParserReturn => {
60-
if (
61-
err instanceof ParserErrorUnsupportedVersion ||
62-
err instanceof ParserErrorNoJS
63-
) {
64-
return { data: null, error: { message: err.message } };
65-
}
66-
67-
if (err.parsedJSON && err.parsedJSON.asyncapi.startsWith('1')) {
55+
private handleError = (err: ErrorObject): ParserReturn => {
56+
if (err.type === VALIDATION_ERRORS_TYPE) {
6857
return {
69-
data: null,
70-
error: { message: UNSUPPORTED_SCHEMA_VERSION },
58+
data: err.parsedJSON || null,
59+
error: err,
7160
};
7261
}
7362

74-
return {
75-
data: err.parsedJSON || null,
76-
error: { message: err.message, validationError: err.errors },
77-
};
63+
return { data: null, error: err };
7864
};
7965

8066
private extractDocument = (data: any): ParserReturn => {

library/src/types.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ErrorObject } from 'ajv';
21
import { ConfigInterface } from './config';
32

43
// Helpers
@@ -374,7 +373,7 @@ export type NullableAsyncApi = AsyncAPI | null;
374373

375374
export interface AsyncApiState {
376375
validatedSchema: NullableAsyncApi;
377-
error?: ParserError;
376+
error?: ErrorObject;
378377
}
379378

380379
export function isFetchingSchemaInterface(
@@ -388,14 +387,9 @@ export interface FetchingSchemaInterface {
388387
requestOptions?: RequestInit;
389388
}
390389

391-
export interface ParserError {
392-
message: string;
393-
validationError?: ErrorObject[] | null;
394-
}
395-
396390
export interface ParserReturn {
397391
data: NullableAsyncApi;
398-
error?: ParserError;
392+
error?: ErrorObject;
399393
}
400394

401395
export type TableColumnName = string;
@@ -406,3 +400,37 @@ export interface Identifier {
406400
id: string;
407401
toKebabCase?: boolean;
408402
}
403+
404+
export interface ValidationError {
405+
title: string;
406+
jsonPointer: string;
407+
startLine: number;
408+
startColumn: number;
409+
startOffset: number;
410+
endLine: number;
411+
endColumn: number;
412+
endOffset: number;
413+
}
414+
415+
export interface ErrorObject {
416+
type: string;
417+
title: string;
418+
detail?: string;
419+
parsedJSON?: any;
420+
validationErrors?: ValidationError[];
421+
location?: {
422+
startLine: number;
423+
startColumn: number;
424+
startOffset: number;
425+
};
426+
refs?: {
427+
title: string;
428+
jsonPointer: string;
429+
startLine: number;
430+
startColumn: number;
431+
startOffset: number;
432+
endLine: number;
433+
endColumn: number;
434+
endOffset: number;
435+
}[];
436+
}

0 commit comments

Comments
 (0)