mielk-api 1.5.0 → 1.5.1

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 (28) hide show
  1. package/dist/middlewares/index.d.ts +11 -5
  2. package/dist/middlewares/index.js +11 -3
  3. package/dist/{rateLimit → middlewares/rateLimit}/checkRateLimit.js +1 -3
  4. package/dist/{rateLimit → middlewares/rateLimit}/createRateLimiter.js +6 -5
  5. package/dist/{rateLimit → middlewares/rateLimit}/index.d.ts +1 -1
  6. package/dist/{rateLimit → middlewares/rateLimit}/index.js +1 -1
  7. package/dist/{rateLimit → middlewares/rateLimit}/rateLimitConfigFactory.js +3 -4
  8. package/dist/{rateLimit → middlewares/rateLimit}/types.d.ts +2 -2
  9. package/dist/middlewares/redis/redisLockChecker.d.ts +5 -0
  10. package/dist/middlewares/{redisLocks/redisLocker.js → redis/redisLockChecker.js} +4 -19
  11. package/dist/middlewares/redis/redisLockCheckerConfigFactory.d.ts +4 -0
  12. package/dist/middlewares/redis/redisLockCheckerConfigFactory.js +16 -0
  13. package/dist/middlewares/redis/redisLocker.d.ts +2 -0
  14. package/dist/middlewares/redis/redisLocker.js +23 -0
  15. package/dist/middlewares/{redisLocks → redis}/redisLockerConfigFactory.js +2 -5
  16. package/dist/middlewares/{redisLocks → redis}/types.d.ts +11 -5
  17. package/dist/routing/express.d.ts +1 -1
  18. package/dist/routing/express.js +1 -1
  19. package/package.json +4 -4
  20. package/dist/middlewares/redisLocks/redisLocker.d.ts +0 -7
  21. /package/dist/{rateLimit → middlewares/rateLimit}/checkRateLimit.d.ts +0 -0
  22. /package/dist/{rateLimit → middlewares/rateLimit}/createRateLimiter.d.ts +0 -0
  23. /package/dist/{rateLimit → middlewares/rateLimit}/rateLimitConfigFactory.d.ts +0 -0
  24. /package/dist/middlewares/{redisLocks → rateLimit}/types.js +0 -0
  25. /package/dist/{rateLimit → middlewares/redis}/redisKeysFactory.d.ts +0 -0
  26. /package/dist/{rateLimit → middlewares/redis}/redisKeysFactory.js +0 -0
  27. /package/dist/middlewares/{redisLocks → redis}/redisLockerConfigFactory.d.ts +0 -0
  28. /package/dist/{rateLimit → middlewares/redis}/types.js +0 -0
@@ -3,12 +3,18 @@ import { privateCors } from './cors/privateCors.js';
3
3
  import { publicCors } from './cors/publicCors.js';
4
4
  import { apiKeyAuthorization } from './requestAuth/auth.middleware.js';
5
5
  import { validateQueryParams, validateBodyJson } from './zod/validate.js';
6
- import { createRedisLocker, setRedisLock, removeRedisLock, checkIfLockedInRedis } from './redisLocks/redisLocker.js';
7
- import { RedisLockerConfigFactory } from './redisLocks/redisLockerConfigFactory.js';
8
- import { RedisLocker, RedisLockerConfig, RedisLockerConfigParams } from './redisLocks/types.js';
6
+ import { RedisLockerConfigFactory } from './redis/redisLockerConfigFactory.js';
7
+ import { RedisLockerConfig, RedisLockerConfigParams } from './redis/types.js';
8
+ import { setRedisLock, removeRedisLock } from './redis/redisLocker.js';
9
+ import { RedisLockCheckerConfigFactory } from './redis/redisLockCheckerConfigFactory.js';
10
+ import { RedisLockChecker, RedisLockCheckerConfig, RedisLockCheckerConfigParams } from './redis/types.js';
11
+ import { createRedisLockChecker, checkIfLockedInRedis } from './redis/redisLockChecker.js';
9
12
  export { CorsConfig, initCors, privateCors, publicCors };
10
13
  export { apiKeyAuthorization };
11
14
  export { validateQueryParams, validateBodyJson };
12
- export { createRedisLocker, setRedisLock, removeRedisLock as removeLock, checkIfLockedInRedis as isLocked };
13
15
  export { RedisLockerConfigFactory };
