2015-09-16 20:38:09 +00:00
|
|
|
# loopback-component-explorer
|
2013-11-05 17:59:05 +00:00
|
|
|
|
2013-11-05 19:24:48 +00:00
|
|
|
Browse and test your LoopBack app's APIs.
|
|
|
|
|
2016-12-20 23:49:26 +00:00
|
|
|
## Supported versions
|
|
|
|
|
|
|
|
Current|Long Term Support
|
|
|
|
:-:|:-:
|
|
|
|
4.x|2.x
|
|
|
|
|
|
|
|
Learn more about our LTS plan in [docs](http://loopback.io/doc/en/contrib/Long-term-support.html).
|
|
|
|
|
|
|
|
*The only difference between 3.x and 4.x versions is which Node.js versions
|
|
|
|
are supported (loopback-component-explorer 4.x dropped support for pre-v4
|
|
|
|
versions of Node.js). Therefore we decided to not maintain 3.x version line and
|
|
|
|
provide LTS support for the 2.x version line instead.*
|
|
|
|
|
2013-11-05 19:24:48 +00:00
|
|
|
## Basic Usage
|
|
|
|
|
|
|
|
Below is a simple LoopBack application. The explorer is mounted at `/explorer`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
var loopback = require('loopback');
|
|
|
|
var app = loopback();
|
2014-07-04 19:28:47 +00:00
|
|
|
var explorer = require('../');
|
|
|
|
var port = 3000;
|
2013-11-05 19:24:48 +00:00
|
|
|
|
|
|
|
var Product = loopback.Model.extend('product');
|
|
|
|
Product.attachTo(loopback.memory());
|
|
|
|
app.model(Product);
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
app.use('/api', loopback.rest());
|
2015-07-01 16:09:38 +00:00
|
|
|
|
|
|
|
// Register explorer using component-centric API:
|
|
|
|
explorer(app, { basePath: '/api', mountPath: '/explorer' });
|
|
|
|
// Alternatively, register as a middleware:
|
|
|
|
app.use('/explorer', explorer.routes(app, { basePath: '/api' }));
|
|
|
|
|
2014-07-04 19:28:47 +00:00
|
|
|
console.log("Explorer mounted at localhost:" + port + "/explorer");
|
2013-11-05 19:24:48 +00:00
|
|
|
|
2014-07-04 19:28:47 +00:00
|
|
|
app.listen(port);
|
2013-11-05 19:24:48 +00:00
|
|
|
```
|
2014-07-04 19:28:47 +00:00
|
|
|
|
2015-07-01 16:09:38 +00:00
|
|
|
## Upgrading from v1.x
|
|
|
|
|
|
|
|
To upgrade your application using loopback-explorer version 1.x, just replace
|
|
|
|
`explorer()` with `explorer.routes()` in your server script:
|
|
|
|
|
|
|
|
```js
|
2015-09-16 20:38:09 +00:00
|
|
|
var explorer = require('loopback-component-explorer'); // Module was loopback-explorer in v. 2.0.1 and earlier
|
2015-07-01 16:09:38 +00:00
|
|
|
|
|
|
|
// v1.x - does not work anymore
|
|
|
|
app.use('/explorer', explorer(app, options);
|
|
|
|
// v2.x
|
|
|
|
app.use('/explorer', explorer.routes(app, options));
|
|
|
|
```
|
|
|
|
|
2017-02-02 01:36:01 +00:00
|
|
|
In applications scaffolded by `lb app`, the idiomatic way is to register
|
2015-09-16 20:38:09 +00:00
|
|
|
loopback-component-explorer in `server/component-config.json`:
|
2015-07-01 16:09:38 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
2015-09-16 20:38:09 +00:00
|
|
|
"loopback-component-explorer": {
|
2015-07-01 16:09:38 +00:00
|
|
|
"mountPath": "/explorer"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
## Advanced Usage
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
Many aspects of the explorer are configurable.
|
2014-07-09 22:38:05 +00:00
|
|
|
|
|
|
|
See [options](#options) for a description of these options:
|
|
|
|
|
|
|
|
```js
|
2014-07-10 19:16:10 +00:00
|
|
|
// Mount middleware before calling `explorer()` to add custom headers, auth, etc.
|
|
|
|
app.use('/explorer', loopback.basicAuth('user', 'password'));
|
2015-07-01 16:09:38 +00:00
|
|
|
explorer(app, {
|
2014-07-09 22:38:05 +00:00
|
|
|
basePath: '/custom-api-root',
|
2014-10-22 08:55:25 +00:00
|
|
|
uiDirs: [
|
|
|
|
path.resolve(__dirname, 'public'),
|
|
|
|
path.resolve(__dirname, 'node_modules', 'swagger-ui')
|
|
|
|
]
|
2014-07-09 22:38:05 +00:00
|
|
|
apiInfo: {
|
|
|
|
'title': 'My API',
|
|
|
|
'description': 'Explorer example app.'
|
|
|
|
},
|
2015-09-30 18:34:24 +00:00
|
|
|
resourcePath: 'swagger.json',
|
2014-07-09 22:38:05 +00:00
|
|
|
version: '0.1-unreleasable'
|
|
|
|
}));
|
|
|
|
app.use('/custom-api-root', loopback.rest());
|
|
|
|
```
|
2017-02-02 01:36:01 +00:00
|
|
|
In applications scaffolded by `lb app`, you can edit the `server/component-config.json`:
|
2016-10-23 08:53:43 +00:00
|
|
|
|
|
|
|
```json
|
|
|
|
{
|
|
|
|
"loopback-component-explorer": {
|
|
|
|
"mountPath": "/explorer",
|
|
|
|
"apiInfo": {
|
|
|
|
"title": "My App",
|
|
|
|
"description": "Description of my app APIs.",
|
|
|
|
"termsOfServiceUrl": "http://api.mycompany.io/terms/",
|
|
|
|
"contact": "apiteam@mycompany.io",
|
|
|
|
"license": "Apache 2.0",
|
|
|
|
"licenseUrl": "http://www.apache.org/licenses/LICENSE-2.0.html"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2014-07-09 22:38:05 +00:00
|
|
|
|
2014-07-04 19:28:47 +00:00
|
|
|
## Options
|
|
|
|
|
|
|
|
Options are passed to `explorer(app, options)`.
|
|
|
|
|
|
|
|
`basePath`: **String**
|
|
|
|
|
2014-07-09 22:38:05 +00:00
|
|
|
> Default: `app.get('restAPIRoot')` or `'/api'`.
|
|
|
|
|
|
|
|
> Sets the API's base path. This must be set if you are mounting your api
|
|
|
|
> to a path different than '/api', e.g. with
|
|
|
|
> `loopback.use('/custom-api-root', loopback.rest());
|
|
|
|
|
2015-07-01 16:09:38 +00:00
|
|
|
`mountPath`: **String**
|
|
|
|
|
|
|
|
> Default: `/explorer`
|
|
|
|
|
|
|
|
> Set the path where to mount the explorer component.
|
|
|
|
|
2014-07-26 15:53:08 +00:00
|
|
|
`protocol`: **String**
|
|
|
|
|
|
|
|
> Default: `null`
|
|
|
|
|
|
|
|
> A hard override for the outgoing protocol (`http` or `https`) that is designated in Swagger
|
2015-09-16 20:38:09 +00:00
|
|
|
> resource documents. By default, `loopback-component-explorer` will write the protocol that was used to retrieve
|
2014-07-26 15:53:08 +00:00
|
|
|
> the doc. This option is useful if, for instance, your API sits behind an SSL terminator
|
|
|
|
> and thus needs to report its endpoints as `https`, even though incoming traffic is auto-detected
|
|
|
|
> as `http`.
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
`uiDirs`: **Array of Strings**
|
2014-07-04 19:28:47 +00:00
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
> Sets a list of paths within your application for overriding Swagger UI files.
|
2014-07-04 19:28:47 +00:00
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
> If present, will search `uiDirs` first when attempting to load Swagger UI,
|
|
|
|
> allowing you to pick and choose overrides to the interface. Use this to
|
|
|
|
> style your explorer or add additional functionality.
|
2014-07-04 19:28:47 +00:00
|
|
|
|
|
|
|
> See [index.html](public/index.html), where you may want to begin your overrides.
|
|
|
|
> The rest of the UI is provided by [Swagger UI](https://github.com/wordnik/swagger-ui).
|
2014-07-09 22:38:05 +00:00
|
|
|
|
|
|
|
`apiInfo`: **Object**
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
> Additional information about your API. See the
|
2014-07-09 22:38:05 +00:00
|
|
|
> [spec](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#513-info-object).
|
|
|
|
|
|
|
|
`resourcePath`: **String**
|
|
|
|
|
|
|
|
> Default: `'resources'`
|
|
|
|
|
2014-10-22 08:55:25 +00:00
|
|
|
> Sets a different path for the
|
2014-07-09 22:38:05 +00:00
|
|
|
> [resource listing](https://github.com/wordnik/swagger-spec/blob/master/versions/1.2.md#51-resource-listing).
|
|
|
|
> You generally shouldn't have to change this.
|
|
|
|
|
|
|
|
`version`: **String**
|
|
|
|
|
|
|
|
> Default: Read from package.json
|
|
|
|
|
|
|
|
> Sets your API version. If not present, will read from your app's package.json.
|