-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): add local UI for api explorer
- Loading branch information
1 parent
70a78fe
commit b2ca7dc
Showing
9 changed files
with
182 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/explorer | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {inject} from '@loopback/context'; | ||
import {Application, Component, CoreBindings} from '@loopback/core'; | ||
import {RestServer} from '@loopback/rest'; | ||
import {ApiExplorerUIOptions, apiExplorerUI} from './explorer'; | ||
import {ExplorerBindings} from './keys'; | ||
|
||
/** | ||
* We have a few options: | ||
* | ||
* 1. The Explorer component contributes an express middleware so that REST | ||
* servers can mount the UI | ||
* | ||
* 2. The Explorer component contributes a route to REST servers | ||
* | ||
* 3. The Explorer component proactively mount itself with the RestApplication | ||
*/ | ||
export class ExplorerComponent implements Component { | ||
constructor( | ||
@inject(CoreBindings.APPLICATION_INSTANCE) private application: Application, | ||
@inject(ExplorerBindings.CONFIG, {optional: true}) | ||
private config: ApiExplorerUIOptions, | ||
) { | ||
this.init(); | ||
} | ||
|
||
init() { | ||
// FIXME: We should be able to receive the servers via injection | ||
const restServerBindings = this.application.find( | ||
binding => | ||
binding.key.startsWith(CoreBindings.SERVERS) && | ||
binding.valueConstructor === RestServer, | ||
); | ||
for (const binding of restServerBindings) { | ||
const restServer = this.application.getSync<RestServer>(binding.key); | ||
restServer.bind(ExplorerBindings.MIDDLEWARE).to({ | ||
path: this.config.path || '/explorer', | ||
handler: apiExplorerUI(this.config), | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/explorer | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {BindingKey} from '@loopback/context'; | ||
import {ApiExplorerUIOptions} from './explorer'; | ||
import {Middleware} from '@loopback/rest'; | ||
|
||
/** | ||
* Binding keys used by this component. | ||
*/ | ||
export namespace ExplorerBindings { | ||
export const MIDDLEWARE = BindingKey.create<Middleware>( | ||
'middleware.explorer', | ||
); | ||
|
||
export const CONFIG = BindingKey.create<ApiExplorerUIOptions | undefined>( | ||
'explorer.config', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright IBM Corp. 2018. All Rights Reserved. | ||
// Node module: @loopback/explorer | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
import {createClientForRestServer} from '@loopback/testlab'; | ||
import {RestServerConfig, RestComponent, RestServer} from '@loopback/rest'; | ||
import {Application} from '@loopback/core'; | ||
import {ExplorerComponent, ExplorerBindings} from '../..'; | ||
|
||
describe('API Explorer for REST Server', () => { | ||
let server: RestServer; | ||
|
||
before(async () => { | ||
server = await givenAServer({ | ||
rest: {port: 0}, | ||
}); | ||
}); | ||
|
||
after(async () => { | ||
await server.stop(); | ||
}); | ||
|
||
it('exposes "GET /explorer"', async () => { | ||
const test = await createClientForRestServer(server); | ||
test | ||
.get('/explorer') | ||
.expect(200, /\<title\>LoopBack API Explorer<\/title\>/) | ||
.expect('content-type', /text\/html.*/); | ||
}); | ||
}); | ||
|
||
async function givenAServer(options?: {rest: RestServerConfig}) { | ||
const app = new Application(options); | ||
app.component(RestComponent); | ||
app.bind(ExplorerBindings.CONFIG).to({}); | ||
// FIXME: Can we mount the ExplorerComponent to a given server? | ||
app.component(ExplorerComponent); | ||
const server = await app.getServer(RestServer); | ||
return server; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters