Skip to content

Commit

Permalink
feat(editor-preview-asyncapi): add initial support for AsyncAPI 3.0.0 (
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n committed Mar 29, 2024
1 parent d7c0ffc commit 1626243
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
22 changes: 22 additions & 0 deletions src/plugins/editor-content-type/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ export const detectContentType = (content) => {
return editorActions.detectContentTypeSuccess({ contentType, content, requestId });
}

const asyncApi3JSONMatch = content.match(
/"asyncapi"\s*:\s*"(?<version_json>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))"/
);
if (asyncApi3JSONMatch !== null && fn.isValidJSONObject(content)) {
const { groups } = asyncApi3JSONMatch;
const version = groups?.version_json;
const contentType = `application/vnd.aai.asyncapi+json;version=${version}`;

return editorActions.detectContentTypeSuccess({ contentType, content, requestId });
}

const asyncApi3YAMLMatch = content.match(
/(?<YAML>^(["']?)asyncapi\2\s*:\s*(["']?)(?<version_yaml>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))\3(?:\s+|$))|(?<JSON>"asyncapi"\s*:\s*"(?<version_json>3\.(?:[1-9]\d*|0)\.(?:[1-9]\d*|0))")/m
);
if (asyncApi3YAMLMatch !== null && fn.isValidYAMLObject(content)) {
const { groups } = asyncApi3YAMLMatch;
const version = groups?.version_json ?? groups?.version_yaml;
const contentType = `application/vnd.aai.asyncapi+yaml;version=${version}`;

return editorActions.detectContentTypeSuccess({ contentType, content, requestId });
}

const openApi30xJSONMatch = content.match(detectionRegExpOpenAPIJSON30x);
if (openApi30xJSONMatch !== null && fn.isValidJSONObject(content)) {
const { groups } = openApi30xJSONMatch;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/editor-preview-asyncapi/monaco.contribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const lazyMonacoContribution = ({ system }) => {
const modelValue = model.getValue();
const modelVersionId = model.getVersionId();
const conditionFn = () =>
editorSelectors.selectIsContentTypeAsyncAPI2() &&
editorSelectors.selectIsContentTypeAsyncAPI() &&
modelValue === editorSelectors.selectContent() &&
!editorPreviewAsyncAPISelectors.selectIsParseInProgress();

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/editor-preview-asyncapi/wrap-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const detectContentTypeSuccess = createSafeActionWrapper(
({ content }) => {
const { editorSelectors, editorPreviewAsyncAPIActions } = system;

if (editorSelectors.selectIsContentTypeAsyncAPI2()) {
if (editorSelectors.selectIsContentTypeAsyncAPI()) {
editorPreviewAsyncAPIActions.parse(content);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import PropTypes from 'prop-types';
const EditorPreviewWrapper = (Original, system) => {
const EditorPreview = ({ getComponent, editorSelectors }) => {
const EditorPreviewAsyncAPI = getComponent('EditorPreviewAsyncAPI', true);
const isAsyncAPI = editorSelectors.selectIsContentTypeAsyncAPI();

return editorSelectors.selectIsContentTypeAsyncAPI2() ? (
<EditorPreviewAsyncAPI />
) : (
<Original {...system} /> // eslint-disable-line react/jsx-props-no-spreading
);
if (isAsyncAPI) {
return <EditorPreviewAsyncAPI />;
}

return <Original {...system} />; // eslint-disable-line react/jsx-props-no-spreading
};

EditorPreview.propTypes = {
getComponent: PropTypes.func.isRequired,
editorSelectors: PropTypes.shape({
selectIsContentTypeAsyncAPI2: PropTypes.func.isRequired,
selectIsContentTypeAsyncAPI: PropTypes.func.isRequired,
}).isRequired,
};

Expand Down

0 comments on commit 1626243

Please sign in to comment.