This commit is contained in:
2024-11-26 13:19:48 +07:00
commit df875117b0
31 changed files with 1273 additions and 0 deletions

66
src/helpers/common.ts Normal file
View File

@ -0,0 +1,66 @@
import { Logger, ILogObj } from 'tslog';
const log: Logger<ILogObj> = new Logger({ name: '[CommonHelper]', type: 'pretty' });
export default class CommonHelper {
static randomInteger(min: number, max: number): number | null {
if (min > 0 && max > 0) {
return Math.floor(Math.random() * (max - min + 1)) + min;
} else {
log.error(new Error('Min & Max must be > 0'));
return null
}
}
static inArray(array: string[], keyword: string): boolean {
for (let val of array) {
if (val === keyword) {
return true;
}
}
return false;
}
static async sleep(seconds: number): Promise<void> {
if (seconds > 0) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
} else {
log.error(new Error('Second must be > 0'));
}
}
static convertArrayToObject(object: any, key: string, val = ''): any {
const ret: any = {};
for (let i in object) {
const d = object[i];
if (val) {
ret[d[key]] = d[val];
} else {
ret[d[key]] = d;
}
}
return ret;
}
static countObject(obj: any): number {
let c = 0;
for (let s in obj) {
c++;
}
return c;
}
static capitalizeFirstLetter(str: string): string {
if (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
return '';
}
}

View File

@ -0,0 +1,14 @@
import cors from 'cors';
import express from 'express';
import compression from 'compression';
export class CompressionHelper {
static setup(app: express.Application) {
const compressionOptions: compression.CompressionOptions = {
level: 6,
memLevel: 8
};
app.use(compression(compressionOptions));
}
}

View File

@ -0,0 +1,14 @@
import config from 'config';
import cors from 'cors';
import express from 'express';
export class CorsHelper {
static setup(app: express.Application) {
const corsOptions: cors.CorsOptions = {
origin: config.get('server.cors'),
optionsSuccessStatus: 200,
};
app.use(cors(corsOptions));
}
}

View File

@ -0,0 +1,12 @@
import config from 'config';
import express from 'express';
export class JsonHelper {
static setup(app: express.Application) {
app.use(
express.json({
limit: config.get('server.max_body'),
})
);
}
}

View File

@ -0,0 +1,9 @@
import config from 'config';
import morgan from 'morgan';
import express from 'express';
export class MorganHelper {
static setup(app: express.Application) {
app.use(morgan(config.get('server.morgan')));
}
}

View File

@ -0,0 +1,58 @@
import { Response } from 'express';
import { ErrorType, ErrorValidation } from '../../types/error';
export class ReturnHelper {
static successResponseAny(
res: Response,
status_code: number,
message: string,
data: any = null
): Response {
return res.status(status_code || 200).json({
status: true,
code: status_code || 200,
message: message || "success",
data: data || {},
});
};
static successResponseList(
res: Response,
status_code: number,
message: string,
count_data: number = 0,
current_page: number = 0,
total_count_data: number = 0,
list_data: any = null
): Response {
return res.status(status_code || 200).json({
status: true,
code: status_code || 200,
message: message || "success",
data: {
count: count_data,
page: current_page,
total_count: total_count_data,
list: list_data
},
});
};
static errorResponse(
res: Response,
status_code: number,
error_code: number,
message: string,
error: any = null
): Response {
return res.status(status_code || 200).json({
status: false,
error_code: error_code || 400,
message: message || "success",
error: error,
});
};
}

View File

@ -0,0 +1,14 @@
import config from 'config';
import express from 'express';
import swaggerUi from 'swagger-ui-express';
import swaggerDocument from '../../swagger/swagger.json';
export class SwaggerHelper {
static setup(app: express.Application) {
var options = {
explorer: false
};
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, options));
}
}

View File

@ -0,0 +1,13 @@
import config from 'config';
import express from 'express';
export class UrlencodedHelper {
static setup(app: express.Application) {
app.use(
express.urlencoded({
extended: true,
limit: config.get('server.max_body'),
})
);
}
}

37
src/helpers/orm.ts Normal file
View File

@ -0,0 +1,37 @@
import config from 'config';
import { DataSource } from "typeorm";
import { User } from "../entity/users";
import { TrialBalance } from '../entity/trial_balance';
import { ILogObj, Logger } from 'tslog';
import { LNKOLEK } from '../entity/lnkolek';
import { CifAccount } from '../entity/cif_account';
export class OrmHelper {
static DB: DataSource = null
static setup() {
const log: Logger<ILogObj> = new Logger({ name: '[OrmHelper]', type: 'pretty' });
const engine: 'mysql' | 'postgres' = config.get("database.engine")
OrmHelper.DB = new DataSource({
type: engine,
host: config.get("database.host"),
port: Number(config.get("database.port")),
username: String(config.get("database.username")),
password: String(config.get("database.password")),
database: String(config.get("database.database")),
synchronize: true,
logging: true,
entities: [User, TrialBalance, LNKOLEK, CifAccount],
subscribers: [],
migrations: [],
})
OrmHelper.DB.initialize()
.then(() => {
// here you can start to work with your database
})
.catch((error: any) => log.error(error))
}
}