forked from Yara724/api
Added API for externalAPI, added env for clients
This commit is contained in:
14
src/core/config/config.module.ts
Normal file
14
src/core/config/config.module.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ConfigModule } from "@nestjs/config";
|
||||
import { validate } from "./config.validation";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
isGlobal: true,
|
||||
expandVariables: true,
|
||||
validate,
|
||||
}),
|
||||
],
|
||||
})
|
||||
export class AppConfigModule {}
|
||||
75
src/core/config/config.schema.ts
Normal file
75
src/core/config/config.schema.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import {
|
||||
IsBooleanString,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsPositive,
|
||||
IsString,
|
||||
IsUrl,
|
||||
Matches,
|
||||
Max,
|
||||
Min,
|
||||
MinLength,
|
||||
} from "class-validator";
|
||||
import { StringValue } from "ms";
|
||||
|
||||
export enum Environment {
|
||||
Development = "development",
|
||||
Production = "production",
|
||||
}
|
||||
|
||||
export class EnvironmentVariables {
|
||||
@IsEnum(Environment, {
|
||||
message: "NODE_ENV should be development or production",
|
||||
})
|
||||
NODE_ENV: Environment;
|
||||
// --------------------------------------------------------- //
|
||||
@IsNumber(
|
||||
{
|
||||
allowNaN: false,
|
||||
allowInfinity: false,
|
||||
},
|
||||
{
|
||||
message: "PORT must be a valid number",
|
||||
},
|
||||
)
|
||||
@IsPositive({ message: "PORT must be positive" })
|
||||
@Min(0, { message: "PORT must be between 0 and 65535" })
|
||||
@Max(65535, { message: "PORT must be between 0 and 65535" })
|
||||
PORT: number;
|
||||
// --------------------------------------------------------- //
|
||||
@IsString({ message: "MONGO_URI must be string" })
|
||||
@IsUrl(
|
||||
{
|
||||
require_tld: false,
|
||||
protocols: ["mongodb", "mongodb+srv"],
|
||||
},
|
||||
{
|
||||
message: "MONGO_URI must be in the correct format (mongodb://...)",
|
||||
},
|
||||
)
|
||||
MONGO_URI: string;
|
||||
// --------------------------------------------------------- //
|
||||
@IsBooleanString({ message: "MONGO_TLS must be boolean" })
|
||||
MONGO_TLS: string;
|
||||
// --------------------------------------------------------- //
|
||||
@IsBooleanString({ message: "MONGO_TLS_ALLOW_INVALID_CERTS must be boolean" })
|
||||
MONGO_TLS_ALLOW_INVALID_CERTS: string;
|
||||
// --------------------------------------------------------- //
|
||||
@IsString({ message: "JWT_SECRET must be string" })
|
||||
@MinLength(32, {
|
||||
message: "JWT_SECRET must be at least 32 characters",
|
||||
})
|
||||
JWT_SECRET: string;
|
||||
// --------------------------------------------------------- //
|
||||
@Matches(/^\d+(ms|s|m|h|d|w|y)$/, {
|
||||
message: "JWT_EXPIRY must be in the correct format",
|
||||
})
|
||||
JWT_EXPIRY: StringValue;
|
||||
// --------------------------------------------------------- //
|
||||
// @IsString({ message: "HASH_PEPPER must be string" })
|
||||
// @MinLength(32, {
|
||||
// message: "HASH_PEPPER must be at least 32 characters",
|
||||
// })
|
||||
// HASH_PEPPER: string;
|
||||
// --------------------------------------------------------- //
|
||||
}
|
||||
23
src/core/config/config.validation.ts
Normal file
23
src/core/config/config.validation.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { plainToInstance } from "class-transformer";
|
||||
import { validateSync } from "class-validator";
|
||||
import { EnvironmentVariables } from "./config.schema";
|
||||
|
||||
export function validate(config: Record<string, unknown>) {
|
||||
const validatedConfig = plainToInstance(EnvironmentVariables, config, {
|
||||
enableImplicitConversion: true,
|
||||
});
|
||||
|
||||
const errors = validateSync(validatedConfig, {
|
||||
skipMissingProperties: false,
|
||||
});
|
||||
|
||||
if (errors.length > 0) {
|
||||
const messages = errors
|
||||
.flatMap((error) => Object.values(error.constraints ?? {}))
|
||||
.join("\n");
|
||||
|
||||
throw new Error(`Environment validation failed:\n${messages}`);
|
||||
}
|
||||
|
||||
return validatedConfig;
|
||||
}
|
||||
19
src/core/database/database.module.ts
Normal file
19
src/core/database/database.module.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { MongooseModule } from "@nestjs/mongoose";
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
MongooseModule.forRootAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (configService: ConfigService) => ({
|
||||
uri: configService.get<string>("MONGO_URI"),
|
||||
tls: configService.get<string>("MONGO_TLS") === "true",
|
||||
tlsAllowInvalidCertificates:
|
||||
configService.get<string>("MONGO_TLS_ALLOW_INVALID_CERTS") === "true",
|
||||
autoIndex: configService.get<string>("NODE_ENV") !== "production",
|
||||
}),
|
||||
}),
|
||||
],
|
||||
})
|
||||
export class DatabaseModule {}
|
||||
0
src/core/database/plugins/iran-timestamp.plugin.ts
Normal file
0
src/core/database/plugins/iran-timestamp.plugin.ts
Normal file
Reference in New Issue
Block a user