-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shared && auth): adding env service and using it at the auth module
- Loading branch information
1 parent
4a26fb5
commit 35bd004
Showing
15 changed files
with
422 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ DB_PORT=5432 | |
DB_USERNAME=user | ||
DB_PASSWORD=password | ||
DB_DATABASE=backend_db | ||
JWT_SECRET=SECRET | ||
JWT_EXPIRES_IN=7d | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 8 additions & 2 deletions
10
src/modules/auth/infra/strategies/auth-jwt.strategy.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
import { DataSource } from "typeorm"; | ||
import { dataSourceOptions } from "./datasource-options"; | ||
import { DataSource } from 'typeorm'; | ||
import { getDatasourceOptions } from './datasource-options'; | ||
|
||
export default new DataSource(dataSourceOptions); | ||
import { DotEnvService } from '@shared/infra/env/services/dot-env.service'; | ||
import { config as initDotEnv } from 'dotenv'; | ||
import { cleanEnv } from 'envalid'; | ||
|
||
initDotEnv(); | ||
|
||
const env = cleanEnv(process.env, DotEnvService.dotEnvSpecs); | ||
|
||
export default new DataSource(getDatasourceOptions(env)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import { EnvVariables } from '@shared/infra/env/interfaces/env-variables'; | ||
import { join } from 'path'; | ||
import * as dotenv from 'dotenv'; | ||
import { DataSourceOptions } from 'typeorm'; | ||
|
||
dotenv.config(); | ||
|
||
export const dataSourceOptions: DataSourceOptions = { | ||
type: 'postgres', | ||
host: process.env.DB_HOST, | ||
port: Number(process.env.DB_PORT), | ||
username: process.env.DB_USERNAME, | ||
password: process.env.DB_PASSWORD, | ||
database: process.env.DB_DATABASE, | ||
migrations: [join(__dirname, 'migrations', '*.ts')], | ||
entities: [join(__dirname, '../../../../modules', '**', '*.schema.{ts,js}')], | ||
synchronize: false, | ||
export const getDatasourceOptions = (env: EnvVariables): DataSourceOptions => { | ||
return { | ||
type: 'postgres', | ||
host: env.DB_HOST, | ||
port: env.DB_PORT, | ||
username: env.DB_USERNAME, | ||
password: env.DB_PASSWORD, | ||
database: env.DB_DATABASE, | ||
migrations: [join(__dirname, 'migrations', '*.ts')], | ||
entities: [ | ||
join(__dirname, '../../../../modules', '**', '*.schema.{ts,js}'), | ||
], | ||
synchronize: false, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EnvService } from './interfaces/env.service'; | ||
import { DotEnvService } from './services/dot-env.service'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: EnvService, | ||
useClass: DotEnvService, | ||
}, | ||
], | ||
exports: [EnvService], | ||
}) | ||
export class EnvModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export enum EnvVariableKeys { | ||
DB_HOST = 'DB_HOST', | ||
DB_PORT = 'DB_PORT', | ||
DB_USERNAME = 'DB_USERNAME', | ||
DB_PASSWORD = 'DB_PASSWORD', | ||
DB_DATABASE = 'DB_DATABASE', | ||
JWT_SECRET = 'JWT_SECRET', | ||
JWT_EXPIRES_IN = 'JWT_EXPIRES_IN', | ||
} | ||
|
||
export type EnvVariables = { | ||
[EnvVariableKeys.DB_HOST]: string; | ||
[EnvVariableKeys.DB_PORT]: number; | ||
[EnvVariableKeys.DB_USERNAME]: string; | ||
[EnvVariableKeys.DB_PASSWORD]: string; | ||
[EnvVariableKeys.DB_DATABASE]: string; | ||
[EnvVariableKeys.JWT_SECRET]: string; | ||
[EnvVariableKeys.JWT_EXPIRES_IN]: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { EnvVariableKeys, EnvVariables } from './env-variables'; | ||
|
||
export abstract class EnvService { | ||
abstract get<K extends EnvVariableKeys>(key: K): EnvVariables[K]; | ||
abstract getAll(): EnvVariables; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { config as initDotEnv } from 'dotenv'; | ||
import { ValidatorSpec, cleanEnv, port, str } from 'envalid'; | ||
import { EnvVariableKeys, EnvVariables } from '../interfaces/env-variables'; | ||
import { EnvService } from '../interfaces/env.service'; | ||
|
||
type EnvSpecs = { | ||
[K in EnvVariableKeys]: ValidatorSpec<EnvVariables[K]>; | ||
}; | ||
|
||
@Injectable() | ||
export class DotEnvService implements EnvService { | ||
constructor() { | ||
initDotEnv(); | ||
const env = cleanEnv(process.env, DotEnvService.dotEnvSpecs); | ||
|
||
this.env = env; | ||
} | ||
|
||
static dotEnvSpecs: EnvSpecs = { | ||
[EnvVariableKeys.DB_HOST]: str(), | ||
[EnvVariableKeys.DB_PORT]: port(), | ||
[EnvVariableKeys.DB_USERNAME]: str(), | ||
[EnvVariableKeys.DB_PASSWORD]: str(), | ||
[EnvVariableKeys.DB_DATABASE]: str(), | ||
[EnvVariableKeys.JWT_SECRET]: str(), | ||
[EnvVariableKeys.JWT_EXPIRES_IN]: str(), | ||
}; | ||
|
||
private readonly env: EnvVariables; | ||
|
||
get<K extends EnvVariableKeys>(key: K): EnvVariables[K] { | ||
return this.env[key]; | ||
} | ||
|
||
getAll(): EnvVariables { | ||
return this.env; | ||
} | ||
} |