From 3b3e587696abde907bf5a8aa3b87c6180d74a958 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 7 Feb 2019 22:20:56 -0800 Subject: [PATCH] chore(rest-explorer): remove redirect from /explorer/ to /explorer --- .../acceptance/rest-explorer.acceptance.ts | 10 +++++----- .../rest-explorer/src/rest-explorer.component.ts | 3 +-- .../rest-explorer/src/rest-explorer.controller.ts | 15 +++++++++------ packages/rest-explorer/templates/index.html.ejs | 10 +++++----- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/packages/rest-explorer/src/__tests__/acceptance/rest-explorer.acceptance.ts b/packages/rest-explorer/src/__tests__/acceptance/rest-explorer.acceptance.ts index 61edfa291295..85056f373481 100644 --- a/packages/rest-explorer/src/__tests__/acceptance/rest-explorer.acceptance.ts +++ b/packages/rest-explorer/src/__tests__/acceptance/rest-explorer.acceptance.ts @@ -36,11 +36,12 @@ describe('API Explorer (acceptance)', () => { .expect(/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 () => { @@ -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); }); diff --git a/packages/rest-explorer/src/rest-explorer.component.ts b/packages/rest-explorer/src/rest-explorer.component.ts index 5346f8573943..6687de331fa3 100644 --- a/packages/rest-explorer/src/rest-explorer.component.ts +++ b/packages/rest-explorer/src/rest-explorer.component.ts @@ -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()); diff --git a/packages/rest-explorer/src/rest-explorer.controller.ts b/packages/rest-explorer/src/rest-explorer.controller.ts index 85dac4796852..24c4369c19b3 100644 --- a/packages/rest-explorer/src/rest-explorer.controller.ts +++ b/packages/rest-explorer/src/rest-explorer.controller.ts @@ -5,15 +5,17 @@ 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'); @@ -21,24 +23,25 @@ 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); diff --git a/packages/rest-explorer/templates/index.html.ejs b/packages/rest-explorer/templates/index.html.ejs index 8ced18cbe11d..69c014a71759 100644 --- a/packages/rest-explorer/templates/index.html.ejs +++ b/packages/rest-explorer/templates/index.html.ejs @@ -5,9 +5,9 @@ <head> <meta charset="UTF-8"> <title>LoopBack API Explorer - - - + + +