mielk-api 1.5.13 → 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.
Files changed (63) hide show
  1. package/dist/http/apiResponse/apiResponse.js +1 -1
  2. package/dist/http/express.d.ts +5 -0
  3. package/dist/{routing → http}/express.js +1 -14
  4. package/dist/http/httpResponseStatus/HttpResponseStatus.js +1 -1
  5. package/dist/index.d.ts +11 -6
  6. package/dist/index.js +6 -6
  7. package/dist/middlewares/cors/cors.js +1 -1
  8. package/dist/middlewares/rateLimit/createRateLimiter.js +2 -2
  9. package/dist/middlewares/rateLimit/rateLimitConfigFactory.js +1 -1
  10. package/dist/middlewares/redis/redisLockChecker.js +2 -2
  11. package/dist/middlewares/redis/redisLockCheckerConfigFactory.js +1 -1
  12. package/dist/middlewares/requestAuth/auth.middleware.js +4 -4
  13. package/dist/middlewares/zod/validate.js +1 -1
  14. package/dist/{internal/messaging → msg}/messageTags.d.ts +0 -8
  15. package/dist/{internal/messaging → msg}/messageTags.js +0 -9
  16. package/dist/routing/controllers/db.controller.d.ts +7 -2
  17. package/dist/routing/controllers/db.controller.js +24 -20
  18. package/dist/routing/controllers/health.controller.js +3 -2
  19. package/dist/routing/index.d.ts +6 -0
  20. package/dist/routing/index.js +4 -0
  21. package/dist/routing/routes/db.route.d.ts +2 -3
  22. package/dist/routing/routes/db.route.js +8 -6
  23. package/dist/routing/routes/health.route.d.ts +1 -3
  24. package/dist/routing/routes/health.route.js +6 -5
  25. package/dist/routing/services/db.service.d.ts +7 -2
  26. package/dist/routing/services/db.service.js +6 -23
  27. package/dist/routing/types/DbRepository.d.ts +4 -0
  28. package/package.json +6 -11
  29. package/dist/Config.d.ts +0 -1
  30. package/dist/Config.js +0 -1
  31. package/dist/db/mssql/connection/pool.d.ts +0 -6
  32. package/dist/db/mssql/connection/pool.js +0 -55
  33. package/dist/db/mssql/index.d.ts +0 -4
  34. package/dist/db/mssql/index.js +0 -2
  35. package/dist/db/mssql/types/DbConfig.d.ts +0 -8
  36. package/dist/db/pg/connection/pool.d.ts +0 -4
  37. package/dist/db/pg/connection/pool.js +0 -34
  38. package/dist/db/pg/connection/transaction.d.ts +0 -2
  39. package/dist/db/pg/connection/transaction.js +0 -29
  40. package/dist/db/pg/connection/tunnel.d.ts +0 -2
  41. package/dist/db/pg/connection/tunnel.js +0 -30
  42. package/dist/db/pg/errors/PostgreErrorCodes.d.ts +0 -4
  43. package/dist/db/pg/errors/PostgreErrorCodes.js +0 -4
  44. package/dist/db/pg/errors/customErrors.d.ts +0 -2
  45. package/dist/db/pg/errors/customErrors.js +0 -2
  46. package/dist/db/pg/errors/errorChecks.d.ts +0 -1
  47. package/dist/db/pg/errors/errorChecks.js +0 -2
  48. package/dist/db/pg/index.d.ts +0 -11
  49. package/dist/db/pg/index.js +0 -8
  50. package/dist/db/pg/types/DbConfig.d.ts +0 -14
  51. package/dist/db/pg/types/DbConfig.js +0 -1
  52. package/dist/db/pg/types/Executor.d.ts +0 -4
  53. package/dist/db/pg/types/Executor.js +0 -2
  54. package/dist/http/index.d.ts +0 -9
  55. package/dist/http/index.js +0 -4
  56. package/dist/routing/DbProvider.d.ts +0 -1
  57. package/dist/routing/DbProvider.js +0 -1
  58. package/dist/routing/express.d.ts +0 -10
  59. package/dist/routing/repositories/mssql.repository.d.ts +0 -2
  60. package/dist/routing/repositories/mssql.repository.js +0 -29
  61. package/dist/routing/repositories/postgre.repository.d.ts +0 -2
  62. package/dist/routing/repositories/postgre.repository.js +0 -24
  63. /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 '../../routing/express.js';
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, dbConfig, corsConfig, redisConfig, rateLimitConfig) => __awaiter(void 0, void 0, void 0, function* () {
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 "../../internal/messaging/messageTags.js";
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
- export * as pg from './db/pg/index.js';
2
- export * as mssql from './db/mssql/index.js';
3
- export * as http from './http/index.js';
4
- export { dbRouter } from './routing/routes/db.route.js';
5
- export { healthRouter } from './routing/routes/health.route.js';
6
- export { createExpressApp } from './routing/express.js';
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
- export * as pg from './db/pg/index.js';
2
- export * as mssql from './db/mssql/index.js';
3
- export * as http from './http/index.js';
4
- export { dbRouter } from './routing/routes/db.route.js';
5
- export { healthRouter } from './routing/routes/health.route.js';
6
- export { createExpressApp } from './routing/express.js';
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 };
@@ -1,4 +1,4 @@
1
- import { isProd } from "../../routing/express.js";
1
+ import { isProd } from "../../http/express.js";
2
2
  const corsConfig = {
3
3
  allowedOrigins: '',
4
4
  apiKey: ''
@@ -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 '../../http/index.js';
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 '../../internal/messaging/messageTags.js';
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 "../../internal/messaging/messageTags.js";
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 "../../http/index.js";
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 "../../internal/messaging/messageTags.js";
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,4 +1,4 @@
1
- import { Msg } from "../../internal/messaging/messageTags.js";
1
+ import { Msg } from "../../msg/messageTags.js";
2
2
  import { getRedisPrefix, getRedisSuffixCallback } from "./redisKeysFactory.js";
3
3
  const defaultConfig = {
4
4
  keyPrefix: getRedisPrefix("authLockEmail"),
@@ -1,7 +1,7 @@
1
1
  import { HttpResponseStatus } from '../../http/httpResponseStatus/HttpResponseStatus.js';
2
- import { Msg } from '../../internal/messaging/messageTags.js';
3
- import { isProd } from '../../routing/express.js';
4
- import { failure } from '../../http/index.js';
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.connection.unauthorizedRequest);
13
+ return failure(res, HttpResponseStatus.UNAUTHORIZED, Msg.apiStatus.unauthorized);
14
14
  }
15
15
  next();
16
16
  }
