Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
🏷️ Use interface for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 31, 2020
1 parent 560c03a commit c5cb8d8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
39 changes: 39 additions & 0 deletions src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export interface Configuration {
frontendUrl: string;

meta: {
totpServiceName: string;
};

caching: {
geolocationLruSize: number;
};

security: {
saltRounds: number;
jwtSecret: string;
totpWindowPast: number;
totpWindowFuture: number;
mfaTokenExpiry: string;
accessTokenExpiry: string;
passwordPwnedCheck: boolean;
unusedRefreshTokenExpiryDays: number;
};

email: {
name: string;
from: string;
host: string;
port: number;
secure: boolean;
auth: {
user: string;
pass: string;
};
};

payments: {
stripeApiKey: string;
stripeProductId: string;
};
}
37 changes: 23 additions & 14 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
export default () => ({
import { ConfigFactory } from '@nestjs/config/dist/interfaces';
import { Configuration } from './configuration.interface';

export const int = (val: string | undefined, num: number): number =>
val ? (isNaN(parseInt(val)) ? num : parseInt(val)) : num;

const configuration: Configuration = {
frontendUrl: process.env.FRONTEND_URL ?? 'http://localhost:3000',
meta: {
totpServiceName: process.env.TOPT_SERVICE_NAME ?? 'Staart',
},
caching: {
geolocationLruSize: process.env.GEOLOCATION_LRU_SIZE ?? 100,
geolocationLruSize: int(process.env.GEOLOCATION_LRU_SIZE, 100),
},
security: {
saltRounds: process.env.SALT_ROUNDS ?? 10,
saltRounds: int(process.env.SALT_ROUNDS, 10),
jwtSecret: process.env.JWT_SECRET ?? 'staart',
totpWindowPast: process.env.TOTP_WINDOW_PAST ?? 1,
totpWindowFuture: process.env.TOTP_WINDOW_PAST ?? 0,
totpWindowPast: int(process.env.TOTP_WINDOW_PAST, 1),
totpWindowFuture: int(process.env.TOTP_WINDOW_PAST, 0),
mfaTokenExpiry: process.env.MFA_TOKEN_EXPIRY ?? '10m',
accessTokenExpiry: process.env.ACCESS_TOKEN_EXPIRY ?? '1h',
passwordPwnedCheck: !!process.env.PASSWORD_PWNED_CHECK,
unusedRefreshTokenExpiryDays: process.env.DELETE_EXPIRED_SESSIONS ?? 30,
unusedRefreshTokenExpiryDays: int(process.env.DELETE_EXPIRED_SESSIONS, 30),
},
email: {
name: process.env.EMAIL_NAME ?? 'Staart',
from: process.env.EMAIL_FROM,
host: process.env.EMAIL_HOST,
port: process.env.EMAIL_PORT,
from: process.env.EMAIL_FROM ?? '',
host: process.env.EMAIL_HOST ?? '',
port: int(process.env.EMAIL_PORT, 587),
secure: !!process.env.EMAIL_SECURE,
auth: {
user: process.env.EMAIL_USER ?? process.env.EMAIL_FROM,
pass: process.env.EMAIL_PASSWORD,
user: process.env.EMAIL_USER ?? process.env.EMAIL_FROM ?? '',
pass: process.env.EMAIL_PASSWORD ?? '',
},
},
payments: {
stripeApiKey: process.env.STRIPE_API_KEY,
stripeProductId: process.env.STRIPE_PRODUCT_ID,
stripeApiKey: process.env.STRIPE_API_KEY ?? '',
stripeProductId: process.env.STRIPE_PRODUCT_ID ?? '',
},
});
};

const configFunction: ConfigFactory<Configuration> = () => configuration;
export default configFunction;

0 comments on commit c5cb8d8

Please sign in to comment.