Skip to content

Commit

Permalink
v0.21.4
Browse files Browse the repository at this point in the history
fix: remove hbs and use handlebars instead
  • Loading branch information
bastienterrier authored Jul 11, 2019
2 parents ff41e94 + 71b05bb commit b98e1c2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 53 deletions.
57 changes: 15 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"fs-extra": "8.1.0",
"github-release-notes": "0.17.0",
"googleapis": "27.0.0",
"hbs": "4.0.4",
"js-yaml": "3.13.1",
"prom-client": "11.5.3",
"reflect-metadata": "0.1.13",
Expand All @@ -70,6 +69,7 @@
"@types/node": "11.13.17",
"@types/supertest": "2.0.8",
"@types/universal-analytics": "0.4.2",
"@types/handlebars": "4.1.0",
"coveralls": "3.0.4",
"cpx": "1.5.0",
"husky": "3.0.0",
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/application.controller.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ describe('ApplicationController (e2e)', () => {
}).compile();

app = moduleFixture.createNestApplication();
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');
await app.init();
});

Expand Down
9 changes: 6 additions & 3 deletions src/controllers/application.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Controller, Get, Res } from '@nestjs/common';
import { Controller, Get, Res, Header, HttpStatus } from '@nestjs/common';
import { PrometheusService } from '../logger/prometheus.service';
import { logger } from '../logger/logger.service';
import { join } from 'path';
import { Utils } from '../utils/utils';

@Controller()
export class ApplicationController {
constructor(private readonly prometheus: PrometheusService) {}

@Get('/')
welcome(@Res() response): string {
return response.render('homepage.hbs');
async welcome(@Res() response): Promise<string> {
return response.status(HttpStatus.OK).send(Utils.renderHbs('homepage'));
}

@Get('/metrics')
Expand Down
5 changes: 4 additions & 1 deletion src/exceptions/allExceptionFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Utils } from '../utils/utils';

/**
* Handle all ExceptionFilter
Expand All @@ -21,7 +22,9 @@ export class AllExceptionsFilter implements ExceptionFilter {
: HttpStatus.INTERNAL_SERVER_ERROR;

if (status === HttpStatus.NOT_FOUND) {
return response.status(status).render('404.hbs', { path: request.url });
return response
.status(status)
.send(Utils.renderHbs('404', { path: request.url }));
}

return response.status(status).json({
Expand Down
4 changes: 0 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ async function bootstrap() {
// Handle every exceptions
app.useGlobalFilters(new AllExceptionsFilter());

// Set View Engine
app.setBaseViewsDir(join(__dirname, 'views'));
app.setViewEngine('hbs');

const port = process.env.PORT || 3000;

app.enableCors({
Expand Down
10 changes: 10 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { logger } from '../logger/logger.service';

import * as Handlebars from 'handlebars';
import { PreconditionException } from '../exceptions/precondition.exception';
import { join } from 'path';
Handlebars.registerHelper('foreach', (items, options) => {
return items.map(item => options.fn(item)).join(',');
});
Expand Down Expand Up @@ -189,4 +190,13 @@ export class Utils {
const CryptoJS = require('crypto-js');
return CryptoJS.AES.encrypt(str, process.env.ENCRYPTION_KEY).toString();
}

static renderHbs(filePath: string, dataSource: any = {}): string {
const fs = require('fs-extra');
const path = join(__dirname, '../views/', `${filePath}.hbs`);
let template = fs.readFileSync(path);
template = template.toString();

return Utils.render(template, dataSource);
}
}

0 comments on commit b98e1c2

Please sign in to comment.