Skip to content

Commit

Permalink
fix: lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Souvikns committed Dec 4, 2023
1 parent 2374e0d commit cd85748
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 64 deletions.
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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.
Expand All @@ -97,7 +97,6 @@ export default async function bundle(files: string[], options: any = {}) {
});
}


return new Document(resolvedJsons as AsyncAPIObject[], options.base);
}

Expand Down
27 changes: 8 additions & 19 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
80 changes: 40 additions & 40 deletions src/v3/parser.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
}

0 comments on commit cd85748

Please sign in to comment.