mielk-api 1.3.8 → 1.3.10

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.
@@ -1,5 +1,6 @@
1
- import { RawData } from 'msnodesqlv8/types';
1
+ import type { RawData, SqlClient, sqlQueryParamType } from 'msnodesqlv8/types';
2
2
  import { MsSqlDbConfig } from '../types/DbConfig.js';
3
+ export declare const getMsSql: () => Promise<SqlClient>;
3
4
  export declare const initDb: (dbConfig: MsSqlDbConfig) => void;
4
5
  export declare const query: (queryText: string) => Promise<any[]>;
5
- export declare const spExecute: (sqlText: string, params: MsNodeSqlV8.sqlQueryParamType[]) => Promise<RawData | undefined>;
6
+ export declare const spExecute: (sqlText: string, params: sqlQueryParamType[]) => Promise<RawData | undefined>;
@@ -1,12 +1,32 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import sql from 'msnodesqlv8';
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;
3
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
+ });
4
23
  export const initDb = (dbConfig) => {
5
24
  const { host, database, user, password, driver } = dbConfig;
6
25
  connectionString = `Server=${host};Database=${database};UID=${user};PWD=${password};Driver=${driver}`;
7
26
  console.log(connectionString);
8
27
  };
9
- export const query = (queryText) => {
28
+ export const query = (queryText) => __awaiter(void 0, void 0, void 0, function* () {
29
+ const sql = yield getMsSql();
10
30
  return new Promise((resolve, reject) => {
11
31
  sql.query(connectionString, queryText, (err, rows) => {
12
32
  if (err)
@@ -17,8 +37,9 @@ export const query = (queryText) => {
17
37
  resolve(rows);
18
38
  });
19
39
  });
20
- };
21
- export const spExecute = (sqlText, params) => {
40
+ });
41
+ export const spExecute = (sqlText, params) => __awaiter(void 0, void 0, void 0, function* () {
42
+ const sql = yield getMsSql();
22
43
  return new Promise((resolve, reject) => {
23
44
  sql.open(connectionString, (err, conn) => {
24
45
  if (err)
@@ -31,4 +52,4 @@ export const spExecute = (sqlText, params) => {
31
52
  });
32
53
  });
33
54
  });
34
- };
55
+ });
@@ -4,6 +4,7 @@ export declare const HttpResponseStatus: {
4
4
  CREATED: HttpStatus;
5
5
  BAD_REQUEST: HttpStatus;
6
6
  UNAUTHORIZED: HttpStatus;
7
+ FORBIDDEN: HttpStatus;
7
8
  NOT_FOUND: HttpStatus;
8
9
  CONFLICT: HttpStatus;
9
10
  SERVER_ERROR: HttpStatus;
@@ -6,6 +6,7 @@ export const HttpResponseStatus = {
6
6
  CREATED: createApiStatus(true, 201, msg.created),
7
7
  BAD_REQUEST: createApiStatus(false, 400, msg.badRequest),
8
8
  UNAUTHORIZED: createApiStatus(false, 401, msg.unauthorized),
9
+ FORBIDDEN: createApiStatus(false, 403, msg.forbidden),
9
10
  NOT_FOUND: createApiStatus(false, 404, msg.notFound),
10
11
  CONFLICT: createApiStatus(false, 409, msg.conflict),
11
12
  SERVER_ERROR: createApiStatus(false, 500, msg.serverError),
@@ -2,8 +2,9 @@ export declare const Msg: {
2
2
  apiStatus: {
3
3
  ok: string;
4
4
  created: string;
5
- unauthorized: string;
6
5
  badRequest: string;
6
+ unauthorized: string;
7
+ forbidden: string;
7
8
  notFound: string;
8
9
  conflict: string;
9
10
  serverError: string;
@@ -11,6 +12,7 @@ export declare const Msg: {
11
12
  connection: {
12
13
  corsBlocked: string;
13
14
  notInitialized: string;
15
+ rateLimitExceededMessage: string;
14
16
  sshOptionsNotSpecified: string;
15
17
  sshTunnelFailed: string;
16
18
  postgreConnectionError: string;
@@ -5,8 +5,9 @@ export const Msg = {
5
5
  apiStatus: {
6
6
  ok: `${___HTTP_STATUS___}:ok`,
7
7
  created: `${___HTTP_STATUS___}:created`,
8
- unauthorized: `${___HTTP_STATUS___}:unauthorized`,
9
8
  badRequest: `${___HTTP_STATUS___}:badRequest`,
9
+ unauthorized: `${___HTTP_STATUS___}:unauthorized`,
10
+ forbidden: `${___HTTP_STATUS___}:forbidden`,
10
11
  notFound: `${___HTTP_STATUS___}:notFound`,
11
12
  conflict: `${___HTTP_STATUS___}:conflict`,
12
13
  serverError: `${___HTTP_STATUS___}:serverError`,
@@ -14,6 +15,7 @@ export const Msg = {
14
15
  connection: {
15
16
  corsBlocked: `${___CONNECTION___}:corsBlocked`,
16
17
  notInitialized: `${___CONNECTION___}:notInitialized`,
18
+ rateLimitExceededMessage: `${___CONNECTION___}:rateLimitExceededMessage`,
17
19
  sshOptionsNotSpecified: `${___CONNECTION___}:sshOptionsNotSpecified`,
18
20
  sshTunnelFailed: `${___CONNECTION___}:sshTunnelFailed`, // ❌ SSH tunnel failed
19
21
  postgreConnectionError: `${___CONNECTION___}:postgreConnectionError`, // Unexpected PostgreSQL error
@@ -1,10 +1,10 @@
1
1
  import { CorsConfig, initCors } from './cors/cors.js';
2
2
  import { privateCors } from './cors/privateCors.js';
3
3
  import { publicCors } from './cors/publicCors.js';
4
- import { RateLimitConfig, setRateLimit } from './rateLimit/rateLimit.js';
4
+ import { RateLimitConfig, setAppRateLimit, getRateLimit } from './rateLimit/rateLimit.js';
5
5
  import { apiKeyAuthorization } from './requestAuth/auth.middleware.js';
6
6
  import { validateQueryParams, validateBodyJson } from './zod/validate.js';
7
7
  export { CorsConfig, initCors, privateCors, publicCors };
8
- export { RateLimitConfig, setRateLimit };
8
+ export { RateLimitConfig, setAppRateLimit, getRateLimit };
9
9
  export { apiKeyAuthorization };
10
10
  export { validateQueryParams, validateBodyJson };
@@ -1,10 +1,10 @@
1
1
  import { initCors } from './cors/cors.js';
2
2
  import { privateCors } from './cors/privateCors.js';
3
3
  import { publicCors } from './cors/publicCors.js';
4
- import { setRateLimit } from './rateLimit/rateLimit.js';
4
+ import { setAppRateLimit, getRateLimit } from './rateLimit/rateLimit.js';
5
5
  import { apiKeyAuthorization } from './requestAuth/auth.middleware.js';
6
6
  import { validateQueryParams, validateBodyJson } from './zod/validate.js';
7
7
  export { initCors, privateCors, publicCors };
8
- export { setRateLimit };
8
+ export { setAppRateLimit, getRateLimit };
9
9
  export { apiKeyAuthorization };
10
10
  export { validateQueryParams, validateBodyJson };
@@ -1,6 +1,8 @@
1
1
  import { Express } from 'express';
2
+ import { RateLimitRequestHandler } from 'express-rate-limit';
2
3
  export interface RateLimitConfig {
3
4
  windowsMs?: number;
4
5
  max?: number;
5
6
  }
6
- export declare const setRateLimit: (app: Express, customConfig?: RateLimitConfig) => void;
7
+ export declare const setAppRateLimit: (app: Express, customConfig?: RateLimitConfig) => void;
8
+ export declare const getRateLimit: (minutes: number, maxAttempts: number, message?: string) => RateLimitRequestHandler;
@@ -1,10 +1,18 @@
1
1
  import rateLimit from 'express-rate-limit';
2
+ import { Msg } from '../../internal/messaging/messageTags.js';
2
3
  const rateLimitConfig = {
3
4
  windowMs: 60 * 1000,
4
5
  max: 100,
5
6
  };
6
- export const setRateLimit = (app, customConfig) => {
7
+ export const setAppRateLimit = (app, customConfig) => {
7
8
  const config = Object.assign(Object.assign({}, rateLimitConfig), customConfig);
8
9
  const limiter = rateLimit(config);
9
10
  app.use(limiter);
10
11
  };
12
+ export const getRateLimit = (minutes, maxAttempts, message) => {
13
+ return rateLimit({
14
+ windowMs: minutes * 60 * 1000,
15
+ max: maxAttempts,
16
+ message: message || Msg.connection.rateLimitExceededMessage
17
+ });
18
+ };
@@ -1,7 +1,7 @@
1
1
  import express from 'express';
2
2
  import { initDb as postgreInitDb } from '../db/pg/index.js';
3
3
  import { initDb as msSqlInitDb } from '../db/mssql/index.js';
4
- import { initCors, setRateLimit } from '../middlewares/index.js';
4
+ import { initCors, setAppRateLimit } from '../middlewares/index.js';
5
5
  const env = {
6
6
  isProd: true,
7
7
  provider: undefined
@@ -19,7 +19,7 @@ export const createExpressApp = (isProd, dbConfig, corsConfig, rateLimitConfig)
19
19
  break;
20
20
  }
21
21
  initCors(corsConfig);
22
- setRateLimit(app, rateLimitConfig);
22
+ setAppRateLimit(app, rateLimitConfig);
23
23
  app.use(express.json());
24
24
  return app;
25
25
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mielk-api",
3
- "version": "1.3.8",
3
+ "version": "1.3.10",
4
4
  "keywords": [],
5
5
  "author": "mielk",
6
6
  "description": "Wrapper for API operations",