mielk-api 1.5.3 → 1.5.5
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 +2 -2
- package/dist/middlewares/index.d.ts +0 -12
- package/dist/middlewares/index.js +0 -12
- package/dist/middlewares/rateLimit/createRateLimiter.js +3 -6
- package/dist/middlewares/rateLimit/index.d.ts +1 -2
- package/dist/middlewares/rateLimit/index.js +1 -2
- package/dist/middlewares/rateLimit/rateLimitConfigFactory.js +3 -3
- package/dist/middlewares/redis/index.d.ts +14 -0
- package/dist/middlewares/redis/index.js +16 -0
- package/dist/middlewares/redis/redisKeysFactory.d.ts +5 -4
- package/dist/middlewares/redis/redisKeysFactory.js +7 -4
- package/dist/middlewares/redis/redisLockCheckerConfigFactory.js +1 -1
- package/dist/middlewares/redis/redisLockerConfigFactory.js +1 -1
- package/package.json +6 -1
|
@@ -1,5 +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
4
|
export function success(res, status, data) {
|
|
4
5
|
const apiResponse = {
|
|
5
6
|
success: true,
|
|
@@ -28,8 +29,7 @@ export function serverError(res, err) {
|
|
|
28
29
|
const status = HttpResponseStatus.SERVER_ERROR;
|
|
29
30
|
const apiError = {
|
|
30
31
|
success: false,
|
|
31
|
-
message: `message: ${err.message} | stack: ${(_a = err.stack) === null || _a === void 0 ? void 0 : _a.toString()}
|
|
32
|
-
// message: isProd() ? status.defaultMessageTag : `message: ${err.message} | stack: ${err.stack?.toString()}`,
|
|
32
|
+
message: isProd() ? status.defaultMessageTag : `message: ${err.message} | stack: ${(_a = err.stack) === null || _a === void 0 ? void 0 : _a.toString()}`,
|
|
33
33
|
};
|
|
34
34
|
return res.status(status.code).json(apiError);
|
|
35
35
|
}
|
|
@@ -3,18 +3,6 @@ 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 { 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';
|
|
12
6
|
export { CorsConfig, initCors, privateCors, publicCors };
|
|
13
7
|
export { apiKeyAuthorization };
|
|
14
8
|
export { validateQueryParams, validateBodyJson };
|
|
15
|
-
export { RedisLockerConfigFactory };
|
|
16
|
-
export { RedisLockerConfig, RedisLockerConfigParams };
|
|
17
|
-
export { setRedisLock, removeRedisLock };
|
|
18
|
-
export { createRedisLockChecker, checkIfLockedInRedis };
|
|
19
|
-
export { RedisLockCheckerConfigFactory };
|
|
20
|
-
export { RedisLockChecker, RedisLockCheckerConfig, RedisLockCheckerConfigParams };
|
|
@@ -3,18 +3,6 @@ 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
|
-
// 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';
|
|
12
6
|
export { initCors, privateCors, publicCors };
|
|
13
7
|
export { apiKeyAuthorization };
|
|
14
8
|
export { validateQueryParams, validateBodyJson };
|
|
15
|
-
// Redis locker
|
|
16
|
-
export { RedisLockerConfigFactory };
|
|
17
|
-
export { setRedisLock, removeRedisLock };
|
|
18
|
-
// Redis lock checker
|
|
19
|
-
export { createRedisLockChecker, checkIfLockedInRedis };
|
|
20
|
-
export { RedisLockCheckerConfigFactory };
|
|
@@ -12,6 +12,7 @@ import { failure, HttpResponseStatus } from '../../http/index.js';
|
|
|
12
12
|
import { RateLimitConfigFactory } from './rateLimitConfigFactory.js';
|
|
13
13
|
import { generateRedisLockKey } from '../redis/redisLockChecker.js';
|
|
14
14
|
import { Msg } from '../../internal/messaging/messageTags.js';
|
|
15
|
+
import { generateRedisKey } from '../redis/redisKeysFactory.js';
|
|
15
16
|
export const createGlobalRateLimiter = (config) => {
|
|
16
17
|
return createRateLimiter(RateLimitConfigFactory.globalIp(config));
|
|
17
18
|
};
|
|
@@ -32,8 +33,8 @@ export const createRateLimiter = (config) => {
|
|
|
32
33
|
});
|
|
33
34
|
};
|
|
34
35
|
const createRateLimitFromConfig = (config, req) => {
|
|
35
|
-
const { limit, windowSec, message, redisLockerConfig } = config;
|
|
36
|
-
const key =
|
|
36
|
+
const { keyPrefix, keySuffixes, limit, windowSec, message, redisLockerConfig } = config;
|
|
37
|
+
const key = generateRedisKey(keyPrefix, keySuffixes, req);
|
|
37
38
|
const rateLimit = { key, limit, windowSec, message };
|
|
38
39
|
if (redisLockerConfig) {
|
|
39
40
|
const { keyPrefix, ttlSeconds } = redisLockerConfig;
|
|
@@ -44,7 +45,3 @@ const createRateLimitFromConfig = (config, req) => {
|
|
|
44
45
|
}
|
|
45
46
|
return rateLimit;
|
|
46
47
|
};
|
|
47
|
-
const generateKeyFromConfig = (config, req) => {
|
|
48
|
-
const { keyPrefix, keySuffixes } = config;
|
|
49
|
-
return `${keyPrefix}:${keySuffixes(req).join(':')}`;
|
|
50
|
-
};
|
|
@@ -1,8 +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 '../redis/redisKeysFactory.js';
|
|
5
4
|
import { type RateLimiter, RateLimitConfig, RateLimit, RateLimitCheck } from './types.js';
|
|
6
|
-
export { checkRateLimit, createRateLimiter, RateLimitConfigFactory
|
|
5
|
+
export { checkRateLimit, createRateLimiter, RateLimitConfigFactory };
|
|
7
6
|
export { RateLimitConfig, RateLimit, RateLimitCheck };
|
|
8
7
|
export type { RateLimiter };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { checkRateLimit } from './checkRateLimit.js';
|
|
2
2
|
import { createRateLimiter } from './createRateLimiter.js';
|
|
3
3
|
import { RateLimitConfigFactory } from './rateLimitConfigFactory.js';
|
|
4
|
-
|
|
5
|
-
export { checkRateLimit, createRateLimiter, RateLimitConfigFactory, RedisKeyFactory };
|
|
4
|
+
export { checkRateLimit, createRateLimiter, RateLimitConfigFactory };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RedisKeyFactory } from "../redis/redisKeysFactory.js";
|
|
2
2
|
import { Msg } from "../../internal/messaging/messageTags.js";
|
|
3
3
|
const defaultRateLimitConfig = {
|
|
4
|
-
keyPrefix: RedisKeyFactory.prefixes.
|
|
4
|
+
keyPrefix: RedisKeyFactory.prefixes.authLogin,
|
|
5
5
|
keySuffixes: RedisKeyFactory.suffixes.ip,
|
|
6
6
|
windowSec: 15 * 60,
|
|
7
7
|
limit: 5,
|
|
@@ -11,7 +11,7 @@ const createRateLimitConfig = (params) => {
|
|
|
11
11
|
return Object.assign(Object.assign({}, defaultRateLimitConfig), params);
|
|
12
12
|
};
|
|
13
13
|
const globalIp = (params) => {
|
|
14
|
-
return createRateLimitConfig(Object.assign({ keyPrefix: RedisKeyFactory.prefixes.
|
|
14
|
+
return createRateLimitConfig(Object.assign({ keyPrefix: RedisKeyFactory.prefixes.authGlobal, windowSec: 60, limit: 2 }, (params || {})));
|
|
15
15
|
};
|
|
16
16
|
const loginIp = (params) => {
|
|
17
17
|
return createRateLimitConfig(Object.assign({}, (params || {})));
|
|
@@ -20,7 +20,7 @@ const loginIpMail = (params) => {
|
|
|
20
20
|
return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.ipMail }, (params || {})));
|
|
21
21
|
};
|
|
22
22
|
const loginMail = (params) => {
|
|
23
|
-
return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.mail, redisLockerConfig: { keyPrefix: RedisKeyFactory.prefixes.
|
|
23
|
+
return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.mail, redisLockerConfig: { keyPrefix: RedisKeyFactory.prefixes.authLockEmail, ttlSeconds: 15 * 60 } }, (params || {})));
|
|
24
24
|
};
|
|
25
25
|
export const RateLimitConfigFactory = {
|
|
26
26
|
globalIp,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RedisLockerConfigFactory } from './redisLockerConfigFactory.js';
|
|
2
|
+
import { RedisLockerConfig, RedisLockerConfigParams } from './types.js';
|
|
3
|
+
import { setRedisLock, removeRedisLock } from './redisLocker.js';
|
|
4
|
+
import { RedisLockCheckerConfigFactory } from './redisLockCheckerConfigFactory.js';
|
|
5
|
+
import { RedisLockChecker, RedisLockCheckerConfig, RedisLockCheckerConfigParams } from './types.js';
|
|
6
|
+
import { createRedisLockChecker, checkIfLockedInRedis } from './redisLockChecker.js';
|
|
7
|
+
import { generateRedisKey, RedisKeyFactory } from './redisKeysFactory.js';
|
|
8
|
+
export { RedisLockerConfigFactory };
|
|
9
|
+
export { RedisLockerConfig, RedisLockerConfigParams };
|
|
10
|
+
export { setRedisLock, removeRedisLock };
|
|
11
|
+
export { createRedisLockChecker, checkIfLockedInRedis };
|
|
12
|
+
export { RedisLockCheckerConfigFactory };
|
|
13
|
+
export { RedisLockChecker, RedisLockCheckerConfig, RedisLockCheckerConfigParams };
|
|
14
|
+
export { generateRedisKey, RedisKeyFactory };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Redis locker
|
|
2
|
+
import { RedisLockerConfigFactory } from './redisLockerConfigFactory.js';
|
|
3
|
+
import { setRedisLock, removeRedisLock } from './redisLocker.js';
|
|
4
|
+
// Redis lock checker
|
|
5
|
+
import { RedisLockCheckerConfigFactory } from './redisLockCheckerConfigFactory.js';
|
|
6
|
+
import { createRedisLockChecker, checkIfLockedInRedis } from './redisLockChecker.js';
|
|
7
|
+
// Redis keys
|
|
8
|
+
import { generateRedisKey, RedisKeyFactory } from './redisKeysFactory.js';
|
|
9
|
+
// Redis locker
|
|
10
|
+
export { RedisLockerConfigFactory };
|
|
11
|
+
export { setRedisLock, removeRedisLock };
|
|
12
|
+
// Redis lock checker
|
|
13
|
+
export { createRedisLockChecker, checkIfLockedInRedis };
|
|
14
|
+
export { RedisLockCheckerConfigFactory };
|
|
15
|
+
// Redis keys
|
|
16
|
+
export { generateRedisKey, RedisKeyFactory };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { Request } from 'express';
|
|
2
2
|
export declare const RedisKeyFactory: {
|
|
3
3
|
prefixes: {
|
|
4
|
-
readonly
|
|
5
|
-
readonly
|
|
6
|
-
readonly
|
|
7
|
-
readonly
|
|
4
|
+
readonly authGlobal: "auth.global";
|
|
5
|
+
readonly authLogin: "auth.login";
|
|
6
|
+
readonly authLockEmail: "auth.lock:email";
|
|
7
|
+
readonly authRefresh: "auth.refresh";
|
|
8
8
|
};
|
|
9
9
|
suffixes: {
|
|
10
10
|
readonly ip: (req: Request) => string[];
|
|
@@ -12,3 +12,4 @@ export declare const RedisKeyFactory: {
|
|
|
12
12
|
readonly mail: (req: Request) => string[];
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
|
+
export declare const generateRedisKey: (prefix: string, suffixCallback: (req: Request) => string[], req: Request) => string;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const prefixes = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
authGlobal: 'auth.global',
|
|
3
|
+
authLogin: 'auth.login',
|
|
4
|
+
authLockEmail: 'auth.lock:email',
|
|
5
|
+
authRefresh: 'auth.refresh',
|
|
6
6
|
};
|
|
7
7
|
const getIpSuffixCallback = (req) => {
|
|
8
8
|
const ip = req.ip || 'undefined';
|
|
@@ -28,3 +28,6 @@ export const RedisKeyFactory = {
|
|
|
28
28
|
prefixes: prefixes,
|
|
29
29
|
suffixes: suffixCallbacks
|
|
30
30
|
};
|
|
31
|
+
export const generateRedisKey = (prefix, suffixCallback, req) => {
|
|
32
|
+
return `${prefix}:${suffixCallback(req).join(':')}`;
|
|
33
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RedisKeyFactory } from "./redisKeysFactory.js";
|
|
2
2
|
import { Msg } from "../../internal/messaging/messageTags.js";
|
|
3
3
|
const defaultRedisLockCheckerConfig = {
|
|
4
|
-
keyPrefix: RedisKeyFactory.prefixes.
|
|
4
|
+
keyPrefix: RedisKeyFactory.prefixes.authLockEmail,
|
|
5
5
|
keySuffixes: RedisKeyFactory.suffixes.mail,
|
|
6
6
|
message: Msg.apiStatus.locked,
|
|
7
7
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { RedisKeyFactory } from "./redisKeysFactory.js";
|
|
2
2
|
const defaultRedisLockerConfig = {
|
|
3
|
-
keyPrefix: RedisKeyFactory.prefixes.
|
|
3
|
+
keyPrefix: RedisKeyFactory.prefixes.authLockEmail,
|
|
4
4
|
ttlSeconds: 30 * 60,
|
|
5
5
|
};
|
|
6
6
|
const createRedisLockerConfig = (params) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mielk-api",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.5",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": "mielk",
|
|
6
6
|
"description": "Wrapper for API operations",
|
|
@@ -37,6 +37,11 @@
|
|
|
37
37
|
"types": "./dist/middlewares/rateLimit/index.d.ts",
|
|
38
38
|
"import": "./dist/middlewares/rateLimit/index.js",
|
|
39
39
|
"default": "./dist/middlewares/rateLimit/index.js"
|
|
40
|
+
},
|
|
41
|
+
"./redis": {
|
|
42
|
+
"types": "./dist/middlewares/redis/index.d.ts",
|
|
43
|
+
"import": "./dist/middlewares/redis/index.js",
|
|
44
|
+
"default": "./dist/middlewares/redis/index.js"
|
|
40
45
|
}
|
|
41
46
|
},
|
|
42
47
|
"scripts": {
|