Skip to content

Commit

Permalink
feat: add x-origin property
Browse files Browse the repository at this point in the history
  • Loading branch information
aeworxet committed Apr 6, 2024
1 parent 0ed1b8e commit eab6e89
Show file tree
Hide file tree
Showing 7 changed files with 2,821 additions and 11,568 deletions.
2 changes: 1 addition & 1 deletion example/bundle-cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const bundle = require('@asyncapi/bundler');

async function main() {
const document = await bundle([readFileSync('./main.yaml', 'utf-8')], {
'x-origin': true,
xOrigin: true,
});
if (document.yml()) {
writeFileSync('asyncapi.yaml', document.yml());
Expand Down
14,200 changes: 2,747 additions & 11,453 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"devDependencies": {
"@babel/cli": "^7.19.3",
"@babel/core": "^7.19.3",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-object-rest-spread": "^7.20.7",
"@babel/preset-env": "^7.19.3",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^29.1.1",
Expand Down
14 changes: 2 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { toJS, resolve, versionCheck } from './util';
import { Document } from './document';

import type { AsyncAPIObject } from './spec-types';
import { resolveV3Document } from './v3/parser';

/**
*
Expand Down Expand Up @@ -81,18 +80,9 @@ export default async function bundle(files: string[], options: any = {}) {
const parsedJsons = files.map(file => toJS(file)) as AsyncAPIObject[];

const majorVersion = versionCheck(parsedJsons);
let resolvedJsons;

if (majorVersion === 3) {
resolvedJsons = await resolveV3Document(parsedJsons, options);
} else {
/**
* Bundle all external references for each file.
* @private
*/
resolvedJsons = await resolve(parsedJsons, options);
}

const resolvedJsons: AsyncAPIObject[] = await resolve(parsedJsons, majorVersion, options);

return new Document(resolvedJsons, options.base);
}

Expand Down
84 changes: 66 additions & 18 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,81 @@
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { Parser } from '@asyncapi/parser';

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

const parser = new Parser();

let RefParserOptions: $RefParserOptions;

/**
* Fully dereferences the AsyncAPI Document.
* Function fully dereferences the provided AsyncAPI Document.
* @param {Object[]} JSONSchema
* @param {number} specVersion
* @param {Object} options
* @private
*/
export async function parse(JSONSchema: AsyncAPIObject, options: any = {}) {
export async function parse(
JSONSchema: AsyncAPIObject,
specVersion: number,
options: any = {}
) {
let validationResult: any[] = [];
/* eslint-disable indent */
// It is assumed that there will be major Spec versions 4, 5 and on.
switch (specVersion) {
case 2:
RefParserOptions = {
dereference: {
circular: false, // prettier-ignore
excludedPathMatcher: (path: string): any => { // eslint-disable-line
return;
},
onDereference: (path: string, value: AsyncAPIObject) => {
if (options.xOrigin) {
value['x-origin'] = path;
}
},
},
};
break;
case 3:
RefParserOptions = {
dereference: {
circular: false,
excludedPathMatcher: (path: string): any => {
return (
// prettier-ignore
(/#\/channels\/[a-zA-Z0-9]*\/servers/).test(path) ||
(/#\/operations\/[a-zA-Z0-9]*\/channel/).test(path) ||
(/#\/operations\/[a-zA-Z0-9]*\/messages/).test(path) ||
(/#\/operations\/[a-zA-Z0-9]*\/reply\/channel/).test(path) ||
(/#\/operations\/[a-zA-Z0-9]*\/reply\/messages/).test(path) ||
(/#\/components\/channels\/[a-zA-Z0-9]*\/servers/).test(path) ||
(/#\/components\/operations\/[a-zA-Z0-9]*\/channel/).test(path) ||
(/#\/components\/operations\/[a-zA-Z0-9]*\/messages/).test(path) ||
(/#\/components\/operations\/[a-zA-Z0-9]*\/reply\/channel/).test(path) ||
(/#\/components\/operations\/[a-zA-Z0-9]*\/reply\/messages/).test(path)
);
},
onDereference: (path: string, value: AsyncAPIObject) => {
if (options.xOrigin) {
value['x-origin'] = path;
}
},
},
};
break;
default:
console.error(
`There is no support for AsyncAPI Specification v${specVersion}.`
);
}

const dereferencedJSONSchema = await $RefParser.dereference(JSONSchema, {
dereference: {
circular: false,
// excludedPathMatcher: (path: string): any => {
// return (
// // prettier-ignore
// );
// },
onDereference: (path: string, value: AsyncAPIObject) => {
if (options['x-origin']) {
value['x-origin'] = path;
}
},
},
});
const dereferencedJSONSchema = await $RefParser.dereference(
JSONSchema,
RefParserOptions
);

// Option `noValidation: true` is used by the testing system, which
// intentionally feeds Bundler wrong AsyncAPI Documents, thus it is not
Expand Down
5 changes: 3 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,17 @@ export const toJS = (asyncapiYAMLorJSON: string | object) => {
*/
export const resolve = async (
asyncapiDocuments: AsyncAPIObject[],
specVersion: number,
options: any
) => {
const docs = [];

try {
for (const asyncapiDocument of asyncapiDocuments) {
await parse(asyncapiDocument, options);
await parse(asyncapiDocument, specVersion, options);
docs.push(asyncapiDocument);
}
} catch (e) {}
} catch (e) {} // eslint-disable-line

return docs;
};
Expand Down
82 changes: 0 additions & 82 deletions src/v3/parser.ts

This file was deleted.

0 comments on commit eab6e89

Please sign in to comment.