diff --git a/Accounts/Index.ts b/Accounts/Index.ts index dcab0561e2..daac35c659 100755 --- a/Accounts/Index.ts +++ b/Accounts/Index.ts @@ -3,7 +3,6 @@ import App from 'CommonServer/Utils/StartServer'; export const APP_NAME: string = 'accounts'; const app = App(APP_NAME); - import path from 'path'; import { ExpressRequest, diff --git a/AdminDashboard/index.ts b/AdminDashboard/index.ts index 5d3563dd8d..fe9c97a298 100644 --- a/AdminDashboard/index.ts +++ b/AdminDashboard/index.ts @@ -9,7 +9,6 @@ import App from 'CommonServer/Utils/StartServer'; export const APP_NAME: string = 'admin'; const app = App(APP_NAME); - app.use(ExpressStatic(path.join(__dirname, 'build'))); app.use(`/${APP_NAME}`, ExpressStatic(path.join(__dirname, 'build'))); app.use( diff --git a/ApplicationScanner/index.ts b/ApplicationScanner/index.ts index 145ff9bf9e..7c306e2e28 100644 --- a/ApplicationScanner/index.ts +++ b/ApplicationScanner/index.ts @@ -1,8 +1,5 @@ -import { ExpressRequest, ExpressResponse } from 'CommonServer/utils/Express'; - import App from 'CommonServer/utils/StartServer'; - export const APP_NAME: string = 'application'; const app = App(APP_NAME); diff --git a/Common/Models/BaseModel.ts b/Common/Models/BaseModel.ts index 1c5872f368..45d946a986 100644 --- a/Common/Models/BaseModel.ts +++ b/Common/Models/BaseModel.ts @@ -25,7 +25,6 @@ import AccessControl from '../Types/Database/AccessControls/AccessControl'; import Dictionary from '../Types/Dictionary'; export default class BaseModel extends BaseEntity { - @TableColumn({ title: 'ID' }) @PrimaryGeneratedColumn('uuid') public _id?: string = undefined; @@ -609,7 +608,10 @@ export default class BaseModel extends BaseEntity { } } - private static _fromJSON(json: JSONObject, type: { new(): T ;} ): T { + private static _fromJSON( + json: JSONObject, + type: { new (): T } + ): T { const baseModel: T = new type(); for (const key of Object.keys(json)) { @@ -619,15 +621,18 @@ export default class BaseModel extends BaseEntity { return baseModel as T; } - public static fromJSON(json: JSONObject | JSONArray, type: { new(): T ;} ): T | Array { + public static fromJSON( + json: JSONObject | JSONArray, + type: { new (): T } + ): T | Array { if (Array.isArray(json)) { - let arr: Array = []; + const arr: Array = []; for (const item of json) { - arr.push(this._fromJSON(item, type)) + arr.push(this._fromJSON(item, type)); } - return arr; + return arr; } return this._fromJSON(json, type); @@ -636,7 +641,7 @@ export default class BaseModel extends BaseEntity { private static keepColumns( data: T, columnsToKeep: Columns, - type: { new(): T ;} + type: { new (): T } ): T { const baseModel: T = new type(); @@ -658,24 +663,32 @@ export default class BaseModel extends BaseEntity { } public static asPublicCreateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); } if (!data.canPublicCreateRecord) { - // throw new BadRequestException( - // 'A user of role public cannot create this record.' - // ); + /* + * throw new BadRequestException( + * 'A user of role public cannot create this record.' + * ); + */ } - data = this.keepColumns(data as T, data.getPublicCreateableColumns(), type); + data = this.keepColumns( + data as T, + data.getPublicCreateableColumns(), + type + ); return data; } public static asPublicUpdateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -691,7 +704,8 @@ export default class BaseModel extends BaseEntity { } public static asPublicReadableItem( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -703,11 +717,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getPublicReadableAsItemColumns(), type); + return this.keepColumns( + data, + data.getPublicReadableAsItemColumns(), + type + ); } public static asPublicReadableList( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -719,11 +738,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getPublicReadableAsListColumns(), type); + return this.keepColumns( + data, + data.getPublicReadableAsListColumns(), + type + ); } public static asPublicDeleteable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -739,7 +763,8 @@ export default class BaseModel extends BaseEntity { } public static asOwnerCreateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -755,7 +780,8 @@ export default class BaseModel extends BaseEntity { } public static asOwnerUpdateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -771,7 +797,8 @@ export default class BaseModel extends BaseEntity { } public static asOwnerReadableItem( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -783,11 +810,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getOwnerReadableAsItemColumns(), type); + return this.keepColumns( + data, + data.getOwnerReadableAsItemColumns(), + type + ); } public static asOwnerReadableList( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -799,11 +831,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getOwnerReadableAsListColumns(), type); + return this.keepColumns( + data, + data.getOwnerReadableAsListColumns(), + type + ); } public static asOwnerDeleteable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -819,7 +856,8 @@ export default class BaseModel extends BaseEntity { } public static asUserCreateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -835,7 +873,8 @@ export default class BaseModel extends BaseEntity { } public static asUserUpdateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -851,7 +890,8 @@ export default class BaseModel extends BaseEntity { } public static asUserReadableItem( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -863,11 +903,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getUserReadableAsItemColumns(), type); + return this.keepColumns( + data, + data.getUserReadableAsItemColumns(), + type + ); } public static asUserReadableList( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -879,11 +924,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getUserReadableAsListColumns(), type); + return this.keepColumns( + data, + data.getUserReadableAsListColumns(), + type + ); } public static asUserDeleteable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -899,7 +949,8 @@ export default class BaseModel extends BaseEntity { } public static asViewerCreateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -915,7 +966,8 @@ export default class BaseModel extends BaseEntity { } public static asViewerUpdateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -931,7 +983,8 @@ export default class BaseModel extends BaseEntity { } public static asViewerReadableItem( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -943,11 +996,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getViewerReadableAsItemColumns(), type); + return this.keepColumns( + data, + data.getViewerReadableAsItemColumns(), + type + ); } public static asViewerReadableList( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -959,11 +1017,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getViewerReadableAsListColumns(), type); + return this.keepColumns( + data, + data.getViewerReadableAsListColumns(), + type + ); } public static asViewerDeleteable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -979,7 +1042,8 @@ export default class BaseModel extends BaseEntity { } public static asMemberCreateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -995,7 +1059,8 @@ export default class BaseModel extends BaseEntity { } public static asMemberUpdateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1011,7 +1076,8 @@ export default class BaseModel extends BaseEntity { } public static asMemberReadableItem( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1023,11 +1089,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getMemberReadableAsItemColumns(), type); + return this.keepColumns( + data, + data.getMemberReadableAsItemColumns(), + type + ); } public static asMemberReadableList( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1039,11 +1110,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getMemberReadableAsListColumns(), type); + return this.keepColumns( + data, + data.getMemberReadableAsListColumns(), + type + ); } public static asMemberDeleteable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1059,7 +1135,8 @@ export default class BaseModel extends BaseEntity { } public static asAdminCreateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1075,7 +1152,8 @@ export default class BaseModel extends BaseEntity { } public static asAdminUpdateable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1091,7 +1169,8 @@ export default class BaseModel extends BaseEntity { } public static asAdminReadableList( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1103,11 +1182,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getAdminReadableAsListColumns(), type); + return this.keepColumns( + data, + data.getAdminReadableAsListColumns(), + type + ); } public static asAdminReadableItem( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); @@ -1119,11 +1203,16 @@ export default class BaseModel extends BaseEntity { ); } - return this.keepColumns(data, data.getAdminReadableAsItemColumns(), type); + return this.keepColumns( + data, + data.getAdminReadableAsItemColumns(), + type + ); } public static asAdminDeleteable( - data: JSONObject | T, type: { new(): T ;} + data: JSONObject | T, + type: { new (): T } ): T { if (!(data instanceof BaseModel)) { data = this._fromJSON(data, type); diff --git a/Common/Types/API/EmptyResponse.ts b/Common/Types/API/EmptyResponse.ts index e41f3c6d40..55d4560bb3 100644 --- a/Common/Types/API/EmptyResponse.ts +++ b/Common/Types/API/EmptyResponse.ts @@ -1,5 +1,3 @@ -import { JSONObject } from "../JSON"; +import { JSONObject } from '../JSON'; -export default interface EmptyResponseData extends JSONObject { - -} \ No newline at end of file +export default interface EmptyResponseData extends JSONObject {} diff --git a/Common/Types/API/HTTPResponse.ts b/Common/Types/API/HTTPResponse.ts index 2f4f06b70e..2ab986cc16 100644 --- a/Common/Types/API/HTTPResponse.ts +++ b/Common/Types/API/HTTPResponse.ts @@ -1,7 +1,9 @@ -import BaseModel from "../../Models/BaseModel"; -import { JSONObjectOrArray } from "../JSON"; +import BaseModel from '../../Models/BaseModel'; +import { JSONObjectOrArray } from '../JSON'; -export default class HTTPResponse> { +export default class HTTPResponse< + T extends JSONObjectOrArray | BaseModel | Array +> { private _statusCode: number = -1; public get statusCode(): number { return this._statusCode; @@ -18,21 +20,19 @@ export default class HTTPResponse; isMasterAdmin: boolean; } - diff --git a/Common/Utils/API.ts b/Common/Utils/API.ts index 09812ef7c5..0e1f448b54 100644 --- a/Common/Utils/API.ts +++ b/Common/Utils/API.ts @@ -12,7 +12,6 @@ import Route from '../Types/API/Route'; import BaseModel from '../Models/BaseModel'; export default class API { - private _protocol: Protocol = Protocol.HTTPS; public get protocol(): Protocol { return this._protocol; @@ -29,71 +28,96 @@ export default class API { this._hostname = v; } - - - private _baseRoute! : Route; - public get baseRoute() : Route { + private _baseRoute!: Route; + public get baseRoute(): Route { return this._baseRoute; } - public set baseRoute(v : Route) { + public set baseRoute(v: Route) { this._baseRoute = v; } - - public constructor(protocol: Protocol, hostname: Hostname, baseRoute?: Route) { + public constructor( + protocol: Protocol, + hostname: Hostname, + baseRoute?: Route + ) { this.protocol = protocol; this.hostname = hostname; if (baseRoute) { this.baseRoute = baseRoute; } else { - this.baseRoute = new Route("/"); + this.baseRoute = new Route('/'); } } - public async get>( + public async get< + T extends JSONObject | JSONArray | BaseModel | Array + >( path: Route, data?: JSONObject | JSONArray, headers?: Headers ): Promise> { return await API.get( - new URL(this.protocol, this.hostname, this.baseRoute.addRoute(path)), + new URL( + this.protocol, + this.hostname, + this.baseRoute.addRoute(path) + ), data, headers ); } - public async delete>( + public async delete< + T extends JSONObject | JSONArray | BaseModel | Array + >( path: Route, data?: JSONObject | JSONArray, headers?: Headers ): Promise> { return await API.delete( - new URL(this.protocol, this.hostname, this.baseRoute.addRoute(path)), + new URL( + this.protocol, + this.hostname, + this.baseRoute.addRoute(path) + ), data, headers ); } - public async put>( + public async put< + T extends JSONObject | JSONArray | BaseModel | Array + >( path: Route, data?: JSONObject | JSONArray, headers?: Headers ): Promise> { return await API.put( - new URL(this.protocol, this.hostname, this.baseRoute.addRoute(path)), + new URL( + this.protocol, + this.hostname, + this.baseRoute.addRoute(path) + ), data, headers ); } - public async post>( + public async post< + T extends JSONObject | JSONArray | BaseModel | Array + >( path: Route, data?: JSONObject | JSONArray, headers?: Headers ): Promise> { return await API.post( - new URL(this.protocol, this.hostname, this.baseRoute.addRoute(path)), + new URL( + this.protocol, + this.hostname, + this.baseRoute.addRoute(path) + ), data, headers ); @@ -128,7 +152,9 @@ export default class API { return defaultHeaders; } - public static async get>( + public static async get< + T extends JSONObject | JSONArray | BaseModel | Array + >( url: URL, data?: JSONObject | JSONArray, headers?: Headers @@ -136,7 +162,9 @@ export default class API { return await this.fetch(HTTPMethod.GET, url, data, headers); } - public static async delete>( + public static async delete< + T extends JSONObject | JSONArray | BaseModel | Array + >( url: URL, data?: JSONObject | JSONArray, headers?: Headers @@ -144,7 +172,9 @@ export default class API { return await this.fetch(HTTPMethod.DELETE, url, data, headers); } - public static async put>( + public static async put< + T extends JSONObject | JSONArray | BaseModel | Array + >( url: URL, data?: JSONObject | JSONArray, headers?: Headers @@ -152,7 +182,9 @@ export default class API { return await this.fetch(HTTPMethod.PUT, url, data, headers); } - public static async post>( + public static async post< + T extends JSONObject | JSONArray | BaseModel | Array + >( url: URL, data?: JSONObject | JSONArray, headers?: Headers @@ -160,7 +192,9 @@ export default class API { return await this.fetch(HTTPMethod.POST, url, data, headers); } - private static async fetch>( + private static async fetch< + T extends JSONObject | JSONArray | BaseModel | Array + >( method: HTTPMethod, url: URL, data?: JSONObject | JSONArray, @@ -169,7 +203,6 @@ export default class API { const apiHeaders: Headers = this.getHeaders(headers); try { - const result: { data: JSONObject | JSONArray; status: number; diff --git a/CommonServer/API/BaseAPI.ts b/CommonServer/API/BaseAPI.ts index 4367ac3ca8..7d46c22283 100644 --- a/CommonServer/API/BaseAPI.ts +++ b/CommonServer/API/BaseAPI.ts @@ -145,7 +145,8 @@ export default class BaseAPI< const body: JSONObject = req.body; const item: TBaseModel = BaseModel.fromJSON( - body['data'] as JSONObject, this.entityType + body['data'] as JSONObject, + this.entityType ) as TBaseModel; await this.service.updateByRole(oneuptimeRequest.role, { @@ -166,7 +167,8 @@ export default class BaseAPI< const body: JSONObject = req.body; const item: TBaseModel = BaseModel.fromJSON( - body['data'] as JSONObject, this.entityType + body['data'] as JSONObject, + this.entityType ) as TBaseModel; const savedItem: BaseModel = await this.service.createByRole( diff --git a/CommonServer/API/Index.ts b/CommonServer/API/Index.ts index 7b0c03d57c..e79d6ca68c 100644 --- a/CommonServer/API/Index.ts +++ b/CommonServer/API/Index.ts @@ -7,6 +7,4 @@ const app: ExpressApplication = Express.getExpressApp(); export default (appName: string) => { app.use([`/${appName}`, '/'], version); app.use([`/${appName}`, '/'], status); -} - - +}; diff --git a/CommonServer/API/StatusAPI.ts b/CommonServer/API/StatusAPI.ts index c6cfaad019..ebf3fc473a 100644 --- a/CommonServer/API/StatusAPI.ts +++ b/CommonServer/API/StatusAPI.ts @@ -8,7 +8,7 @@ import LocalCache from '../Infrastructure/LocalCache'; const router: ExpressRouter = Express.getRouter(); router.get('/', (_req: ExpressRequest, res: ExpressResponse) => { - res.send({ app: LocalCache.getString("app", "name") }); + res.send({ app: LocalCache.getString('app', 'name') }); }); // General status diff --git a/CommonServer/Config.ts b/CommonServer/Config.ts index 401e24ddd5..a5a64c7b0a 100644 --- a/CommonServer/Config.ts +++ b/CommonServer/Config.ts @@ -3,9 +3,9 @@ import ObjectID from 'Common/Types/ObjectID'; import Port from 'Common/Types/Port'; import Hostname from 'Common/Types/API/Hostname'; -export const DisableSignup: boolean = process.env['DISABLE_SIGNUP'] === "true"; +export const DisableSignup: boolean = process.env['DISABLE_SIGNUP'] === 'true'; -export const IsSaaSService: boolean = process.env['IS_SAAS_SERVICE'] === "true"; +export const IsSaaSService: boolean = process.env['IS_SAAS_SERVICE'] === 'true'; export const DatabaseHost: Hostname = new Hostname( process.env['DATABASE_HOST'] || '' diff --git a/CommonServer/Infrastructure/PostgresDatabase.ts b/CommonServer/Infrastructure/PostgresDatabase.ts index 797fdd9d94..511bd60be9 100644 --- a/CommonServer/Infrastructure/PostgresDatabase.ts +++ b/CommonServer/Infrastructure/PostgresDatabase.ts @@ -63,12 +63,13 @@ export default class Database { const PostgresDataSource: DataSource = new DataSource( dataSourceOptions ); - const dataSource: DataSource = await PostgresDataSource.initialize(); - logger.info("Posgres Database Connected"); + const dataSource: DataSource = + await PostgresDataSource.initialize(); + logger.info('Posgres Database Connected'); this.dataSource = dataSource; return dataSource; } catch (err) { - logger.error("Posgres Database Connection Failed"); + logger.error('Posgres Database Connection Failed'); logger.error(err); throw err; } diff --git a/CommonServer/Services/DatabaseService.ts b/CommonServer/Services/DatabaseService.ts index 0411140324..199e753619 100644 --- a/CommonServer/Services/DatabaseService.ts +++ b/CommonServer/Services/DatabaseService.ts @@ -31,13 +31,13 @@ import Role from 'Common/Types/Role'; class DatabaseService { private postgresDatabase!: PostgresDatabase; - private entityType!: { new(): TBaseModel }; - + private entityType!: { new (): TBaseModel }; + public constructor( type: { new (): TBaseModel }, postgresDatabase?: PostgresDatabase ) { - this.entityType = type; + this.entityType = type; if (postgresDatabase) { this.postgresDatabase = postgresDatabase; } @@ -380,31 +380,46 @@ class DatabaseService { ): Promise { if (role === Role.Administrator) { return await this.create({ - data: BaseModel.asAdminCreateable(createBy.data, this.entityType), + data: BaseModel.asAdminCreateable( + createBy.data, + this.entityType + ), }); } if (role === Role.Member) { return await this.create({ - data: BaseModel.asMemberCreateable(createBy.data, this.entityType), + data: BaseModel.asMemberCreateable( + createBy.data, + this.entityType + ), }); } if (role === Role.Public) { return await this.create({ - data: BaseModel.asPublicCreateable(createBy.data, this.entityType), + data: BaseModel.asPublicCreateable( + createBy.data, + this.entityType + ), }); } if (role === Role.Viewer) { return await this.create({ - data: BaseModel.asViewerCreateable(createBy.data, this.entityType), + data: BaseModel.asViewerCreateable( + createBy.data, + this.entityType + ), }); } if (role === Role.Owner) { return await this.create({ - data: BaseModel.asOwnerCreateable(createBy.data, this.entityType), + data: BaseModel.asOwnerCreateable( + createBy.data, + this.entityType + ), }); } diff --git a/CommonServer/Utils/Express.ts b/CommonServer/Utils/Express.ts index 00fc40e26f..a330942e54 100644 --- a/CommonServer/Utils/Express.ts +++ b/CommonServer/Utils/Express.ts @@ -61,13 +61,14 @@ class Express { return this.app; } - public static async launchApplication(appName: string): Promise { - + public static async launchApplication( + appName: string + ): Promise { return new Promise((resolve) => { if (!this.app) { this.setupExpress(); } - + this.app.listen(this.app.get('port'), () => { // eslint-disable-next-line logger.info(`${appName} server started on port: ${this.app.get('port')}`); diff --git a/CommonServer/Utils/StartServer.ts b/CommonServer/Utils/StartServer.ts index a6be3466b3..d325c372a0 100644 --- a/CommonServer/Utils/StartServer.ts +++ b/CommonServer/Utils/StartServer.ts @@ -63,8 +63,6 @@ const setDefaultHeaders: RequestHandler = ( app.use(cors()); app.use(setDefaultHeaders); - - /* * Add limit of 10 MB to avoid "Request Entity too large error" * https://stackoverflow.com/questions/19917401/error-request-entity-too-large @@ -73,31 +71,35 @@ app.use(setDefaultHeaders); app.use(ExpressJson({ limit: '10mb' })); app.use(ExpressUrlEncoded({ limit: '10mb' })); +// Error Handler. +app.use( + ( + err: Error, + _req: ExpressRequest, + res: ExpressResponse, + next: NextFunction + ) => { + logger.error(err); -// Error Handler. -app.use((err: Error, _req: ExpressRequest, res: ExpressResponse, next: NextFunction) => { - - logger.error(err); + if (res.headersSent) { + return next(err); + } - if (res.headersSent) { - return next(err) + if (err instanceof Exception) { + res.status((err as Exception).code); + res.send({ error: (err as Exception).message }); + } else { + res.status(500); + res.send({ error: err }); + } } - - if (err instanceof Exception) { - res.status((err as Exception).code) - res.send({ error: (err as Exception).message }) - } else { - res.status(500) - res.send({ error: err }); - } - -}); +); app.use(logRequest); export default async (appName: string) => { await Express.launchApplication(appName); - LocalCache.setString("app", "name", appName); + LocalCache.setString('app', 'name', appName); CommonAPI(appName); return app; }; diff --git a/CommonUI/src/Config.ts b/CommonUI/src/Config.ts index 2f6e002b19..22a131cfa2 100644 --- a/CommonUI/src/Config.ts +++ b/CommonUI/src/Config.ts @@ -2,13 +2,15 @@ import Hostname from 'Common/Types/API/Hostname'; import Protocol from 'Common/Types/API/Protocol'; import Route from 'Common/Types/API/Route'; import Version from 'Common/Types/Version'; -import URL from 'Common/Types/API/URL' +import URL from 'Common/Types/API/URL'; export const env: Function = (key: string): string => { return process.env[key] || ''; }; -export const HTTP_PROTOCOL: Protocol = window.location.protocol.includes('https') +export const HTTP_PROTOCOL: Protocol = window.location.protocol.includes( + 'https' +) ? Protocol.HTTPS : Protocol.HTTP; @@ -16,40 +18,23 @@ export const IS_SAAS_SERVICE: boolean = env('IS_SAAS_SERVICE') === 'true'; export const DISABLE_SIGNUP: boolean = env('DISABLE_SIGNUP') === 'true'; export const VERSION: Version = new Version(env('VERSION') || '1.0.0'); -export const DASHBOARD_API_ROUTE: Route = new Route( - env('DASHBOARD_API_Route') -); +export const DASHBOARD_API_ROUTE: Route = new Route(env('DASHBOARD_API_Route')); -export const IDENTITY_ROUTE: Route = new Route( - env('IDENTITY_ROUTE') -); +export const IDENTITY_ROUTE: Route = new Route(env('IDENTITY_ROUTE')); -export const DASHBOARD_ROUTE: Route = new Route( - env('DASHBOARD_ROUTE') -); +export const DASHBOARD_ROUTE: Route = new Route(env('DASHBOARD_ROUTE')); -export const INTEGRATION_ROUTE: Route = new Route( - env('INTEGRATION_ROUTE') -); +export const INTEGRATION_ROUTE: Route = new Route(env('INTEGRATION_ROUTE')); -export const HELM_ROUTE: Route = new Route( - env('HELMCHART_ROUTE') -); +export const HELM_ROUTE: Route = new Route(env('HELMCHART_ROUTE')); -export const API_DOCS_ROUTE: Route = new Route( - env('APIDOCS_ROUTE') -); +export const API_DOCS_ROUTE: Route = new Route(env('APIDOCS_ROUTE')); export const ADMIN_DASHBOARD_ROUTE: Route = new Route( env('ADMINDASHBOARD_ROUTE') ); -export const ACCOUNTS_ROUTE: Route = new Route( - env('ACCOUNTS_ROUTE') -); -export const HOME_ROUTE: Route = new Route( - env('HOME_ROUTE') -); - +export const ACCOUNTS_ROUTE: Route = new Route(env('ACCOUNTS_ROUTE')); +export const HOME_ROUTE: Route = new Route(env('HOME_ROUTE')); export const DASHBOARD_API_HOSTNAME: Hostname = new Hostname( window.location.hostname @@ -67,9 +52,7 @@ export const INTEGRATION_HOSTNAME: Hostname = new Hostname( window.location.hostname ); -export const HELM_HOSTNAME: Hostname = new Hostname( - window.location.hostname -); +export const HELM_HOSTNAME: Hostname = new Hostname(window.location.hostname); export const API_DOCS_HOSTNAME: Hostname = new Hostname( window.location.hostname @@ -81,41 +64,48 @@ export const ADMIN_DASHBOARD_HOSTNAME: Hostname = new Hostname( export const ACCOUNTS_HOSTNAME: Hostname = new Hostname( window.location.hostname ); -export const HOME_HOSTNAME: Hostname = new Hostname( - window.location.hostname -); +export const HOME_HOSTNAME: Hostname = new Hostname(window.location.hostname); export const DASHBOARD_API_URL: URL = new URL( - HTTP_PROTOCOL, INTEGRATION_HOSTNAME, INTEGRATION_ROUTE + HTTP_PROTOCOL, + INTEGRATION_HOSTNAME, + INTEGRATION_ROUTE ); export const IDENTITY_URL: URL = new URL( - HTTP_PROTOCOL, IDENTITY_HOSTNAME, IDENTITY_ROUTE + HTTP_PROTOCOL, + IDENTITY_HOSTNAME, + IDENTITY_ROUTE ); export const DASHBOARD_URL: URL = new URL( - HTTP_PROTOCOL, DASHBOARD_HOSTNAME, DASHBOARD_ROUTE + HTTP_PROTOCOL, + DASHBOARD_HOSTNAME, + DASHBOARD_ROUTE ); export const INTEGRATION_URL: URL = new URL( - HTTP_PROTOCOL, INTEGRATION_HOSTNAME, INTEGRATION_ROUTE + HTTP_PROTOCOL, + INTEGRATION_HOSTNAME, + INTEGRATION_ROUTE ); -export const HELM_URL: URL = new URL( - HTTP_PROTOCOL, HELM_HOSTNAME, HELM_ROUTE -); +export const HELM_URL: URL = new URL(HTTP_PROTOCOL, HELM_HOSTNAME, HELM_ROUTE); export const API_DOCS_URL: URL = new URL( - HTTP_PROTOCOL, API_DOCS_HOSTNAME, API_DOCS_ROUTE + HTTP_PROTOCOL, + API_DOCS_HOSTNAME, + API_DOCS_ROUTE ); export const ADMIN_DASHBOARD_URL: URL = new URL( - HTTP_PROTOCOL, ADMIN_DASHBOARD_HOSTNAME, ADMIN_DASHBOARD_ROUTE + HTTP_PROTOCOL, + ADMIN_DASHBOARD_HOSTNAME, + ADMIN_DASHBOARD_ROUTE ); export const ACCOUNTS_URL: URL = new URL( - HTTP_PROTOCOL, ACCOUNTS_HOSTNAME, ACCOUNTS_ROUTE + HTTP_PROTOCOL, + ACCOUNTS_HOSTNAME, + ACCOUNTS_ROUTE ); -export const HOME_URL: URL = new URL( - HTTP_PROTOCOL, HOME_HOSTNAME, HOME_ROUTE -); - +export const HOME_URL: URL = new URL(HTTP_PROTOCOL, HOME_HOSTNAME, HOME_ROUTE); diff --git a/CommonUI/src/Utils/API/DashboardAPI.ts b/CommonUI/src/Utils/API/DashboardAPI.ts index f7f2683579..949597868f 100755 --- a/CommonUI/src/Utils/API/DashboardAPI.ts +++ b/CommonUI/src/Utils/API/DashboardAPI.ts @@ -1,4 +1,8 @@ -import { DASHBOARD_API_HOSTNAME, HTTP_PROTOCOL, DASHBOARD_API_ROUTE } from '../../Config'; +import { + DASHBOARD_API_HOSTNAME, + HTTP_PROTOCOL, + DASHBOARD_API_ROUTE, +} from '../../Config'; import BaseAPI from './BaseAPI'; class BackendAPI extends BaseAPI { diff --git a/ContainerScanner/index.ts b/ContainerScanner/index.ts index 46d1990b18..f77e247a2d 100644 --- a/ContainerScanner/index.ts +++ b/ContainerScanner/index.ts @@ -7,7 +7,6 @@ import cron from 'node-cron'; export const APP_NAME: string = 'container'; const app = App(APP_NAME); - const cronContainerSecurityStartTime: $TSFixMe = Math.floor(Math.random() * 50); //Run this cron every 5 minute. diff --git a/Dashboard/Index.ts b/Dashboard/Index.ts index 760a2b9f09..d878a1498e 100755 --- a/Dashboard/Index.ts +++ b/Dashboard/Index.ts @@ -9,7 +9,6 @@ import { export const APP_NAME: string = 'dashboard'; const app = App(APP_NAME); - app.use(ExpressStatic(path.join(__dirname, 'build'))); app.use( diff --git a/DashboardAPI/index.ts b/DashboardAPI/index.ts index 4f5fb955cb..84062cdc38 100755 --- a/DashboardAPI/index.ts +++ b/DashboardAPI/index.ts @@ -168,7 +168,10 @@ app.use( IncidentPriorityAPI ); -app.use(['/incidentSettings', `${APP_NAME}/incidentSettings`], IncidentSettingsAPI); +app.use( + ['/incidentSettings', `${APP_NAME}/incidentSettings`], + IncidentSettingsAPI +); app.use(['/reports', `${APP_NAME}/reports`], ReportAPI); @@ -196,7 +199,10 @@ app.use(['/stripe', `${APP_NAME}/stripe`], StripeAPI); app.use(['/subscriber', `${APP_NAME}/subscriber`], SubscriberAPI); -app.use(['/subscriberAlert', `${APP_NAME}/subscriberAlert`], SubscriberAlertAPI); +app.use( + ['/subscriberAlert', `${APP_NAME}/subscriberAlert`], + SubscriberAlertAPI +); app.use(['/emailTemplate', `${APP_NAME}/emailTemplate`], EmailTemplateAPI); @@ -206,14 +212,20 @@ app.use(['/smsTemplate', `${APP_NAME}/smsTemplate`], SmsTemplateAPI); app.use(['/smsSmtp', `${APP_NAME}/smsSmtp`], SmsSmtpAPI); -app.use(['/resourceCategory', `${APP_NAME}/resourceCategory`], ResourceCategoryAPI); +app.use( + ['/resourceCategory', `${APP_NAME}/resourceCategory`], + ResourceCategoryAPI +); app.use( ['/statusPageCategory', `${APP_NAME}/statusPageCategory`], StatusPageCategoryAPI ); -app.use(['/monitorCriteria', `${APP_NAME}/monitorCriteria`], MonitorCriteriaAPI); +app.use( + ['/monitorCriteria', `${APP_NAME}/monitorCriteria`], + MonitorCriteriaAPI +); app.use(['/scheduledEvent', `${APP_NAME}/scheduledEvent`], ScheduledEventAPI); @@ -233,7 +245,10 @@ app.use(['/email-logs', `${APP_NAME}/email-logs`], EmailLogsAPI); app.use(['/call-logs', `${APP_NAME}/call-logs`], CallLogsAPI); -app.use(['/automated-scripts', `${APP_NAME}/automated-scripts`], AutomatedScriptAPI); +app.use( + ['/automated-scripts', `${APP_NAME}/automated-scripts`], + AutomatedScriptAPI +); app.use(['/sms-logs', `${APP_NAME}/sms-logs`], SMSLogAPI); @@ -262,11 +277,17 @@ app.use(['/securityLog', `${APP_NAME}/securityLog`], ContainerSecurityLogAPI); app.use(['/error-tracker', `${APP_NAME}/error-tracker`], ErrorTrackerAPI); -app.use(['/incidentSla', `${APP_NAME}/incidentSla`], IncidentCommunicationSlaAPI); +app.use( + ['/incidentSla', `${APP_NAME}/incidentSla`], + IncidentCommunicationSlaAPI +); app.use(['/monitorSla', `${APP_NAME}/monitorSla`], MonitorSlaAPI); -app.use(['/incoming-request', `${APP_NAME}/incoming-request`], IncomingHTTPRequestAPI); +app.use( + ['/incoming-request', `${APP_NAME}/incoming-request`], + IncomingHTTPRequestAPI +); app.use(['/ScriptRunner', `${APP_NAME}/ScriptRunner`], ScriptRunnerAPI); diff --git a/HelmChart/index.ts b/HelmChart/index.ts index fe147c8484..1a29d6439f 100644 --- a/HelmChart/index.ts +++ b/HelmChart/index.ts @@ -5,8 +5,6 @@ import path from 'path'; export const APP_NAME: string = 'chart'; const app = App(APP_NAME); - - // Set the view engine to ejs app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); diff --git a/Home/index.ts b/Home/index.ts index edd16b3664..32e838e544 100755 --- a/Home/index.ts +++ b/Home/index.ts @@ -13,7 +13,6 @@ import productCompare, { Product } from './config/product-compare'; import builder from 'xmlbuilder2'; import { XMLBuilder } from 'xmlbuilder2/lib/interfaces'; - export const APP_NAME: string = 'home'; const app = App(APP_NAME); diff --git a/HttpTestServer/index.ts b/HttpTestServer/index.ts index d5742c198a..9885bbfa4a 100644 --- a/HttpTestServer/index.ts +++ b/HttpTestServer/index.ts @@ -12,11 +12,9 @@ import HTTPTestServerResponse from './types/HttpTestServerResponse'; import ResponseType from 'Common/Types/api/ResponseType'; import Headers from 'Common/Types/API/Headers'; - export const APP_NAME: string = 'home'; const app = App(APP_NAME); - app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); app.use(ExpressStatic('public')); diff --git a/Identity/API/AuthenticationAPI.ts b/Identity/API/AuthenticationAPI.ts index c194cbed1a..f846abfb73 100644 --- a/Identity/API/AuthenticationAPI.ts +++ b/Identity/API/AuthenticationAPI.ts @@ -33,16 +33,21 @@ const router: ExpressRouter = Express.getRouter(); router.post( '/signup', - async (req: ExpressRequest, res: ExpressResponse, next: NextFunction): Promise => { + async ( + req: ExpressRequest, + res: ExpressResponse, + next: NextFunction + ): Promise => { try { if (DisableSignup) { throw new BadRequestException('Sign up is disabled.'); } const data: JSONObject = req.body; - + const user: User = User.asPublicCreateable( - data['user'] as JSONObject, User + data['user'] as JSONObject, + User ); if (IsSaaSService) { @@ -75,7 +80,7 @@ router.post( emailVerificationToken && user && alreadySavedUser?.id?.toString() === - emailVerificationToken?.userId?.toString() + emailVerificationToken?.userId?.toString() ) { user.isEmailVerified = true; } diff --git a/Identity/Index.ts b/Identity/Index.ts index 72e837be77..5e707da1e1 100644 --- a/Identity/Index.ts +++ b/Identity/Index.ts @@ -15,12 +15,13 @@ const init = async () => { // init the app await App(APP_NAME); // connect to the database. - await PostgresAppInstance.connect(PostgresAppInstance.getDatasourceOptions()); + await PostgresAppInstance.connect( + PostgresAppInstance.getDatasourceOptions() + ); } catch (err) { - logger.error("App Init Failed:") + logger.error('App Init Failed:'); logger.error(err); } }; init(); - diff --git a/Integration/Index.ts b/Integration/Index.ts index c99fa97f8a..f2ed3f2841 100644 --- a/Integration/Index.ts +++ b/Integration/Index.ts @@ -1,6 +1,7 @@ import App from 'CommonServer/Utils/StartServer'; export const APP_NAME: string = 'integration'; + const app = App(APP_NAME); export default app; diff --git a/LighthouseRunner/index.ts b/LighthouseRunner/index.ts index e162383d09..7440ed2f01 100644 --- a/LighthouseRunner/index.ts +++ b/LighthouseRunner/index.ts @@ -10,7 +10,6 @@ import cron from 'node-cron'; export const APP_NAME: string = 'licensing'; const app = App(APP_NAME); - const cronMinuteStartTime: $TSFixMe = Math.floor(Math.random() * 50); //App Version