Skip to content

Commit

Permalink
refactor: rename folder shared and others improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony133 committed Jan 27, 2025
1 parent 5d162d1 commit 770d6eb
Show file tree
Hide file tree
Showing 41 changed files with 124 additions and 73 deletions.
6 changes: 3 additions & 3 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export default tseslint.config(
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-unsafe-argument': 'warn',
'@typescript-eslint/no-unsafe-argument': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-unsafe-return': 'off',
'@typescript-eslint/no-unsafe-assignment': 'warn',
'@typescript-eslint/no-unsafe-assignment': 'off',
'@typescript-eslint/no-unsafe-call': 'warn',
'@typescript-eslint/no-unsafe-member-access': 'warn',
'@typescript-eslint/require-await': 'warn',
'@typescript-eslint/no-unused-vars': 'warn',
'@typescript-eslint/no-unused-vars': 'warn'
},
},
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@fastify/cors": "^10.0.2",
"@fastify/helmet": "^13.0.1",
"@fastify/rate-limit": "^10.2.2",
"@fastify/static": "^8.0.4",
"@nestjs/common": "^11.0.3",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

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

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions src/common/plugins/register-fastify.plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NestFastifyApplication } from '@nestjs/platform-fastify';

export async function registerFastifyPlugins(app: NestFastifyApplication) {

await app.register(require('@fastify/cors'), {
origin: true || [process.env.ENDPOINT_URL_CORS],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders:
'Content-Type, Accept, Access-Control-Allow-Origin, Access-Control-Allow-Methods',
credentials: true,
});

await app.register(require('@fastify/rate-limit'), {
max: 100,
timeWindow: '1 minute',
});

await app.register(require('@fastify/helmet'), {
crossOriginResourcePolicy: true,
contentSecurityPolicy: false,
referrerPolicy: {
policy: 'same-origin',
},
hsts: {
maxAge: 31536000, // 1 year
includeSubDomains: true, // Optional: Include subdomains
preload: true, // Optional: Indicate to browsers to preload HSTS
},
frameguard: {
action: 'deny',
},
});
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/helpers/configure-auth-swagger-docs.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ export function configureAuthSwaggerDocs(
if (!req.headers.authorization) {
return unauthorizedResponse();
}

const credentials = parseAuthHeader(req.headers.authorization);

if (
credentials?.user !== apiDocumentationCredentials.user ||
credentials?.password !== apiDocumentationCredentials.password
) {
return unauthorizedResponse();
}

next();
});
}
6 changes: 2 additions & 4 deletions src/helpers/configure-swagger-docs.helper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

export function configureSwaggerDocs(
app: INestApplication,
configService: ConfigService,
) {
if (
configService.get<string | undefined>('NODE_ENV') !== 'production'
) {
if (configService.get<string | undefined>('NODE_ENV') !== 'production') {
const config = new DocumentBuilder()
.setTitle('API')
.setDescription('The API description')
Expand Down
6 changes: 3 additions & 3 deletions src/iam/change-password/change-password.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ChangePasswordService } from './change-password.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Users } from '../../users/models/users.model';
import { UsersService } from '../../users/users.service';
import { MailerModule } from '../../shared/mailer/mailer.module';
import { BcryptService } from '../../shared/hashing/bcrypt.service';
import { HashingService } from '../../shared/hashing/hashing.service';
import { MailerModule } from '../../common/mailer/mailer.module';
import { BcryptService } from '../../common/hashing/bcrypt.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { APP_GUARD } from '@nestjs/core';
import { AuthenticationGuard } from '../login/guards/authentication/authentication.guard';
import { AccessTokenGuard } from '../login/guards/access-token/access-token.guard';
Expand Down
2 changes: 1 addition & 1 deletion src/iam/change-password/change-password.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getRepositoryToken } from '@nestjs/typeorm';
import { Users } from '../../users/models/users.model';
import { Repository } from 'typeorm';
import { UsersService } from '../../users/users.service';
import { MailerService } from '../../shared/mailer/mailer.service';
import { MailerService } from '../../common/mailer/mailer.service';
import { ConfigService } from '@nestjs/config';

const changePasswordUser = {
Expand Down
4 changes: 2 additions & 2 deletions src/iam/change-password/change-password.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Injectable, Logger } from '@nestjs/common';
import { UsersService } from '../../users/users.service';
import { ChangePasswordDto } from './dto/change-password.dto';
import { MailerService } from '../../shared/mailer/mailer.service';
import { changePasswordEmail } from '../../shared/mailer/mailer.constants';
import { MailerService } from '../../common/mailer/mailer.service';
import { changePasswordEmail } from '../../common/mailer/mailer.constants';

@Injectable()
export class ChangePasswordService {
Expand Down
8 changes: 4 additions & 4 deletions src/iam/forgot-password/forgot-password.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Module } from '@nestjs/common';
import { ForgotPasswordService } from './forgot-password.service';
import { ForgotPasswordController } from './forgot-password.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BcryptService } from '../../shared/hashing/bcrypt.service';
import { HashingService } from '../../shared/hashing/hashing.service';
import { MailerModule } from '../../shared/mailer/mailer.module';
import { UtilsModule } from '../../shared/utils/utils.module';
import { BcryptService } from '../../common/hashing/bcrypt.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { MailerModule } from '../../common/mailer/mailer.module';
import { UtilsModule } from '../../common/utils/utils.module';
import { Users } from '../../users/models/users.model';
import { provideUsersRepository } from '../../users/repositories/users.repository.provider';
import { UsersService } from '../../users/users.service';
Expand Down
6 changes: 3 additions & 3 deletions src/iam/forgot-password/forgot-password.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Users } from '../../users/models/users.model';
import { ForgotPasswordService } from './forgot-password.service';
import { MailerService } from '../../shared/mailer/mailer.service';
import { UtilsService } from '../../shared/utils/utils.service';
import { MailerService } from '../../common/mailer/mailer.service';
import { UtilsService } from '../../common/utils/utils.service';
import { ConfigService } from '@nestjs/config';
import { HashingService } from '../../shared/hashing/hashing.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { Repository } from 'typeorm';
import { UsersService } from '../../users/users.service';