@@ -1,4 +1,4 @@
1
- import { failure, HttpResponseStatus } from '../../http/index.js';
1
+ import { failure, HttpResponseStatus } from '../../index.js';
2
2
  export const validateQueryParams = (schema) => (req, res, next) => {
3
3
  const result = schema.safeParse({
4
4
  params: req.params,
@@ -13,12 +13,4 @@ export declare const Msg: {
13
13
  tooManyRequests: string;
14
14
  serverError: string;
15
15
  };
16
- connection: {
17
- corsBlocked: string;
18
- notInitialized: string;
19
- sshOptionsNotSpecified: string;
20
- sshTunnelFailed: string;
21
- postgreConnectionError: string;
22
- unauthorizedRequest: string;
23
- };
24
16
  };
@@ -1,6 +1,5 @@
1
1
  const PARENT_FOLDER = 'api/';
2
2
  const ___HTTP_STATUS___ = `${PARENT_FOLDER}/httpStatus`;
3
- const ___CONNECTION___ = `${PARENT_FOLDER}/connection`;
4
3
  export const Msg = {
5
4
  apiStatus: {
6
5
  ok: `${___HTTP_STATUS___}:ok`,
@@ -15,13 +14,5 @@ export const Msg = {
15
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.
16
15
  tooManyRequests: `${___HTTP_STATUS___}:tooManyRequests`,
17
16
  serverError: `${___HTTP_STATUS___}:serverError`,
18
- },
19
- connection: {
20
- corsBlocked: `${___CONNECTION___}:corsBlocked`,
21
- notInitialized: `${___CONNECTION___}:notInitialized`,
22
- sshOptionsNotSpecified: `${___CONNECTION___}:sshOptionsNotSpecified`,
23
- sshTunnelFailed: `${___CONNECTION___}:sshTunnelFailed`, // ❌ SSH tunnel failed
24
- postgreConnectionError: `${___CONNECTION___}:postgreConnectionError`, // Unexpected PostgreSQL error
25
- unauthorizedRequest: `${___CONNECTION___}:unauthorizedRequest`
26
17
  }
27
18
  };
@@ -1,3 +1,8 @@
1
1
  import { Request, Response } from 'express';
2
- export declare const getDbTest: (_req: Request, res: Response) => Promise<Response<Record<string, any>, Record<string, any>>>;
3
- export declare function getDbVersion(_req: Request, res: Response): Promise<Response<Record<string, any>, Record<string, any>>>;
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 * as DbService from '../services/db.service.js';
11
- import { HttpResponseStatus, serverError, success } from '../../http/index.js';
12
- export const getDbTest = (_req, res) => __awaiter(void 0, void 0, void 0, function* () {
13
- try {
14
- const dbTime = yield DbService.getDatabaseTime();
15
- return success(res, HttpResponseStatus.OK, dbTime);
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 { HttpResponseStatus, success } from '../../http/index.js';
2
- import { isProd } from '../express.js';
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 };
@@ -0,0 +1,4 @@
1
+ import { createDbRouter } from './routes/db.route.js';
2
+ import { createHealthRouter } from './routes/health.route.js';
3
+ export { createDbRouter };
4
+ export { createHealthRouter };
@@ -1,4 +1,3 @@
1
1
  import { Router } from 'express';
2
- declare const dbRouter: Router;
3
- export { dbRouter };
4
- export default dbRouter;
2
+ import { DbRepository } from '../types/DbRepository.js';
3
+ export declare const createDbRouter: (repository: DbRepository) => Router;
@@ -1,7 +1,9 @@
1
1
  import { Router } from 'express';
2
- import { getDbTest, getDbVersion } from '../controllers/db.controller.js';
3
- const dbRouter = Router();
4
- dbRouter.get('/test/', getDbTest);
5
- dbRouter.get('/version/', getDbVersion);
6
- export { dbRouter };
7
- export default dbRouter;
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,4 +1,2 @@
1
1
  import { Router } from 'express';
2
- declare const healthRouter: Router;
3
- export { healthRouter };
4
- export default healthRouter;
2
+ export declare const createHealthRouter: () => Router;
@@ -1,7 +1,8 @@
1
1
  import { Router } from 'express';
2
2
  import { getHealth, getEnv } from '../controllers/health.controller.js';
3
- const healthRouter = Router();
4
- healthRouter.get('/', getHealth);
5
- healthRouter.get('/environment', getEnv);
6
- export { healthRouter };
7
- export default healthRouter;
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
- export declare function getDatabaseTime(): Promise<Date>;
2
- export declare function getDatabaseVersion(): Promise<string>;
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
- import * as postgreRepository from '../repositories/postgre.repository.js';
11
- import * as msSqlRepository from '../repositories/mssql.repository.js';
12
- import { getDbProvider } from '../express.js';
13
- import { Msg } from '../../internal/messaging/messageTags.js';
14
- export function getDatabaseTime() {
15
- return __awaiter(this, void 0, void 0, function* () {
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
  }
@@ -0,0 +1,4 @@
1
+ export interface DbRepository {
2
+ getDatabaseTime: () => Promise<Date>;
3
+ getDatabaseVersion: () => Promise<string>;
4
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mielk-api",
3
- "version": "1.5.13",
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
- });
@@ -1,4 +0,0 @@
1
- import { initDb, query, spExecute } from './connection/pool.js';
2
- import { MsSqlDbConfig } from './types/DbConfig.js';
3
- export { initDb, query, spExecute };
4
- export { MsSqlDbConfig };
@@ -1,2 +0,0 @@
1
- import { initDb, query, spExecute } from './connection/pool.js';
2
- export { initDb, query, spExecute };
@@ -1,8 +0,0 @@
1
- export interface MsSqlDbConfig {
2
- provider: 'mssql';
3
- host: string;
4
- database: string;
5
- user: string;
6
- password: string;
7
- driver: string;
8
- }
@@ -1,4 +0,0 @@
1
- import { Pool } from 'pg';
2
- import { PostgreDbConfig } from '../types/DbConfig.js';
3
- export declare const initDb: (dbConfig: PostgreDbConfig) => void;
4
- export declare function getPool(): Promise<Pool>;
@@ -1,34 +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 { Msg } from '../../../internal/messaging/messageTags.js';
11
- import { Pool } from 'pg';
12
- let pool = null;
13
- let config = null;
14
- export const initDb = (dbConfig) => {
15
- config = Object.assign({ max: 10, min: 0, idleTimeoutMillis: 30000, connectionTimeoutMillis: 10000 }, dbConfig);
16
- };
17
- export function getPool() {
18
- return __awaiter(this, void 0, void 0, function* () {
19
- if (pool)
20
- return pool;
21
- if (config === null)
22
- throw new Error(Msg.connection.notInitialized);
23
- // if (!isProd()) {
24
- // if (!config.ssh) throw new Error(Msg.connection.sshOptionsNotSpecified)
25
- // const port: number = await openSshTunnel(config.ssh, config.port);
26
- // config.port = port;
27
- // }
28
- pool = new Pool(config);
29
- pool.on('error', (err) => {
30
- console.error(Msg.connection.postgreConnectionError, err);
31
- });
32
- return pool;
33
- });
34
- }
@@ -1,2 +0,0 @@
1
- import { PoolClient } from "pg";
2
- export declare function withTransaction<T>(fn: (client: PoolClient) => Promise<T>): Promise<T>;
@@ -1,29 +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
- export function withTransaction(fn) {
12
- return __awaiter(this, void 0, void 0, function* () {
13
- const pool = yield getPool();
14
- const client = yield pool.connect();
15
- try {
16
- yield client.query('BEGIN');
17
- const result = yield fn(client);
18
- yield client.query('COMMIT');
19
- return result;
20
- }
21
- catch (err) {
22
- yield client.query('ROLLBACK');
23
- throw err;
24
- }
25
- finally {
26
- client.release();
27
- }
28
- });
29
- }
@@ -1,2 +0,0 @@
1
- import { SshOptions } from 'tunnel-ssh';
2
- export declare function openSshTunnel(sshOptions: SshOptions, dstPort: number): Promise<number>;
@@ -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,4 +0,0 @@
1
- export declare const POSTGRE_ERRORS: {
2
- UNIQUE_VIOLATION: string;
3
- FOREIGN_KEY_VIOLATION: string;
4
- };
@@ -1,4 +0,0 @@
1
- export const POSTGRE_ERRORS = {
2
- UNIQUE_VIOLATION: '23505',
3
- FOREIGN_KEY_VIOLATION: '23503',
4
- };
@@ -1,2 +0,0 @@
1
- export declare class UniqueKeyViolationError extends Error {
2
- }
@@ -1,2 +0,0 @@
1
- export class UniqueKeyViolationError extends Error {
2
- }
@@ -1 +0,0 @@
1
- export declare const isUniqueViolation: (err: any) => boolean;
@@ -1,2 +0,0 @@
1
- import { POSTGRE_ERRORS } from "./PostgreErrorCodes.js";
2
- export const isUniqueViolation = (err) => err.code === POSTGRE_ERRORS.UNIQUE_VIOLATION;
@@ -1,11 +0,0 @@
1
- import { PostgreDbConfig } from './types/DbConfig.js';
2
- import { QueryExecutor } from './types/Executor.js';
3
- import { initDb, getPool } 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, getPool };
9
- export { withTransaction };
10
- export { isUniqueViolation };
11
- export { UniqueKeyViolationError };
@@ -1,8 +0,0 @@
1
- import { initDb, getPool } 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, getPool };
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 {};
@@ -1,4 +0,0 @@
1
- import { QueryResult, QueryResultRow } from "pg";
2
- export interface QueryExecutor {
3
- query<T extends QueryResultRow = any>(text: string, params?: any[]): Promise<QueryResult<T>>;
4
- }
@@ -1,2 +0,0 @@
1
- ;
2
- export {};
@@ -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 };
@@ -1,4 +0,0 @@
1
- import { HttpResponseStatus } from './httpResponseStatus/HttpResponseStatus.js';
2
- import { success, failure, serverError } from './apiResponse/apiResponse.js';
3
- export { HttpResponseStatus };
4
- export { success, failure, serverError };
@@ -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,2 +0,0 @@
1
- export declare function selectDatabaseTime(): Promise<Date>;
2
- export declare function selectDatabaseVersion(): Promise<string>;
@@ -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,2 +0,0 @@
1
- export declare function selectDatabaseTime(): Promise<any>;
2
- export declare function selectDatabaseVersion(): Promise<any>;
@@ -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
- }