-
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
bf10d3a
commit 2e9730e
Showing
11 changed files
with
193 additions
and
27 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
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,44 @@ | ||
// 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 {createRestAppClient} from '@loopback/testlab'; | ||
import { | ||
RestServerConfig, | ||
RestComponent, | ||
RestServer, | ||
RestApplication, | ||
} from '@loopback/rest'; | ||
import {Application} from '@loopback/core'; | ||
import {ExplorerComponent, ExplorerBindings} from '../..'; | ||
|
||
describe('API Explorer for REST Server', () => { | ||
let restApp: RestApplication; | ||
|
||
before(async () => { | ||
restApp = await givenRestApp({ | ||
rest: {port: 0}, | ||
}); | ||
}); | ||
|
||
after(async () => { | ||
await restApp.stop(); | ||
}); | ||
|
||
it('exposes "GET /explorer"', async () => { | ||
const test = await createRestAppClient(restApp); | ||
test | ||
.get('/explorer') | ||
.expect(200, /\<title\>LoopBack API Explorer<\/title\>/) | ||
.expect('content-type', /text\/html.*/); | ||
}); | ||
}); | ||
|
||
async function givenRestApp(options?: {rest: RestServerConfig}) { | ||
const app = new RestApplication(options); | ||
app.bind(ExplorerBindings.CONFIG).to({}); | ||
// FIXME: Can we mount the ExplorerComponent to a given server? | ||
app.component(ExplorerComponent); | ||
await app.start(); | ||
return app; | ||
} |
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