Skip to content

Commit 4019280

Browse files
committed
Update: Updated documentation
1 parent cfcab19 commit 4019280

File tree

3 files changed

+52
-61
lines changed

3 files changed

+52
-61
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
**See live example of the API console in our [demo application].**
2+
13
# The API Console
24

35
MuleSoft's API Console is a full-fledged API documentation tool that generates mobile-friendly web documentation based on RAML (Restful API Modeling Language) documents. In addition to providing documentation, the tool provides the capability for users to try out requests on the fly.
46

57
[![API Console](docs/new-console-header.png)](https://mulesoft.github.io/api-console)
68

7-
**See live example of the API console in our [demo application].**
8-
99
## Introduction
1010

1111
In this repository, you can find the source for a single HTML element that represents API Console.
@@ -16,6 +16,26 @@ The following sections briefly describe how to build and use the console. For mo
1616

1717
## Using the API console
1818

19+
Install our CLI tool globally using `-g` if possible:
20+
21+
```shell
22+
$ sudo npm install -g api-console-cli
23+
```
24+
25+
Generate API console from your RAML file:
26+
27+
```shell
28+
$ api-console build https://domain.com/api.raml # works with local files too
29+
```
30+
31+
Preview the console:
32+
33+
```shell
34+
$ api-console serve build/
35+
```
36+
37+
That's all you need to build the API console for your API. Below we'll describe how to customize the console.
38+
1939
API Console comes in two flavors.
2040

2141
* A **standalone web-application**

docs/passing-raml-data.md

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The default version of the **API console does not contain the RAML parser**. It is by design to minimize the size of the console and to optimize startup time.
44

5-
You can however use the console with the RAML parser setting up the build / CLI tools or by adding the dependency manually. **This document describes why the parser has been removed from the core console code and how to use the parser with new console.**
5+
You can use the console with the RAML parser by setting up the build / CLI tools or by adding the dependency manually. **This document describes why the parser has been removed from the core console code and how to use the parser with new console.**
66

77
## Performance of the API console
88

@@ -44,7 +44,7 @@ You can install parser and enhancer in your project by calling [bower][9] comman
4444
$ bower install --save advanced-rest-client/raml-json-enhance advanced-rest-client/raml-js-parser
4545
```
4646

47-
You can use `package.json` script's declaration for the same command:
47+
You can use `package.json` script declaration for the same command:
4848

4949
```json
5050
"scripts": {
@@ -77,59 +77,32 @@ Also check out our usage guide of the `<api-console>` element in our [element's
7777
<api-console></api-console>
7878

7979
<script>
80-
var parser = document.querySelector('raml-js-parser');
81-
parser.addEventListener('api-parse-ready', function(e) {
82-
var enhacer = document.querySelector('raml-json-enhance');
83-
enhacer.json = e.detail.json.specification;
84-
});
85-
window.addEventListener('raml-json-enhance-ready', function(e) {
86-
// The e.detail.json contains the final JavaScript object
87-
var apiConsole = document.querySelector('api-console');
88-
apiConsole.raml = e.detail.json;
89-
});
90-
// Assuming components are already loaded and registered
91-
parser.loadApi('https://domain.com/api.raml');
92-
</script>
93-
```
94-
95-
The `json` attribute set on `raml-js-parser` element tells the parser that the output should be a JavaScript object instead of the AST.
96-
97-
## Asynchronous environment
98-
99-
Web components are asynchronous by nature. Importing the elements, registering them in the DOM, and finally initializing an element is done asynchronously. Therefore, you can't expect the element to work immediately after loading the page as a typical HTML element does. Consider the following example:
100-
101-
```html
102-
<link rel="import" href="bower_components/raml-js-parser/raml-js-parser.html">
103-
<raml-js-parser json></raml-js-parser>
104-
<script>
105-
var parser = document.querySelector('raml-js-parser');
106-
parser.loadApi(apiFileUrl);
107-
</script>
108-
```
109-
110-
Running this code as the page loads throws a `TypeError` with the message: `parser.loadApi is not a function`.
111-
112-
At the time of execution of this script block, the browser knows nothing about the `raml-js-parser` element. At this time, the element is an instance of `HTMLUnknownElement`. You can read more about custom elements registration in our [starters guide to web components][2].
113-
114-
The browser has to import the source of the element first, and then the Polymer library has to register custom HTML element called `raml-js-parser`.
115-
116-
To run the code properly you have to listen for the `WebComponentsReady` event. It's fired when the elements are ready to use.
117-
118-
```html
119-
<link rel="import" href="bower_components/raml-js-parser/raml-js-parser.html">
120-
<raml-js-parser json></raml-js-parser>
121-
<script>
122-
function init() {
123-
var parser = document.querySelector('raml-js-parser');
124-
parser.loadApi(apiFileUrl);
80+
var MyAPiApp = {
81+
init: function() {
82+
var parser = document.querySelector('raml-js-parser');
83+
parser.addEventListener('api-parse-ready', MyAPiApp._ramlReady);
84+
parser.loadApi('https://domain.com/api.raml');
85+
window.addEventListener('raml-json-enhance-ready', MyAPiApp._jsonReady);
86+
},
87+
88+
_ramlReady: function(e) {
89+
var enhacer = document.querySelector('raml-json-enhance');
90+
enhacer.json = e.detail.json.specification;
91+
},
92+
93+
_jsonReady: function(e) {
94+
// The e.detail.json contains the final JavaScript object
95+
var apiConsole = document.querySelector('api-console');
96+
apiConsole.raml = e.detail.json;
97+
}
12598
};
126-
window.addEventListener('WebComponentsReady', init);
99+
window.addEventListener('WebComponentsReady', MyAPiApp.init);
127100
</script>
128101
```
129102

130-
Because of differences between `v0` and `v1` spec of custom elements and fallback provided by the Polymer library read [our guide](api-console-element.md) of how to properly listen for the elements registration event.
103+
The `json` attribute set on `raml-js-parser` element tells the parser that the output should be a JavaScript object instead of the AST.
131104

132-
### Setting RAML data as an HTML attribute
105+
### Setting RAML data as a HTML attribute
133106

134107
The basic method for determining what API Console displays is to use the `raml` attribute. The attribute accepts the JavaScript object produced by the parser and the enhancer described above.
135108

docs/web-components.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Starting with web components
1+
# Getting started with web components
22

33
Web Components is relatively new specification for the web platform and therefore it is understandable that not all developers know how to use them.
44

@@ -51,21 +51,19 @@ When the element is already registered and it is instantiated and inserted into
5151

5252
**adoptedCallback** is called when the custom element has been moved into a new document.
5353

54-
## So when an element is ready to be used?
54+
## So when an element is ready to use?
5555

56-
If you include definition of your element into the source of the web page and register it before application code runs then you can use the element's API right away. When using API console build tools one of the options do exactly that. At the time when browser parser runs JavaScript code of the application (when it suppose to parse RAML or set JavaScript object on the console) all elements are already registered and full API is available.
57-
58-
However, even when using our build tools, you can include console elements definition from other file to defer the source code download. In this case you should call element's API differently.
56+
If you include definition of your element into the source of the web page and register it before application code runs then you can use the element's API right away. When using API console build tools the default build is a standalone application that contains all dependencies in index page source. At the time when browser parser runs JavaScript code of the application (when it suppose to parse RAML data or set JavaScript object on the console) all elements are already registered and full API is available.
5957

58+
It is possible to include console's sources from an external file (to reduce initial load time, for example). In this
59+
case web components are not yet registered when running application's JavaScript code.
6060
Properties can be set whether the element is `HTMLUnknownElement` or defined element. Once the element is upgraded created the `attributeChangedCallback` is called for each set attribute.
6161

62-
Calling element's API methods exposed to external environment (outside shadow DOM) the methods will be `undefined` for `HTMLUnknownElement`. Therefore the application has to wait until the element is upgraded.
63-
64-
In the API console that still works on so called `v0` specification of the web components the application would wait until `WebComponentsReady` event is fired (it is provided by the Polymer library if not supported natively). After the event is fired all element should be registered and upgraded (if already in the DOM).
62+
Calling element's API methods exposed to external environment (outside shadow DOM) will result with error because at the time the element hasn't been upgraded. Application must wait until `WebComponentsReady` event is fired by the polyfill library. After that event all components are upgraded and ready to use.
6563

6664
## Creating a web component
6765

68-
There are already a lot of nice tutorials about creating web components so it's redundant. Personally I can suggest Eric Bidelman's [Custom Elements v1: Reusable Web Components](https://developers.google.com/web/fundamentals/architecture/building-components/customelements). It is a good introduction into web components and explanation how to register and use them.
66+
There are already a lot of good tutorials about creating web components so it's redundant. Personally I can suggest Eric Bidelman's [Custom Elements v1: Reusable Web Components](https://developers.google.com/web/fundamentals/architecture/building-components/customelements). It is a good introduction to web components and explanation how to register and use them.
6967

7068
If you have any question use our issue tracker to contact me. I'll be happy to help you to use the API console.
7169

0 commit comments

Comments
 (0)