initial
This commit is contained in:
66
src/helpers/common.ts
Normal file
66
src/helpers/common.ts
Normal 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 '';
|
||||
}
|
||||
}
|
||||
14
src/helpers/express/compression.ts
Normal file
14
src/helpers/express/compression.ts
Normal 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));
|
||||
}
|
||||
}
|
||||
14
src/helpers/express/cors.ts
Normal file
14
src/helpers/express/cors.ts
Normal 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));
|
||||
}
|
||||
}
|
||||
12
src/helpers/express/json.ts
Normal file
12
src/helpers/express/json.ts
Normal 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'),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
9
src/helpers/express/morgan.ts
Normal file
9
src/helpers/express/morgan.ts
Normal 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')));
|
||||
}
|
||||
}
|
||||
58
src/helpers/express/return.ts
Normal file
58
src/helpers/express/return.ts
Normal 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,
|
||||
});
|
||||
};
|
||||
}
|
||||
14
src/helpers/express/swagger.ts
Normal file
14
src/helpers/express/swagger.ts
Normal 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));
|
||||
}
|
||||
}
|
||||
13
src/helpers/express/urlencoded.ts
Normal file
13
src/helpers/express/urlencoded.ts
Normal 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'),
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
38
src/helpers/jwt.ts
Normal file
38
src/helpers/jwt.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { NextFunction, Response } from 'express';
|
||||
import { Logger, ILogObj } from 'tslog';
|
||||
import { Request, expressjwt } from "express-jwt";
|
||||
import fs from 'fs';
|
||||
import { Language } from '../langs/lang';
|
||||
import { ReturnHelper } from './express/return';
|
||||
import express from 'express';
|
||||
|
||||
const log: Logger<ILogObj> = new Logger({ name: '[JwtHelper]', type: 'pretty' });
|
||||
|
||||
export default class JwtHelper {
|
||||
static secure = (app: express.Application) => {
|
||||
|
||||
var publicKey = fs.readFileSync("src/helpers/key/public.key");
|
||||
|
||||
app.use(
|
||||
expressjwt({
|
||||
secret: publicKey, algorithms: ["RS256"]
|
||||
}).unless({ path: ["/token"] }),
|
||||
function (req: Request, res: Response, next: NextFunction) {
|
||||
if (!req.auth?.data.id) {
|
||||
return ReturnHelper.errorResponse(res, 403, 666, Language.lang.failed_access);
|
||||
}
|
||||
|
||||
return next();
|
||||
}
|
||||
)
|
||||
|
||||
app.use(function (err: any, req: Request, res: Response, next: NextFunction) {
|
||||
if (err.name === 'UnauthorizedError') {
|
||||
return ReturnHelper.errorResponse(res, 403, 666, Language.lang.failed_access);
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
28
src/helpers/key/private.key
Normal file
28
src/helpers/key/private.key
Normal file
@ -0,0 +1,28 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC0sdcneY7W32xE
|
||||
8hbjFPR4/AZFdtQqwQ3lEsuUFcsYh2KR0wP0umqtIMGLle3603lhQ/AMXDlKBBJt
|
||||
IAP1SJNvGYY6fUdm+AZ0NFmst3CCiFsw7N5F+KXKEW1g/vUO9OKJ2Hg9lXuOkYAh
|
||||
DYv+d0R5nsHu5l7WAesHtrcuNwezmENeAwjgWNezlwJmf54soNrV+EE7zPxTyye1
|
||||
se3b8LjMgB/zDTF2UkdrvrVulpshG0JMvDy1n7QG/+K3u3BcGakRws5GZu7LZug4
|
||||
ZNdURajL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9mk7m1gEtiLKr9iv4IpnwzOchYQRazsYhRtGhPit
|
||||
EH6ZRTArldbV3jDvFw6fwEQhp0YM83k4S6PxguEYWxFeo0ZYXebR67+KHjE5zzfm
|
||||
9sAqvCvFs/yiLyi9try8ubBUwjTgN3ZFxT+OrVDhAoGAUhFkd/Fy0FCsMROPKK5l
|
||||
uadRukj5zKLMw+JJegXO+0AntV2vWPam73x2ulvVig9TqrJLqtOfqYNNX789Fzri
|
||||
PBcqDaMJKfMc6ac7YgZTNP6vk7avjqWUpoJqlj1g8y3suAYkm/xfLd1HvAqQh0p8
|
||||
i3/cmSzkc1MCE2YxYop0gpc=
|
||||
-----END PRIVATE KEY-----
|
||||
9
src/helpers/key/public.key
Normal file
9
src/helpers/key/public.key
Normal file
@ -0,0 +1,9 @@
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtLHXJ3mO1t9sRPIW4xT0
|
||||
ePwGRXbUKsEN5RLLlBXLGIdikdMD9LpqrSDBi5Xt+tN5YUPwDFw5SgQSbSAD9UiT
|
||||
bxmGOn1HZvgGdDRZrLdwgohbMOzeRfilyhFtYP71DvTiidh4PZV7jpGAIQ2L/ndE
|
||||
eZ7B7uZe1gHrB7a3LjcHs5hDXgMI4FjXs5cCZn+eLKDa1fhBO8z8U8sntbHt2/C4
|
||||
zIAf8w0xdlJHa761bpabIRtCTLw8tZ+0Bv/it7twXBmpEcLORmbuy2boOGTXVEWo
|
||||
y9KKjfW8T0EVkefu41aptYGDURkHzybgV1IqnfwF2vmqdDNETwRVn6Jo38Z9tUKX
|
||||
VQIDAQAB
|
||||
-----END PUBLIC KEY-----
|
||||
37
src/helpers/orm.ts
Normal file
37
src/helpers/orm.ts
Normal 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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user