Expand Down
8 changes: 4 additions & 4 deletions src/iam/forgot-password/forgot-password.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { Users } from '../../users/models/users.model';
import { ForgotPasswordDto } from './dto/forgot-password.dto';
import { MailerService } from '../../shared/mailer/mailer.service';
import { UtilsService } from '../../shared/utils/utils.service';
import { HashingService } from '../../shared/hashing/hashing.service';
import { forgotPasswordEmail } from '../../shared/mailer/mailer.constants';
import { MailerService } from '../../common/mailer/mailer.service';
import { UtilsService } from '../../common/utils/utils.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { forgotPasswordEmail } from '../../common/mailer/mailer.constants';

@Injectable()
export class ForgotPasswordService {
Expand Down
2 changes: 1 addition & 1 deletion src/iam/iam.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { UtilsModule } from '../shared/utils/utils.module';
import { UtilsModule } from '../common/utils/utils.module';
import { UsersModule } from '../users/users.module';
import { ChangePasswordModule } from './change-password/change-password.module';
import { ForgotPasswordModule } from './forgot-password/forgot-password.module';
Expand Down
4 changes: 2 additions & 2 deletions src/iam/login/login.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Users } from '../../users/models/users.model';
import { JwtModule } from '@nestjs/jwt';
import { UsersService } from '../../users/users.service';
import { ConfigModule } from '@nestjs/config';
import { HashingService } from '../../shared/hashing/hashing.service';
import { BcryptService } from '../../shared/hashing/bcrypt.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { BcryptService } from '../../common/hashing/bcrypt.service';
import { APP_GUARD } from '@nestjs/core';
import { AuthenticationGuard } from './guards/authentication/authentication.guard';
import { AccessTokenGuard } from './guards/access-token/access-token.guard';
Expand Down
2 changes: 1 addition & 1 deletion src/iam/login/login.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConfigModule, ConfigService, ConfigType } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { Test, TestingModule } from '@nestjs/testing';
import { HashingService } from '../../shared/hashing/hashing.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { LoginService } from './login.service';
import { UsersService } from '../../users/users.service';
import { Users } from '../../users/models/users.model';
Expand Down
2 changes: 1 addition & 1 deletion src/iam/login/login.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { UsersService } from '../../users/users.service';
import { AccountsUsers } from '../../users/interfaces/accounts-users.interface';
import { LoginDto } from './dto/login.dto';
import { ConfigType } from '@nestjs/config';
import { HashingService } from '../../shared/hashing/hashing.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { JWTPayload } from './interfaces/jwt-payload.interface';
import { RefreshTokenDto } from './dto/refresh-token.dto';
import { Users } from '../../users/models/users.model';
Expand Down
2 changes: 1 addition & 1 deletion src/iam/register/register.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { RegisterController } from './register.controller';
import { RegisterService } from './register.service';
import { UsersService } from '../../users/users.service';
import { MailerService } from '../../shared/mailer/mailer.service';
import { MailerService } from '../../common/mailer/mailer.service';
import { ConfigService } from '@nestjs/config';
import { RegisterUserDto } from './dto/register-user.dto';
import { BadRequestException } from '@nestjs/common';
Expand Down
6 changes: 3 additions & 3 deletions src/iam/register/register.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BcryptService } from '../../shared/hashing/bcrypt.service';
import { HashingService } from '../../shared/hashing/hashing.service';
import { MailerModule } from '../../shared/mailer/mailer.module';
import { BcryptService } from '../../common/hashing/bcrypt.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { MailerModule } from '../../common/mailer/mailer.module';
import { Users } from '../../users/models/users.model';
import { UsersService } from '../../users/users.service';
import { RegisterController } from './register.controller';
Expand Down
4 changes: 2 additions & 2 deletions src/iam/register/register.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { RegisterService } from './register.service';
import { Users } from '../../users/models/users.model';
import { getRepositoryToken } from '@nestjs/typeorm';
import { RegisterUserDto } from './dto/register-user.dto';
import { HashingService } from '../../shared/hashing/hashing.service';
import { MailerService } from '../../shared/mailer/mailer.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { MailerService } from '../../common/mailer/mailer.service';
import { ConfigService } from '@nestjs/config';
import { Repository } from 'typeorm';

