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 9, 2024
1 parent 2d73557 commit 5799c6e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 113 deletions.
16 changes: 4 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 @@ -73,6 +72,8 @@ import { resolveV3Document } from './v3/parser';
*
*/
export default async function bundle(files: string[], options: any = {}) {
let resolvedJsons: AsyncAPIObject[];

// if (typeof options.base !== 'undefined') {
// options.base = toJS(options.base);
// await parse(options.base, options);
Expand All @@ -81,18 +82,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);
}

resolvedJsons = 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[] = [];

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;
}
},
},
});
// It is assumed that there will be major Spec versions 4, 5 and on.
switch (specVersion) {
case 2:
$RefParserOptions = {
dereference: {
circular: false,
excludedPathMatcher: (path: string): any => {
return;
},
onDereference: (path: string, value: AsyncAPIObject) => {
if (options['x-origin']) {
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['x-origin']) {
value['x-origin'] = path;
}
},
},
};
break;
default:
console.error(
`There is no support for AsyncAPI Specification v${specVersion}.`
);
}

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
3 changes: 2 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ 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) {}
Expand Down
82 changes: 0 additions & 82 deletions src/v3/parser.ts

This file was deleted.

0 comments on commit 5799c6e

Please sign in to comment.