@samuelaj1/eazisend-service-kit 1.0.1 → 1.0.2
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 +88 -23
- package/dist/index.d.cts +21 -5
- package/dist/index.d.ts +21 -5
- package/dist/index.js +77 -19
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -31,17 +31,24 @@ 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
|
+
elasticSearchRepo: () => elasticSearchRepo,
|
|
35
|
+
loggerRepo: () => loggerRepo,
|
|
36
|
+
registerErrorMiddleware: () => registerErrorMiddleware,
|
|
37
|
+
registerSecurityMiddleware: () => registerSecurityMiddleware,
|
|
38
|
+
registerStandardMiddleware: () => registerStandardMiddleware,
|
|
39
|
+
runServer: () => runServer,
|
|
40
|
+
verifyGatewayRequest: () => verifyGatewayRequest
|
|
38
41
|
});
|
|
39
42
|
module.exports = __toCommonJS(index_exports);
|
|
40
43
|
|
|
44
|
+
// node_modules/tsup/assets/cjs_shims.js
|
|
45
|
+
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;
|
|
46
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
47
|
+
|
|
41
48
|
// src/middlewares/standard.middleware.ts
|
|
42
49
|
var import_express = require("express");
|
|
43
50
|
var import_compression = __toESM(require("compression"), 1);
|
|
44
|
-
function
|
|
51
|
+
function registerStandardMiddleware(app) {
|
|
45
52
|
app.use((0, import_compression.default)());
|
|
46
53
|
app.use((0, import_express.json)({ limit: "200mb" }));
|
|
47
54
|
app.use((0, import_express.urlencoded)({ extended: true, limit: "200mb" }));
|
|
@@ -55,27 +62,20 @@ var import_cors = __toESM(require("cors"), 1);
|
|
|
55
62
|
// src/config.ts
|
|
56
63
|
var Config = class {
|
|
57
64
|
API_GATEWAY_URL;
|
|
65
|
+
GATEWAY_TOKEN;
|
|
58
66
|
ELASTIC_SEARCH_NODE_URL;
|
|
59
67
|
PORT;
|
|
60
68
|
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;
|
|
69
|
+
this.API_GATEWAY_URL = process.env.API_GATEWAY_URL || "";
|
|
70
|
+
this.GATEWAY_TOKEN = process.env.GATEWAY_TOKEN || "";
|
|
71
|
+
this.ELASTIC_SEARCH_NODE_URL = process.env.ELASTIC_SEARCH_NODE_URL || "";
|
|
72
|
+
this.PORT = process.env.PORT || "";
|
|
73
73
|
}
|
|
74
74
|
};
|
|
75
75
|
var libConfig = new Config();
|
|
76
76
|
|
|
77
77
|
// src/middlewares/security.middleware.ts
|
|
78
|
-
function
|
|
78
|
+
function registerSecurityMiddleware(app, args) {
|
|
79
79
|
const { origin } = args ? args : {};
|
|
80
80
|
app.set("trust proxy", true);
|
|
81
81
|
app.use((0, import_hpp.default)());
|
|
@@ -152,19 +152,84 @@ var LoggerRepo = class {
|
|
|
152
152
|
var loggerRepo = new LoggerRepo();
|
|
153
153
|
|
|
154
154
|
// src/processes/server.process.ts
|
|
155
|
-
|
|
155
|
+
var logger = loggerRepo.getInstance(importMetaUrl);
|
|
156
|
+
function runServer(app) {
|
|
156
157
|
try {
|
|
157
158
|
app.listen(libConfig.PORT, () => {
|
|
159
|
+
logger.log("info" /* INFO */, `Gateway STARTED! -- Server listening on port ${libConfig.PORT}`);
|
|
158
160
|
});
|
|
159
161
|
} catch (error) {
|
|
162
|
+
logger.log("info" /* INFO */, "Error starting the server:", error);
|
|
160
163
|
process.exit(1);
|
|
161
164
|
}
|
|
162
165
|
}
|
|
166
|
+
|
|
167
|
+
// src/repositories/elasticsearch.repo.ts
|
|
168
|
+
var import_elasticsearch = require("@elastic/elasticsearch");
|
|
169
|
+
var ElasticsearchRepo = class {
|
|
170
|
+
elasticSearchClient;
|
|
171
|
+
/** Creates an Elasticsearch client using the configured cluster URL. */
|
|
172
|
+
constructor() {
|
|
173
|
+
this.elasticSearchClient = new import_elasticsearch.Client({
|
|
174
|
+
node: libConfig.ELASTIC_SEARCH_NODE_URL
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/** Elasticsearch connection is established. */
|
|
178
|
+
async establishElasticSearchConnection(args) {
|
|
179
|
+
let isConnected = false;
|
|
180
|
+
while (!isConnected) {
|
|
181
|
+
try {
|
|
182
|
+
const health = await this.elasticSearchClient.cluster.health({});
|
|
183
|
+
console.log("debug" /* DEBUG */, `${args.serviceName} Elasticsearch health status - ${health.status}`);
|
|
184
|
+
isConnected = true;
|
|
185
|
+
} catch (error) {
|
|
186
|
+
console.log("error" /* ERROR */, "Connection to Elasticsearch failed. Retrying...");
|
|
187
|
+
console.log("error" /* ERROR */, `${args.serviceName} checkConnection() method:`, error);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
var elasticSearchRepo = new ElasticsearchRepo();
|
|
193
|
+
|
|
194
|
+
// src/middlewares/gateway.middleware.ts
|
|
195
|
+
var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1);
|
|
196
|
+
function verifyGatewayRequest(serviceId) {
|
|
197
|
+
return (req, _res, next) => {
|
|
198
|
+
if (!req.headers?.gatewaytoken) {
|
|
199
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
200
|
+
}
|
|
201
|
+
const token = req.headers.gatewaytoken;
|
|
202
|
+
if (!token) {
|
|
203
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
204
|
+
}
|
|
205
|
+
const payload = import_jsonwebtoken.default.verify(token, libConfig.GATEWAY_TOKEN);
|
|
206
|
+
if (serviceId !== payload.id) {
|
|
207
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request payload is invalid");
|
|
208
|
+
}
|
|
209
|
+
next();
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// src/middlewares/error.middleware.ts
|
|
214
|
+
var import_http_status_codes = require("http-status-codes");
|
|
215
|
+
var logger2 = loggerRepo.getInstance(importMetaUrl);
|
|
216
|
+
function registerErrorMiddleware(app) {
|
|
217
|
+
return (err, _req, res, _next) => {
|
|
218
|
+
logger2.log("error" /* ERROR */, "GatewayService custom error", {
|
|
219
|
+
name: err.name,
|
|
220
|
+
message: err.message
|
|
221
|
+
});
|
|
222
|
+
res.status(import_http_status_codes.StatusCodes.INTERNAL_SERVER_ERROR).json({ error: err.message });
|
|
223
|
+
};
|
|
224
|
+
}
|
|
163
225
|
// Annotate the CommonJS export names for ESM import in node:
|
|
164
226
|
0 && (module.exports = {
|
|
165
227
|
LogLevelTypes,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
228
|
+
elasticSearchRepo,
|
|
229
|
+
loggerRepo,
|
|
230
|
+
registerErrorMiddleware,
|
|
231
|
+
registerSecurityMiddleware,
|
|
232
|
+
registerStandardMiddleware,
|
|
233
|
+
runServer,
|
|
234
|
+
verifyGatewayRequest
|
|
170
235
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Express } from 'express';
|
|
1
|
+
import { Express, Request, Response, NextFunction, Application } from 'express';
|
|
2
2
|
import { LogEntry } from 'winston';
|
|
3
3
|
|
|
4
|
-
declare function
|
|
4
|
+
declare function registerStandardMiddleware(app: Express): void;
|
|
5
5
|
|
|
6
|
-
declare function
|
|
6
|
+
declare function registerSecurityMiddleware(app: Express, args?: {
|
|
7
7
|
origin?: string;
|
|
8
8
|
}): void;
|
|
9
9
|
|
|
@@ -23,6 +23,22 @@ declare class LoggerRepo {
|
|
|
23
23
|
}
|
|
24
24
|
declare const loggerRepo: LoggerRepo;
|
|
25
25
|
|
|
26
|
-
declare function
|
|
26
|
+
declare function runServer(app: Express): void;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
/** Configured Elasticsearch cluster */
|
|
29
|
+
declare class ElasticsearchRepo {
|
|
30
|
+
private elasticSearchClient;
|
|
31
|
+
/** Creates an Elasticsearch client using the configured cluster URL. */
|
|
32
|
+
constructor();
|
|
33
|
+
/** Elasticsearch connection is established. */
|
|
34
|
+
establishElasticSearchConnection(args: {
|
|
35
|
+
serviceName: string;
|
|
36
|
+
}): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
declare const elasticSearchRepo: ElasticsearchRepo;
|
|
39
|
+
|
|
40
|
+
declare function verifyGatewayRequest(serviceId: string): (req: Request, _res: Response, next: NextFunction) => void;
|
|
41
|
+
|
|
42
|
+
declare function registerErrorMiddleware(app: Application): (err: Error, _req: Request, res: Response, _next: NextFunction) => void;
|
|
43
|
+
|
|
44
|
+
export { LogLevelTypes, elasticSearchRepo, loggerRepo, registerErrorMiddleware, registerSecurityMiddleware, registerStandardMiddleware, runServer, verifyGatewayRequest };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Express } from 'express';
|
|
1
|
+
import { Express, Request, Response, NextFunction, Application } from 'express';
|
|
2
2
|
import { LogEntry } from 'winston';
|
|
3
3
|
|
|
4
|
-
declare function
|
|
4
|
+
declare function registerStandardMiddleware(app: Express): void;
|
|
5
5
|
|
|
6
|
-
declare function
|
|
6
|
+
declare function registerSecurityMiddleware(app: Express, args?: {
|
|
7
7
|
origin?: string;
|
|
8
8
|
}): void;
|
|
9
9
|
|
|
@@ -23,6 +23,22 @@ declare class LoggerRepo {
|
|
|
23
23
|
}
|
|
24
24
|
declare const loggerRepo: LoggerRepo;
|
|
25
25
|
|
|
26
|
-
declare function
|
|
26
|
+
declare function runServer(app: Express): void;
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
/** Configured Elasticsearch cluster */
|
|
29
|
+
declare class ElasticsearchRepo {
|
|
30
|
+
private elasticSearchClient;
|
|
31
|
+
/** Creates an Elasticsearch client using the configured cluster URL. */
|
|
32
|
+
constructor();
|
|
33
|
+
/** Elasticsearch connection is established. */
|
|
34
|
+
establishElasticSearchConnection(args: {
|
|
35
|
+
serviceName: string;
|
|
36
|
+
}): Promise<void>;
|
|
37
|
+
}
|
|
38
|
+
declare const elasticSearchRepo: ElasticsearchRepo;
|
|
39
|
+
|
|
40
|
+
declare function verifyGatewayRequest(serviceId: string): (req: Request, _res: Response, next: NextFunction) => void;
|
|
41
|
+
|
|
42
|
+
declare function registerErrorMiddleware(app: Application): (err: Error, _req: Request, res: Response, _next: NextFunction) => void;
|
|
43
|
+
|
|
44
|
+
export { LogLevelTypes, elasticSearchRepo, loggerRepo, 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());
|
|
@@ -112,18 +105,83 @@ var LoggerRepo = class {
|
|
|
112
105
|
var loggerRepo = new LoggerRepo();
|
|
113
106
|
|
|
114
107
|
// src/processes/server.process.ts
|
|
115
|
-
|
|
108
|
+
var logger = loggerRepo.getInstance(import.meta.url);
|
|
109
|
+
function runServer(app) {
|
|
116
110
|
try {
|
|
117
111
|
app.listen(libConfig.PORT, () => {
|
|
112
|
+
logger.log("info" /* INFO */, `Gateway STARTED! -- Server listening on port ${libConfig.PORT}`);
|
|
118
113
|
});
|
|
119
114
|
} catch (error) {
|
|
115
|
+
logger.log("info" /* INFO */, "Error starting the server:", error);
|
|
120
116
|
process.exit(1);
|
|
121
117
|
}
|
|
122
118
|
}
|
|
119
|
+
|
|
120
|
+
// src/repositories/elasticsearch.repo.ts
|
|
121
|
+
import { Client } from "@elastic/elasticsearch";
|
|
122
|
+
var ElasticsearchRepo = class {
|
|
123
|
+
elasticSearchClient;
|
|
124
|
+
/** Creates an Elasticsearch client using the configured cluster URL. */
|
|
125
|
+
constructor() {
|
|
126
|
+
this.elasticSearchClient = new Client({
|
|
127
|
+
node: libConfig.ELASTIC_SEARCH_NODE_URL
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** Elasticsearch connection is established. */
|
|
131
|
+
async establishElasticSearchConnection(args) {
|
|
132
|
+
let isConnected = false;
|
|
133
|
+
while (!isConnected) {
|
|
134
|
+
try {
|
|
135
|
+
const health = await this.elasticSearchClient.cluster.health({});
|
|
136
|
+
console.log("debug" /* DEBUG */, `${args.serviceName} Elasticsearch health status - ${health.status}`);
|
|
137
|
+
isConnected = true;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.log("error" /* ERROR */, "Connection to Elasticsearch failed. Retrying...");
|
|
140
|
+
console.log("error" /* ERROR */, `${args.serviceName} checkConnection() method:`, error);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
var elasticSearchRepo = new ElasticsearchRepo();
|
|
146
|
+
|
|
147
|
+
// src/middlewares/gateway.middleware.ts
|
|
148
|
+
import jwt from "jsonwebtoken";
|
|
149
|
+
function verifyGatewayRequest(serviceId) {
|
|
150
|
+
return (req, _res, next) => {
|
|
151
|
+
if (!req.headers?.gatewaytoken) {
|
|
152
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
153
|
+
}
|
|
154
|
+
const token = req.headers.gatewaytoken;
|
|
155
|
+
if (!token) {
|
|
156
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request not coming from api gateway");
|
|
157
|
+
}
|
|
158
|
+
const payload = jwt.verify(token, libConfig.GATEWAY_TOKEN);
|
|
159
|
+
if (serviceId !== payload.id) {
|
|
160
|
+
throw new Error("Invalid request: verifyGatewayRequest() method: Request payload is invalid");
|
|
161
|
+
}
|
|
162
|
+
next();
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// src/middlewares/error.middleware.ts
|
|
167
|
+
import { StatusCodes } from "http-status-codes";
|
|
168
|
+
var logger2 = loggerRepo.getInstance(import.meta.url);
|
|
169
|
+
function registerErrorMiddleware(app) {
|
|
170
|
+
return (err, _req, res, _next) => {
|
|
171
|
+
logger2.log("error" /* ERROR */, "GatewayService custom error", {
|
|
172
|
+
name: err.name,
|
|
173
|
+
message: err.message
|
|
174
|
+
});
|
|
175
|
+
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: err.message });
|
|
176
|
+
};
|
|
177
|
+
}
|
|
123
178
|
export {
|
|
124
179
|
LogLevelTypes,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
180
|
+
elasticSearchRepo,
|
|
181
|
+
loggerRepo,
|
|
182
|
+
registerErrorMiddleware,
|
|
183
|
+
registerSecurityMiddleware,
|
|
184
|
+
registerStandardMiddleware,
|
|
185
|
+
runServer,
|
|
186
|
+
verifyGatewayRequest
|
|
129
187
|
};
|