mielk-api 1.3.9 → 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.
@@ -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.9",
3
+ "version": "1.3.10",
4
4
  "keywords": [],
5
5
  "author": "mielk",
6
6
  "description": "Wrapper for API operations",