Skip to content

Commit 597f0b7

Browse files
committed
Docs: Updating documentation
1 parent 9a2fdd9 commit 597f0b7

File tree

11 files changed

+174
-400
lines changed

11 files changed

+174
-400
lines changed

docs/cors.md

Lines changed: 3 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -38,192 +38,9 @@ This configuration changes the request endpoint from `http://domain.com/path/?qu
3838

3939
The proxy URL won't be visible by the user. The user can't do anything to change this behavior unless your application includes a custom UI that supports such a change.
4040

41-
## Handling HTTP request by the hosting website / application
41+
## Handling HTTP request by the hosting application
4242

43-
When the user runs the request from the "try it" screen, API Console fires the `api-console-request` [custom event](https://developer.mozilla.org/en/docs/Web/API/CustomEvent). If your application can handle the transport (by providing a proxy, for example), listen for this event, and cancel it by calling `event.preventDefault()`. If the event is cancelled, API Console listens for the `api-console-response` custom
43+
When the user runs the request from the "try it" screen, API Console fires the `api-request` [custom event](https://developer.mozilla.org/en/docs/Web/API/CustomEvent). If your application can handle the transport (by providing a proxy, for example), listen for this event, and cancel it by calling `event.preventDefault()`. If the event is cancelled, API Console listens for the `api-response` custom
4444
event that should contain response details. Otherwise, the console uses the build in fallback function to get the resource using Fetch API / XHR.
4545

46-
#### api-console-request custom event
47-
48-
The Event `detail` object contains following properties:
49-
50-
Property | Type | Description
51-
----------------|-------------|----------
52-
`url` | `String` | The request URL. If proxy is set, it will be the final URL with the proxy value.
53-
`method` | `String` | The HTTP method
54-
`headers` | `String` | HTTP headers string to send with the message
55-
`payload` | `String` | Body to send
56-
57-
#### api-console-response
58-
59-
This event must be fired when the hosting app finishes the request. It must contain a generated Request
60-
and Response object as defined in the [Fetch specification](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch). API Console includes a polyfill for the Fetch API.
61-
62-
Property | Type | Description
63-
----------------|-------------|----------
64-
`request` | `Request` | The request object as defined in the Fetch API spec. See [Request docs](https://developer.mozilla.org/en-US/docs/Web/API/Request) for more information.
65-
`response` | `Response` | The response object as defined in the Fetch API spec. See [Response docs](https://developer.mozilla.org/en-US/docs/Web/API/Response) for more information.
66-
`isXhr` | `Boolean` | Default is `true`. Indicates that the transport method doesn't support advanced timings and redirects information. See [request-panel](https://elements.advancedrestclient.com/elements/raml-request-panel) documentation for more information.
67-
`error` | `Error` | When the request / response encounters an error (`request.ok` equals `false`), the error object is set with the human readable message for the user.
68-
69-
#### Example with handling request / response events
70-
71-
```javascript
72-
// Start time of executing the request
73-
var startTime;
74-
// Initial request data passed by the event.
75-
var requestData;
76-
/**
77-
* Creates a Headers object based on the HTTP headers string.
78-
*
79-
* @param {String} headers HTTP headers.
80-
* @return {Headers} Parsed headers object.
81-
*/
82-
function createHeaders(headers) {
83-
if (!headers) {
84-
return new Headers();
85-
}
86-
var result = new Headers();
87-
var list = headers.split('\n').map(function(line) {
88-
var _parts = line.split(':');
89-
var _name = _parts[0];
90-
var _value = _parts[1];
91-
_name = _name ? _name.trim() : null;
92-
_value = _value ? _value.trim() : null;
93-
if (!_name || !_value) {
94-
return null;
95-
}
96-
return {
97-
name: _name,
98-
value: _value
99-
};
100-
}).filter(function(item) {
101-
return !!item;
102-
});
103-
list.forEach(function(item) {
104-
result.append(item.name, item.value);
105-
});
106-
return result;
107-
}
108-
/**
109-
* Creates a request object from the event's request data.
110-
*
111-
* @param {Object} data Latest request data as in the `api-console-request` object event.
112-
* @return {Request} The Request object.
113-
*/
114-
function createRequest(data) {
115-
var init = {
116-
method: data.method,
117-
mode: 'cors'
118-
};
119-
if (data.headers) {
120-
init.headers = createHeaders(data.headers);
121-
}
122-
if (['GET', 'HEAD'].indexOf(data.method) !== -1) {
123-
data.payload = undefined;
124-
} else {
125-
if (data.payload) {
126-
init.body = data.payload;
127-
}
128-
}
129-
return new Request(data.url, init);
130-
}
131-
/**
132-
* Creates a response object from the response data.
133-
* If the response is invalid then returned Response object will be errored.
134-
*
135-
* @param {XMLHttpRequest} xhr The XHR object used to make a connection.
136-
* @return {Response} The response object.
137-
*/
138-
function createResponse(xhr) {
139-
var status = xhr.status;
140-
if (!status || status < 200) {
141-
return Response.error();
142-
}
143-
var init = {
144-
status: status,
145-
statusText: xhr.statusText
146-
};
147-
var headers = xhr.getAllResponseHeaders();
148-
if (headers) {
149-
init.headers = createHeaders(headers);
150-
}
151-
try {
152-
return new Response(xhr.responseText, init);
153-
} catch (e) {
154-
return Response.error();
155-
}
156-
}
157-
// General error handler.
158-
function errorHandler(e) {
159-
var loadTime = performance.now() - startTime;
160-
var request = createRequest(requestData);
161-
var detail = {
162-
request: request,
163-
response: Response.error(),
164-
loadingTime: loadTime,
165-
isXhr: true,
166-
error: new Error('Resource is unavailable')
167-
};
168-
var event = new CustomEvent('api-console-response', {
169-
cancelable: false,
170-
bubbles: true,
171-
composed: true,
172-
detail: detail
173-
});
174-
document.body.dispatchEvent(event);
175-
}
176-
// Handler for load event
177-
function loadHandler(e) {
178-
var loadTime = performance.now() - startTime;
179-
var request = createRequest(requestData);
180-
var response = createResponse(e.target);
181-
var detail = {
182-
request: request,
183-
response: response,
184-
loadingTime: loadTime,
185-
isXhr: true
186-
};
187-
if (!response.ok) {
188-
detail.error = new Error('Resource is unavailable');
189-
}
190-
var event = new CustomEvent('api-console-response', {
191-
cancelable: false,
192-
bubbles: true,
193-
composed: true,
194-
detail: detail
195-
});
196-
document.body.dispatchEvent(event);
197-
}
198-
// Handler to the event, sends the request
199-
function consoleRequestHandler(e) {
200-
requestData = e.detail;
201-
var xhr = new XMLHttpRequest();
202-
xhr.open(requestData.method, requestData.url, true);
203-
if (requestData.headers) {
204-
requestData.headers.split('\n').forEach(function(header) {
205-
var data = header.split(':');
206-
var name = data[0].trim();
207-
var value = '';
208-
if (data[1]) {
209-
value = data[1].trim();
210-
}
211-
try {
212-
xhr.setRequestHeader(name, value);
213-
} catch (e) {
214-
console.log('Can\'t set header ' + name ' in the XHR call.');
215-
}
216-
});
217-
}
218-
xhr.addEventListener('load', loadHandler);
219-
xhr.addEventListener('error', errorHandler);
220-
xhr.addEventListener('timeout', errorHandler);
221-
try {
222-
startTime = performance.now();
223-
xhr.send(requestData.payload);
224-
} catch (e) {
225-
errorHandler(e);
226-
}
227-
}
228-
window.addEventListener('api-console-request', consoleRequestHandler);
229-
```
46+
See https://github.com/advanced-rest-client/api-components-api/blob/master/docs/api-request-and-response.md for detailed documentation of the request events.

docs/gh-pages.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,9 @@ This example shows how to use Travis-CI to push API Console with the API documen
66

77
## Overview
88

9-
[docs/gh-pages/.travis.yml](gh-pages/.travis.yml) file contains a set of directives that Travis understands and executes. This file contains a minimal setup for the API console generation but you can change it.
9+
[docs/gh-pages/.travis.yml](gh-pages/.travis.yml) file contains a set of directives that Travis understands and executes. This file contains a minimal setup for the API console generation.
1010

11-
First, Travis installs node dependencies, which in this case is the
12-
[api-console-builder](https://www.npmjs.com/package/api-console-builder) module as defined in the [docs/gh-pages/package.json](gh-pages/package.json) file:
13-
14-
```yaml
15-
before_script:
16-
- npm install
17-
```
18-
19-
Next, Travis executes [docs/gh-pages/deploy.sh](gh-pages/deploy.sh) that processes your private key associated with a GitHub account, as discussed below. Finally, Travis checks out the latest version of your API, creates the console, and publishes the console in `gh-pages` branch.
11+
Travis executes [docs/gh-pages/deploy.sh](gh-pages/deploy.sh) that processes your private key associated with a GitHub account, as discussed below. Finally, Travis checks out the latest version of your API, creates the console, and publishes the console in `gh-pages` branch.
2012

2113
```yaml
2214
script: bash ./deploy.sh
@@ -44,7 +36,7 @@ If you already have generated keys, go to step 5.
4436
```
4537
Enter a file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/gh-travis_rsa
4638
```
47-
We use this filename in the build script.
39+
We use this filename in the build script.
4840

