mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 05:09:26 +00:00
add email account validation
This commit is contained in:
@@ -2,53 +2,44 @@
|
||||
//по умолчанию переменные беруться сначала из ENV илм смотрят всегда на staging
|
||||
//для подстановки локальных значений переменных использовать исключительно локальные env файлы env.development.local
|
||||
//при необзодимости добавляем сюда нужные приложению переменные
|
||||
import * as dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
import * as dotenv from 'dotenv'
|
||||
dotenv.config()
|
||||
|
||||
export type EnvironmentVariable = { [key: string]: string | undefined };
|
||||
export type EnvironmentsTypes =
|
||||
| 'DEVELOPMENT'
|
||||
| 'STAGING'
|
||||
| 'PRODUCTION'
|
||||
| 'TEST';
|
||||
export type EnvironmentVariable = { [key: string]: string | undefined }
|
||||
export type EnvironmentsTypes = 'DEVELOPMENT' | 'STAGING' | 'PRODUCTION' | 'TEST'
|
||||
export class EnvironmentSettings {
|
||||
constructor(private env: EnvironmentsTypes) {}
|
||||
getEnv() {
|
||||
return this.env;
|
||||
return this.env
|
||||
}
|
||||
isProduction() {
|
||||
return this.env === 'PRODUCTION';
|
||||
return this.env === 'PRODUCTION'
|
||||
}
|
||||
isStaging() {
|
||||
return this.env === 'STAGING';
|
||||
return this.env === 'STAGING'
|
||||
}
|
||||
isDevelopment() {
|
||||
return this.env === 'DEVELOPMENT';
|
||||
return this.env === 'DEVELOPMENT'
|
||||
}
|
||||
isTesting() {
|
||||
return this.env === 'TEST';
|
||||
return this.env === 'TEST'
|
||||
}
|
||||
}
|
||||
|
||||
class AuthSettings {
|
||||
public readonly BASE_AUTH_HEADER: string;
|
||||
public readonly ACCESS_JWT_SECRET_KEY: string;
|
||||
public readonly REFRESH_JWT_SECRET_KEY: string;
|
||||
public readonly BASE_AUTH_HEADER: string
|
||||
public readonly ACCESS_JWT_SECRET_KEY: string
|
||||
public readonly REFRESH_JWT_SECRET_KEY: string
|
||||
constructor(private envVariables: EnvironmentVariable) {
|
||||
this.BASE_AUTH_HEADER =
|
||||
envVariables.BASE_AUTH_HEADER || 'Basic YWRtaW46cXdlcnR5';
|
||||
this.ACCESS_JWT_SECRET_KEY =
|
||||
envVariables.ACCESS_JWT_SECRET_KEY || 'accessJwtSecret';
|
||||
this.REFRESH_JWT_SECRET_KEY =
|
||||
envVariables.REFRESH_JWT_SECRET_KEY || 'refreshJwtSecret';
|
||||
this.BASE_AUTH_HEADER = envVariables.BASE_AUTH_HEADER || 'Basic YWRtaW46cXdlcnR5'
|
||||
this.ACCESS_JWT_SECRET_KEY = envVariables.ACCESS_JWT_SECRET_KEY || 'accessJwtSecret'
|
||||
this.REFRESH_JWT_SECRET_KEY = envVariables.REFRESH_JWT_SECRET_KEY || 'refreshJwtSecret'
|
||||
}
|
||||
}
|
||||
|
||||
export class AppSettings {
|
||||
constructor(public env: EnvironmentSettings, public auth: AuthSettings) {}
|
||||
}
|
||||
const env = new EnvironmentSettings(
|
||||
(process.env.NODE_ENV || 'DEVELOPMENT') as EnvironmentsTypes,
|
||||
);
|
||||
const auth = new AuthSettings(process.env);
|
||||
export const appSettings = new AppSettings(env, auth);
|
||||
const env = new EnvironmentSettings((process.env.NODE_ENV || 'DEVELOPMENT') as EnvironmentsTypes)
|
||||
const auth = new AuthSettings(process.env)
|
||||
export const appSettings = new AppSettings(env, auth)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { appSettings, AppSettings } from './app-settings';
|
||||
import { Global, Module } from '@nestjs/common'
|
||||
import { appSettings, AppSettings } from './app-settings'
|
||||
|
||||
//главный config модуль для управления env переменными импортируется в app.module.ts глобально
|
||||
//поскольку он глобальный то импортировать в каждый модуль его не надо
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
INestApplication,
|
||||
ValidationPipe,
|
||||
} from '@nestjs/common';
|
||||
import { ValidationError } from 'class-validator';
|
||||
import { BadRequestException, INestApplication, ValidationPipe } from '@nestjs/common'
|
||||
import { ValidationError } from 'class-validator'
|
||||
|
||||
export const validationErrorsMapper = {
|
||||
mapValidationErrorArrayToValidationPipeErrorTypeArray(
|
||||
errors: ValidationError[],
|
||||
errors: ValidationError[]
|
||||
): ValidationPipeErrorType[] {
|
||||
return errors.flatMap((error) => {
|
||||
const constraints = error.constraints ?? [];
|
||||
return errors.flatMap(error => {
|
||||
const constraints = error.constraints ?? []
|
||||
return Object.entries(constraints).map(([_, value]) => ({
|
||||
field: error.property,
|
||||
message: value,
|
||||
}));
|
||||
});
|
||||
}))
|
||||
})
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function pipesSetup(app: INestApplication) {
|
||||
app.useGlobalPipes(
|
||||
@@ -28,16 +24,14 @@ export function pipesSetup(app: INestApplication) {
|
||||
stopAtFirstError: true,
|
||||
exceptionFactory: (errors: ValidationError[]) => {
|
||||
const err =
|
||||
validationErrorsMapper.mapValidationErrorArrayToValidationPipeErrorTypeArray(
|
||||
errors,
|
||||
);
|
||||
throw new BadRequestException(err);
|
||||
validationErrorsMapper.mapValidationErrorArrayToValidationPipeErrorTypeArray(errors)
|
||||
throw new BadRequestException(err)
|
||||
},
|
||||
}),
|
||||
);
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export type ValidationPipeErrorType = {
|
||||
field: string;
|
||||
message: string;
|
||||
};
|
||||
field: string
|
||||
message: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user