Expand Down
6 changes: 3 additions & 3 deletions src/iam/register/register.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable, Logger } from '@nestjs/common';
import { HashingService } from '../../shared/hashing/hashing.service';
import { MailerService } from '../../shared/mailer/mailer.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { MailerService } from '../../common/mailer/mailer.service';
import { AccountsUsers } from '../../users/interfaces/accounts-users.interface';
import { UsersService } from '../../users/users.service';
import { RegisterUserDto } from './dto/register-user.dto';
import { registrationEmail } from '../../shared/mailer/mailer.constants';
import { registrationEmail } from '../../common/mailer/mailer.constants';

@Injectable()
export class RegisterService {
Expand Down
42 changes: 22 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Logger, ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ConsoleLogger, Logger, ValidationPipe } from '@nestjs/common';
import { configureSwaggerDocs } from './helpers/configure-swagger-docs.helper';
import { configureAuthSwaggerDocs } from './helpers/configure-auth-swagger-docs.helper';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import {
FastifyAdapter,
NestFastifyApplication
} from '@nestjs/platform-fastify';
import { registerFastifyPlugins } from './common/plugins/register-fastify.plugins';
import { ConfigService } from '@nestjs/config';

async function bootstrap() {
const fastifyAdapter = new FastifyAdapter();
const app =
await NestFactory.create<NestFastifyApplication>(AppModule, fastifyAdapter);
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
fastifyAdapter,
{
logger: new ConsoleLogger({
json: true,
colors: true,
}),
},
);
const configService = app.get<ConfigService>(ConfigService);

await fastifyAdapter.register(require('@fastify/cors'), {
origin: true || [configService.get<string>('ENDPOINT_URL_CORS')],
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders:
'Content-Type, Accept, Access-Control-Allow-Origin, Access-Control-Allow-Methods',
credentials: true,
});

await fastifyAdapter.register(require('@fastify/rate-limit'), {
max: 100,
timeWindow: '1 minute',
});
// Plugins for Fastify
registerFastifyPlugins(app);
// Swagger Configurations
configureAuthSwaggerDocs(app, configService);
configureSwaggerDocs(app, configService);

app.setGlobalPrefix('api');
app.useGlobalPipes(
Expand All @@ -37,9 +42,6 @@ async function bootstrap() {
}),
);

configureAuthSwaggerDocs(app, configService);
configureSwaggerDocs(app, configService);

const port = configService.get<number>('SERVER_PORT') || 3000;
await app.listen(port, '0.0.0.0');
if (configService.get<string>('NODE_ENV') !== 'production') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Repository, UpdateResult } from 'typeorm';
import { UserProfileDto } from '../../../users/dto/user-profile.dto';
import { UserUpdateDto } from '../../../users/dto/user-update.dto';
import { UserDto } from '../../../users/dto/user.dto';
import { HashingService } from '../../../shared/hashing/hashing.service';
import { HashingService } from '../../../common/hashing/hashing.service';
import { AccountsUsers } from '../../../users/interfaces/accounts-users.interface';

export class UsersTypeOrmRepository implements UsersRepository {
Expand Down
2 changes: 1 addition & 1 deletion src/users/repositories/users.repository.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Repository } from 'typeorm';
import { USERS_REPOSITORY_TOKEN } from './users.repository.interface';
import { UsersTypeOrmRepository } from './implementations/users.typeorm.repository';
import { Users } from '../models/users.model';
import { HashingService } from '../../shared/hashing/hashing.service';
import { HashingService } from '../../common/hashing/hashing.service';
import { ConfigService } from '@nestjs/config';
import { config } from 'dotenv';

Expand Down
Loading

0 comments on commit 770d6eb

Please sign in to comment.