mielk-api 1.5.1 → 1.5.3
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,6 +1,5 @@
|
|
|
1
1
|
import { isPrimitive } from 'mielk-fn/variables';
|
|
2
2
|
import { HttpResponseStatus } from '../httpResponseStatus/HttpResponseStatus.js';
|
|
3
|
-
import { isProd } from '../../routing/express.js';
|
|
4
3
|
export function success(res, status, data) {
|
|
5
4
|
const apiResponse = {
|
|
6
5
|
success: true,
|
|
@@ -29,7 +28,8 @@ export function serverError(res, err) {
|
|
|
29
28
|
const status = HttpResponseStatus.SERVER_ERROR;
|
|
30
29
|
const apiError = {
|
|
31
30
|
success: false,
|
|
32
|
-
message:
|
|
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()}`,
|
|
33
33
|
};
|
|
34
34
|
return res.status(status.code).json(apiError);
|
|
35
35
|
}
|
|
@@ -23,8 +23,8 @@ export const createRateLimiter = (config) => {
|
|
|
23
23
|
res.setHeader('X-RateLimit-Limit', limit);
|
|
24
24
|
res.setHeader('X-RateLimit-Remaining', Math.max(0, limit - currentCounter));
|
|
25
25
|
res.setHeader('X-RateLimit-Reset', ttl);
|
|
26
|
-
console.log('IP:', req.ip);
|
|
27
|
-
console.log('Forwarded:', req.headers['x-forwarded-for']);
|
|
26
|
+
// console.log('IP:', req.ip);
|
|
27
|
+
// console.log('Forwarded:', req.headers['x-forwarded-for']);
|
|
28
28
|
if (!allowed) {
|
|
29
29
|
return failure(res, HttpResponseStatus.TOO_MANY_REQUESTS, message || Msg.apiStatus.tooManyRequests);
|
|
30
30
|
}
|
|
@@ -7,7 +7,7 @@ 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 { getRedisClient } from
|
|
10
|
+
import { getRedisClient } from 'mielk-fn/redis';
|
|
11
11
|
export const setRedisLock = (key, ttlSeconds) => __awaiter(void 0, void 0, void 0, function* () {
|
|
12
12
|
const redisClient = yield getRedisClient();
|
|
13
13
|
yield redisClient.set(key, JSON.stringify({ locked: true }), {
|
|
@@ -3,7 +3,8 @@ import { MsSqlDbConfig } from '../db/mssql/index.js';
|
|
|
3
3
|
import { CorsConfig } from '../middlewares/index.js';
|
|
4
4
|
import { DbProvider } from './DbProvider.js';
|
|
5
5
|
import { RateLimitConfigParams } from '../middlewares/rateLimit/types.js';
|
|
6
|
+
import { RedisConfig } from 'mielk-fn/redis';
|
|
6
7
|
export type DbConfig = PostgreDbConfig | MsSqlDbConfig;
|
|
7
|
-
export declare const createExpressApp: (isProd: boolean, dbConfig: DbConfig, corsConfig: CorsConfig, rateLimitConfig?: RateLimitConfigParams) => Promise<import("express-serve-static-core").Express>;
|
|
8
|
+
export declare const createExpressApp: (isProd: boolean, dbConfig: DbConfig, corsConfig: CorsConfig, redisConfig: RedisConfig, rateLimitConfig?: RateLimitConfigParams) => Promise<import("express-serve-static-core").Express>;
|
|
8
9
|
export declare const isProd: () => boolean;
|
|
9
10
|
export declare const getDbProvider: () => DbProvider;
|
package/dist/routing/express.js
CHANGED
|
@@ -12,11 +12,12 @@ 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
14
|
import { createGlobalRateLimiter } from '../middlewares/rateLimit/createRateLimiter.js';
|
|
15
|
+
import { getRedisClient } from 'mielk-fn/redis';
|
|
15
16
|
const env = {
|
|
16
17
|
isProd: true,
|
|
17
18
|
provider: undefined
|
|
18
19
|
};
|
|
19
|
-
export const createExpressApp = (isProd, dbConfig, corsConfig, rateLimitConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
export const createExpressApp = (isProd, dbConfig, corsConfig, redisConfig, rateLimitConfig) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
21
|
const app = express();
|
|
21
22
|
env.isProd = isProd;
|
|
22
23
|
env.provider = dbConfig.provider;
|
|
@@ -29,6 +30,7 @@ export const createExpressApp = (isProd, dbConfig, corsConfig, rateLimitConfig)
|
|
|
29
30
|
break;
|
|
30
31
|
}
|
|
31
32
|
initCors(corsConfig);
|
|
33
|
+
getRedisClient(redisConfig);
|
|
32
34
|
app.use(createGlobalRateLimiter(rateLimitConfig));
|
|
33
35
|
app.use(express.json());
|
|
34
36
|
if (isProd)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mielk-api",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"keywords": [],
|
|
5
5
|
"author": "mielk",
|
|
6
6
|
"description": "Wrapper for API operations",
|
|
@@ -84,7 +84,7 @@
|
|
|
84
84
|
"dependencies": {
|
|
85
85
|
"cors": "^2.8.6",
|
|
86
86
|
"express-rate-limit": "^8.3.1",
|
|
87
|
-
"mielk-fn": "^1.2.
|
|
87
|
+
"mielk-fn": "^1.2.2",
|
|
88
88
|
"pg": "^8.20.0",
|
|
89
89
|
"tunnel-ssh": "^5.2.0"
|
|
90
90
|
}
|