4941
4. Do _not_ set a password. When Travis runs the script there's no way to respond to a password prompt. Consequently, do not use this key for any other purpose. Doing so would be unsafe. Also, keep the key in a location that nobody can access.
5042

docs/gh-pages/.travis.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
language: node_js
22
node_js:
33
- stable
4-
before_script:
5-
- npm install
64
script: bash ./deploy.sh
75
before_install:
86
# Will be filled by travis CLI command

docs/gh-pages/build.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
/**
2-
* This file runs the API Console builder node module to create a bundled file of sources.
2+
* This file runs the API Console builder node module to create
3+
* a bundled file of sources.
34
*/
5+
46
const builder = require('api-console-builder');
5-
const fs = require('fs');
67

78
builder({
8-
src: 'https://github.com/mulesoft/api-console/archive/release/4.0.0.zip',
9-
dest: 'build',
10-
raml: 'api/api.raml', // note that the deploy.sh script will clone the repo to api/ folder.
11-
useJson: true,
12-
verbose: true
9+
verbose: true,
10+
destination: 'build', // Optional, default to "build"
11+
// note that the deploy.sh script will clone the repo to api/ folder.
12+
api: 'api/api.raml',
13+
apiType: 'RAML 1.0',
14+
tagName: '5.0.0-preview' // builds the console from specific version
1315
})
1416
.then(() => {
1517
console.log('Build complete');

docs/gh-pages/deploy.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ chmod 600 gh-travis_rsa
99
eval `ssh-agent -s`
1010
ssh-add gh-travis_rsa
1111

12-
# Just an example, you can build the console for any branch. We will buil for master only.
12+
# Just an example, you can build the console for any branch. We will build it for master only.
1313
if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "$SOURCE_BRANCH" ]; then
1414
echo "Skipping deploy."
1515
exit 0
@@ -20,7 +20,7 @@ REPO=`git config remote.origin.url`
2020
SSH_REPO=${REPO/https:\/\/github.com\//git@github.com:}
2121
SHA=`git rev-parse --verify HEAD`
2222

23-
# Cclone the repo a foler. We'll use api/ as a working dir.
23+
# Clone the repo to current location. We'll use "api/" as a working dir.
2424
git clone $REPO api
2525

2626
# Build the console out of the latest API release.

0 commit comments

Comments
 (0)