14
- export { RedisLocker, RedisLockerConfig, RedisLockerConfigParams };
16
+ export { RedisLockerConfig, RedisLockerConfigParams };
17
+ export { setRedisLock, removeRedisLock };
18
+ export { createRedisLockChecker, checkIfLockedInRedis };
19
+ export { RedisLockCheckerConfigFactory };
20
+ export { RedisLockChecker, RedisLockCheckerConfig, RedisLockCheckerConfigParams };
@@ -3,10 +3,18 @@ import { privateCors } from './cors/privateCors.js';
3
3
  import { publicCors } from './cors/publicCors.js';
4
4
  import { apiKeyAuthorization } from './requestAuth/auth.middleware.js';
5
5
  import { validateQueryParams, validateBodyJson } from './zod/validate.js';
6
- import { createRedisLocker, setRedisLock, removeRedisLock, checkIfLockedInRedis } from './redisLocks/redisLocker.js';
7
- import { RedisLockerConfigFactory } from './redisLocks/redisLockerConfigFactory.js';
6
+ // Redis locker
7
+ import { RedisLockerConfigFactory } from './redis/redisLockerConfigFactory.js';
8
+ import { setRedisLock, removeRedisLock } from './redis/redisLocker.js';
9
+ // Redis lock checker
10
+ import { RedisLockCheckerConfigFactory } from './redis/redisLockCheckerConfigFactory.js';
11
+ import { createRedisLockChecker, checkIfLockedInRedis } from './redis/redisLockChecker.js';
8
12
  export { initCors, privateCors, publicCors };
9
13
  export { apiKeyAuthorization };
10
14
  export { validateQueryParams, validateBodyJson };
11
- export { createRedisLocker, setRedisLock, removeRedisLock as removeLock, checkIfLockedInRedis as isLocked };
15
+ // Redis locker
12
16
  export { RedisLockerConfigFactory };
