mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-21 09:03:02 +00:00
refactor: split api into api, api-cli & api-plugin
This commit is contained in:
@@ -45,7 +45,7 @@
|
||||
"ts-node": "10.9.1",
|
||||
"tsup": "6.7.0",
|
||||
"typedoc": "0.24.7",
|
||||
"typescript": "4.8.4"
|
||||
"typescript": "4.9.5"
|
||||
},
|
||||
"tsup": {
|
||||
"entry": [
|
||||
|
||||
@@ -29,7 +29,6 @@ export type RecursivePartial<T> = {
|
||||
|
||||
/**
|
||||
* Deletes all properties that are undefined from an object
|
||||
*
|
||||
* @param object Object to delete undefined properties from
|
||||
*/
|
||||
export function deleteUndefinedProperties(object: unknown): unknown {
|
||||
@@ -47,7 +46,7 @@ export function deleteUndefinedProperties(object: unknown): unknown {
|
||||
|
||||
const indexedObject = object as {[k: string]: unknown};
|
||||
|
||||
if (typeof indexedObject[key] === 'undefined') {
|
||||
if (indexedObject[key] === undefined) {
|
||||
// delete undefined keys
|
||||
delete indexedObject[key];
|
||||
} else {
|
||||
@@ -72,7 +71,7 @@ export function isNodeEnvironment(): boolean {
|
||||
export function isProductiveEnvironment(): boolean {
|
||||
return (
|
||||
typeof process.env === 'object' &&
|
||||
typeof process.env.NODE_ENV !== 'undefined' &&
|
||||
process.env.NODE_ENV !== undefined &&
|
||||
process.env.NODE_ENV === 'production'
|
||||
);
|
||||
}
|
||||
@@ -86,7 +85,6 @@ export function isProductiveNodeEnvironment(): boolean {
|
||||
|
||||
/**
|
||||
* Check if a transport is a verifiable transport
|
||||
*
|
||||
* @param transport Transport to check
|
||||
*/
|
||||
export function isTransportWithVerification(transport: Transport): transport is VerifiableTransport {
|
||||
|
||||
@@ -20,7 +20,6 @@ import {Transport} from './transport.js';
|
||||
|
||||
/**
|
||||
* Check if something has property STAPPS_LOG_LEVEL
|
||||
*
|
||||
* @param something Something to check
|
||||
*/
|
||||
// tslint:disable-next-line:completed-docs
|
||||
@@ -30,7 +29,6 @@ function hasStAppsLogLevel(something: object): something is {STAPPS_LOG_LEVEL: n
|
||||
|
||||
/**
|
||||
* Check if something has property STAPPS_EXIT_LEVEL
|
||||
*
|
||||
* @param something Something to check
|
||||
*/
|
||||
// tslint:disable-next-line:completed-docs
|
||||
@@ -92,13 +90,12 @@ export class Logger {
|
||||
/**
|
||||
* Apply transformations to an output
|
||||
* Will strip newlines in production environment
|
||||
*
|
||||
* @param logLevel Log level of the output
|
||||
* @param output Output to apply transformations to
|
||||
*/
|
||||
private static applyTransformers(logLevel: LogLevel, output: string): string {
|
||||
if (isProductiveEnvironment()) {
|
||||
output = output.replace(/[\n\r]/g, ' ');
|
||||
output = output.replaceAll(/[\n\r]/g, ' ');
|
||||
}
|
||||
|
||||
if (!Array.isArray(Logger.transformations) || Logger.transformations.length === 0) {
|
||||
@@ -115,7 +112,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Check if intended exit level is allowed in environment exit level
|
||||
*
|
||||
* @param exitLevel Log level to check
|
||||
*/
|
||||
private static checkExitLevel(exitLevel: LogLevel): boolean {
|
||||
@@ -129,7 +125,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Check if intended log level is allowed in environment log level
|
||||
*
|
||||
* @param logLevel Log level to check
|
||||
*/
|
||||
private static checkLogLevel(logLevel: LogLevel): boolean {
|
||||
@@ -168,7 +163,7 @@ export class Logger {
|
||||
|
||||
const environmentLevel = level === 'LOG' ? process.env.STAPPS_LOG_LEVEL : process.env.STAPPS_EXIT_LEVEL;
|
||||
|
||||
if (isNodeEnvironment() && typeof environmentLevel !== 'undefined') {
|
||||
if (isNodeEnvironment() && environmentLevel !== undefined) {
|
||||
// Node.js environment exists
|
||||
return Number.parseInt(environmentLevel, 10);
|
||||
}
|
||||
@@ -186,7 +181,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Get number of specific log level
|
||||
*
|
||||
* @param logLevel Log level to check
|
||||
*/
|
||||
private static logLevelNumber(logLevel: LogLevel): number {
|
||||
@@ -195,7 +189,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Log an error
|
||||
*
|
||||
* @param arguments_ Arguments to log
|
||||
*/
|
||||
public static async error(...arguments_: unknown[]): Promise<string | void> {
|
||||
@@ -207,7 +200,7 @@ export class Logger {
|
||||
console.error(Logger.applyTransformers('ERROR', Logger.stringifyArguments(...arguments_)));
|
||||
|
||||
if (isProductiveNodeEnvironment()) {
|
||||
if (typeof Logger.transport !== 'undefined') {
|
||||
if (Logger.transport !== undefined) {
|
||||
return Logger.transport.send('Error', Logger.stringifyArguments(...arguments_));
|
||||
}
|
||||
|
||||
@@ -224,7 +217,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Log an information
|
||||
*
|
||||
* @param arguments_ Arguments to log
|
||||
*/
|
||||
public static info(...arguments_: unknown[]): void {
|
||||
@@ -243,7 +235,7 @@ export class Logger {
|
||||
* Check if the logger is initialized correctly
|
||||
*/
|
||||
public static initialized(): void {
|
||||
if (isProductiveNodeEnvironment() && typeof Logger.transport === 'undefined') {
|
||||
if (isProductiveNodeEnvironment() && Logger.transport === undefined) {
|
||||
if (process.env.ALLOW_NO_TRANSPORT !== 'true') {
|
||||
throw new Error(`Productive environment doesn't set a transport for error notifications.`);
|
||||
}
|
||||
@@ -254,7 +246,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Log something
|
||||
*
|
||||
* @param arguments_ Arguments to log
|
||||
*/
|
||||
public static log(...arguments_: unknown[]): void {
|
||||
@@ -271,7 +262,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Log something successful
|
||||
*
|
||||
* @param arguments_ Arguments to log
|
||||
*/
|
||||
public static ok(...arguments_: unknown[]): void {
|
||||
@@ -288,7 +278,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Set transformations for log output
|
||||
*
|
||||
* @param transformations List of transformations
|
||||
*/
|
||||
public static setTransformations(transformations: Transformation[]) {
|
||||
@@ -300,7 +289,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Set a transport
|
||||
*
|
||||
* @param transport Transport to set
|
||||
*/
|
||||
public static setTransport(transport?: Transport) {
|
||||
@@ -309,7 +297,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Stringify a list of arguments
|
||||
*
|
||||
* @param arguments_ Arguments to stringify
|
||||
*/
|
||||
public static stringifyArguments(...arguments_: unknown[]): string {
|
||||
@@ -320,7 +307,7 @@ export class Logger {
|
||||
result.push(argument.toString());
|
||||
} else if (argument instanceof Error) {
|
||||
result.push(argument.message);
|
||||
if (typeof argument.stack !== 'undefined') {
|
||||
if (argument.stack !== undefined) {
|
||||
result.push(argument.stack);
|
||||
}
|
||||
} else {
|
||||
@@ -333,7 +320,6 @@ export class Logger {
|
||||
|
||||
/**
|
||||
* Log a warning
|
||||
*
|
||||
* @param arguments_ Arguments to log
|
||||
*/
|
||||
public static warn(...arguments_: unknown[]): void {
|
||||
|
||||
@@ -125,12 +125,11 @@ export class SMTP extends VerifiableTransport {
|
||||
* SMTP_SENDER_MAIL: sender of the mail
|
||||
* SMTP_SENDER_NAME: name of the sender
|
||||
* SMTP_SECURE: `true` to enable tls
|
||||
*
|
||||
* @param config SMTP config for instance
|
||||
*/
|
||||
public static getInstance(config?: SMTPConfig): SMTP | undefined {
|
||||
// if an instance of SMTP already exists
|
||||
if (typeof SMTP._instance !== 'undefined') {
|
||||
if (SMTP._instance !== undefined) {
|
||||
return SMTP._instance;
|
||||
}
|
||||
|
||||
@@ -158,7 +157,6 @@ export class SMTP extends VerifiableTransport {
|
||||
*
|
||||
* For more information please consider reading
|
||||
* https://stackoverflow.com/a/2071250
|
||||
*
|
||||
* @param address Address to validate
|
||||
*/
|
||||
public static isValidEmailAddress(address: string): boolean {
|
||||
@@ -170,7 +168,6 @@ export class SMTP extends VerifiableTransport {
|
||||
|
||||
/**
|
||||
* Checks a list of mail addresses for validity
|
||||
*
|
||||
* @param recipients List of recipients to check
|
||||
*/
|
||||
public static isValidRecipientsList(recipients: string[] | undefined): boolean {
|
||||
@@ -181,7 +178,6 @@ export class SMTP extends VerifiableTransport {
|
||||
* Creates an SMTP connection.
|
||||
*
|
||||
* WARNING: This class is supposed to be used as a singleton. You should never call `new SMTP()`
|
||||
*
|
||||
* @param smtpConfig SMTP config
|
||||
*/
|
||||
private constructor(smtpConfig?: SMTPConfig) {
|
||||
@@ -191,13 +187,11 @@ export class SMTP extends VerifiableTransport {
|
||||
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: process.env.SMTP_CC === undefined ? [] : (process.env.SMTP_CC as string).split(','),
|
||||
host: process.env.SMTP_HOST,
|
||||
port:
|
||||
typeof process.env.SMTP_PORT === 'undefined' ? undefined : Number.parseInt(process.env.SMTP_PORT, 10),
|
||||
recipients:
|
||||
typeof process.env.SMTP_RECIPIENTS === 'undefined' ? [] : process.env.SMTP_RECIPIENTS.split(','),
|
||||
secure: typeof process.env.SMTP_SECURE === 'undefined' ? false : process.env.SMTP_SECURE === 'true',
|
||||
port: process.env.SMTP_PORT === undefined ? undefined : Number.parseInt(process.env.SMTP_PORT, 10),
|
||||
recipients: process.env.SMTP_RECIPIENTS === undefined ? [] : process.env.SMTP_RECIPIENTS.split(','),
|
||||
secure: process.env.SMTP_SECURE === undefined ? false : process.env.SMTP_SECURE === 'true',
|
||||
sender: {
|
||||
mail: process.env.SMTP_SENDER_MAIL,
|
||||
name: process.env.SMTP_SENDER_NAME,
|
||||
@@ -210,13 +204,13 @@ export class SMTP extends VerifiableTransport {
|
||||
...(deleteUndefinedProperties(environmentConfig) as object),
|
||||
} as SMTPConfig;
|
||||
|
||||
if (typeof config.host === 'undefined') {
|
||||
if (config.host === undefined) {
|
||||
throw new TypeError(
|
||||
'SMTP configuration needs a host. Add it to the config or use environment variables (SMTP_HOST).',
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof config.port === 'undefined' || Number.isNaN(config.port)) {
|
||||
if (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).',
|
||||
);
|
||||
@@ -229,13 +223,13 @@ export class SMTP extends VerifiableTransport {
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof config.auth.user === 'undefined') {
|
||||
if (config.auth.user === undefined) {
|
||||
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') {
|
||||
if (config.auth.password === undefined) {
|
||||
throw new TypeError(
|
||||
'SMTP auth configuration needs a password.' +
|
||||
'Add it to the config or use environment variables (SMTP_AUTH_PASSWORD).',
|
||||
@@ -248,7 +242,7 @@ export class SMTP extends VerifiableTransport {
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof config.sender.mail === 'undefined') {
|
||||
if (config.sender.mail === undefined) {
|
||||
throw new TypeError(
|
||||
'SMTP configuration needs a sender. Add it to the config or use environment variables (SMTP_SENDER_MAIL).',
|
||||
);
|
||||
@@ -262,7 +256,7 @@ export class SMTP extends VerifiableTransport {
|
||||
throw new Error('Invalid recipients found');
|
||||
}
|
||||
|
||||
if (typeof config.cc === 'undefined') {
|
||||
if (config.cc === undefined) {
|
||||
this.cc = [];
|
||||
} else {
|
||||
if (SMTP.isValidRecipientsList(config.cc)) {
|
||||
@@ -284,7 +278,7 @@ export class SMTP extends VerifiableTransport {
|
||||
},
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
secure: typeof config.secure === 'undefined' ? false : config.secure,
|
||||
secure: config.secure === undefined ? false : config.secure,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -298,7 +292,6 @@ export class SMTP extends VerifiableTransport {
|
||||
/**
|
||||
* Sends a preconfigured mail with recipients and sender configured on
|
||||
* creation of the class (set by environment or on creation of this class)
|
||||
*
|
||||
* @param subject Subject of the mail
|
||||
* @param message message of the mail
|
||||
*/
|
||||
@@ -315,7 +308,6 @@ export class SMTP extends VerifiableTransport {
|
||||
|
||||
/**
|
||||
* Sends a mail object
|
||||
*
|
||||
* @param mail Mail to send
|
||||
*/
|
||||
public async sendMail(mail: MailOptions): Promise<string> {
|
||||
@@ -323,7 +315,7 @@ export class SMTP extends VerifiableTransport {
|
||||
let info = await this.transportAgent.sendMail(mail);
|
||||
|
||||
// it can be undefined (empty response)
|
||||
if (typeof info === 'undefined') {
|
||||
if (info === undefined) {
|
||||
info = 'Successfully sent mail';
|
||||
}
|
||||
|
||||
@@ -337,8 +329,6 @@ export class SMTP extends VerifiableTransport {
|
||||
|
||||
/**
|
||||
* Verify authentication with SMTP server
|
||||
*
|
||||
*
|
||||
* @throws error if you are in a productive environment and don't set ALLOW_NO_TRANSPORT to true
|
||||
* @returns true if the transport is valid
|
||||
*/
|
||||
|
||||
@@ -25,7 +25,6 @@ export interface Transformation {
|
||||
|
||||
/**
|
||||
* Transform an output
|
||||
*
|
||||
* @param logLevel Log level to transform output for
|
||||
* @param output Output to transform
|
||||
*/
|
||||
|
||||
@@ -26,7 +26,6 @@ export class AddLogLevel implements Transformation {
|
||||
|
||||
/**
|
||||
* Add log level to output
|
||||
*
|
||||
* @param logLevel Log level to add to output
|
||||
* @param output Output to colorize
|
||||
*/
|
||||
|
||||
@@ -29,7 +29,6 @@ export class Colorize implements Transformation {
|
||||
|
||||
/**
|
||||
* Instantiate a new colorize transformation
|
||||
*
|
||||
* @param logLevelToColor Map from log level to color transformation to apply
|
||||
*/
|
||||
constructor(
|
||||
@@ -47,7 +46,6 @@ export class Colorize implements Transformation {
|
||||
|
||||
/**
|
||||
* Colorize log output
|
||||
*
|
||||
* @param logLevel Log level to choose color for
|
||||
* @param output Output to colorize
|
||||
*/
|
||||
|
||||
@@ -26,7 +26,6 @@ export class Timestamp implements Transformation {
|
||||
|
||||
/**
|
||||
* Add timestamp to output
|
||||
*
|
||||
* @param _logLevel Log level to add timestamp to output for
|
||||
* @param output Output to add timestamp to
|
||||
*/
|
||||
|
||||
@@ -21,7 +21,6 @@ export abstract class Transport {
|
||||
* Send message with subject
|
||||
*
|
||||
* A message can be a mail or something completely different depending on what transport is implemented.
|
||||
*
|
||||
* @param subject Subject of the message
|
||||
* @param message Message to send
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user