Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Jun 25, 2024
1 parent 4ecac89 commit 497f64a
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 226 deletions.
9 changes: 5 additions & 4 deletions examples/simple-react/src/SimpleAsyncapi.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useState, useEffect } from 'react';
import { ApplicationView } from '@asyncapi/edavisualiser';
import '@asyncapi/edavisualiser/styles/default.css';
const AsyncapiParser = require('@asyncapi/parser/browser');
import Parser from '@asyncapi/parser/browser';


const asyncAPIDocument = `
asyncapi: '2.4.0'
Expand Down Expand Up @@ -219,10 +220,10 @@ components:
`;

function Asyncapi() {
const [document, setDocument] = useState(undefined);
const [document, setDocument] = useState<any | undefined>(undefined);

useEffect(() => {
const parser = new AsyncapiParser.Parser();
const parser = new Parser();
const fetchData = async () => {
const {document} = await parser.parse(asyncAPIDocument)
setDocument(document);
Expand All @@ -233,7 +234,7 @@ function Asyncapi() {
let node;
if (document !== undefined) {
node = (
<ApplicationView asyncapi={{ document }} edgeType={'animated'} />
<ApplicationView asyncapi={{ document: document }} edgeType={'animated'} />
);
} else {
node = <h1>Wait...</h1>;
Expand Down
4 changes: 2 additions & 2 deletions examples/simple-react/src/gamingapi/System.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { useState, useEffect } from 'react';
import { SystemView, fromURL } from '@asyncapi/edavisualiser';
import { apps } from './apps';
import '@asyncapi/edavisualiser/styles/default.css';
const AsyncapiParser = require('@asyncapi/parser/browser');
import Parser from '@asyncapi/parser/browser';

function Asyncapi() {
const [asyncapiDocuments, setAsyncapiDocuments] = useState<Array<{ parsedDoc: any, name: string }>>([]);

useEffect(() => {
const fetchData = async () => {
const parser = new AsyncapiParser.Parser();
const parser = new Parser();
const data = [];
for (const [name, asyncapiUrl] of Object.entries(apps)) {
const result = fromURL(parser, asyncapiUrl);
Expand Down
1 change: 0 additions & 1 deletion examples/simple-react/types/asyncapi.d.ts

This file was deleted.

634 changes: 616 additions & 18 deletions library/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"highlight.js": "^11.5.1",
"marked": "^4.0.16",
"react-flow-renderer": "9.6.9",
"tailwindcss": "^2.1.1"
"tailwindcss": "^2.1.1",
"@smoya/multi-parser": "^5.0.1"
},
"devDependencies": {
"@cypress/webpack-preprocessor": "^5.9.0",
Expand Down
32 changes: 29 additions & 3 deletions library/src/helpers/AsyncAPIParserHelper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
Parser,
ParseOptions,
ParseOutput,
ValidateOptions,
Input,
Diagnostic,
AsyncAPIDocumentInterface,
} from '@asyncapi/parser';
import AsyncAPIParser from '@asyncapi/parser/browser';
import { AsyncapiApplicationData } from '../types';

/**
* Functionality taken from the AsyncAPI parser
Expand All @@ -17,15 +19,14 @@ export interface FromResult {
}

export function fromURL(
parser: Parser,
parser: any,
source: string,
options?: RequestInit,
): FromResult {
async function fetchUrl(): Promise<Input> {
const fetchFn = await getFetch();
return (await fetchFn(source, options as any)).text();
}

return {
async parse(options: ParseOptions = {}) {
const schema = await fetchUrl();
Expand All @@ -37,6 +38,31 @@ export function fromURL(
},
};
}
export async function getDocument({
document,
documentUrl,
rawDocument,
}: AsyncapiApplicationData): Promise<AsyncAPIDocumentInterface> {
if (document) {
return Promise.resolve(document);
}
let output: ParseOutput | undefined;
const parserOptions = { ruleset: {} };
const parser = new (AsyncAPIParser as any).Parser(parserOptions);
if (documentUrl) {
output = await fromURL(parser, documentUrl).parse();
}
if (rawDocument) {
output = await parser.parse(rawDocument);
}
if (!output) {
return Promise.reject('Could not resolve document');
}
if (output.document === undefined) {
return Promise.reject(output.diagnostics);
}
return Promise.resolve(output.document);
}

let __fetchFn: typeof fetch | undefined;
async function getFetch(): Promise<typeof fetch> {
Expand Down
4 changes: 3 additions & 1 deletion library/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export interface ApplicationServerData {
}

export interface AsyncapiApplicationData {
document: AsyncAPIDocumentInterface;
document?: AsyncAPIDocumentInterface;
rawDocument?: string;
documentUrl?: string;
topExtended?: JSX.Element;
}

Expand Down
Loading

0 comments on commit 497f64a

Please sign in to comment.