17
+ export { setRedisLock, removeRedisLock };
18
+ // Redis lock checker
19
+ export { createRedisLockChecker, checkIfLockedInRedis };
20
+ export { RedisLockCheckerConfigFactory };
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { getRedisClient } from 'mielk-fn/redis';
11
- import { setRedisLock } from '../middlewares/redisLocks/redisLocker.js';
11
+ import { setRedisLock } from '../redis/redisLocker.js';
12
12
  export const checkRateLimit = (rateLimit) => __awaiter(void 0, void 0, void 0, function* () {
13
13
  const { key, limit, windowSec, redisLock } = rateLimit;
14
14
  const client = yield getRedisClient();
@@ -20,10 +20,8 @@ export const checkRateLimit = (rateLimit) => __awaiter(void 0, void 0, void 0, f
20
20
  const allowed = currentCounter <= limit;
21
21
  if (!allowed && redisLock) {
22
22
  const { key, ttlSeconds } = redisLock;
23
- console.log(`Setting Redis lock for ${key} | ${ttlSeconds} seconds`);
24
23
  setRedisLock(key, ttlSeconds);
25
24
  }
26
- console.log('RL key:', key, 'current:', currentCounter, 'ttl:', ttl);
27
25
  return {
28
26
  currentCounter,
29
27
  limit,
@@ -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';
12
- import { Msg } from '../internal/messaging/messageTags.js';
11
+ import { failure, HttpResponseStatus } from '../../http/index.js';
13
12
  import { RateLimitConfigFactory } from './rateLimitConfigFactory.js';
14
- import { generateRedisLockKey } from '../middlewares/redisLocks/redisLocker.js';
13
+ import { generateRedisLockKey } from '../redis/redisLockChecker.js';
14
+ import { Msg } from '../../internal/messaging/messageTags.js';
15
15
  export const createGlobalRateLimiter = (config) => {
16
16
  return createRateLimiter(RateLimitConfigFactory.globalIp(config));
17
17
  };
@@ -36,8 +36,9 @@ const createRateLimitFromConfig = (config, req) => {
36
36
  const key = generateKeyFromConfig(config, req);
37
37
  const rateLimit = { key, limit, windowSec, message };
38
38
  if (redisLockerConfig) {
39
- const { ttlSeconds } = redisLockerConfig;
40
- const key = generateRedisLockKey(redisLockerConfig, req);
39
+ const { keyPrefix, ttlSeconds } = redisLockerConfig;
40
+ const { keySuffixes } = config;
41
+ const key = generateRedisLockKey(keyPrefix, keySuffixes, req);
41
42
  const redisLock = { key, ttlSeconds };
42
43
  rateLimit.redisLock = redisLock;
43
44
  }
@@ -1,7 +1,7 @@
1
1
  import { checkRateLimit } from './checkRateLimit.js';
2
2
  import { createRateLimiter } from './createRateLimiter.js';
3
3
  import { RateLimitConfigFactory } from './rateLimitConfigFactory.js';
4
- import { RedisKeyFactory } from './redisKeysFactory.js';
4
+ import { RedisKeyFactory } from '../redis/redisKeysFactory.js';
5
5
  import { type RateLimiter, RateLimitConfig, RateLimit, RateLimitCheck } from './types.js';
6
6
  export { checkRateLimit, createRateLimiter, RateLimitConfigFactory, RedisKeyFactory };
7
7
  export { RateLimitConfig, RateLimit, RateLimitCheck };
@@ -1,5 +1,5 @@
1
1
  import { checkRateLimit } from './checkRateLimit.js';
2
2
  import { createRateLimiter } from './createRateLimiter.js';
3
3
  import { RateLimitConfigFactory } from './rateLimitConfigFactory.js';
4
- import { RedisKeyFactory } from './redisKeysFactory.js';
4
+ import { RedisKeyFactory } from '../redis/redisKeysFactory.js';
5
5
  export { checkRateLimit, createRateLimiter, RateLimitConfigFactory, RedisKeyFactory };
@@ -1,6 +1,5 @@
1
- import { RedisKeyFactory } from "./redisKeysFactory.js";
2
- import { Msg } from "../internal/messaging/messageTags.js";
3
- import { RedisLockerConfigFactory } from "../middlewares/index.js";
1
+ import { RedisKeyFactory } from "../redis/redisKeysFactory.js";
2
+ import { Msg } from "../../internal/messaging/messageTags.js";
4
3
  const defaultRateLimitConfig = {
5
4
  keyPrefix: RedisKeyFactory.prefixes.login,
6
5
  keySuffixes: RedisKeyFactory.suffixes.ip,
@@ -21,7 +20,7 @@ const loginIpMail = (params) => {
21
20
  return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.ipMail }, (params || {})));
22
21
  };
23
22
  const loginMail = (params) => {
24
- return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.mail, redisLockerConfig: RedisLockerConfigFactory.mail() }, (params || {})));
23
+ return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.mail, redisLockerConfig: { keyPrefix: RedisKeyFactory.prefixes.lockEmail, ttlSeconds: 15 * 60 } }, (params || {})));
25
24
  };
