Skip to content

Commit

Permalink
refactor(be): ⚡ using protocol-like for nodejs built-in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
lehuygiang28 committed Jul 2, 2024
1 parent e1fe46d commit e631264
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
15 changes: 10 additions & 5 deletions apps/be/common/src/i18n/i18n.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'node:path';
import { Module } from '@nestjs/common';
import { join } from 'path';
import {
I18nModule as I18nModuleCore,
AcceptLanguageResolver,
Expand All @@ -8,22 +8,27 @@ import {
} from 'nestjs-i18n';
import { ConfigService } from '@nestjs/config';

import { AllConfig } from '~be/app/config';

@Module({
imports: [
I18nModuleCore.forRootAsync({
useFactory: (configService: ConfigService) => ({
fallbackLanguage: configService.get('FALLBACK_LANGUAGE') || 'en',
useFactory: (configService: ConfigService<AllConfig>) => ({
fallbackLanguage: configService.getOrThrow('app.fallbackLanguage', { infer: true }),
loaderOptions: {
path: join(
__dirname,
configService.get('DEPLOY_ENV')?.toLowerCase() === 'serverless'
configService
.getOrThrow('app.deployEnv', { infer: true })
?.toLowerCase() === 'serverless'
? './lang/'
: './assets/i18n/lang/',
),
watch: true,
},
typesOutputPath:
configService.get('DEPLOY_ENV')?.toLowerCase() === 'serverless'
configService.getOrThrow('app.deployEnv', { infer: true })?.toLowerCase() ===
'serverless'
? undefined
: join(process.cwd(), `./apps/be/common/src/i18n/i18n.generated.ts`),
}),
Expand Down
6 changes: 3 additions & 3 deletions apps/be/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import { AppModule } from './app/app.module';
import { AllConfig } from './app/config/all-config.type';

async function bootstrap() {
const port = process.env.PORT || 8000;
const port = process.env?.PORT || 8000;

const app = await NestFactory.create(AppModule, { bufferLogs: true });
const configService: ConfigService<AllConfig> = app.get(ConfigService);
const configService = app.get(ConfigService<AllConfig>);
const logger = app.get(Logger);

app.enableCors();
Expand All @@ -35,7 +35,7 @@ async function bootstrap() {
app.enableShutdownHooks();

app.useGlobalPipes(new ValidationPipe(validationOptions));
app.useGlobalFilters(new ProblemDetailsFilter(app.get(Logger)));
app.useGlobalFilters(new ProblemDetailsFilter(logger));

app.useGlobalInterceptors(
// ResolvePromisesInterceptor is used to resolve promises in responses because class-transformer can't do it
Expand Down
2 changes: 1 addition & 1 deletion apps/be/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { NxAppWebpackPlugin } = require('@nx/webpack/app-plugin');
const { join, relative } = require('path');
const { join, relative } = require('node:path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion serverless/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Config for using path-aliases
*/

const path = require('path');
const path = require('node:path');
const moduleAlias = require('module-alias');
const tsConfigPaths = require('tsconfig-paths');
const tsConfig = require('./tsconfig.json');
Expand Down

0 comments on commit e631264

Please sign in to comment.