Skip to content

Commit

Permalink
Use typescript type for declaring options
Browse files Browse the repository at this point in the history
  • Loading branch information
Laupetin committed Jul 18, 2024
1 parent 7d70682 commit aada076
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export class Document {
* @param {Object[]} parsedJSONList
* @param {Object} base
*/
constructor(parsedJSONList: AsyncAPIObject[], base: AsyncAPIObject) {
constructor(parsedJSONList: AsyncAPIObject[], base?: AsyncAPIObject) {
for (const resolvedJSON of parsedJSONList) {
this._doc = merge(this._doc, resolvedJSON);
}

if (typeof base !== 'undefined') {
if (base) {
this._doc = merge(this._doc, base);
}
}
Expand Down
42 changes: 27 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ import type { AsyncAPIObject } from './spec-types';
// remember the directory where execution of the program started
const originDir = String(process.cwd());

export declare interface BundlerOptions {
/**
* One relative/absolute path to base object whose properties will be retained.
*/
base?: string;
/**
* One relative/absolute path to directory relative to which paths to AsyncAPI
* Documents that should be bundled will be resolved.
*/
baseDir?: string;
/**
* Pass `true` to generate properties `x-origin` that will contain historical
* values of dereferenced `$ref`s.
*/
xOrigin?: boolean;
}

/**
*
* @param {string | string[]} files One or more relative/absolute paths to
* AsyncAPI Documents that should be bundled.
* @param {Object} [options]
* @param {string} [options.base] One relative/absolute path to base object whose
* properties will be retained.
* @param {string} [options.baseDir] One relative/absolute path to directory
* relative to which paths to AsyncAPI Documents that should be bundled will be
* resolved.
* @param {boolean} [options.xOrigin] Pass `true` to generate properties
* `x-origin` that will contain historical values of dereferenced `$ref`s.
*
* @return {Document}
*
* @example
Expand Down Expand Up @@ -83,7 +92,7 @@ const originDir = String(process.cwd());
*/
export default async function bundle(
files: string[] | string,
options: any = {}
options: BundlerOptions & Record<string, unknown> = {}
) {
// if one string was passed, convert it to an array
if (typeof files === 'string') {
Expand All @@ -102,14 +111,17 @@ export default async function bundle(

const majorVersion = versionCheck(parsedJsons);

if (typeof options.base !== 'undefined') {
let parsedBaseFile: AsyncAPIObject | undefined;
if (options.base) {
let baseFile = '';

if (typeof options.base === 'string') {
options.base = readFileSync(options.base, 'utf-8'); // eslint-disable-line
baseFile = readFileSync(options.base, 'utf-8'); // eslint-disable-line
} else if (Array.isArray(options.base)) {
options.base = readFileSync(String(options.base[0]), 'utf-8'); // eslint-disable-line
baseFile = readFileSync(String(options.base[0]), 'utf-8'); // eslint-disable-line
}
options.base = toJS(options.base);
await parse(options.base, majorVersion, options);
parsedBaseFile = toJS(baseFile) as AsyncAPIObject;
await parse(parsedBaseFile, majorVersion, options);
}

const resolvedJsons: AsyncAPIObject[] = await resolve(
Expand All @@ -123,7 +135,7 @@ export default async function bundle(
process.chdir(originDir);
}

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

// 'module.exports' is added to maintain backward compatibility with Node.js
Expand Down
4 changes: 3 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Parser } from '@asyncapi/parser';

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

const parser = new Parser();

Expand All @@ -18,7 +19,7 @@ let RefParserOptions: $RefParserOptions;
export async function parse(
JSONSchema: AsyncAPIObject,
specVersion: number,
options: any = {}
options: BundlerOptions = {}
) {
let validationResult: any[] = [];

Expand Down Expand Up @@ -82,6 +83,7 @@ export async function parse(
// Option `noValidation: true` is used by the testing system, which
// intentionally feeds Bundler wrong AsyncAPI Documents, thus it is not
// documented.
// @ts-ignore
if (!options.noValidation) {
validationResult = await parser.validate(
JSON.parse(JSON.stringify(dereferencedJSONSchema))
Expand Down
19 changes: 6 additions & 13 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parse } from './parser';
import { ParserError } from './errors';

import type { AsyncAPIObject } from './spec-types';
import type { BundlerOptions } from './index';

/**
* @private
Expand Down Expand Up @@ -39,28 +40,20 @@ export const toJS = (asyncapiYAMLorJSON: string | object) => {
return yaml.load(asyncapiYAMLorJSON);
};

/**
*
* @param {Object} asyncapiDocuments
* @param {Object} options
* @param {boolean} options.xOrigin
* @returns {Array<Object>}
* @private
*/
export const resolve = async (
export async function resolve(
asyncapiDocuments: AsyncAPIObject[],
specVersion: number,
options: any
) => {
const docs = [];
options: BundlerOptions
) {
const docs: AsyncAPIObject[] = [];

for (const asyncapiDocument of asyncapiDocuments) {
await parse(asyncapiDocument, specVersion, options);
docs.push(asyncapiDocument);
}

return docs;
};
}

/**
*
Expand Down

0 comments on commit aada076

Please sign in to comment.