Skip to content

Commit f677295

Browse files
mrmodisedhmlau
authored andcommitted
refactor(rest-explorer): use indexTitle when available
Signed-off-by: mrmodise <[email protected]>
1 parent 0e4c714 commit f677295

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

packages/rest-explorer/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ this.bind(RestExplorerBindings.CONFIG).to({
4444
});
4545
```
4646

47+
Similarly, the index page title for the explorer can be customized via
48+
RestExplorer configuration as follows:
49+
50+
```ts
51+
this.configure(RestExplorerBindings.COMPONENT).to({
52+
indexTitle: 'My LoopBack API Explorer',
53+
});
54+
```
55+
56+
Or:
57+
58+
```ts
59+
this.bind(RestExplorerBindings.CONFIG).to({
60+
indexTitle: 'My LoopBack API Explorer',
61+
});
62+
```
63+
4764
### Advanced Configuration and Reverse Proxies
4865

4966
By default, the component will add an additional OpenAPI spec endpoint, in the

packages/rest-explorer/src/__tests__/acceptance/rest-explorer.acceptance.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ describe('API Explorer (acceptance)', () => {
184184
.expect('content-type', /html/)
185185
.expect(/TEST LoopBack/);
186186
});
187+
188+
it('honors custom explorer title', async () => {
189+
await givenAppWithCustomExplorerConfig(undefined, {
190+
indexTitle: 'Custom LoopBack API Explorer',
191+
});
192+
193+
await request
194+
.get('/explorer/')
195+
.expect(200, /<title>Custom LoopBack API Explorer/);
196+
});
187197
});
188198

189199
context('with custom basePath', () => {

packages/rest-explorer/src/rest-explorer.controller.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class ExplorerController {
3131
private useSelfHostedSpec: boolean;
3232
private swaggerThemeFile: string;
3333
private indexTemplatePath: string;
34+
private indexTemplateTitle: string;
3435

3536
constructor(
3637
@inject(RestBindings.CONFIG, {optional: true})
@@ -48,6 +49,8 @@ export class ExplorerController {
4849
this.indexTemplatePath =
4950
explorerConfig.indexTemplatePath ??
5051
path.resolve(__dirname, '../templates/index.html.ejs');
52+
this.indexTemplateTitle =
53+
explorerConfig?.indexTitle ?? 'LoopBack API Explorer';
5154
}
5255

5356
indexRedirect() {
@@ -65,6 +68,7 @@ export class ExplorerController {
6568
index() {
6669
const swaggerThemeFile = this.swaggerThemeFile;
6770
let openApiSpecUrl = this.openApiSpecUrl;
71+
const indexTemplateTitle = this.indexTemplateTitle;
6872

6973
// if using self-hosted openapi spec, then the path to use is always the
7074
// exact relative path, and no base path logic needs to be applied
@@ -87,6 +91,7 @@ export class ExplorerController {
8791
const data = {
8892
openApiSpecUrl,
8993
swaggerThemeFile,
94+
indexTemplateTitle,
9095
};
9196

9297
if (prevIndexTemplatePath !== this.indexTemplatePath) {

packages/rest-explorer/src/rest-explorer.types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ export type RestExplorerConfig = {
3737
templates/index.html.ejs
3838
*/
3939
indexTemplatePath?: string;
40+
41+
// Index page title
42+
indexTitle?: string;
4043
};

packages/rest-explorer/templates/index.html.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<head>
66
<meta charset="UTF-8">
7-
<title>LoopBack API Explorer</title>
7+
<title><%- indexTemplateTitle %></title>
88
<link rel="stylesheet" type="text/css" href="<%- swaggerThemeFile %>">
99
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
1010
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />

0 commit comments

Comments
 (0)