mielk-api 1.6.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/http/apiResponse/apiResponse.js +1 -1
- package/dist/http/express.d.ts +5 -0
- package/dist/{routing → http}/express.js +1 -14
- package/dist/http/httpResponseStatus/HttpResponseStatus.js +1 -1
- package/dist/index.d.ts +11 -6
- package/dist/index.js +6 -6
- package/dist/middlewares/cors/cors.js +1 -1
- package/dist/middlewares/rateLimit/createRateLimiter.js +2 -2
- package/dist/middlewares/rateLimit/rateLimitConfigFactory.js +1 -1
- package/dist/middlewares/redis/redisLockChecker.js +2 -2
- package/dist/middlewares/redis/redisLockCheckerConfigFactory.js +1 -1
- package/dist/middlewares/requestAuth/auth.middleware.js +4 -4
- package/dist/middlewares/zod/validate.js +1 -1
- package/dist/msg/messageTags.d.ts +16 -0
- package/dist/{internal/messaging → msg}/messageTags.js +0 -15
- package/dist/routing/controllers/db.controller.d.ts +7 -2
- package/dist/routing/controllers/db.controller.js +24 -20
- package/dist/routing/controllers/health.controller.js +3 -2
- package/dist/routing/index.d.ts +6 -0
- package/dist/routing/index.js +4 -0
- package/dist/routing/routes/db.route.d.ts +2 -3
- package/dist/routing/routes/db.route.js +8 -6
- package/dist/routing/routes/health.route.d.ts +1 -3
- package/dist/routing/routes/health.route.js +6 -5
- package/dist/routing/services/db.service.d.ts +7 -2
- package/dist/routing/services/db.service.js +6 -23
- package/dist/routing/types/DbRepository.d.ts +4 -0
- package/package.json +6 -11
- package/dist/Config.d.ts +0 -1
- package/dist/Config.js +0 -1
- package/dist/db/mssql/connection/pool.d.ts +0 -6
- package/dist/db/mssql/connection/pool.js +0 -55
- package/dist/db/mssql/index.d.ts +0 -4
- package/dist/db/mssql/index.js +0 -2
- package/dist/db/mssql/types/DbConfig.d.ts +0 -8
- package/dist/db/pg/connection/pool.d.ts +0 -6
- package/dist/db/pg/connection/pool.js +0 -41
- package/dist/db/pg/connection/transaction.d.ts +0 -6
- package/dist/db/pg/connection/transaction.js +0 -56
- package/dist/db/pg/connection/tunnel.d.ts +0 -2
- package/dist/db/pg/connection/tunnel.js +0 -30
- package/dist/db/pg/errors/PostgreErrorCodes.d.ts +0 -4
- package/dist/db/pg/errors/PostgreErrorCodes.js +0 -4
- package/dist/db/pg/errors/customErrors.d.ts +0 -2
- package/dist/db/pg/errors/customErrors.js +0 -2
- package/dist/db/pg/errors/errorChecks.d.ts +0 -1
- package/dist/db/pg/errors/errorChecks.js +0 -2
- package/dist/db/pg/index.d.ts +0 -11
- package/dist/db/pg/index.js +0 -8
- package/dist/db/pg/types/DbConfig.d.ts +0 -14
- package/dist/db/pg/types/DbConfig.js +0 -1
- package/dist/db/pg/types/Executor.d.ts +0 -4
- package/dist/db/pg/types/Executor.js +0 -2
- package/dist/http/index.d.ts +0 -9
- package/dist/http/index.js +0 -4
- package/dist/internal/messaging/messageTags.d.ts +0 -29
- package/dist/routing/DbProvider.d.ts +0 -1
- package/dist/routing/DbProvider.js +0 -1
- package/dist/routing/express.d.ts +0 -10
- package/dist/routing/repositories/mssql.repository.d.ts +0 -2
- package/dist/routing/repositories/mssql.repository.js +0 -29
- package/dist/routing/repositories/postgre.repository.d.ts +0 -2
- package/dist/routing/repositories/postgre.repository.js +0 -24
- /package/dist/{db/mssql/types/DbConfig.js → routing/types/DbRepository.js} +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isPrimitive } from 'mielk-fn/variables';
|
|
2
2
|
import { HttpResponseStatus } from '../httpResponseStatus/HttpResponseStatus.js';
|
|
3
|
-
import { isProd } from '
|
|
3
|
+
import { isProd } from '../express.js';
|
|
4
4
|
export function success(res, status, data) {
|
|
5
5
|
const apiResponse = {
|
|
6
6
|
success: true,
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CorsConfig } from '../middlewares/index.js';
|
|
2
|
+
import { RateLimitConfig } from '../middlewares/rateLimit/types.js';
|
|
3
|
+
import { RedisConfig } from 'mielk-fn/redis';
|
|
4
|
+
export declare const createExpressApp: (isProd: boolean, corsConfig: CorsConfig, redisConfig: RedisConfig, rateLimitConfig?: Partial<RateLimitConfig>) => Promise<import("express-serve-static-core").Express>;
|
|
5
|
+
export declare const isProd: () => boolean;
|
|
@@ -8,27 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import express from 'express';
|
|
11
|
-
import { initDb as postgreInitDb } from '../db/pg/index.js';
|
|
12
|
-
import { initDb as msSqlInitDb } from '../db/mssql/index.js';
|
|
13
11
|
import { initCors } from '../middlewares/index.js';
|
|
14
12
|
import { createGlobalRateLimiter } from '../middlewares/rateLimit/createRateLimiter.js';
|
|
15
13
|
import { getRedisClient } from 'mielk-fn/redis';
|
|
16
14
|
const env = {
|
|
17
15
|
isProd: true,
|
|
18
|
-
provider: undefined
|
|
19
16
|
};
|
|
20
|
-
export const createExpressApp = (isProd,
|
|
17
|
+
export const createExpressApp = (isProd, corsConfig, redisConfig, rateLimitConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
18
|
const app = express();
|
|
22
19
|
env.isProd = isProd;
|
|
23
|
-
env.provider = dbConfig.provider;
|
|
24
|
-
switch (dbConfig.provider) {
|
|
25
|
-
case 'mssql':
|
|
26
|
-
msSqlInitDb(dbConfig);
|
|
27
|
-
break;
|
|
28
|
-
case 'postgre':
|
|
29
|
-
postgreInitDb(dbConfig);
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
20
|
initCors(corsConfig);
|
|
33
21
|
getRedisClient(redisConfig);
|
|
34
22
|
app.use(createGlobalRateLimiter(rateLimitConfig));
|
|
@@ -38,4 +26,3 @@ export const createExpressApp = (isProd, dbConfig, corsConfig, redisConfig, rate
|
|
|
38
26
|
return app;
|
|
39
27
|
});
|
|
40
28
|
export const isProd = () => env.isProd;
|
|
41
|
-
export const getDbProvider = () => env.provider;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Msg } from "../../
|
|
1
|
+
import { Msg } from "../../msg/messageTags.js";
|
|
2
2
|
const createApiStatus = (success, code, defaultMessageTag) => ({ success, code, defaultMessageTag });
|
|
3
3
|
const msg = Msg.apiStatus;
|
|
4
4
|
export const HttpResponseStatus = {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { ApiSuccess, ApiError, ApiErrorDetails } from './http/types/ApiResponse.js';
|
|
2
|
+
import type { ApiResponse } from './http/types/ApiResponse.js';
|
|
3
|
+
import { HttpStatus } from './http/types/HttpStatus.js';
|
|
4
|
+
import { HttpResponseStatus } from './http/httpResponseStatus/HttpResponseStatus.js';
|
|
5
|
+
import { success, failure, serverError } from './http/apiResponse/apiResponse.js';
|
|
6
|
+
import { createExpressApp } from './http/express.js';
|
|
7
|
+
export type { ApiResponse };
|
|
8
|
+
export { HttpStatus, ApiError, ApiSuccess, ApiErrorDetails };
|
|
9
|
+
export { HttpResponseStatus };
|
|
10
|
+
export { success, failure, serverError };
|
|
11
|
+
export { createExpressApp };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export {
|
|
5
|
-
export {
|
|
6
|
-
export { createExpressApp }
|
|
1
|
+
import { HttpResponseStatus } from './http/httpResponseStatus/HttpResponseStatus.js';
|
|
2
|
+
import { success, failure, serverError } from './http/apiResponse/apiResponse.js';
|
|
3
|
+
import { createExpressApp } from './http/express.js';
|
|
4
|
+
export { HttpResponseStatus };
|
|
5
|
+
export { success, failure, serverError };
|
|
6
|
+
export { createExpressApp };
|
|
@@ -8,10 +8,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { checkRateLimit } from './checkRateLimit.js';
|
|
11
|
-
import { failure, HttpResponseStatus } from '../../
|
|
11
|
+
import { failure, HttpResponseStatus } from '../../index.js';
|
|
12
12
|
import { createRateLimitConfig } from './rateLimitConfigFactory.js';
|
|
13
13
|
import { generateRedisKeyFromRequest } from '../redis/redisKeysFactory.js';
|
|
14
|
-
import { Msg } from '../../
|
|
14
|
+
import { Msg } from '../../msg/messageTags.js';
|
|
15
15
|
export const createGlobalRateLimiter = (config) => {
|
|
16
16
|
return createRateLimiter({ type: 'globalIp', customConfig: config });
|
|
17
17
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getRedisPrefix, getRedisSuffixCallback } from "../redis/redisKeysFactory.js";
|
|
2
|
-
import { Msg } from "../../
|
|
2
|
+
import { Msg } from "../../msg/messageTags.js";
|
|
3
3
|
const defaultConfig = {
|
|
4
4
|
keyPrefix: getRedisPrefix("authLogin"),
|
|
5
5
|
keySuffixes: getRedisSuffixCallback("ip"),
|
|
@@ -8,10 +8,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { getRedisClient } from "mielk-fn/redis";
|
|
11
|
-
import { failure, HttpResponseStatus } from "../../
|
|
11
|
+
import { failure, HttpResponseStatus } from "../../index.js";
|
|
12
12
|
import { createRedisLockCheckerConfig } from "./redisLockCheckerConfigFactory.js";
|
|
13
13
|
import { generateRedisKeyFromRequest } from "./redisKeysFactory.js";
|
|
14
|
-
import { Msg } from "../../
|
|
14
|
+
import { Msg } from "../../msg/messageTags.js";
|
|
15
15
|
export const createRedisLockChecker = (params) => {
|
|
16
16
|
const config = createRedisLockCheckerConfig(params);
|
|
17
17
|
return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { HttpResponseStatus } from '../../http/httpResponseStatus/HttpResponseStatus.js';
|
|
2
|
-
import { Msg } from '../../
|
|
3
|
-
import { isProd } from '../../
|
|
4
|
-
import { failure } from '../../
|
|
2
|
+
import { Msg } from '../../msg/messageTags.js';
|
|
3
|
+
import { isProd } from '../../http/express.js';
|
|
4
|
+
import { failure } from '../../index.js';
|
|
5
5
|
import { apiKey } from '../cors/cors.js';
|
|
6
6
|
export function apiKeyAuthorization(req, res, next) {
|
|
7
7
|
var _a;
|
|
@@ -10,7 +10,7 @@ export function apiKeyAuthorization(req, res, next) {
|
|
|
10
10
|
}
|
|
11
11
|
const key = (_a = req.headers['authorization']) === null || _a === void 0 ? void 0 : _a.split(' ')[1]; // Bearer <key>
|
|
12
12
|
if (!apiKey && (!key || key !== apiKey)) {
|
|
13
|
-
return failure(res, HttpResponseStatus.UNAUTHORIZED, Msg.
|
|
13
|
+
return failure(res, HttpResponseStatus.UNAUTHORIZED, Msg.apiStatus.unauthorized);
|
|
14
14
|
}
|
|
15
15
|
next();
|
|
16
16
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const Msg: {
|
|
2
|
+
apiStatus: {
|
|
3
|
+
ok: string;
|
|
4
|
+
created: string;
|
|
5
|
+
accepted: string;
|
|
6
|
+
noContent: string;
|
|
7
|
+
badRequest: string;
|
|
8
|
+
unauthorized: string;
|
|
9
|
+
forbidden: string;
|
|
10
|
+
notFound: string;
|
|
11
|
+
conflict: string;
|
|
12
|
+
locked: string;
|
|
13
|
+
tooManyRequests: string;
|
|
14
|
+
serverError: string;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
const PARENT_FOLDER = 'api/';
|
|
2
2
|
const ___HTTP_STATUS___ = `${PARENT_FOLDER}/httpStatus`;
|
|
3
|
-
const ___CONNECTION___ = `${PARENT_FOLDER}/connection`;
|
|
4
|
-
const ___TRANSACTIONS___ = `${PARENT_FOLDER}/transactions`;
|
|
5
3
|
export const Msg = {
|
|
6
4
|
apiStatus: {
|
|
7
5
|
ok: `${___HTTP_STATUS___}:ok`,
|
|
@@ -16,18 +14,5 @@ export const Msg = {
|
|
|
16
14
|
locked: `${___HTTP_STATUS___}:locked`, // Account is temporarily locked. Try again later or reset your password. OR Your account has been temporarily locked due to multiple failed login attempts. Please reset your password to regain access.
|
|
17
15
|
tooManyRequests: `${___HTTP_STATUS___}:tooManyRequests`,
|
|
18
16
|
serverError: `${___HTTP_STATUS___}:serverError`,
|
|
19
|
-
},
|
|
20
|
-
connection: {
|
|
21
|
-
corsBlocked: `${___CONNECTION___}:corsBlocked`,
|
|
22
|
-
notInitialized: `${___CONNECTION___}:notInitialized`,
|
|
23
|
-
sshOptionsNotSpecified: `${___CONNECTION___}:sshOptionsNotSpecified`,
|
|
24
|
-
sshTunnelFailed: `${___CONNECTION___}:sshTunnelFailed`, // ❌ SSH tunnel failed
|
|
25
|
-
postgreConnectionError: `${___CONNECTION___}:postgreConnectionError`, // Unexpected PostgreSQL error
|
|
26
|
-
unauthorizedRequest: `${___CONNECTION___}:unauthorizedRequest`
|
|
27
|
-
},
|
|
28
|
-
transactions: {
|
|
29
|
-
alreadyActive: `${___TRANSACTIONS___}:alreadyActive`,
|
|
30
|
-
noActiveTransactionToCommit: `${___TRANSACTIONS___}:noActiveTransactionToCommit`,
|
|
31
|
-
noActiveTransactionToRollback: `${___TRANSACTIONS___}:noActiveTransactionToRollback`
|
|
32
17
|
}
|
|
33
18
|
};
|
|
@@ -1,3 +1,8 @@
|
|
|
1
1
|
import { Request, Response } from 'express';
|
|
2
|
-
|
|
3
|
-
export declare
|
|
2
|
+
import { DbRepository } from '../types/DbRepository.js';
|
|
3
|
+
export declare class DbController {
|
|
4
|
+
private service;
|
|
5
|
+
constructor(repository: DbRepository);
|
|
6
|
+
getDbTime: (_req: Request, res: Response) => Promise<Response<Record<string, any>, Record<string, any>>>;
|
|
7
|
+
getDbVersion: (_req: Request, res: Response) => Promise<Response<Record<string, any>, Record<string, any>>>;
|
|
8
|
+
}
|
|
@@ -7,25 +7,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import
|
|
11
|
-
import { HttpResponseStatus
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
import { DbService } from '../services/db.service.js';
|
|
11
|
+
import { HttpResponseStatus } from '../../http/httpResponseStatus/HttpResponseStatus.js';
|
|
12
|
+
import { success, serverError } from '../../http/apiResponse/apiResponse.js';
|
|
13
|
+
export class DbController {
|
|
14
|
+
constructor(repository) {
|
|
15
|
+
this.getDbTime = (_req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
16
|
+
try {
|
|
17
|
+
const dbTime = yield this.service.getDatabaseTime();
|
|
18
|
+
return success(res, HttpResponseStatus.OK, dbTime);
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
return serverError(res, err);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
this.getDbVersion = (_req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
try {
|
|
26
|
+
const version = yield this.service.getDatabaseVersion();
|
|
27
|
+
return success(res, HttpResponseStatus.OK, version);
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
return serverError(res, err);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
this.service = new DbService(repository);
|
|
16
34
|
}
|
|
17
|
-
catch (err) {
|
|
18
|
-
return serverError(res, err);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
export function getDbVersion(_req, res) {
|
|
22
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
try {
|
|
24
|
-
const version = yield DbService.getDatabaseVersion();
|
|
25
|
-
return success(res, HttpResponseStatus.OK, version);
|
|
26
|
-
}
|
|
27
|
-
catch (err) {
|
|
28
|
-
return serverError(res, err);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
35
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { isProd } from '../../http/express.js';
|
|
2
|
+
import { HttpResponseStatus } from '../../http/httpResponseStatus/HttpResponseStatus.js';
|
|
3
|
+
import { success } from '../../http/apiResponse/apiResponse.js';
|
|
3
4
|
export const getHealth = (_req, res) => {
|
|
4
5
|
return success(res, HttpResponseStatus.OK, new Date().toISOString());
|
|
5
6
|
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DbRepository } from './types/DbRepository.js';
|
|
2
|
+
import { createDbRouter } from './routes/db.route.js';
|
|
3
|
+
import { createHealthRouter } from './routes/health.route.js';
|
|
4
|
+
export { DbRepository as DbRouterRepository };
|
|
5
|
+
export { createDbRouter };
|
|
6
|
+
export { createHealthRouter };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
|
-
import {
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
import { DbController } from '../controllers/db.controller.js';
|
|
3
|
+
export const createDbRouter = (repository) => {
|
|
4
|
+
const router = Router();
|
|
5
|
+
const controller = new DbController(repository);
|
|
6
|
+
router.get('/test/', controller.getDbTime);
|
|
7
|
+
router.get('/version/', controller.getDbVersion);
|
|
8
|
+
return router;
|
|
9
|
+
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
2
|
import { getHealth, getEnv } from '../controllers/health.controller.js';
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
export const createHealthRouter = () => {
|
|
4
|
+
const router = Router();
|
|
5
|
+
router.get('/', getHealth);
|
|
6
|
+
router.get('/environment', getEnv);
|
|
7
|
+
return router;
|
|
8
|
+
};
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export declare
|
|
1
|
+
import { DbRepository } from "../types/DbRepository.js";
|
|
2
|
+
export declare class DbService {
|
|
3
|
+
private repository;
|
|
4
|
+
constructor(repository: DbRepository);
|
|
5
|
+
getDatabaseTime: () => Promise<Date>;
|
|
6
|
+
getDatabaseVersion: () => Promise<string>;
|
|
7
|
+
}
|
|
@@ -7,27 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const provider = getDbProvider();
|
|
17
|
-
const repository = provider === 'postgre' ? postgreRepository : provider === 'mssql' ? msSqlRepository : undefined;
|
|
18
|
-
if (repository) {
|
|
19
|
-
return repository.selectDatabaseTime();
|
|
20
|
-
}
|
|
21
|
-
throw new Error(Msg.connection.notInitialized);
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
export function getDatabaseVersion() {
|
|
25
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
const provider = getDbProvider();
|
|
27
|
-
const repository = provider === 'postgre' ? postgreRepository : provider === 'mssql' ? msSqlRepository : undefined;
|
|
28
|
-
if (repository) {
|
|
29
|
-
return repository.selectDatabaseVersion();
|
|
30
|
-
}
|
|
31
|
-
throw new Error(Msg.connection.notInitialized);
|
|
32
|
-
});
|
|
10
|
+
export class DbService {
|
|
11
|
+
constructor(repository) {
|
|
12
|
+
this.getDatabaseTime = () => __awaiter(this, void 0, void 0, function* () { return yield this.repository.getDatabaseTime(); });
|
|
13
|
+
this.getDatabaseVersion = () => __awaiter(this, void 0, void 0, function* () { return yield this.repository.getDatabaseVersion(); });
|
|
14
|
+
this.repository = repository;
|
|
15
|
+
}
|
|
33
16
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mielk-api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": "mielk",
|
|
6
6
|
"description": "Wrapper for API operations",
|
|
@@ -13,16 +13,6 @@
|
|
|
13
13
|
"import": "./dist/index.js",
|
|
14
14
|
"default": "./dist/index.js"
|
|
15
15
|
},
|
|
16
|
-
"./db-mssql": {
|
|
17
|
-
"types": "./dist/db/mssql/index.d.ts",
|
|
18
|
-
"import": "./dist/db/mssql/index.js",
|
|
19
|
-
"default": "./dist/db/mssql/index.js"
|
|
20
|
-
},
|
|
21
|
-
"./db-pg": {
|
|
22
|
-
"types": "./dist/db/pg/index.d.ts",
|
|
23
|
-
"import": "./dist/db/pg/index.js",
|
|
24
|
-
"default": "./dist/db/pg/index.js"
|
|
25
|
-
},
|
|
26
16
|
"./http": {
|
|
27
17
|
"types": "./dist/http/index.d.ts",
|
|
28
18
|
"import": "./dist/http/index.js",
|
|
@@ -42,6 +32,11 @@
|
|
|
42
32
|
"types": "./dist/middlewares/redis/index.d.ts",
|
|
43
33
|
"import": "./dist/middlewares/redis/index.js",
|
|
44
34
|
"default": "./dist/middlewares/redis/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./routers": {
|
|
37
|
+
"types": "./dist/routers/index.d.ts",
|
|
38
|
+
"import": "./dist/routers/index.js",
|
|
39
|
+
"default": "./dist/routers/index.js"
|
|
45
40
|
}
|
|
46
41
|
},
|
|
47
42
|
"scripts": {
|
package/dist/Config.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const ApiUrl = "http://localhost:4000";
|
package/dist/Config.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const ApiUrl = 'http://localhost:4000';
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { RawData, SqlClient, sqlQueryParamType } from 'msnodesqlv8/types';
|
|
2
|
-
import { MsSqlDbConfig } from '../types/DbConfig.js';
|
|
3
|
-
export declare const getMsSql: () => Promise<SqlClient>;
|
|
4
|
-
export declare const initDb: (dbConfig: MsSqlDbConfig) => void;
|
|
5
|
-
export declare const query: (queryText: string) => Promise<any[]>;
|
|
6
|
-
export declare const spExecute: (sqlText: string, params: sqlQueryParamType[]) => Promise<RawData | undefined>;
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
let sql = null;
|
|
11
|
-
let connectionString = '';
|
|
12
|
-
export const getMsSql = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
13
|
-
if (!sql) {
|
|
14
|
-
try {
|
|
15
|
-
sql = yield import('msnodesqlv8');
|
|
16
|
-
}
|
|
17
|
-
catch (e) {
|
|
18
|
-
throw new Error('MSSQL driver not installed');
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return sql;
|
|
22
|
-
});
|
|
23
|
-
export const initDb = (dbConfig) => {
|
|
24
|
-
const { host, database, user, password, driver } = dbConfig;
|
|
25
|
-
connectionString = `Server=${host};Database=${database};UID=${user};PWD=${password};Driver=${driver}`;
|
|
26
|
-
console.log(connectionString);
|
|
27
|
-
};
|
|
28
|
-
export const query = (queryText) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
-
const sql = yield getMsSql();
|
|
30
|
-
return new Promise((resolve, reject) => {
|
|
31
|
-
sql.query(connectionString, queryText, (err, rows) => {
|
|
32
|
-
if (err)
|
|
33
|
-
reject(err);
|
|
34
|
-
if (rows === undefined)
|
|
35
|
-
reject();
|
|
36
|
-
else
|
|
37
|
-
resolve(rows);
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
export const spExecute = (sqlText, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
42
|
-
const sql = yield getMsSql();
|
|
43
|
-
return new Promise((resolve, reject) => {
|
|
44
|
-
sql.open(connectionString, (err, conn) => {
|
|
45
|
-
if (err)
|
|
46
|
-
return reject(err);
|
|
47
|
-
conn.queryRaw(sqlText, params, (err, results) => {
|
|
48
|
-
if (err)
|
|
49
|
-
reject(err);
|
|
50
|
-
else
|
|
51
|
-
resolve(results);
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
});
|
package/dist/db/mssql/index.d.ts
DELETED
package/dist/db/mssql/index.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { Pool } from 'pg';
|
|
2
|
-
import { PostgreDbConfig } from '../types/DbConfig.js';
|
|
3
|
-
import { QueryExecutor } from '../types/Executor.js';
|
|
4
|
-
export declare const initDb: (dbConfig: PostgreDbConfig) => void;
|
|
5
|
-
export declare function getPool(): Promise<Pool>;
|
|
6
|
-
export declare function getQueryExecutor(): Promise<QueryExecutor>;
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { Pool } from 'pg';
|
|
11
|
-
import { getClient } from './transaction.js';
|
|
12
|
-
import { Msg } from '../../../internal/messaging/messageTags.js';
|
|
13
|
-
let config = null;
|
|
14
|
-
let pool = null;
|
|
15
|
-
export const initDb = (dbConfig) => {
|
|
16
|
-
config = Object.assign({ max: 10, min: 0, idleTimeoutMillis: 30000, connectionTimeoutMillis: 10000 }, dbConfig);
|
|
17
|
-
};
|
|
18
|
-
export function getPool() {
|
|
19
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
if (pool)
|
|
21
|
-
return pool;
|
|
22
|
-
if (config === null)
|
|
23
|
-
throw new Error(Msg.connection.notInitialized);
|
|
24
|
-
pool = new Pool(config);
|
|
25
|
-
pool.on('error', (err) => {
|
|
26
|
-
console.error(Msg.connection.postgreConnectionError, err);
|
|
27
|
-
});
|
|
28
|
-
return pool;
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
export function getQueryExecutor() {
|
|
32
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
const client = getClient();
|
|
34
|
-
if (client) {
|
|
35
|
-
return client;
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
return yield getPool();
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { PoolClient } from "pg";
|
|
2
|
-
export declare function withTransaction<T>(fn: () => Promise<T>): Promise<T>;
|
|
3
|
-
export declare const getClient: () => PoolClient | null;
|
|
4
|
-
export declare function beginTransaction(): Promise<void>;
|
|
5
|
-
export declare function commitTransaction(): Promise<void>;
|
|
6
|
-
export declare function rollbackTransaction(): Promise<void>;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { getPool } from "./pool.js";
|
|
11
|
-
import { Msg } from "../../../internal/messaging/messageTags.js";
|
|
12
|
-
let client = null;
|
|
13
|
-
export function withTransaction(fn) {
|
|
14
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
-
try {
|
|
16
|
-
yield beginTransaction();
|
|
17
|
-
const result = yield fn();
|
|
18
|
-
yield commitTransaction();
|
|
19
|
-
return result;
|
|
20
|
-
}
|
|
21
|
-
catch (err) {
|
|
22
|
-
yield rollbackTransaction();
|
|
23
|
-
throw err;
|
|
24
|
-
}
|
|
25
|
-
finally {
|
|
26
|
-
if (client) {
|
|
27
|
-
client.release();
|
|
28
|
-
client = null;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
export const getClient = () => client;
|
|
34
|
-
export function beginTransaction() {
|
|
35
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
-
if (client)
|
|
37
|
-
throw new Error(Msg.transactions.alreadyActive);
|
|
38
|
-
const pool = yield getPool();
|
|
39
|
-
client = yield pool.connect();
|
|
40
|
-
yield client.query('BEGIN');
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
export function commitTransaction() {
|
|
44
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
-
if (!client)
|
|
46
|
-
throw new Error(Msg.transactions.noActiveTransactionToCommit);
|
|
47
|
-
yield client.query('COMMIT');
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
export function rollbackTransaction() {
|
|
51
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
if (!client)
|
|
53
|
-
throw new Error(Msg.transactions.noActiveTransactionToRollback);
|
|
54
|
-
yield client.query('ROLLBACK');
|
|
55
|
-
});
|
|
56
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { createTunnel } from 'tunnel-ssh';
|
|
11
|
-
import { Msg } from '../../../internal/messaging/messageTags.js';
|
|
12
|
-
const tunnelOptions = {
|
|
13
|
-
autoClose: true,
|
|
14
|
-
reconnectOnError: true,
|
|
15
|
-
};
|
|
16
|
-
const serverOptions = {};
|
|
17
|
-
export function openSshTunnel(sshOptions, dstPort) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
try {
|
|
20
|
-
const forwardOptions = { dstPort };
|
|
21
|
-
const [server] = yield createTunnel(tunnelOptions, serverOptions, sshOptions, forwardOptions);
|
|
22
|
-
const port = server.address().port;
|
|
23
|
-
return port;
|
|
24
|
-
}
|
|
25
|
-
catch (err) {
|
|
26
|
-
console.error(Msg.connection.sshTunnelFailed, err);
|
|
27
|
-
throw err;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const isUniqueViolation: (err: any) => boolean;
|
package/dist/db/pg/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { PostgreDbConfig } from './types/DbConfig.js';
|
|
2
|
-
import { QueryExecutor } from './types/Executor.js';
|
|
3
|
-
import { initDb, getQueryExecutor } from './connection/pool.js';
|
|
4
|
-
import { withTransaction } from './connection/transaction.js';
|
|
5
|
-
import { isUniqueViolation } from './errors/errorChecks.js';
|
|
6
|
-
import { UniqueKeyViolationError } from './errors/customErrors.js';
|
|
7
|
-
export { PostgreDbConfig, QueryExecutor };
|
|
8
|
-
export { initDb, getQueryExecutor };
|
|
9
|
-
export { withTransaction };
|
|
10
|
-
export { isUniqueViolation };
|
|
11
|
-
export { UniqueKeyViolationError };
|
package/dist/db/pg/index.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { initDb, getQueryExecutor } from './connection/pool.js';
|
|
2
|
-
import { withTransaction } from './connection/transaction.js';
|
|
3
|
-
import { isUniqueViolation } from './errors/errorChecks.js';
|
|
4
|
-
import { UniqueKeyViolationError } from './errors/customErrors.js';
|
|
5
|
-
export { initDb, getQueryExecutor };
|
|
6
|
-
export { withTransaction };
|
|
7
|
-
export { isUniqueViolation };
|
|
8
|
-
export { UniqueKeyViolationError };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { SshOptions } from "tunnel-ssh";
|
|
2
|
-
export interface PostgreDbConfig {
|
|
3
|
-
provider: 'postgre';
|
|
4
|
-
host: string;
|
|
5
|
-
port: number;
|
|
6
|
-
user: string;
|
|
7
|
-
password: string;
|
|
8
|
-
database: string;
|
|
9
|
-
ssh?: SshOptions;
|
|
10
|
-
max?: number;
|
|
11
|
-
min?: number;
|
|
12
|
-
idleTimeoutMillis?: number;
|
|
13
|
-
connectionTimeoutMillis?: number;
|
|
14
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/http/index.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { ApiSuccess, ApiError, ApiErrorDetails } from '../http/types/ApiResponse.js';
|
|
2
|
-
import type { ApiResponse } from '../http/types/ApiResponse.js';
|
|
3
|
-
import { HttpStatus } from './types/HttpStatus.js';
|
|
4
|
-
import { HttpResponseStatus } from './httpResponseStatus/HttpResponseStatus.js';
|
|
5
|
-
import { success, failure, serverError } from './apiResponse/apiResponse.js';
|
|
6
|
-
export type { ApiResponse };
|
|
7
|
-
export { HttpStatus, ApiError, ApiSuccess, ApiErrorDetails };
|
|
8
|
-
export { HttpResponseStatus };
|
|
9
|
-
export { success, failure, serverError };
|
package/dist/http/index.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export declare const Msg: {
|
|
2
|
-
apiStatus: {
|
|
3
|
-
ok: string;
|
|
4
|
-
created: string;
|
|
5
|
-
accepted: string;
|
|
6
|
-
noContent: string;
|
|
7
|
-
badRequest: string;
|
|
8
|
-
unauthorized: string;
|
|
9
|
-
forbidden: string;
|
|
10
|
-
notFound: string;
|
|
11
|
-
conflict: string;
|
|
12
|
-
locked: string;
|
|
13
|
-
tooManyRequests: string;
|
|
14
|
-
serverError: string;
|
|
15
|
-
};
|
|
16
|
-
connection: {
|
|
17
|
-
corsBlocked: string;
|
|
18
|
-
notInitialized: string;
|
|
19
|
-
sshOptionsNotSpecified: string;
|
|
20
|
-
sshTunnelFailed: string;
|
|
21
|
-
postgreConnectionError: string;
|
|
22
|
-
unauthorizedRequest: string;
|
|
23
|
-
};
|
|
24
|
-
transactions: {
|
|
25
|
-
alreadyActive: string;
|
|
26
|
-
noActiveTransactionToCommit: string;
|
|
27
|
-
noActiveTransactionToRollback: string;
|
|
28
|
-
};
|
|
29
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type DbProvider = 'postgre' | 'mssql' | undefined;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { PostgreDbConfig } from '../db/pg/index.js';
|
|
2
|
-
import { MsSqlDbConfig } from '../db/mssql/index.js';
|
|
3
|
-
import { CorsConfig } from '../middlewares/index.js';
|
|
4
|
-
import { DbProvider } from './DbProvider.js';
|
|
5
|
-
import { RateLimitConfig } from '../middlewares/rateLimit/types.js';
|
|
6
|
-
import { RedisConfig } from 'mielk-fn/redis';
|
|
7
|
-
export type DbConfig = PostgreDbConfig | MsSqlDbConfig;
|
|
8
|
-
export declare const createExpressApp: (isProd: boolean, dbConfig: DbConfig, corsConfig: CorsConfig, redisConfig: RedisConfig, rateLimitConfig?: Partial<RateLimitConfig>) => Promise<import("express-serve-static-core").Express>;
|
|
9
|
-
export declare const isProd: () => boolean;
|
|
10
|
-
export declare const getDbProvider: () => DbProvider;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
import { query } from '../../db/mssql/connection/pool.js';
|
|
12
|
-
export function selectDatabaseTime() {
|
|
13
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
-
const field = 'timestamp';
|
|
15
|
-
const rows = yield query(`SELECT SYSDATETIME() AS [${field}]`);
|
|
16
|
-
const row = rows[0];
|
|
17
|
-
const value = row[field];
|
|
18
|
-
return value;
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
export function selectDatabaseVersion() {
|
|
22
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
const field = 'version';
|
|
24
|
-
const rows = yield query(`SELECT @@VERSION AS [${field}]`);
|
|
25
|
-
const row = rows[0];
|
|
26
|
-
const value = row[field];
|
|
27
|
-
return value;
|
|
28
|
-
});
|
|
29
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { getPool } from '../../db/pg/connection/pool.js';
|
|
11
|
-
export function selectDatabaseTime() {
|
|
12
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
13
|
-
const pool = yield getPool();
|
|
14
|
-
const result = yield pool.query('SELECT NOW()');
|
|
15
|
-
return result.rows[0].now;
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
export function selectDatabaseVersion() {
|
|
19
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
const pool = yield getPool();
|
|
21
|
-
const result = yield pool.query('SELECT version()');
|
|
22
|
-
return result.rows[0].version;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
File without changes
|