Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: conversion of openapi '3.0' to asyncapi '3.0' #269

Merged
merged 16 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ import { dump } from 'js-yaml';
import { converters as firstConverters } from "./first-version";
import { converters as secondConverters } from "./second-version";
import { converters as thirdConverters } from "./third-version";
import { converters as openapiConverters } from "./openapi";

import { serializeInput } from "./utils";

import type { AsyncAPIDocument, ConvertVersion, ConvertOptions, ConvertFunction } from './interfaces';
import type { AsyncAPIDocument, AsyncAPIConvertVersion, OpenAPIConvertVersion, ConvertOptions, ConvertFunction, ConvertOpenAPIFunction, OpenAPIDocument, OpenAPIToAsyncAPIOptions } from './interfaces';

/**
* Value for key (version) represents the function which converts specification from previous version to the given as key.
*/
const converters: Record<string, ConvertFunction> = {
const asyncAPIconverters: Record<string, ConvertFunction> = {
...firstConverters,
...secondConverters,
...thirdConverters,
};
const conversionVersions = Object.keys(converters);

export function convert(asyncapi: string, version?: ConvertVersion, options?: ConvertOptions): string;
export function convert(asyncapi: AsyncAPIDocument, version?: ConvertVersion, options?: ConvertOptions): AsyncAPIDocument;
export function convert(asyncapi: string | AsyncAPIDocument, version: ConvertVersion = '2.6.0', options: ConvertOptions = {}): string | AsyncAPIDocument {
const { format, document } = serializeInput(asyncapi);
const conversionVersions = Object.keys(asyncAPIconverters);

export function convert(input: string, version: AsyncAPIConvertVersion, options?: ConvertOptions): string;
export function convert(input: AsyncAPIDocument, version: AsyncAPIConvertVersion, options?: ConvertOptions): AsyncAPIDocument;
export function convert(input: string | AsyncAPIDocument, version: AsyncAPIConvertVersion , options: ConvertOptions= {}): string | AsyncAPIDocument {
const { format, document } = serializeInput(input);

if ('openapi' in document) {
throw new Error('Cannot convert OpenAPI document. Use convertOpenAPI function instead.');
}

const asyncapiVersion = document.asyncapi;
let fromVersion = conversionVersions.indexOf(asyncapiVersion);
Expand All @@ -41,12 +47,34 @@ export function convert(asyncapi: string | AsyncAPIDocument, version: ConvertVer
fromVersion++;
let converted = document;
for (let i = fromVersion; i <= toVersion; i++) {
const v = conversionVersions[i] as ConvertVersion;
converted = converters[v](converted, options);
const v = conversionVersions[i] as AsyncAPIConvertVersion;
converted = asyncAPIconverters[v](converted, options);
}

if (format === 'yaml') {
return dump(converted, { skipInvalid: true });
}
return converted;
}

export function convertOpenAPI(input: string ,version: OpenAPIConvertVersion,options?: OpenAPIToAsyncAPIOptions): string;
export function convertOpenAPI(input: OpenAPIDocument, version: OpenAPIConvertVersion ,options?: OpenAPIToAsyncAPIOptions): AsyncAPIDocument;
export function convertOpenAPI(input: string | OpenAPIDocument, version: OpenAPIConvertVersion, options: OpenAPIToAsyncAPIOptions = {}): string | AsyncAPIDocument {

const { format, document } = serializeInput(input);
const openApiVersion = document.openapi;
const converterVersion = openApiVersion === '3.0.0' ? '3.0.0' : openApiVersion;
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

const openapiToAsyncapiConverter = openapiConverters[converterVersion as OpenAPIConvertVersion] as ConvertOpenAPIFunction;

if (!openapiToAsyncapiConverter) {
throw new Error("OpenAPI to AsyncAPI converter is not available.");
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
}

const convertedAsyncAPI = openapiToAsyncapiConverter(document as OpenAPIDocument, options);

if (format === "yaml") {
return dump(convertedAsyncAPI, { skipInvalid: true });
}
return convertedAsyncAPI;
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { convert } from './convert';

export type { AsyncAPIDocument, ConvertVersion, ConvertOptions } from './interfaces';
export type { AsyncAPIDocument, AsyncAPIConvertVersion, OpenAPIConvertVersion, ConvertOptions } from './interfaces';
11 changes: 10 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@
* PUBLIC TYPES
*/
export type AsyncAPIDocument = { asyncapi: string } & Record<string, any>;
export type ConvertVersion = '1.1.0' | '1.2.0' | '2.0.0-rc1' | '2.0.0-rc2' | '2.0.0' | '2.1.0' | '2.2.0' | '2.3.0' | '2.4.0' | '2.5.0' | '2.6.0' | '3.0.0';
export type OpenAPIDocument = { openapi: string } & Record<string, any>;
export type AsyncAPIConvertVersion = '1.1.0' | '1.2.0' | '2.0.0-rc1' | '2.0.0-rc2' | '2.0.0' | '2.1.0' | '2.2.0' | '2.3.0' | '2.4.0' | '2.5.0' | '2.6.0' | '3.0.0';

export type OpenAPIConvertVersion = '3.0.0';
export type ConvertV2ToV3Options = {
idGenerator?: (data: { asyncapi: AsyncAPIDocument, kind: 'channel' | 'operation' | 'message', key: string | number | undefined, path: Array<string | number>, object: any, parentId?: string }) => string,
pointOfView?: 'application' | 'client',
useChannelIdExtension?: boolean;
convertServerComponents?: boolean;
convertChannelComponents?: boolean;
}

export type OpenAPIToAsyncAPIOptions = {
perspective?: 'client' | 'server';
};
export type ConvertOptions = {
v2tov3?: ConvertV2ToV3Options;
openAPIToAsyncAPI?: OpenAPIToAsyncAPIOptions;
}

/**
* PRIVATE TYPES
*/
export type ConvertFunction = (asyncapi: AsyncAPIDocument, options: ConvertOptions) => AsyncAPIDocument;
export type ConvertOpenAPIFunction = (openapi: OpenAPIDocument, options: OpenAPIToAsyncAPIOptions) => AsyncAPIDocument;

Loading
Loading