From cd85748a69d7fa3cba13dd00c0a6087ea5b2d054 Mon Sep 17 00:00:00 2001 From: souvik Date: Mon, 4 Dec 2023 19:10:49 +0530 Subject: [PATCH] fix: lint fixes --- src/index.ts | 9 +++--- src/util.ts | 27 +++++----------- src/v3/parser.ts | 80 ++++++++++++++++++++++++------------------------ 3 files changed, 52 insertions(+), 64 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0894cf9..3c5b9d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import { toJS, resolve, isVersionThree, versionCheck } from './util'; +import { toJS, resolve, versionCheck } from './util'; import { Document } from './document'; import { parse } from './parser'; @@ -80,13 +80,13 @@ export default async function bundle(files: string[], options: any = {}) { const parsedJsons = files.map(file => toJS(file)) as AsyncAPIObject[]; - const majorVersion = versionCheck(parsedJsons) - let resolvedJsons + const majorVersion = versionCheck(parsedJsons); + let resolvedJsons; if (majorVersion === 3) { resolvedJsons = await resolveV3Document(parsedJsons, { referenceIntoComponents: options.referenceIntoComponents - }) + }); } else { /** * Bundle all external references for each file. @@ -97,7 +97,6 @@ export default async function bundle(files: string[], options: any = {}) { }); } - return new Document(resolvedJsons as AsyncAPIObject[], options.base); } diff --git a/src/util.ts b/src/util.ts index 5a64040..93b2cdf 100644 --- a/src/util.ts +++ b/src/util.ts @@ -3,7 +3,6 @@ import { cloneDeep } from 'lodash'; import yaml from 'js-yaml'; import { parse } from './parser'; import { ParserError } from './errors'; -import {parse as parseV3} from './v3/parser' import type { AsyncAPIObject } from './spec-types'; @@ -84,30 +83,20 @@ export const resolve = async ( * @param asyncapiDocument {AsyncAPIObject} * @returns {boolean} */ -export const isVersionThree = (asyncapiDocuments: AsyncAPIObject[]): boolean => { - for (const asyncapiDocument of asyncapiDocuments ) { - const version = asyncapiDocument.asyncapi - const [major, rest] = version.split('.') - if (parseInt(major) < 3) { - return false - } - } - return true -} -export function getSpecVersion(asyncapiDocument: AsyncAPIObject): number{ - const versionString = asyncapiDocument.asyncapi - return parseInt(versionString) +export function getSpecVersion(asyncapiDocument: AsyncAPIObject): number { + const versionString = asyncapiDocument.asyncapi; + return parseInt(versionString, 10); } export function versionCheck(asyncapiDocuments: AsyncAPIObject[]): number { - let currentVersion = getSpecVersion(asyncapiDocuments[0]) + let currentVersion = getSpecVersion(asyncapiDocuments[0]); for (const asyncapiDocument of asyncapiDocuments) { - const majorVersion = getSpecVersion(asyncapiDocument) + const majorVersion = getSpecVersion(asyncapiDocument); if (majorVersion !== currentVersion) { - throw new Error('Unable to bundle specification file of different major versions') + throw new Error('Unable to bundle specification file of different major versions'); } - currentVersion = majorVersion + currentVersion = majorVersion; } - return currentVersion + return currentVersion; } \ No newline at end of file diff --git a/src/v3/parser.ts b/src/v3/parser.ts index 149c8bc..c7f92e7 100644 --- a/src/v3/parser.ts +++ b/src/v3/parser.ts @@ -1,63 +1,63 @@ -import $RefParser from '@apidevtools/json-schema-ref-parser' -import { JSONPath } from 'jsonpath-plus' -import { merge } from 'lodash' +import $RefParser from '@apidevtools/json-schema-ref-parser'; +import { JSONPath } from 'jsonpath-plus'; +import { merge } from 'lodash'; -import { $Refs } from '@apidevtools/json-schema-ref-parser' -import { AsyncAPIObject } from 'spec-types' +import { $Refs } from '@apidevtools/json-schema-ref-parser'; +import { AsyncAPIObject } from 'spec-types'; class ExternalComponents { - ref - resolvedJSON + ref; + resolvedJSON; constructor(ref: string, resolvedJSON: string) { - this.ref = ref - this.resolvedJSON = resolvedJSON + this.ref = ref; + this.resolvedJSON = resolvedJSON; } getKey() { - const keys = this.ref.split('/') - return keys[keys.length - 1] + const keys = this.ref.split('/'); + return keys[keys.length - 1]; } getValue() { - return this.resolvedJSON + return this.resolvedJSON; } } function crawlChannelPropertiesForRefs(JSONSchema: any) { return JSONPath({ json: JSONSchema, - path: `$.channels.*.messages.*.['$ref']`, - }) + path: '$.channels.*.messages.*.[\'$ref\']', + }); } export function isExternalReference(ref: string): boolean { - return typeof ref === 'string' && !ref.startsWith('#') + return typeof ref === 'string' && !ref.startsWith('#'); } async function resolveExternalRefs(parsedJSON: any, $refs: $Refs) { - const componentObj: any = { messages: {} } + const componentObj: any = { messages: {} }; JSONPath({ json: parsedJSON, resultType: 'all', path: '$.channels.*.messages.*', }).forEach( ({ parent, parentProperty }: { parent: any; parentProperty: string }) => { - const ref = parent[String(parentProperty)]['$ref'] + const ref = parent[String(parentProperty)]['$ref']; if (isExternalReference(ref)) { - const value: any = $refs.get(ref) - const component = new ExternalComponents(ref, value) + const value: any = $refs.get(ref); + const component = new ExternalComponents(ref, value); if (componentObj.messages) { componentObj.messages[String(component.getKey())] = - component.getValue() as unknown + component.getValue() as unknown; } parent[String(parentProperty)][ '$ref' - ] = `#/components/messages/${component.getKey()}` + ] = `#/components/messages/${component.getKey()}`; } } - ) + ); - return componentObj + return componentObj; } async function resolveExternalRefsForOperation(parsedJSON: any, $refs: $Refs) { @@ -67,40 +67,40 @@ async function resolveExternalRefsForOperation(parsedJSON: any, $refs: $Refs) { path: '$.operations.*.messages.*' }).forEach( ({parent, parentProperty}: {parent: any, parentProperty: string}) => { - parent.forEach( (reference: any) => { - const ref = reference['$ref'] + parent.forEach((reference: any) => { + const ref = reference['$ref']; if (isExternalReference(ref)) { - const value: any = $refs.get(ref) - const component = new ExternalComponents(ref, value) - parent[parentProperty]['$ref'] = `#/components/messages/${component.getKey()}` + const value: any = $refs.get(ref); + const component = new ExternalComponents(ref, value); + parent[parentProperty]['$ref'] = `#/components/messages/${component.getKey()}`; } - }) + }); } - ) + ); } export async function parse(JSONSchema: any) { - const $ref: any = await $RefParser.resolve(JSONSchema) - const refs = crawlChannelPropertiesForRefs(JSONSchema) + const $ref: any = await $RefParser.resolve(JSONSchema); + const refs = crawlChannelPropertiesForRefs(JSONSchema); for (const ref of refs) { if (isExternalReference(ref)) { - const componentObj = await resolveExternalRefs(JSONSchema, $ref) - await resolveExternalRefsForOperation(JSONSchema, $ref) + const componentObj = await resolveExternalRefs(JSONSchema, $ref); + await resolveExternalRefsForOperation(JSONSchema, $ref); if (JSONSchema.components) { - merge(JSONSchema.components, componentObj) + merge(JSONSchema.components, componentObj); } else { - JSONSchema.components = componentObj + JSONSchema.components = componentObj; } } } } export async function resolveV3Document(asyncapiDocuments: AsyncAPIObject[], options: any) { - const docs = [] + const docs = []; for (const asyncapiDocument of asyncapiDocuments) { - await parse(asyncapiDocument) + await parse(asyncapiDocument); //const bundledAsyncAPIDocument = await $RefParser.bundle(asyncapiDocument) - docs.push(asyncapiDocument) + docs.push(asyncapiDocument); } - return docs + return docs; } \ No newline at end of file