26
25
  export const RateLimitConfigFactory = {
27
26
  globalIp,
@@ -1,5 +1,5 @@
1
- import { NextFunction, Request, Response } from "express";
2
- import { RedisLockerConfig } from "../middlewares/index.js";
1
+ import { NextFunction, Request, Response } from 'express';
2
+ import { RedisLockerConfig } from '../redis/types.js';
3
3
  export type RateLimiter = (req: Request, res: Response, next: NextFunction) => Promise<void | Response<Record<string, any>, Record<string, any>>>;
4
4
  export type RateLimitConfigParams = {
5
5
  keyPrefix?: string;
@@ -0,0 +1,5 @@
1
+ import { RedisLockChecker, RedisLockCheckerConfig } from "./types.js";
2
+ import { Request } from "express";
3
+ export declare const createRedisLockChecker: (config: RedisLockCheckerConfig) => RedisLockChecker;
4
+ export declare const generateRedisLockKey: (keyPrefix: string, keySuffixes: (req: Request) => string[], req: Request) => string;
5
+ export declare const checkIfLockedInRedis: (key: string) => Promise<boolean>;
@@ -10,10 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { getRedisClient } from "mielk-fn/redis";
11
11
  import { failure, HttpResponseStatus } from "../../http/index.js";
12
12
  import { Msg } from "../../internal/messaging/messageTags.js";
13
- export const createRedisLocker = (config) => {
13
+ export const createRedisLockChecker = (config) => {
14
14
  return (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
15
- const { message } = config;
16
- const key = generateRedisLockKey(config, req);
15
+ const { message, keyPrefix, keySuffixes } = config;
16
+ const key = generateRedisLockKey(keyPrefix, keySuffixes, req);
17
17
  const isLocked = yield checkIfLockedInRedis(key);
18
18
  if (isLocked) {
19
19
  return failure(res, HttpResponseStatus.LOCKED, message || Msg.apiStatus.locked);
@@ -21,24 +21,9 @@ export const createRedisLocker = (config) => {
21
21
  return next();
22
22
  });
23
23
  };
24
- export const generateRedisLockKey = (config, req) => {
25
- const { keyPrefix, keySuffixes } = config;
24
+ export const generateRedisLockKey = (keyPrefix, keySuffixes, req) => {
26
25
  return `${keyPrefix}:${keySuffixes(req).join(':')}`;
27
26
  };
28
- export const setRedisLock = (key, ttlSeconds) => __awaiter(void 0, void 0, void 0, function* () {
29
- const redisClient = yield getRedisClient();
30
- yield redisClient.set(key, JSON.stringify({ locked: true }), {
31
- expiration: {
32
- type: 'EX',
33
- value: ttlSeconds
34
- }
35
- });
36
- console.log('RL key:', key, ttlSeconds);
37
- });
38
- export const removeRedisLock = (key) => __awaiter(void 0, void 0, void 0, function* () {
39
- const redisClient = yield getRedisClient();
40
- yield redisClient.del(key);
41
- });
42
27
  export const checkIfLockedInRedis = (key) => __awaiter(void 0, void 0, void 0, function* () {
43
28
  const redisClient = yield getRedisClient();
44
29
  const value = yield redisClient.get(key);
@@ -0,0 +1,4 @@
1
+ import { RedisLockCheckerConfig, RedisLockCheckerConfigParams } from "./types.js";
2
+ export declare const RedisLockCheckerConfigFactory: {
3
+ mail: (params?: RedisLockCheckerConfigParams) => RedisLockCheckerConfig;
4
+ };
@@ -0,0 +1,16 @@
1
+ import { RedisKeyFactory } from "./redisKeysFactory.js";
2
+ import { Msg } from "../../internal/messaging/messageTags.js";
3
+ const defaultRedisLockCheckerConfig = {
4
+ keyPrefix: RedisKeyFactory.prefixes.lockEmail,
5
+ keySuffixes: RedisKeyFactory.suffixes.mail,
6
+ message: Msg.apiStatus.locked,
7
+ };
8
+ const createRedisLockCheckerConfig = (params) => {
9
+ return Object.assign(Object.assign({}, defaultRedisLockCheckerConfig), params);
10
+ };
11
+ const mail = (params) => {
12
+ return createRedisLockCheckerConfig(Object.assign({}, (params || {})));
13
+ };
14
+ export const RedisLockCheckerConfigFactory = {
15
+ mail,
16
+ };
@@ -0,0 +1,2 @@
1
+ export declare const setRedisLock: (key: string, ttlSeconds: number) => Promise<void>;
2
+ export declare const removeRedisLock: (key: string) => Promise<void>;
@@ -0,0 +1,23 @@
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 { getRedisClient } from "mielk-fn/redis";
11
+ export const setRedisLock = (key, ttlSeconds) => __awaiter(void 0, void 0, void 0, function* () {
12
+ const redisClient = yield getRedisClient();
13
+ yield redisClient.set(key, JSON.stringify({ locked: true }), {
14
+ expiration: {
15
+ type: 'EX',
16
+ value: ttlSeconds
17
+ }
18
+ });
19
+ });
20
+ export const removeRedisLock = (key) => __awaiter(void 0, void 0, void 0, function* () {
21
+ const redisClient = yield getRedisClient();
22
+ yield redisClient.del(key);
23
+ });
@@ -1,10 +1,7 @@
1
- import { RedisKeyFactory } from "../../rateLimit/redisKeysFactory.js";
2
- import { Msg } from "../../internal/messaging/messageTags.js";
1
+ import { RedisKeyFactory } from "./redisKeysFactory.js";
3
2
  const defaultRedisLockerConfig = {
4
3
  keyPrefix: RedisKeyFactory.prefixes.lockEmail,
5
- keySuffixes: RedisKeyFactory.suffixes.mail,
6
- ttlSeconds: 3 * 60,
7
- message: Msg.apiStatus.locked,
4
+ ttlSeconds: 30 * 60,
8
5
  };
9
6
  const createRedisLockerConfig = (params) => {
10
7
  return Object.assign(Object.assign({}, defaultRedisLockerConfig), params);
@@ -1,14 +1,20 @@
1
1
  import { NextFunction, Request, Response } from "express";
2
- export type RedisLocker = (req: Request, res: Response, next: NextFunction) => Promise<void | Response<Record<string, any>, Record<string, any>>>;
3
- export interface RedisLockerConfigParams {
2
+ export type RedisLockChecker = (req: Request, res: Response, next: NextFunction) => Promise<void | Response<Record<string, any>, Record<string, any>>>;
3
+ export interface RedisLockCheckerConfigParams {
4
4
  keyPrefix?: string;
5
5
  keySuffixes?: (req: Request) => string[];
6
- ttlSeconds?: number;
7
6
  message?: string;
8
7
  }
9
- export interface RedisLockerConfig {
8
+ export interface RedisLockCheckerConfig {
10
9
  keyPrefix: string;
11
10
  keySuffixes: (req: Request) => string[];
12
- ttlSeconds: number;
13
11
  message: string;
14
12
  }
13
+ export interface RedisLockerConfigParams {
14
+ keyPrefix?: string;
15
+ ttlSeconds?: number;
16
+ }
17
+ export interface RedisLockerConfig {
18
+ keyPrefix: string;
19
+ ttlSeconds: number;
20
+ }
@@ -2,7 +2,7 @@ import { PostgreDbConfig } from '../db/pg/index.js';
2
2
  import { MsSqlDbConfig } from '../db/mssql/index.js';
3
3
  import { CorsConfig } from '../middlewares/index.js';
4
4
  import { DbProvider } from './DbProvider.js';
5
- import { RateLimitConfigParams } from '../rateLimit/types.js';
5
+ import { RateLimitConfigParams } from '../middlewares/rateLimit/types.js';
6
6
  export type DbConfig = PostgreDbConfig | MsSqlDbConfig;
7
7
  export declare const createExpressApp: (isProd: boolean, dbConfig: DbConfig, corsConfig: CorsConfig, rateLimitConfig?: RateLimitConfigParams) => Promise<import("express-serve-static-core").Express>;
8
8
  export declare const isProd: () => boolean;
@@ -11,7 +11,7 @@ import express from 'express';
11
11
  import { initDb as postgreInitDb } from '../db/pg/index.js';
12
12
  import { initDb as msSqlInitDb } from '../db/mssql/index.js';
13
13
  import { initCors } from '../middlewares/index.js';
14
- import { createGlobalRateLimiter } from '../rateLimit/createRateLimiter.js';
14
+ import { createGlobalRateLimiter } from '../middlewares/rateLimit/createRateLimiter.js';
15
15
  const env = {
16
16
  isProd: true,
17
17
  provider: undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mielk-api",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "keywords": [],
5
5
  "author": "mielk",
6
6
  "description": "Wrapper for API operations",
@@ -34,9 +34,9 @@
34
34
  "default": "./dist/middlewares/index.js"
35
35
  },
36
36
  "./rate-limit": {
37
- "types": "./dist/rateLimit/index.d.ts",
38
- "import": "./dist/rateLimit/index.js",
39
- "default": "./dist/rateLimit/index.js"
37
+ "types": "./dist/middlewares/rateLimit/index.d.ts",
38
+ "import": "./dist/middlewares/rateLimit/index.js",
39
+ "default": "./dist/middlewares/rateLimit/index.js"
40
40
  }
41
41
  },
42
42
  "scripts": {
@@ -1,7 +0,0 @@
1
- import { RedisLocker, RedisLockerConfig } from "./types.js";
2
- import { Request } from "express";
3
- export declare const createRedisLocker: (config: RedisLockerConfig) => RedisLocker;
4
- export declare const generateRedisLockKey: (config: RedisLockerConfig, req: Request) => string;
5
- export declare const setRedisLock: (key: string, ttlSeconds: number) => Promise<void>;
6
- export declare const removeRedisLock: (key: string) => Promise<void>;
7
- export declare const checkIfLockedInRedis: (key: string) => Promise<boolean>;
File without changes