Skip to content

Commit

Permalink
fix: add interface Options, remove pre-validation with Parser (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Sep 5, 2024
1 parent 7afdae8 commit f4e1224
Show file tree
Hide file tree
Showing 8 changed files with 1,358 additions and 1,888 deletions.
3,165 changes: 1,336 additions & 1,829 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
],
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^11.5.4",
"@asyncapi/parser": "^3.1.0",
"@types/json-schema": "^7.0.11",
"@ungap/structured-clone": "^1.2.0",
"js-yaml": "^4.1.0",
Expand Down
34 changes: 3 additions & 31 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import path from 'path';
import { merge } from 'lodash';
import { Parser } from '@asyncapi/parser';
import {
resolve,
versionCheck,
Expand All @@ -10,7 +9,8 @@ import {

import { Document } from './document';

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

// remember the directory where execution of the program started
const originDir = String(process.cwd());
Expand Down Expand Up @@ -89,12 +89,9 @@ const originDir = String(process.cwd());
*/
export default async function bundle(
files: string[] | string,
options: any = {}
options: Options = {}
) {
let bundledDocument: any = {};
let validationResult: any = [];

const parser = new Parser();

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

// Option `noValidation: true` is used by the testing system, which
// intentionally feeds Bundler wrong AsyncAPI Documents, thus it is not
// documented.
if (!options.noValidation) {
validationResult = await parser.validate(
JSON.parse(JSON.stringify(bundledDocument))
);
}

// If Parser's `validate()` function returns a non-empty array with at least
// one `severity: 0`, that means there was at least one error during
// validation, not a `warning: 1`, `info: 2`, or `hint: 3`. Thus, array's
// elements with `severity: 0` are outputted as a list of remarks, and the
// program throws.
if (
validationResult.length !== 0 &&
validationResult.map((element: any) => element.severity).includes(0)
) {
console.log(
'Validation of the resulting AsyncAPI Document failed.\nList of remarks:\n',
validationResult.filter((element: any) => element.severity === 0)
);
throw new Error();
}

// return to the starting directory before finishing the execution
if (options.baseDir) {
process.chdir(originDir);
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import $RefParser from '@apidevtools/json-schema-ref-parser';

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

let RefParserOptions: $RefParserOptions;

Expand All @@ -15,7 +15,7 @@ let RefParserOptions: $RefParserOptions;
export async function parse(
JSONSchema: AsyncAPIObject,
specVersion: number,
options: any = {}
options: BundlerOptions = {}
) {
/* eslint-disable indent */
// It is assumed that there will be major Spec versions 4, 5 and on.
Expand Down
6 changes: 6 additions & 0 deletions src/spec-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,9 @@ export type SpecificationExtension<T = any> = T;
export interface ReferenceObject {
$ref: string;
}

export interface Options {
base?: string;
baseDir?: string;
xOrigin?: boolean;
}
6 changes: 3 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import structuredClone from '@ungap/structured-clone';
import { parse } from './parser';
import { ParserError } from './errors';

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

/**
* @private
Expand Down Expand Up @@ -79,7 +79,7 @@ export function isExternalReference(ref: string): boolean {
* @returns {Array<Object>}
* @private
*/
export const resolve = async (files: string | string[], options: any) => {
export const resolve = async (files: string | string[], options: Options) => {
const parsedJsons: AsyncAPIObject[] = [];

for (const file of files) {
Expand Down Expand Up @@ -112,7 +112,7 @@ export async function mergeIntoBaseFile(
baseFilePath: string | string[],
bundledDocument: AsyncAPIObject,
majorVersion: number,
options: any = {}
options: Options = {}
) {
// The base file's path must be an array of exactly one element to be properly
// iterated in `resolve()`. Even if it was passed to the main script as a
Expand Down
12 changes: 2 additions & 10 deletions tests/lib/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ describe('[integration testing] bundler should ', () => {
const files = ['./tests/camera.yml', './tests/audio.yml'];
const response = await bundle(files, {
base: path.resolve(process.cwd(), './tests/base.yml'),
noValidation: true,
});
expect(response).toBeDefined();
});
Expand All @@ -29,7 +28,6 @@ describe('[integration testing] bundler should ', () => {
expect(
await bundle(files, {
xOrigin: true,
noValidation: true,
})
).resolves;
});
Expand All @@ -43,7 +41,6 @@ describe('[integration testing] bundler should ', () => {
expect(
await bundle(files, {
xOrigin: true,
noValidation: true,
})
).resolves;
});
Expand All @@ -56,7 +53,6 @@ describe('[integration testing] bundler should ', () => {
xOrigin: true,
base: 'base.yml',
baseDir: path.resolve(process.cwd(), './tests'),
noValidation: true,
});
}).rejects.toThrow(JSONParserError);
});
Expand All @@ -71,15 +67,14 @@ describe('[integration testing] bundler should ', () => {
await bundle(files, {
base: path.resolve(process.cwd(), './tests/base-option/base.yaml'),
xOrigin: true,
noValidation: true,
})
).resolves;
});

test('should be able to change the baseDir folder', async () => {
const files = ['main.yaml'];
expect(
await bundle(files, { baseDir: './tests/specfiles', noValidation: true })
await bundle(files, { baseDir: './tests/specfiles' })
).resolves;
});

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

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

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

const document = await bundle(files, {
noValidation: true,
});
const document = await bundle(files);

expect(document.json()).toMatchObject(resultingObject);
});
Expand Down
18 changes: 6 additions & 12 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"compilerOptions": {
"outDir": "./lib",
"baseUrl": "./src",
"outDir": "lib",
"baseUrl": "./",
"target": "ES2017",
"types": [
"node", "jest"
],
"lib": [
"esnext",
],
"types": ["node", "jest"],
"lib": ["esnext"],
"declaration": true,
"allowJs": true,
"skipLibCheck": true,
Expand All @@ -20,9 +16,7 @@
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"isolatedModules": true
},
"include": [
"src"
]
"include": ["src/**/*.ts"]
}

0 comments on commit f4e1224

Please sign in to comment.