Skip to content

Commit

Permalink
chore(rest-explorer): remove redirect from /explorer/ to /explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 8, 2019
1 parent 56126a0 commit 0eeae5b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ describe('API Explorer (acceptance)', () => {
.expect(/<title>LoopBack API Explorer/);
});

it('redirects from "/explorer" to "/explorer/"', async () => {
it('exposes API Explorer at "/explorer"', async () => {
await request
.get('/explorer')
.expect(301)
.expect('location', '/explorer/');
.expect(200)
.expect('content-type', /html/)
.expect(/<title>LoopBack API Explorer/);
});

it('configures swagger-ui with OpenAPI spec url "/openapi.json', async () => {
Expand Down Expand Up @@ -92,8 +93,7 @@ describe('API Explorer (acceptance)', () => {

await request
.get('/openapi/ui')
.expect(301)
.expect('Location', '/openapi/ui/');
.expect(200, /<title>LoopBack API Explorer/);

await request.get('/explorer').expect(404);
});
Expand Down
3 changes: 1 addition & 2 deletions packages/rest-explorer/src/rest-explorer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export class RestExplorerComponent implements Component {
) {
const explorerPath = config.path || '/explorer';

this.registerControllerRoute('get', explorerPath, 'indexRedirect');
this.registerControllerRoute('get', explorerPath + '/', 'index');
this.registerControllerRoute('get', explorerPath, 'index');

application.static(explorerPath, swaggerUI.getAbsoluteFSPath());

Expand Down
15 changes: 9 additions & 6 deletions packages/rest-explorer/src/rest-explorer.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,43 @@

import {inject} from '@loopback/context';
import {
RestBindings,
RestServerConfig,
OpenApiSpecForm,
Request,
Response,
RestBindings,
RestServerConfig,
} from '@loopback/rest';
import * as ejs from 'ejs';
import * as fs from 'fs';
import * as path from 'path';
import {RestExplorerBindings} from './rest-explorer.keys';
import {RestExplorerConfig} from './rest-explorer.types';

// TODO(bajtos) Allow users to customize the template
const indexHtml = path.resolve(__dirname, '../templates/index.html.ejs');
const template = fs.readFileSync(indexHtml, 'utf-8');
const templateFn = ejs.compile(template);

export class ExplorerController {
private explorerPath: string;
private openApiSpecUrl: string;

constructor(
@inject(RestBindings.CONFIG, {optional: true})
restConfig: RestServerConfig = {},
@inject(RestExplorerBindings.CONFIG, {optional: true})
config: RestExplorerConfig = {},
@inject(RestBindings.Http.REQUEST) private request: Request,
@inject(RestBindings.Http.RESPONSE) private response: Response,
) {
this.openApiSpecUrl = this.getOpenApiSpecUrl(restConfig);
}

indexRedirect() {
this.response.redirect(301, this.request.url + '/');
this.explorerPath = config.path || '/explorer';
}

index() {
const data = {
openApiSpecUrl: this.openApiSpecUrl,
explorerPath: this.explorerPath,
};

const homePage = templateFn(data);
Expand Down
10 changes: 5 additions & 5 deletions packages/rest-explorer/templates/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<head>
<meta charset="UTF-8">
<title>LoopBack API Explorer</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css">
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<link rel="stylesheet" type="text/css" href="<%- explorerPath %>/swagger-ui.css">
<link rel="icon" type="image/png" href="<%- explorerPath %>/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="<%- explorerPath %>/favicon-16x16.png" sizes="16x16" />
<style>
html {
box-sizing: border-box;
Expand All @@ -31,8 +31,8 @@
<body>
<div id="swagger-ui"></div>

<script src="./swagger-ui-bundle.js"> </script>
<script src="./swagger-ui-standalone-preset.js"> </script>
<script src="<%- explorerPath %>/swagger-ui-bundle.js"> </script>
<script src="<%- explorerPath %>/swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function () {
Expand Down

0 comments on commit 0eeae5b

Please sign in to comment.