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

fix!: expose all functions for browser #937

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
24 changes: 24 additions & 0 deletions docs/migrations/v3-to-v4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Migrating from v2 to v3

The ONLY thing that changes between v3 and v4 has to do with the standalone browser bundle.

## New browser bundle

In the old browser bundle you only had access to the parser.

```js
const parser = new window.AsyncAPIParser();
const spec = '{ ... }';
const { document: parsedDocument, diagnostics } = await parser.parse(spec);
```

With the new browser bundle, you have access a bunch of support functions such as `fromURL`, `convertToOldAPI`, `unstringify`, `stringify`.

```js
const parser = new window.AsyncAPIParser.Parser();
const spec = '{ ... }';
const { document: parsedDocument, diagnostics } = await parser.parse(spec);
...
const result = window.AsyncAPIParser.fromURL(parser, 'http://localhost:8080/asyncapi.json');
const {document: parsedDocument, diagnostics} = await result.parse();
```
13 changes: 9 additions & 4 deletions test/browser/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ describe('Test browser Parser in the node env', function() {

console.info('start server');
server = http.createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/html' });
if (req.url === '/') {
res.writeHead(200, { 'content-type': 'text/html' });
return fs.createReadStream(htmlPath).pipe(res);
} else if (req.url === '/parser.js') {
res.writeHead(200, { 'content-type': 'text/html' });
return fs.createReadStream(parserScript).pipe(res);
} else if (req.url === '/asyncapi.json') {
res.writeHead(200, { 'content-type': 'application/json' });
res.write(JSON.stringify({ asyncapi: '2.0.0', info: { title: 'My API', version: '1.0.0' }, channels: { '/test/tester': { subscribe: { operationId: 'subscribeOperation', message: { } } } } }));
res.end();
}
});
server.listen(8080);
Expand All @@ -40,11 +45,11 @@ describe('Test browser Parser in the node env', function() {

console.info('navigating to localhost');
await page.goto('http://localhost:8080', { waitUntil: 'networkidle0' });
});
}, 10000);

afterAll(async function() {
await browser.close();
await server.close();
server.close();
});

it('should parse spec in the browser', async function() {
Expand All @@ -58,4 +63,4 @@ describe('Test browser Parser in the node env', function() {
const diagnostics = await page.evaluate(element => element && element.textContent, diagnosticsDiv);
expect(Number(diagnostics)).toBeGreaterThanOrEqual(0);
}, 5000);
});
});
7 changes: 3 additions & 4 deletions test/browser/sample-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
<script defer>
async function parse() {
try {
const parser = new window.AsyncAPIParser();
const spec = '{ "asyncapi": "2.0.0", "info": { "title": "My API", "version": "1.0.0" }, "channels": { "/test/tester": { "subscribe": { "operationId": "subscribeOperation", "message": { } } } } }';
const { document: parsedDocument, diagnostics } = await parser.parse(spec);

const parser = new window.AsyncAPIParser.Parser();
const result = window.AsyncAPIParser.fromURL(parser, 'http://localhost:8080/asyncapi.json');
const {document: parsedDocument, diagnostics} = await result.parse();
document.getElementById('content').innerHTML = parsedDocument.version();
document.getElementById('diagnostics').innerHTML = String(diagnostics.length);
} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ module.exports = {
globalObject: '(typeof self !== \'undefined\' ? self : this)',
library: {
name: 'AsyncAPIParser',
type: 'umd',
export: 'default',
type: 'umd'
},
},

Expand Down Expand Up @@ -46,4 +45,4 @@ module.exports = {
*/
// (require('webpack-bundle-analyzer').BundleAnalyzerPlugin()),
],
};
};
Loading