-
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
44b5576
commit cba9e21
Showing
7 changed files
with
123 additions
and
25 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,30 @@ | ||
// 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, Setter, Provider} from '@loopback/context'; | ||
import {Component, ProviderMap} from '@loopback/core'; | ||
import {ApiExplorerUIOptions, apiExplorerUI} from './explorer'; | ||
import {Handler} from 'express'; | ||
import {ExplorerBindings} from './keys'; | ||
|
||
export class ExplorerProvider implements Provider<Handler> { | ||
constructor( | ||
@inject(ExplorerBindings.CONFIG, {optional: true}) | ||
private config: ApiExplorerUIOptions, | ||
) {} | ||
|
||
value() { | ||
return apiExplorerUI(this.config); | ||
} | ||
} | ||
|
||
export class ExplorerComponent implements Component { | ||
providers?: ProviderMap; | ||
constructor() { | ||
this.providers = { | ||
[ExplorerBindings.HANDLER.key]: ExplorerProvider, | ||
}; | ||
} | ||
} |
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,19 @@ | ||
// 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 {RequestHandler} from 'express'; | ||
import {ApiExplorerUIOptions} from './explorer'; | ||
|
||
/** | ||
* Binding keys used by this component. | ||
*/ | ||
export namespace ExplorerBindings { | ||
export const HANDLER = BindingKey.create<RequestHandler>('explorer.handler'); | ||
|
||
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,45 @@ | ||
// 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 {createClientForHandler} 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: {}, | ||
}); | ||
|
||
// FIXME: How do we configure the API Explorer UI? | ||
server.bind(ExplorerBindings.CONFIG).to({}); | ||
|
||
// FIXME: Should the ExplorerComponent contribute a handler or route? | ||
const handler = await server.get(ExplorerBindings.HANDLER); | ||
|
||
// FIXME: How to register routes to the rest server by phase or order | ||
// FIXME: Should REST server automatically mounts the route based on registration | ||
// via bindings such as `rest.server.routes`? | ||
server.staticRouter.use('/explorer', handler); | ||
}); | ||
|
||
it('exposes "GET /explorer"', async () => { | ||
await createClientForHandler(server.requestHandler) | ||
.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); | ||
// 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