Skip to content

Commit

Permalink
improve newAsyncAPIDocument function
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Mar 21, 2022
1 parent 1256cc9 commit dbc33df
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
22 changes: 9 additions & 13 deletions src/models/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import { AsyncAPIDocumentV3 } from "./v3";
import type { InfoInterface } from "./info";
import type { BaseModel } from "./base";
import type { ExtensionsMixinInterface } from "./mixins";
import type { DetailedAsyncAPI } from "../types";

export interface AsyncAPIDocumentInterface extends BaseModel, ExtensionsMixinInterface {
version(): string;
info(): InfoInterface;
}

export function newAsyncAPIDocument(json: Record<string, any>): AsyncAPIDocumentInterface {
const version = json['asyncapi']; // Maybe this should be an arg.
if (version == undefined || version == null || version == '') {
throw new Error('Missing AsyncAPI version in document');
}

const major = version.split(".")[0];
switch (major) {
case '2':
return new AsyncAPIDocumentV2(json, { parent: null, asyncapi: json as any, pointer: '/' });
case '3':
return new AsyncAPIDocumentV3(json, { parent: null, asyncapi: json as any, pointer: '/' });
export function newAsyncAPIDocument(document: DetailedAsyncAPI): AsyncAPIDocumentInterface {
const majorVersion = document.semver.major;
switch (majorVersion) {
case 2:
return new AsyncAPIDocumentV2(document.parsed, { parent: null, asyncapi: document, pointer: '/' });
case 3:
return new AsyncAPIDocumentV3(document.parsed, { parent: null, asyncapi: document, pointer: '/' });
default:
throw new Error(`Unsupported version: ${version}`);
throw new Error(`Unsupported AsyncAPI version: ${majorVersion}`);
}
}
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function parse(asyncapi: ParserInput, options?: ParseOptions): Prom

const detailed = createDetailedAsyncAPI(asyncapi as string | Record<string, unknown>, parsed);
await customOperations(detailed, options);
const parsedDoc = newAsyncAPIDocument(parsed);
const parsedDoc = newAsyncAPIDocument(detailed);

return {
source: asyncapi,
Expand Down

0 comments on commit dbc33df

Please sign in to comment.