Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
feat!: add support for parser API v3 (#5)
Browse files Browse the repository at this point in the history
* update dependencies and add support for v3
  • Loading branch information
jonaslagoni authored Nov 17, 2023
1 parent cd52058 commit ce21a37
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 74 deletions.
176 changes: 143 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@
"@asyncapi/openapi-schema-parser": "^3.0.4",
"@asyncapi/protobuf-schema-parser": "^3.0.0",
"@asyncapi/raml-dt-schema-parser": "^4.0.4",
"parserv2": "npm:@asyncapi/parser@^2.1.0",
"parserv3": "npm:@asyncapi/parser@^3.0.0-next-major-spec.3"
"parserapiv1": "npm:@asyncapi/parser@^2.1.0",
"parserapiv2": "npm:@asyncapi/[email protected]",
"parserapiv3": "npm:@asyncapi/parser@^3.0.0-next-major-spec.10"
},
"devDependencies": {
"@jest/types": "^29.0.2",
Expand Down
23 changes: 14 additions & 9 deletions src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { createAsyncAPIDocument as createAsyncAPIDocumentParserV2 } from 'parserv2';
import { createAsyncAPIDocument as createAsyncAPIDocumentParserV3 } from 'parserv3';
import { createAsyncAPIDocument as createAsyncAPIDocumentParserV1 } from 'parserapiv1';
import { createAsyncAPIDocument as createAsyncAPIDocumentParserV2 } from 'parserapiv2';
import { createAsyncAPIDocument as createAsyncAPIDocumentParserV3 } from 'parserapiv3';

import type { AsyncAPIDocumentInterface as AsyncAPIDocumentInterfaceParserV2 } from 'parserv2';
import type { AsyncAPIDocumentInterface as AsyncAPIDocumentInterfaceParserV3 } from 'parserv3';
import type { AsyncAPIDocumentInterface as AsyncAPIDocumentInterfaceParserV1 } from 'parserapiv1';
import type { AsyncAPIDocumentInterface as AsyncAPIDocumentInterfaceParserV2 } from 'parserapiv2';
import type { AsyncAPIDocumentInterface as AsyncAPIDocumentInterfaceParserV3 } from 'parserapiv3';

import type { DetailedAsyncAPI as DetailedAsyncAPIParserV2 } from 'parserv2/esm/types';
import type { DetailedAsyncAPI as DetailedAsyncAPIParserV3 } from 'parserv3/esm/types';
import type { DetailedAsyncAPI as DetailedAsyncAPIParserV1 } from 'parserapiv1/esm/types';
import type { DetailedAsyncAPI as DetailedAsyncAPIParserV2 } from 'parserapiv2/esm/types';
import type { DetailedAsyncAPI as DetailedAsyncAPIParserV3 } from 'parserapiv3/esm/types';

export type AsyncAPIDocument = AsyncAPIDocumentInterfaceParserV2 | AsyncAPIDocumentInterfaceParserV3;
export type AsyncAPIDocument = AsyncAPIDocumentInterfaceParserV1 | AsyncAPIDocumentInterfaceParserV2 | AsyncAPIDocumentInterfaceParserV3;

export function ConvertDocumentParserAPIVersion(doc: AsyncAPIDocument, toParserAPIMajorVersion: number): AsyncAPIDocument {
if (!doc || !doc.json) return doc;
Expand All @@ -22,10 +25,12 @@ export function ConvertDocumentParserAPIVersion(doc: AsyncAPIDocument, toParserA
const detailedAsyncAPI = doc.meta().asyncapi;
switch (toParserAPIMajorVersion) {
case 1:
return createAsyncAPIDocumentParserV2(detailedAsyncAPI as DetailedAsyncAPIParserV2);
return createAsyncAPIDocumentParserV1(detailedAsyncAPI as DetailedAsyncAPIParserV1);
case 2:
return createAsyncAPIDocumentParserV2(detailedAsyncAPI as DetailedAsyncAPIParserV2);
case 3:
return createAsyncAPIDocumentParserV3(detailedAsyncAPI as DetailedAsyncAPIParserV3);
default:
return doc;
}
}
}
20 changes: 12 additions & 8 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { Parser as ParserV2 } from 'parserv2';
import { Parser as ParserV3 } from 'parserv3';
import { Parser as ParserV1 } from 'parserapiv1';
import { Parser as ParserV2 } from 'parserapiv2';
import { Parser as ParserV3 } from 'parserapiv3';

import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';
import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
import { RamlDTSchemaParser } from '@asyncapi/raml-dt-schema-parser';
import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser';

import type { ParserOptions as ParserOptionsParserV2 } from 'parserv2/esm/parser';
import type { ParserOptions as ParserOptionsParserV3 } from 'parserv3/esm/parser';
import type { ParserOptions as ParserOptionsParserV1 } from 'parserapiv1/esm/parser';
import type { ParserOptions as ParserOptionsParserV2 } from 'parserapiv2/esm/parser';
import type { ParserOptions as ParserOptionsParserV3 } from 'parserapiv3/esm/parser';

export type ParserOptions = ParserOptionsParserV2 | ParserOptionsParserV3;
export type ParserOptions = ParserOptionsParserV1 | ParserOptionsParserV2 | ParserOptionsParserV3;
export type Options = {
includeSchemaParsers?: boolean;
parserOptions?: ParserOptions;
}

type Parser = ParserV2 | ParserV3;
type Parser = ParserV1 | ParserV2 | ParserV3;

export function NewParser(parserAPIMajorVersion?: number, options?: Options): Parser {
const parserOptions: ParserOptions = options?.parserOptions || {};
Expand All @@ -42,9 +44,11 @@ export function NewParser(parserAPIMajorVersion?: number, options?: Options): Pa

switch (parserAPIMajorVersion) {
case 1:
return new ParserV1(parserOptions as ParserOptionsParserV1);
case 2:
return new ParserV2(parserOptions as ParserOptionsParserV2);
default: // default to latest version
case 2:
case 3:
return new ParserV3(parserOptions as ParserOptionsParserV3);
}
}
}
Loading

0 comments on commit ce21a37

Please sign in to comment.