@samuelaj1/eazisend-service-kit 1.0.1 → 1.0.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.
- package/dist/index.cjs +59 -27
- package/dist/index.d.cts +8 -15
- package/dist/index.d.ts +8 -15
- package/dist/index.js +50 -23
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -31,17 +31,22 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
LogLevelTypes: () => LogLevelTypes,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
registerErrorMiddleware: () => registerErrorMiddleware,
|
|
35
|
+
registerSecurityMiddleware: () => registerSecurityMiddleware,
|
|
36
|
+
registerStandardMiddleware: () => registerStandardMiddleware,
|
|
37
|
+
runServer: () => runServer,
|
|
38
|
+
verifyGatewayRequest: () => verifyGatewayRequest
|
|
38
39
|
});
|
|
39
40
|
module.exports = __toCommonJS(index_exports);
|
|
40
41
|
|
|
42
|
+
// node_modules/tsup/assets/cjs_shims.js
|
|
43
|
+
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
|
|
44
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
45
|
+
|
|
41
46
|
// src/middlewares/standard.middleware.ts
|
|
42
47
|
var import_express = require("express");
|
|
43
48
|
var import_compression = __toESM(require("compression"), 1);
|
|
44
|
-
function
|
|
49
|
+
function registerStandardMiddleware(app) {
|
|
45
50
|
app.use((0, import_compression.default)());
|
|
46
51
|
app.use((0, import_express.json)({ limit: "200mb" }));
|
|
47
52
|
app.use((0, import_express.urlencoded)({ extended: true, limit: "200mb" }));
|
|
@@ -55,27 +60,20 @@ var import_cors = __toESM(require("cors"), 1);
|
|
|
55
60
|
// src/config.ts
|
|
56
61
|
var Config = class {
|
|
57
62
|
API_GATEWAY_URL;
|
|
63
|
+
GATEWAY_TOKEN;
|
|
58
64
|
ELASTIC_SEARCH_NODE_URL;
|
|
59
65
|
PORT;
|
|
60
66
|
constructor() {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
throw new Error("Missing ELASTIC_SEARCH_NODE_URL in environment variables");
|
|
66
|
-
}
|
|
67
|
-
if (!process.env.PORT) {
|
|
68
|
-
throw new Error("Missing PORT in environment variables");
|
|
69
|
-
}
|
|
70
|
-
this.API_GATEWAY_URL = process.env.API_GATEWAY_URL;
|
|
71
|
-
this.ELASTIC_SEARCH_NODE_URL = process.env.ELASTIC_SEARCH_NODE_URL;
|
|
72
|
-
this.PORT = process.env.PORT;
|
|
67
|
+
this.API_GATEWAY_URL = process.env.API_GATEWAY_URL || "";
|
|
68
|
+
this.GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || "";
|
|
69
|
+
this.ELASTIC_SEARCH_NODE_URL = process.env.ELASTIC_SEARCH_NODE_URL || "";
|
|
70
|
+
this.PORT = process.env.PORT || "";
|
|
73
71
|
}
|
|
74
72
|
};
|
|
75
73
|
var libConfig = new Config();
|
|
76
74
|
|
|
77
75
|
// src/middlewares/security.middleware.ts
|
|
78
|
-
function
|
|
76
|
+
function registerSecurityMiddleware(app, args) {
|
|
79
77
|
const { origin } = args ? args : {};
|
|
80
78
|
app.set("trust proxy", true);
|
|
81
79
|
app.use((0, import_hpp.default)());
|
|
@@ -87,10 +85,6 @@ function enforceSecurityMiddleware(app, args) {
|
|
|
87
85
|
}));
|
|
88
86
|
}
|
|
89
87
|
|
|
90
|
-
// src/repositories/logger.repo.ts
|
|
91
|
-
var import_winston = __toESM(require("winston"), 1);
|
|
92
|
-
var import_winston_elasticsearch = require("winston-elasticsearch");
|
|
93
|
-
|
|
94
88
|
// src/types/log.level.types.ts
|
|
95
89
|
var LogLevelTypes = /* @__PURE__ */ ((LogLevelTypes2) => {
|
|
96
90
|
LogLevelTypes2["DEBUG"] = "debug";
|
|
@@ -100,6 +94,8 @@ var LogLevelTypes = /* @__PURE__ */ ((LogLevelTypes2) => {
|
|
|
100
94
|
})(LogLevelTypes || {});
|
|
101
95
|
|
|
102
96
|
// src/repositories/logger.repo.ts
|
|
97
|
+
var import_winston = __toESM(require("winston"), 1);
|
|
98
|
+
var import_winston_elasticsearch = require("winston-elasticsearch");
|
|
103
99
|
var LoggerRepo = class {
|
|
104
100
|
logger;
|
|
105
101
|
esTransformer = (logData) => {
|
|
@@ -152,19 +148,55 @@ var LoggerRepo = class {
|
|
|
152
148
|
var loggerRepo = new LoggerRepo();
|
|
153
149
|
|
|
154
150
|
// src/processes/server.process.ts
|
|
155
|
-
|
|
151
|
+
var logger = loggerRepo.getInstance(importMetaUrl);
|
|
152
|
+
function runServer(app) {
|
|
156
153
|
try {
|
|
157
154
|
app.listen(libConfig.PORT, () => {
|
|
155
|
+
logger.log("info" /* INFO */, `Gateway STARTED! -- Server listening on port ${libConfig.PORT}`);
|
|
158
156
|
});
|
|
159
157
|
} catch (error) {
|
|
158
|
+
logger.log("info" /* INFO */, "Error starting the server:", error);
|
|
160
159
|
process.exit(1);
|
|
161
160
|
}
|
|
162
161
|
}
|
|
162
|
+
|
|
163
|
+
// src/middlewares/gateway.middleware.ts
|
|
164
|
+
var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
|
|
165
|
+
function verifyGatewayRequest(serviceId) {
|
|
166
|
+
return (req, _res, next) => {
|
|
167
|
+
if (!req.headers?.gatewaytoken) {
|
|
168
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
169
|
+
}
|
|
170
|
+
const token = req.headers.gatewaytoken;
|
|
171
|
+
if (!token) {
|
|
172
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
173
|
+
}
|
|
174
|
+
const payload = import_jsonwebtoken.default.verify(token, libConfig.GATEWAY_TOKEN);
|
|
175
|
+
if (serviceId !== payload.id) {
|
|
176
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request payload is invalid");
|
|
177
|
+
}
|
|
178
|
+
next();
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/middlewares/error.middleware.ts
|
|
183
|
+
var import_http_status_codes = require("http-status-codes");
|
|
184
|
+
var logger2 = loggerRepo.getInstance(importMetaUrl);
|
|
185
|
+
function registerErrorMiddleware(app) {
|
|
186
|
+
return (err, _req, res, _next) => {
|
|
187
|
+
logger2.log("error" /* ERROR */, "GatewayService custom error", {
|
|
188
|
+
name: err.name,
|
|
189
|
+
message: err.message
|
|
190
|
+
});
|
|
191
|
+
res.status(import_http_status_codes.StatusCodes.INTERNAL_SERVER_ERROR).json({ error: err.message });
|
|
192
|
+
};
|
|
193
|
+
}
|
|
163
194
|
// Annotate the CommonJS export names for ESM import in node:
|
|
164
195
|
0 && (module.exports = {
|
|
165
196
|
LogLevelTypes,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
197
|
+
registerErrorMiddleware,
|
|
198
|
+
registerSecurityMiddleware,
|
|
199
|
+
registerStandardMiddleware,
|
|
200
|
+
runServer,
|
|
201
|
+
verifyGatewayRequest
|
|
170
202
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Express } from 'express';
|
|
2
|
-
import { LogEntry } from 'winston';
|
|
1
|
+
import { Express, Request, Response, NextFunction, Application } from 'express';
|
|
3
2
|
|
|
4
|
-
declare function
|
|
3
|
+
declare function registerStandardMiddleware(app: Express): void;
|
|
5
4
|
|
|
6
|
-
declare function
|
|
5
|
+
declare function registerSecurityMiddleware(app: Express, args?: {
|
|
7
6
|
origin?: string;
|
|
8
7
|
}): void;
|
|
9
8
|
|
|
@@ -13,16 +12,10 @@ declare enum LogLevelTypes {
|
|
|
13
12
|
ERROR = "error"
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
declare
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
getInstance: (identifier: string, level?: LogLevelTypes) => LoggerRepo;
|
|
20
|
-
log(entry: LogEntry): void;
|
|
21
|
-
log(level: string, message: string, ...meta: unknown[]): void;
|
|
22
|
-
log(level: string, message: unknown): void;
|
|
23
|
-
}
|
|
24
|
-
declare const loggerRepo: LoggerRepo;
|
|
15
|
+
declare function runServer(app: Express): void;
|
|
16
|
+
|
|
17
|
+
declare function verifyGatewayRequest(serviceId: string): (req: Request, _res: Response, next: NextFunction) => void;
|
|
25
18
|
|
|
26
|
-
declare function
|
|
19
|
+
declare function registerErrorMiddleware(app: Application): (err: Error, _req: Request, res: Response, _next: NextFunction) => void;
|
|
27
20
|
|
|
28
|
-
export { LogLevelTypes,
|
|
21
|
+
export { LogLevelTypes, registerErrorMiddleware, registerSecurityMiddleware, registerStandardMiddleware, runServer, verifyGatewayRequest };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Express } from 'express';
|
|
2
|
-
import { LogEntry } from 'winston';
|
|
1
|
+
import { Express, Request, Response, NextFunction, Application } from 'express';
|
|
3
2
|
|
|
4
|
-
declare function
|
|
3
|
+
declare function registerStandardMiddleware(app: Express): void;
|
|
5
4
|
|
|
6
|
-
declare function
|
|
5
|
+
declare function registerSecurityMiddleware(app: Express, args?: {
|
|
7
6
|
origin?: string;
|
|
8
7
|
}): void;
|
|
9
8
|
|
|
@@ -13,16 +12,10 @@ declare enum LogLevelTypes {
|
|
|
13
12
|
ERROR = "error"
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
declare
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
getInstance: (identifier: string, level?: LogLevelTypes) => LoggerRepo;
|
|
20
|
-
log(entry: LogEntry): void;
|
|
21
|
-
log(level: string, message: string, ...meta: unknown[]): void;
|
|
22
|
-
log(level: string, message: unknown): void;
|
|
23
|
-
}
|
|
24
|
-
declare const loggerRepo: LoggerRepo;
|
|
15
|
+
declare function runServer(app: Express): void;
|
|
16
|
+
|
|
17
|
+
declare function verifyGatewayRequest(serviceId: string): (req: Request, _res: Response, next: NextFunction) => void;
|
|
25
18
|
|
|
26
|
-
declare function
|
|
19
|
+
declare function registerErrorMiddleware(app: Application): (err: Error, _req: Request, res: Response, _next: NextFunction) => void;
|
|
27
20
|
|
|
28
|
-
export { LogLevelTypes,
|
|
21
|
+
export { LogLevelTypes, registerErrorMiddleware, registerSecurityMiddleware, registerStandardMiddleware, runServer, verifyGatewayRequest };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// src/middlewares/standard.middleware.ts
|
|
2
2
|
import { json, urlencoded } from "express";
|
|
3
3
|
import compression from "compression";
|
|
4
|
-
function
|
|
4
|
+
function registerStandardMiddleware(app) {
|
|
5
5
|
app.use(compression());
|
|
6
6
|
app.use(json({ limit: "200mb" }));
|
|
7
7
|
app.use(urlencoded({ extended: true, limit: "200mb" }));
|
|
@@ -15,27 +15,20 @@ import cors from "cors";
|
|
|
15
15
|
// src/config.ts
|
|
16
16
|
var Config = class {
|
|
17
17
|
API_GATEWAY_URL;
|
|
18
|
+
GATEWAY_TOKEN;
|
|
18
19
|
ELASTIC_SEARCH_NODE_URL;
|
|
19
20
|
PORT;
|
|
20
21
|
constructor() {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
throw new Error("Missing ELASTIC_SEARCH_NODE_URL in environment variables");
|
|
26
|
-
}
|
|
27
|
-
if (!process.env.PORT) {
|
|
28
|
-
throw new Error("Missing PORT in environment variables");
|
|
29
|
-
}
|
|
30
|
-
this.API_GATEWAY_URL = process.env.API_GATEWAY_URL;
|
|
31
|
-
this.ELASTIC_SEARCH_NODE_URL = process.env.ELASTIC_SEARCH_NODE_URL;
|
|
32
|
-
this.PORT = process.env.PORT;
|
|
22
|
+
this.API_GATEWAY_URL = process.env.API_GATEWAY_URL || "";
|
|
23
|
+
this.GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || "";
|
|
24
|
+
this.ELASTIC_SEARCH_NODE_URL = process.env.ELASTIC_SEARCH_NODE_URL || "";
|
|
25
|
+
this.PORT = process.env.PORT || "";
|
|
33
26
|
}
|
|
34
27
|
};
|
|
35
28
|
var libConfig = new Config();
|
|
36
29
|
|
|
37
30
|
// src/middlewares/security.middleware.ts
|
|
38
|
-
function
|
|
31
|
+
function registerSecurityMiddleware(app, args) {
|
|
39
32
|
const { origin } = args ? args : {};
|
|
40
33
|
app.set("trust proxy", true);
|
|
41
34
|
app.use(hpp());
|
|
@@ -47,10 +40,6 @@ function enforceSecurityMiddleware(app, args) {
|
|
|
47
40
|
}));
|
|
48
41
|
}
|
|
49
42
|
|
|
50
|
-
// src/repositories/logger.repo.ts
|
|
51
|
-
import winston from "winston";
|
|
52
|
-
import { ElasticsearchTransformer, ElasticsearchTransport } from "winston-elasticsearch";
|
|
53
|
-
|
|
54
43
|
// src/types/log.level.types.ts
|
|
55
44
|
var LogLevelTypes = /* @__PURE__ */ ((LogLevelTypes2) => {
|
|
56
45
|
LogLevelTypes2["DEBUG"] = "debug";
|
|
@@ -60,6 +49,8 @@ var LogLevelTypes = /* @__PURE__ */ ((LogLevelTypes2) => {
|
|
|
60
49
|
})(LogLevelTypes || {});
|
|
61
50
|
|
|
62
51
|
// src/repositories/logger.repo.ts
|
|
52
|
+
import winston from "winston";
|
|
53
|
+
import { ElasticsearchTransformer, ElasticsearchTransport } from "winston-elasticsearch";
|
|
63
54
|
var LoggerRepo = class {
|
|
64
55
|
logger;
|
|
65
56
|
esTransformer = (logData) => {
|
|
@@ -112,18 +103,54 @@ var LoggerRepo = class {
|
|
|
112
103
|
var loggerRepo = new LoggerRepo();
|
|
113
104
|
|
|
114
105
|
// src/processes/server.process.ts
|
|
115
|
-
|
|
106
|
+
var logger = loggerRepo.getInstance(import.meta.url);
|
|
107
|
+
function runServer(app) {
|
|
116
108
|
try {
|
|
117
109
|
app.listen(libConfig.PORT, () => {
|
|
110
|
+
logger.log("info" /* INFO */, `Gateway STARTED! -- Server listening on port ${libConfig.PORT}`);
|
|
118
111
|
});
|
|
119
112
|
} catch (error) {
|
|
113
|
+
logger.log("info" /* INFO */, "Error starting the server:", error);
|
|
120
114
|
process.exit(1);
|
|
121
115
|
}
|
|
122
116
|
}
|
|
117
|
+
|
|
118
|
+
// src/middlewares/gateway.middleware.ts
|
|
119
|
+
import jwt from "jsonwebtoken";
|
|
120
|
+
function verifyGatewayRequest(serviceId) {
|
|
121
|
+
return (req, _res, next) => {
|
|
122
|
+
if (!req.headers?.gatewaytoken) {
|
|
123
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
124
|
+
}
|
|
125
|
+
const token = req.headers.gatewaytoken;
|
|
126
|
+
if (!token) {
|
|
127
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
128
|
+
}
|
|
129
|
+
const payload = jwt.verify(token, libConfig.GATEWAY_TOKEN);
|
|
130
|
+
if (serviceId !== payload.id) {
|
|
131
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request payload is invalid");
|
|
132
|
+
}
|
|
133
|
+
next();
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// src/middlewares/error.middleware.ts
|
|
138
|
+
import { StatusCodes } from "http-status-codes";
|
|
139
|
+
var logger2 = loggerRepo.getInstance(import.meta.url);
|
|
140
|
+
function registerErrorMiddleware(app) {
|
|
141
|
+
return (err, _req, res, _next) => {
|
|
142
|
+
logger2.log("error" /* ERROR */, "GatewayService custom error", {
|
|
143
|
+
name: err.name,
|
|
144
|
+
message: err.message
|
|
145
|
+
});
|
|
146
|
+
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: err.message });
|
|
147
|
+
};
|
|
148
|
+
}
|
|
123
149
|
export {
|
|
124
150
|
LogLevelTypes,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
151
|
+
registerErrorMiddleware,
|
|
152
|
+
registerSecurityMiddleware,
|
|
153
|
+
registerStandardMiddleware,
|
|
154
|
+
runServer,
|
|
155
|
+
verifyGatewayRequest
|
|
129
156
|
};
|