mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 09:32:41 +00:00
refactor: adjust code to updated dependencies
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
*
|
*
|
||||||
* Copied from https://stackoverflow.com/a/51365037
|
* Copied from https://stackoverflow.com/a/51365037
|
||||||
*/
|
*/
|
||||||
import {Transport, VerifiableTransport} from './Transport';
|
import {Transport, VerifiableTransport} from './transport';
|
||||||
|
|
||||||
export type RecursivePartial<T> = {
|
export type RecursivePartial<T> = {
|
||||||
[P in keyof T]?: T[P] extends Array<infer U> ?
|
[P in keyof T]?: T[P] extends Array<infer U> ?
|
||||||
@@ -28,24 +28,32 @@ export type RecursivePartial<T> = {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes all properties that are undefined from an object
|
* Deletes all properties that are undefined from an object
|
||||||
* @param obj
|
*
|
||||||
|
* @param obj Object to delete undefined properties from
|
||||||
*/
|
*/
|
||||||
export function deleteUndefinedProperties(obj: any) {
|
export function deleteUndefinedProperties(obj: unknown) {
|
||||||
// return atomic data types and arrays (recursion anchor)
|
// return atomic data types and arrays (recursion anchor)
|
||||||
if (typeof obj !== 'object' || Array.isArray(obj)) {
|
if (typeof obj !== 'object' || Array.isArray(obj)) {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check each key
|
// check each key
|
||||||
Object.keys(obj).forEach((key) => {
|
for (const key in obj) {
|
||||||
if (typeof obj[key] === 'undefined') {
|
/* istanbul ignore if */
|
||||||
|
if (!obj.hasOwnProperty(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const indexedObj = obj as { [k: string]: unknown; };
|
||||||
|
|
||||||
|
if (typeof indexedObj[key] === 'undefined') {
|
||||||
// delete undefined keys
|
// delete undefined keys
|
||||||
delete obj[key];
|
delete indexedObj[key];
|
||||||
} else {
|
} else {
|
||||||
// check recursive
|
// check recursive
|
||||||
obj[key] = deleteUndefinedProperties(obj[key]);
|
indexedObj[key] = deleteUndefinedProperties(indexedObj[key]);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018, 2019 StApps
|
* Copyright (C) 2019 StApps
|
||||||
* This program is free software: you can redistribute it and/or modify it
|
* This program is free software: you can redistribute it and/or modify it
|
||||||
* under the terms of the GNU General Public License as published by the Free
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* Software Foundation, version 3.
|
||||||
@@ -15,7 +15,17 @@
|
|||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import {stringify} from 'flatted';
|
import {stringify} from 'flatted';
|
||||||
import {isNodeEnvironment, isProductiveNodeEnvironment} from './common';
|
import {isNodeEnvironment, isProductiveNodeEnvironment} from './common';
|
||||||
import {Transport} from './Transport';
|
import {Transport} from './transport';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if something has property STAPPS_LOG_LEVEL
|
||||||
|
*
|
||||||
|
* @param something Something to check
|
||||||
|
*/
|
||||||
|
// tslint:disable-next-line:completed-docs
|
||||||
|
function hasStAppsLogLevel(something: object): something is { STAPPS_LOG_LEVEL: number; } {
|
||||||
|
return 'STAPPS_LOG_LEVEL' in something;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger with colors, loglevel and transport
|
* Logger with colors, loglevel and transport
|
||||||
@@ -33,10 +43,15 @@ import {Transport} from './Transport';
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export class Logger {
|
export class Logger {
|
||||||
|
/**
|
||||||
|
* Base of binary system
|
||||||
|
*/
|
||||||
|
private static readonly binaryBase = 2;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log levels
|
* Log levels
|
||||||
*/
|
*/
|
||||||
private static logLevels = [
|
private static readonly logLevels = [
|
||||||
'INFO',
|
'INFO',
|
||||||
'LOG',
|
'LOG',
|
||||||
'WARN',
|
'WARN',
|
||||||
@@ -44,6 +59,11 @@ export class Logger {
|
|||||||
'OK',
|
'OK',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log level sum, equivalent to all log levels enabled
|
||||||
|
*/
|
||||||
|
private static readonly logLevelSum = Math.pow(Logger.binaryBase, Logger.logLevels.length) - 1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transport for errors
|
* Transport for errors
|
||||||
*/
|
*/
|
||||||
@@ -52,13 +72,13 @@ export class Logger {
|
|||||||
/**
|
/**
|
||||||
* Check if intended log level is allowed in environment log level
|
* Check if intended log level is allowed in environment log level
|
||||||
*
|
*
|
||||||
* @param logLevel
|
* @param logLevel Log level to check
|
||||||
*/
|
*/
|
||||||
private static checkLogLevel(logLevel: string): boolean {
|
private static checkLogLevel(logLevel: string): boolean {
|
||||||
const logLevelNumber = Math.pow(2, Logger.logLevels.indexOf(logLevel) + 1) - 1;
|
const logLevelNumber = Math.pow(Logger.binaryBase, Logger.logLevels.indexOf(logLevel) + 1) - 1;
|
||||||
|
|
||||||
/* tslint:disable-next-line:no-bitwise */
|
// tslint:disable-next-line:no-bitwise
|
||||||
return !!(Logger.getLogLevel() & logLevelNumber);
|
return (Logger.getLogLevel() & logLevelNumber) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,13 +88,15 @@ export class Logger {
|
|||||||
if (isNodeEnvironment() && typeof process.env.STAPPS_LOG_LEVEL !== 'undefined') {
|
if (isNodeEnvironment() && typeof process.env.STAPPS_LOG_LEVEL !== 'undefined') {
|
||||||
// Node.js environment exists
|
// Node.js environment exists
|
||||||
return parseInt(process.env.STAPPS_LOG_LEVEL, 10);
|
return parseInt(process.env.STAPPS_LOG_LEVEL, 10);
|
||||||
} else if (typeof window !== 'undefined' && typeof (window as any).STAPPS_LOG_LEVEL === 'number') {
|
}
|
||||||
|
|
||||||
|
if (typeof window !== 'undefined' && hasStAppsLogLevel(window)) {
|
||||||
// browser environment exists
|
// browser environment exists
|
||||||
return (window as any).STAPPS_LOG_LEVEL;
|
return window.STAPPS_LOG_LEVEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log everything
|
// Log everything
|
||||||
return 31;
|
return Logger.logLevelSum;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,7 +104,7 @@ export class Logger {
|
|||||||
*
|
*
|
||||||
* @param args Arguments to log
|
* @param args Arguments to log
|
||||||
*/
|
*/
|
||||||
public static async error(...args: any[]): Promise<string | void> {
|
public static async error(...args: unknown[]): Promise<string | void> {
|
||||||
if (!Logger.checkLogLevel('ERROR')) {
|
if (!Logger.checkLogLevel('ERROR')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -93,7 +115,9 @@ export class Logger {
|
|||||||
if (isProductiveNodeEnvironment()) {
|
if (isProductiveNodeEnvironment()) {
|
||||||
if (typeof Logger.transport !== 'undefined') {
|
if (typeof Logger.transport !== 'undefined') {
|
||||||
return Logger.transport.send('Error', Logger.stringifyArguments(...args));
|
return Logger.transport.send('Error', Logger.stringifyArguments(...args));
|
||||||
} else if (!process.env.ALLOW_NO_TRANSPORT) {
|
}
|
||||||
|
|
||||||
|
if (process.env.ALLOW_NO_TRANSPORT !== 'true') {
|
||||||
throw new Error(`Error couldn't be transported! Please set a transport or set ALLOW_NO_TRANSPORT='true'.`);
|
throw new Error(`Error couldn't be transported! Please set a transport or set ALLOW_NO_TRANSPORT='true'.`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,13 +128,13 @@ export class Logger {
|
|||||||
*
|
*
|
||||||
* @param args Arguments to log
|
* @param args Arguments to log
|
||||||
*/
|
*/
|
||||||
public static info(...args: any[]): void {
|
public static info(...args: unknown[]): void {
|
||||||
if (!Logger.checkLogLevel('INFO')) {
|
if (!Logger.checkLogLevel('INFO')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:disable-next-line:no-console */
|
/* tslint:disable-next-line:no-console */
|
||||||
console.info(chalk.cyan(`[INFO] ${Logger.stringifyArguments(args)}`));
|
console.info(chalk.cyan(`[INFO] ${Logger.stringifyArguments(...args)}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -118,12 +142,12 @@ export class Logger {
|
|||||||
*/
|
*/
|
||||||
public static initialized(): void {
|
public static initialized(): void {
|
||||||
if (isProductiveNodeEnvironment() && typeof Logger.transport === 'undefined') {
|
if (isProductiveNodeEnvironment() && typeof Logger.transport === 'undefined') {
|
||||||
if (!process.env.ALLOW_NO_TRANSPORT) {
|
if (process.env.ALLOW_NO_TRANSPORT !== 'true') {
|
||||||
throw new Error(`Productive environment doesn't set a transport for error notifications.`);
|
throw new Error(`Productive environment doesn't set a transport for error notifications.`);
|
||||||
} else {
|
|
||||||
/* tslint:disable-next-line:no-console */
|
|
||||||
console.warn(chalk.yellow(`Productive environment doesn't set a transport for error notifications.`));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* tslint:disable-next-line:no-console */
|
||||||
|
console.warn(chalk.yellow(`Productive environment doesn't set a transport for error notifications.`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,13 +156,13 @@ export class Logger {
|
|||||||
*
|
*
|
||||||
* @param args Arguments to log
|
* @param args Arguments to log
|
||||||
*/
|
*/
|
||||||
public static log(...args: any[]): void {
|
public static log(...args: unknown[]): void {
|
||||||
if (!this.checkLogLevel('LOG')) {
|
if (!Logger.checkLogLevel('LOG')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:disable-next-line:no-console */
|
/* tslint:disable-next-line:no-console */
|
||||||
console.log(chalk.white(`[LOG] ${Logger.stringifyArguments(args)}`));
|
console.log(chalk.white(`[LOG] ${Logger.stringifyArguments(...args)}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,13 +170,13 @@ export class Logger {
|
|||||||
*
|
*
|
||||||
* @param args Arguments to log
|
* @param args Arguments to log
|
||||||
*/
|
*/
|
||||||
public static ok(...args: any[]): void {
|
public static ok(...args: unknown[]): void {
|
||||||
if (!this.checkLogLevel('OK')) {
|
if (!Logger.checkLogLevel('OK')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:disable-next-line:no-console */
|
/* tslint:disable-next-line:no-console */
|
||||||
console.log(chalk.bold.green(`[OK] ${Logger.stringifyArguments(args)}`));
|
console.log(chalk.bold.green(`[OK] ${Logger.stringifyArguments(...args)}`));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -169,20 +193,19 @@ export class Logger {
|
|||||||
*
|
*
|
||||||
* @param args Arguments to stringify
|
* @param args Arguments to stringify
|
||||||
*/
|
*/
|
||||||
public static stringifyArguments(...args: any[]): string {
|
public static stringifyArguments(...args: unknown[]): string {
|
||||||
const result: string[] = [];
|
const result: string[] = [];
|
||||||
|
|
||||||
args.forEach((argument) => {
|
args.forEach((argument) => {
|
||||||
const type = typeof argument;
|
if (typeof argument === 'string' || typeof argument === 'number') {
|
||||||
|
result.push(argument.toString());
|
||||||
if (['string', 'number'].indexOf(type) !== -1) {
|
|
||||||
result.push(argument);
|
|
||||||
} else if (argument instanceof Error) {
|
} else if (argument instanceof Error) {
|
||||||
result.push(argument.message);
|
result.push(argument.message);
|
||||||
if (typeof argument.stack !== 'undefined') {
|
if (typeof argument.stack !== 'undefined') {
|
||||||
result.push(argument.stack);
|
result.push(argument.stack);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// tslint:disable-next-line:no-magic-numbers
|
||||||
result.push(stringify(argument, null, 2));
|
result.push(stringify(argument, null, 2));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -195,12 +218,12 @@ export class Logger {
|
|||||||
*
|
*
|
||||||
* @param args Arguments to log
|
* @param args Arguments to log
|
||||||
*/
|
*/
|
||||||
public static warn(...args: any[]): void {
|
public static warn(...args: unknown[]): void {
|
||||||
if (!this.checkLogLevel('WARN')) {
|
if (!Logger.checkLogLevel('WARN')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tslint:disable-next-line:no-console */
|
/* tslint:disable-next-line:no-console */
|
||||||
console.warn(chalk.yellow(`[WARN] ${Logger.stringifyArguments(args)}`));
|
console.warn(chalk.yellow(`[WARN] ${Logger.stringifyArguments(...args)}`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018, 2019 StApps
|
* Copyright (C) 2019 StApps
|
||||||
* This program is free software: you can redistribute it and/or modify it
|
* This program is free software: you can redistribute it and/or modify it
|
||||||
* under the terms of the GNU General Public License as published by the Free
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* Software Foundation, version 3.
|
||||||
@@ -15,23 +15,56 @@
|
|||||||
import * as nodemailer from 'nodemailer';
|
import * as nodemailer from 'nodemailer';
|
||||||
import {MailOptions} from 'nodemailer/lib/sendmail-transport';
|
import {MailOptions} from 'nodemailer/lib/sendmail-transport';
|
||||||
import {deleteUndefinedProperties, isProductiveEnvironment, RecursivePartial} from './common';
|
import {deleteUndefinedProperties, isProductiveEnvironment, RecursivePartial} from './common';
|
||||||
import {VerifiableTransport} from './Transport';
|
import {VerifiableTransport} from './transport';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A configuration of the transport used to send mails via SMTP
|
* A configuration of the transport used to send mails via SMTP
|
||||||
*/
|
*/
|
||||||
export interface SMTPConfig {
|
export interface SMTPConfig {
|
||||||
|
/**
|
||||||
|
* Auth configuration
|
||||||
|
*/
|
||||||
auth: {
|
auth: {
|
||||||
|
/**
|
||||||
|
* Password for login
|
||||||
|
*/
|
||||||
password: string;
|
password: string;
|
||||||
|
/**
|
||||||
|
* User for login
|
||||||
|
*/
|
||||||
user: string;
|
user: string;
|
||||||
};
|
};
|
||||||
|
/**
|
||||||
|
* List of "carbon copy" recipients
|
||||||
|
*/
|
||||||
cc?: string[];
|
cc?: string[];
|
||||||
|
/**
|
||||||
|
* SMTP host server
|
||||||
|
*/
|
||||||
host: string;
|
host: string;
|
||||||
|
/**
|
||||||
|
* SMTP port
|
||||||
|
*/
|
||||||
port: number;
|
port: number;
|
||||||
|
/**
|
||||||
|
* List of recipients
|
||||||
|
*/
|
||||||
recipients: string[];
|
recipients: string[];
|
||||||
|
/**
|
||||||
|
* Whether or not to establish a secure connection
|
||||||
|
*/
|
||||||
secure?: boolean;
|
secure?: boolean;
|
||||||
|
/**
|
||||||
|
* Sender configuration
|
||||||
|
*/
|
||||||
sender: {
|
sender: {
|
||||||
|
/**
|
||||||
|
* Mail of sender
|
||||||
|
*/
|
||||||
mail: string;
|
mail: string;
|
||||||
|
/**
|
||||||
|
* Name of sender
|
||||||
|
*/
|
||||||
name?: string;
|
name?: string;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -40,7 +73,9 @@ export interface SMTPConfig {
|
|||||||
* An implementation of mail transport via SMTP
|
* An implementation of mail transport via SMTP
|
||||||
*/
|
*/
|
||||||
export class SMTP extends VerifiableTransport {
|
export class SMTP extends VerifiableTransport {
|
||||||
|
/**
|
||||||
|
* Singleton instance
|
||||||
|
*/
|
||||||
private static _instance: SMTP;
|
private static _instance: SMTP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,8 +86,14 @@ export class SMTP extends VerifiableTransport {
|
|||||||
/**
|
/**
|
||||||
* Who is using this service
|
* Who is using this service
|
||||||
*/
|
*/
|
||||||
private from: {
|
private readonly from: {
|
||||||
|
/**
|
||||||
|
* Mail of sender
|
||||||
|
*/
|
||||||
mail: string;
|
mail: string;
|
||||||
|
/**
|
||||||
|
* Name of sender
|
||||||
|
*/
|
||||||
name?: string;
|
name?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -64,7 +105,7 @@ export class SMTP extends VerifiableTransport {
|
|||||||
/**
|
/**
|
||||||
* Connection to SMTP server
|
* Connection to SMTP server
|
||||||
*/
|
*/
|
||||||
private transportAgent: nodemailer.Transporter;
|
private readonly transportAgent: nodemailer.Transporter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set to true if the transport was verified
|
* Set to true if the transport was verified
|
||||||
@@ -84,30 +125,31 @@ export class SMTP extends VerifiableTransport {
|
|||||||
* SMTP_SENDER_MAIL: sender of the mail
|
* SMTP_SENDER_MAIL: sender of the mail
|
||||||
* SMTP_SENDER_NAME: name of the sender
|
* SMTP_SENDER_NAME: name of the sender
|
||||||
* SMTP_SECURE: `true` to enable tls
|
* SMTP_SECURE: `true` to enable tls
|
||||||
* @param config {SMTPConfig}
|
*
|
||||||
* @return {Transport}
|
* @param config SMTP config for instance
|
||||||
*/
|
*/
|
||||||
public static getInstance(config?: SMTPConfig): SMTP | undefined {
|
public static getInstance(config?: SMTPConfig): SMTP | undefined {
|
||||||
// if an instance of SMTP already exists
|
// if an instance of SMTP already exists
|
||||||
if (this._instance) {
|
if (typeof SMTP._instance !== 'undefined') {
|
||||||
return this._instance;
|
return SMTP._instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
// monitoring is not required -> SMTP init can fail
|
// monitoring is not required -> SMTP init can fail
|
||||||
if (!isProductiveEnvironment() || process.env.ALLOW_NO_TRANSPORT === 'true') {
|
if (!isProductiveEnvironment() || process.env.ALLOW_NO_TRANSPORT === 'true') {
|
||||||
try {
|
try {
|
||||||
this._instance = new this(config);
|
SMTP._instance = new SMTP(config);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
/* tslint:disable-next-line:no-console */
|
/* tslint:disable-next-line:no-console */
|
||||||
console.warn('SMTP config failed.');
|
console.warn('SMTP config failed.');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// monitoring is required -> SMTP will throw error if config is invalid
|
// monitoring is required -> SMTP will throw error if config is invalid
|
||||||
this._instance = new this(config);
|
SMTP._instance = new SMTP(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._instance;
|
return SMTP._instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,8 +159,7 @@ export class SMTP extends VerifiableTransport {
|
|||||||
* For more information please consider reading
|
* For more information please consider reading
|
||||||
* https://stackoverflow.com/a/2071250
|
* https://stackoverflow.com/a/2071250
|
||||||
*
|
*
|
||||||
* @param {string} address
|
* @param address Address to validate
|
||||||
* @return {boolean}
|
|
||||||
*/
|
*/
|
||||||
public static isValidEmailAddress(address: string): boolean {
|
public static isValidEmailAddress(address: string): boolean {
|
||||||
// tslint:disable-next-line:max-line-length
|
// tslint:disable-next-line:max-line-length
|
||||||
@@ -126,20 +167,22 @@ export class SMTP extends VerifiableTransport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks a list of mail addresses for validity.
|
* Checks a list of mail addresses for validity
|
||||||
* @param {string[]} recipients
|
*
|
||||||
* @return {string[]}
|
* @param recipients List of recipients to check
|
||||||
*/
|
*/
|
||||||
public static isValidRecipientsList(recipients: string[] | undefined): boolean {
|
public static isValidRecipientsList(recipients: string[] | undefined): boolean {
|
||||||
return Array.isArray(recipients) && recipients.length > 0 && recipients.every(this.isValidEmailAddress);
|
return Array.isArray(recipients) && recipients.length > 0 && recipients.every(SMTP.isValidEmailAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an SMTP connection. WARNING: This class is supposed to be used as a singleton. You should never
|
* Creates an SMTP connection.
|
||||||
* call `new SMTP()`
|
*
|
||||||
* @param {SMTPConfig} config
|
* WARNING: This class is supposed to be used as a singleton. You should never call `new SMTP()`
|
||||||
|
*
|
||||||
|
* @param smtpConfig SMTP config
|
||||||
*/
|
*/
|
||||||
private constructor(config?: SMTPConfig) {
|
private constructor(smtpConfig?: SMTPConfig) {
|
||||||
// create a partial config from environment variables that can overwrite the given config
|
// create a partial config from environment variables that can overwrite the given config
|
||||||
const envConfig: RecursivePartial<SMTPConfig> = {
|
const envConfig: RecursivePartial<SMTPConfig> = {
|
||||||
auth: {
|
auth: {
|
||||||
@@ -159,52 +202,51 @@ export class SMTP extends VerifiableTransport {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
// @ts-ignore
|
const config = {
|
||||||
config = {
|
...smtpConfig,
|
||||||
...config,
|
|
||||||
// deleting undefined properties so the actual config doesn't get overwritten by undefined values
|
// deleting undefined properties so the actual config doesn't get overwritten by undefined values
|
||||||
...deleteUndefinedProperties(envConfig),
|
...deleteUndefinedProperties(envConfig),
|
||||||
};
|
} as SMTPConfig;
|
||||||
|
|
||||||
if (typeof config!.host === 'undefined') {
|
if (typeof config.host === 'undefined') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'SMTP configuration needs a host. Add it to the config or use environment variables (SMTP_HOST).',
|
'SMTP configuration needs a host. Add it to the config or use environment variables (SMTP_HOST).',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof config!.port === 'undefined' || isNaN(config!.port)) {
|
if (typeof config.port === 'undefined' || isNaN(config.port)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'SMTP configuration needs a port. Add it to the config or use environment variables (SMTP_PORT).',
|
'SMTP configuration needs a port. Add it to the config or use environment variables (SMTP_PORT).',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof config!.auth !== 'object') {
|
if (typeof config.auth !== 'object') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'SMTP configuration needs an auth object.' +
|
'SMTP configuration needs an auth object.' +
|
||||||
'Add it to the config or use environment variables (SMTP_AUTH_USER, SMTP_AUTH_PASSWORD).',
|
'Add it to the config or use environment variables (SMTP_AUTH_USER, SMTP_AUTH_PASSWORD).',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof config!.auth.user === 'undefined') {
|
if (typeof config.auth.user === 'undefined') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'SMTP auth configuration needs a user. Add it to the config or use environment variables (SMTP_AUTH_USER).',
|
'SMTP auth configuration needs a user. Add it to the config or use environment variables (SMTP_AUTH_USER).',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof config!.auth.password === 'undefined') {
|
if (typeof config.auth.password === 'undefined') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'SMTP auth configuration needs a password.' +
|
'SMTP auth configuration needs a password.' +
|
||||||
'Add it to the config or use environment variables (SMTP_AUTH_PASSWORD).',
|
'Add it to the config or use environment variables (SMTP_AUTH_PASSWORD).',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(config!.recipients) && config!.recipients.length < 1) {
|
if (Array.isArray(config.recipients) && config.recipients.length < 1) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'SMTP configuration needs recipients. Add it to the config or use environment variables (SMTP_RECIPIENTS).',
|
'SMTP configuration needs recipients. Add it to the config or use environment variables (SMTP_RECIPIENTS).',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof config!.sender.mail === 'undefined') {
|
if (typeof config.sender.mail === 'undefined') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'SMTP configuration needs a sender. Add it to the config or use environment variables (SMTP_SENDER_MAIL).',
|
'SMTP configuration needs a sender. Add it to the config or use environment variables (SMTP_SENDER_MAIL).',
|
||||||
);
|
);
|
||||||
@@ -212,15 +254,15 @@ export class SMTP extends VerifiableTransport {
|
|||||||
|
|
||||||
super();
|
super();
|
||||||
|
|
||||||
if (SMTP.isValidRecipientsList(config!.recipients)) {
|
if (SMTP.isValidRecipientsList(config.recipients)) {
|
||||||
this.recipients = config!.recipients;
|
this.recipients = config.recipients;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid recipients found');
|
throw new Error('Invalid recipients found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof config!.cc !== 'undefined') {
|
if (typeof config.cc !== 'undefined') {
|
||||||
if (SMTP.isValidRecipientsList(config!.cc)) {
|
if (SMTP.isValidRecipientsList(config.cc)) {
|
||||||
this.cc = config!.cc!;
|
this.cc = config.cc;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Invalid cc recipients found');
|
throw new Error('Invalid cc recipients found');
|
||||||
}
|
}
|
||||||
@@ -228,25 +270,24 @@ export class SMTP extends VerifiableTransport {
|
|||||||
this.cc = [];
|
this.cc = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
this.from = config!.sender;
|
this.from = config.sender;
|
||||||
|
|
||||||
this.verified = false;
|
this.verified = false;
|
||||||
|
|
||||||
// creating transport with configuration
|
// creating transport with configuration
|
||||||
this.transportAgent = nodemailer.createTransport({
|
this.transportAgent = nodemailer.createTransport({
|
||||||
auth: {
|
auth: {
|
||||||
pass: config!.auth.password,
|
pass: config.auth.password,
|
||||||
user: config!.auth.user,
|
user: config.auth.user,
|
||||||
},
|
},
|
||||||
host: config!.host,
|
host: config.host,
|
||||||
port: config!.port,
|
port: config.port,
|
||||||
secure: typeof config!.secure !== 'undefined' ? config!.secure : false,
|
secure: typeof config.secure !== 'undefined' ? config.secure : false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if instance was verified at least once
|
* Check if instance was verified at least once
|
||||||
* @returns {boolean}
|
|
||||||
*/
|
*/
|
||||||
public isVerified(): boolean {
|
public isVerified(): boolean {
|
||||||
return this.verified;
|
return this.verified;
|
||||||
@@ -255,11 +296,12 @@ export class SMTP extends VerifiableTransport {
|
|||||||
/**
|
/**
|
||||||
* Sends a preconfigured mail with recipients and sender configured on
|
* Sends a preconfigured mail with recipients and sender configured on
|
||||||
* creation of the class (set by environment or on creation of this class)
|
* creation of the class (set by environment or on creation of this class)
|
||||||
* @param {string} subject
|
*
|
||||||
* @param {string} message
|
* @param subject Subject of the mail
|
||||||
|
* @param message message of the mail
|
||||||
*/
|
*/
|
||||||
public async send(subject: string, message: string): Promise<string> {
|
public async send(subject: string, message: string): Promise<string> {
|
||||||
return await this.sendMail({
|
return this.sendMail({
|
||||||
cc: this.cc,
|
cc: this.cc,
|
||||||
// use an address block if name is available, mail otherwise
|
// use an address block if name is available, mail otherwise
|
||||||
from: (typeof this.from.name !== 'string') ? `${this.from.name} <${this.from.mail}>` : this.from.mail,
|
from: (typeof this.from.name !== 'string') ? `${this.from.name} <${this.from.mail}>` : this.from.mail,
|
||||||
@@ -271,7 +313,8 @@ export class SMTP extends VerifiableTransport {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a mail object
|
* Sends a mail object
|
||||||
* @param {MailOptions} mail
|
*
|
||||||
|
* @param mail Mail to send
|
||||||
*/
|
*/
|
||||||
public async sendMail(mail: MailOptions): Promise<string> {
|
public async sendMail(mail: MailOptions): Promise<string> {
|
||||||
// info is the response of the smtp server
|
// info is the response of the smtp server
|
||||||
@@ -299,20 +342,20 @@ export class SMTP extends VerifiableTransport {
|
|||||||
*/
|
*/
|
||||||
public async verify(): Promise<boolean> {
|
public async verify(): Promise<boolean> {
|
||||||
|
|
||||||
let verificationSuccessfull: boolean = false;
|
let verificationSuccessfull = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
verificationSuccessfull = await this.transportAgent.verify();
|
verificationSuccessfull = await this.transportAgent.verify();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (!isProductiveEnvironment() || process.env.ALLOW_NO_TRANSPORT !== 'true') {
|
if (!isProductiveEnvironment() || process.env.ALLOW_NO_TRANSPORT !== 'true') {
|
||||||
throw err;
|
throw err;
|
||||||
} else {
|
|
||||||
/* tslint:disable-next-line:no-console */
|
|
||||||
console.warn(
|
|
||||||
'SMTP verification error was ignored, because tranport failures are allowed: ',
|
|
||||||
err.message,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* tslint:disable-next-line:no-console */
|
||||||
|
console.warn(
|
||||||
|
'SMTP verification error was ignored, because tranport failures are allowed: ',
|
||||||
|
err.message,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!verificationSuccessfull) {
|
if (!verificationSuccessfull) {
|
||||||
@@ -322,12 +365,13 @@ export class SMTP extends VerifiableTransport {
|
|||||||
'If you want to ignore this error set' +
|
'If you want to ignore this error set' +
|
||||||
'`NODE_ENV=dev` or `ALLOW_NO_TRANSPORT=true`',
|
'`NODE_ENV=dev` or `ALLOW_NO_TRANSPORT=true`',
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
/* tslint:disable-next-line:no-console */
|
|
||||||
console.warn('SMTP verification error was ignored, because tranport failures are allowed.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* tslint:disable-next-line:no-console */
|
||||||
|
console.warn('SMTP verification error was ignored, because tranport failures are allowed.');
|
||||||
}
|
}
|
||||||
this.verified = verificationSuccessfull;
|
this.verified = verificationSuccessfull;
|
||||||
|
|
||||||
return verificationSuccessfull;
|
return verificationSuccessfull;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018, 2019 StApps
|
* Copyright (C) 2019 StApps
|
||||||
* This program is free software: you can redistribute it and/or modify it
|
* This program is free software: you can redistribute it and/or modify it
|
||||||
* under the terms of the GNU General Public License as published by the Free
|
* under the terms of the GNU General Public License as published by the Free
|
||||||
* Software Foundation, version 3.
|
* Software Foundation, version 3.
|
||||||
Reference in New Issue
Block a user