Skip to content

Commit f4e1224

Browse files
authored
fix: add interface Options, remove pre-validation with Parser (#188)
1 parent 7afdae8 commit f4e1224

8 files changed

+1358
-1888
lines changed

package-lock.json

+1,336-1,829
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
],
4040
"dependencies": {
4141
"@apidevtools/json-schema-ref-parser": "^11.5.4",
42-
"@asyncapi/parser": "^3.1.0",
4342
"@types/json-schema": "^7.0.11",
4443
"@ungap/structured-clone": "^1.2.0",
4544
"js-yaml": "^4.1.0",

src/index.ts

+3-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from 'path';
22
import { merge } from 'lodash';
3-
import { Parser } from '@asyncapi/parser';
43
import {
54
resolve,
65
versionCheck,
@@ -10,7 +9,8 @@ import {
109

1110
import { Document } from './document';
1211

13-
import type { AsyncAPIObject } from './spec-types';
12+
import type { AsyncAPIObject, Options } from './spec-types';
13+
export type { AsyncAPIObject, Options } from './spec-types';
1414

1515
// remember the directory where execution of the program started
1616
const originDir = String(process.cwd());
@@ -89,12 +89,9 @@ const originDir = String(process.cwd());
8989
*/
9090
export default async function bundle(
9191
files: string[] | string,
92-
options: any = {}
92+
options: Options = {}
9393
) {
9494
let bundledDocument: any = {};
95-
let validationResult: any = [];
96-
97-
const parser = new Parser();
9895

9996
// if one string was passed, convert it to an array
10097
if (typeof files === 'string') {
@@ -128,31 +125,6 @@ export default async function bundle(
128125
// properties into a familiar form.
129126
bundledDocument = orderPropsAccToAsyncAPISpec(bundledDocument);
130127

131-
// Option `noValidation: true` is used by the testing system, which
132-
// intentionally feeds Bundler wrong AsyncAPI Documents, thus it is not
133-
// documented.
134-
if (!options.noValidation) {
135-
validationResult = await parser.validate(
136-
JSON.parse(JSON.stringify(bundledDocument))
137-
);
138-
}
139-
140-
// If Parser's `validate()` function returns a non-empty array with at least
141-
// one `severity: 0`, that means there was at least one error during
142-
// validation, not a `warning: 1`, `info: 2`, or `hint: 3`. Thus, array's
143-
// elements with `severity: 0` are outputted as a list of remarks, and the
144-
// program throws.
145-
if (
146-
validationResult.length !== 0 &&
147-
validationResult.map((element: any) => element.severity).includes(0)
148-
) {
149-
console.log(
150-
'Validation of the resulting AsyncAPI Document failed.\nList of remarks:\n',
151-
validationResult.filter((element: any) => element.severity === 0)
152-
);
153-
throw new Error();
154-
}
155-
156128
// return to the starting directory before finishing the execution
157129
if (options.baseDir) {
158130
process.chdir(originDir);

src/parser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import $RefParser from '@apidevtools/json-schema-ref-parser';
22

33
import type { ParserOptions as $RefParserOptions } from '@apidevtools/json-schema-ref-parser';
4-
import type { AsyncAPIObject } from 'spec-types';
4+
import type { AsyncAPIObject, Options as BundlerOptions } from './spec-types';
55

66
let RefParserOptions: $RefParserOptions;
77

@@ -15,7 +15,7 @@ let RefParserOptions: $RefParserOptions;
1515
export async function parse(
1616
JSONSchema: AsyncAPIObject,
1717
specVersion: number,
18-
options: any = {}
18+
options: BundlerOptions = {}
1919
) {
2020
/* eslint-disable indent */
2121
// It is assumed that there will be major Spec versions 4, 5 and on.

src/spec-types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,9 @@ export type SpecificationExtension<T = any> = T;
450450
export interface ReferenceObject {
451451
$ref: string;
452452
}
453+
454+
export interface Options {
455+
base?: string;
456+
baseDir?: string;
457+
xOrigin?: boolean;
458+
}

src/util.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import structuredClone from '@ungap/structured-clone';
66
import { parse } from './parser';
77
import { ParserError } from './errors';
88

9-
import type { AsyncAPIObject } from './spec-types';
9+
import type { AsyncAPIObject, Options } from './spec-types';
1010

1111
/**
1212
* @private
@@ -79,7 +79,7 @@ export function isExternalReference(ref: string): boolean {
7979
* @returns {Array<Object>}
8080
* @private
8181
*/
82-
export const resolve = async (files: string | string[], options: any) => {
82+
export const resolve = async (files: string | string[], options: Options) => {
8383
const parsedJsons: AsyncAPIObject[] = [];
8484

8585
for (const file of files) {
@@ -112,7 +112,7 @@ export async function mergeIntoBaseFile(
112112
baseFilePath: string | string[],
113113
bundledDocument: AsyncAPIObject,
114114
majorVersion: number,
115-
options: any = {}
115+
options: Options = {}
116116
) {
117117
// The base file's path must be an array of exactly one element to be properly
118118
// iterated in `resolve()`. Even if it was passed to the main script as a

tests/lib/index.spec.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ describe('[integration testing] bundler should ', () => {
1515
const files = ['./tests/camera.yml', './tests/audio.yml'];
1616
const response = await bundle(files, {
1717
base: path.resolve(process.cwd(), './tests/base.yml'),
18-
noValidation: true,
1918
});
2019
expect(response).toBeDefined();
2120
});
@@ -29,7 +28,6 @@ describe('[integration testing] bundler should ', () => {
2928
expect(
3029
await bundle(files, {
3130
xOrigin: true,
32-
noValidation: true,
3331
})
3432
).resolves;
3533
});
@@ -43,7 +41,6 @@ describe('[integration testing] bundler should ', () => {
4341
expect(
4442
await bundle(files, {
4543
xOrigin: true,
46-
noValidation: true,
4744
})
4845
).resolves;
4946
});
@@ -56,7 +53,6 @@ describe('[integration testing] bundler should ', () => {
5653
xOrigin: true,
5754
base: 'base.yml',
5855
baseDir: path.resolve(process.cwd(), './tests'),
59-
noValidation: true,
6056
});
6157
}).rejects.toThrow(JSONParserError);
6258
});
@@ -71,15 +67,14 @@ describe('[integration testing] bundler should ', () => {
7167
await bundle(files, {
7268
base: path.resolve(process.cwd(), './tests/base-option/base.yaml'),
7369
xOrigin: true,
74-
noValidation: true,
7570
})
7671
).resolves;
7772
});
7873

7974
test('should be able to change the baseDir folder', async () => {
8075
const files = ['main.yaml'];
8176
expect(
82-
await bundle(files, { baseDir: './tests/specfiles', noValidation: true })
77+
await bundle(files, { baseDir: './tests/specfiles' })
8378
).resolves;
8479
});
8580

@@ -303,7 +298,6 @@ describe('[integration testing] bundler should ', () => {
303298
const document = await bundle(files, {
304299
base: 'asyncapi/index.yaml',
305300
baseDir: path.resolve(process.cwd(), 'tests/nested-dirs-mixed'),
306-
noValidation: true,
307301
});
308302

309303
expect(document.json()).toMatchObject(resultingObject);
@@ -380,9 +374,7 @@ describe('[integration testing] bundler should ', () => {
380374

381375
const files = 'tests/gh-185.yaml';
382376

383-
const document = await bundle(files, {
384-
noValidation: true,
385-
});
377+
const document = await bundle(files);
386378

387379
expect(document.json()).toMatchObject(resultingObject);
388380
});

tsconfig.json

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
{
22
"compilerOptions": {
3-
"outDir": "./lib",
4-
"baseUrl": "./src",
3+
"outDir": "lib",
4+
"baseUrl": "./",
55
"target": "ES2017",
6-
"types": [
7-
"node", "jest"
8-
],
9-
"lib": [
10-
"esnext",
11-
],
6+
"types": ["node", "jest"],
7+
"lib": ["esnext"],
128
"declaration": true,
139
"allowJs": true,
1410
"skipLibCheck": true,
@@ -20,9 +16,7 @@
2016
"module": "commonjs",
2117
"moduleResolution": "node",
2218
"resolveJsonModule": true,
23-
"isolatedModules": true,
19+
"isolatedModules": true
2420
},
25-
"include": [
26-
"src"
27-
]
21+
"include": ["src/**/*.ts"]
2822
}

0 commit comments

Comments
 (0)