mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-03-07 07:12:24 +00:00
refactor: update to typescript 4.4.4
This commit is contained in:
63
src/smtp.ts
63
src/smtp.ts
@@ -138,8 +138,8 @@ export class SMTP extends VerifiableTransport {
|
||||
if (!isProductiveEnvironment() || process.env.ALLOW_NO_TRANSPORT === 'true') {
|
||||
try {
|
||||
SMTP._instance = new SMTP(config);
|
||||
} catch (err) {
|
||||
/* tslint:disable-next-line:no-console */
|
||||
} catch {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('SMTP config failed.');
|
||||
|
||||
return;
|
||||
@@ -163,7 +163,9 @@ export class SMTP extends VerifiableTransport {
|
||||
*/
|
||||
public static isValidEmailAddress(address: string): boolean {
|
||||
// tslint:disable-next-line:max-line-length
|
||||
return /^(([^<>()\[\].,;:\s@"]+(\.[^<>()\[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i.test(address);
|
||||
return /^(([^<>()\[\].,;:\s@"]+(\.[^<>()\[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i.test(
|
||||
address,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,18 +186,18 @@ export class SMTP extends VerifiableTransport {
|
||||
*/
|
||||
private constructor(smtpConfig?: SMTPConfig) {
|
||||
// create a partial config from environment variables that can overwrite the given config
|
||||
const envConfig: RecursivePartial<SMTPConfig> = {
|
||||
const environmentConfig: RecursivePartial<SMTPConfig> = {
|
||||
auth: {
|
||||
password: process.env.SMTP_AUTH_PASSWORD,
|
||||
user: process.env.SMTP_AUTH_USER,
|
||||
},
|
||||
cc: ((typeof process.env.SMTP_CC !== 'undefined') ? (process.env.SMTP_CC as string).split(',') : []),
|
||||
cc: typeof process.env.SMTP_CC !== 'undefined' ? (process.env.SMTP_CC as string).split(',') : [],
|
||||
host: process.env.SMTP_HOST,
|
||||
port: (typeof process.env.SMTP_PORT !== 'undefined') ? parseInt(process.env.SMTP_PORT, 10) : undefined,
|
||||
recipients: (typeof process.env.SMTP_RECIPIENTS !== 'undefined') ?
|
||||
(process.env.SMTP_RECIPIENTS).split(',') :
|
||||
[],
|
||||
secure: (typeof process.env.SMTP_SECURE !== 'undefined') ? (process.env.SMTP_SECURE === 'true') : false,
|
||||
port:
|
||||
typeof process.env.SMTP_PORT !== 'undefined' ? Number.parseInt(process.env.SMTP_PORT, 10) : undefined,
|
||||
recipients:
|
||||
typeof process.env.SMTP_RECIPIENTS !== 'undefined' ? process.env.SMTP_RECIPIENTS.split(',') : [],
|
||||
secure: typeof process.env.SMTP_SECURE !== 'undefined' ? process.env.SMTP_SECURE === 'true' : false,
|
||||
sender: {
|
||||
mail: process.env.SMTP_SENDER_MAIL,
|
||||
name: process.env.SMTP_SENDER_NAME,
|
||||
@@ -205,49 +207,49 @@ export class SMTP extends VerifiableTransport {
|
||||
const config = {
|
||||
...smtpConfig,
|
||||
// deleting undefined properties so the actual config doesn't get overwritten by undefined values
|
||||
...(deleteUndefinedProperties(envConfig) as object),
|
||||
...(deleteUndefinedProperties(environmentConfig) as object),
|
||||
} as SMTPConfig;
|
||||
|
||||
if (typeof config.host === 'undefined') {
|
||||
throw new Error(
|
||||
throw new TypeError(
|
||||
'SMTP configuration needs a host. Add it to the config or use environment variables (SMTP_HOST).',
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof config.port === 'undefined' || isNaN(config.port)) {
|
||||
throw new Error(
|
||||
if (typeof config.port === 'undefined' || Number.isNaN(config.port)) {
|
||||
throw new TypeError(
|
||||
'SMTP configuration needs a port. Add it to the config or use environment variables (SMTP_PORT).',
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof config.auth !== 'object') {
|
||||
throw new Error(
|
||||
throw new TypeError(
|
||||
'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') {
|
||||
throw new Error(
|
||||
throw new TypeError(
|
||||
'SMTP auth configuration needs a user. Add it to the config or use environment variables (SMTP_AUTH_USER).',
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof config.auth.password === 'undefined') {
|
||||
throw new Error(
|
||||
throw new TypeError(
|
||||
'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 === 0) {
|
||||
throw new Error(
|
||||
'SMTP configuration needs recipients. Add it to the config or use environment variables (SMTP_RECIPIENTS).',
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof config.sender.mail === 'undefined') {
|
||||
throw new Error(
|
||||
throw new TypeError(
|
||||
'SMTP configuration needs a sender. Add it to the config or use environment variables (SMTP_SENDER_MAIL).',
|
||||
);
|
||||
}
|
||||
@@ -304,7 +306,7 @@ export class SMTP extends VerifiableTransport {
|
||||
return this.sendMail({
|
||||
cc: this.cc,
|
||||
// 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,
|
||||
subject: subject,
|
||||
text: message,
|
||||
to: this.recipients,
|
||||
@@ -341,20 +343,19 @@ export class SMTP extends VerifiableTransport {
|
||||
* @returns true if the transport is valid
|
||||
*/
|
||||
public async verify(): Promise<boolean> {
|
||||
|
||||
let verificationSuccessfull = false;
|
||||
|
||||
try {
|
||||
verificationSuccessfull = await this.transportAgent.verify();
|
||||
} catch (err) {
|
||||
} catch (error) {
|
||||
if (!isProductiveEnvironment() || process.env.ALLOW_NO_TRANSPORT !== 'true') {
|
||||
throw err;
|
||||
throw error;
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn(
|
||||
'SMTP verification error was ignored, because tranport failures are allowed: ',
|
||||
(err as Error).message,
|
||||
'SMTP verification error was ignored, because tranport failures are allowed:',
|
||||
(error as Error).message,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -362,12 +363,12 @@ export class SMTP extends VerifiableTransport {
|
||||
if (!isProductiveEnvironment() || process.env.ALLOW_NO_TRANSPORT !== 'true') {
|
||||
throw new Error(
|
||||
'Verification of SMTP transport failed.' +
|
||||
'If you want to ignore this error set' +
|
||||
'`NODE_ENV=dev` or `ALLOW_NO_TRANSPORT=true`',
|
||||
'If you want to ignore this error set' +
|
||||
'`NODE_ENV=dev` or `ALLOW_NO_TRANSPORT=true`',
|
||||
);
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn('SMTP verification error was ignored, because tranport failures are allowed.');
|
||||
}
|
||||
this.verified = verificationSuccessfull;
|
||||
|
||||
Reference in New Issue
Block a user