@nimee/initialize-ms 0.0.2 → 0.0.4
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/src/fastify.d.ts +5 -0
- package/dist/src/fastify.js +17 -1
- package/dist/src/fastify.js.map +1 -1
- package/dist/src/index.js +7 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/mongooseConnection.js +3 -3
- package/dist/src/mongooseConnection.js.map +1 -1
- package/package.json +9 -6
- package/src/fastify.ts +25 -2
- package/src/index.ts +7 -1
- package/src/mongooseConnection.ts +1 -1
- package/nodemon.json +0 -6
package/dist/src/fastify.d.ts
CHANGED
package/dist/src/fastify.js
CHANGED
|
@@ -14,15 +14,31 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const fastify_1 = __importDefault(require("fastify"));
|
|
16
16
|
const logger_1 = __importDefault(require("@nimee/logger"));
|
|
17
|
+
const request_context_1 = __importDefault(require("@fastify/request-context"));
|
|
18
|
+
const uuid_1 = require("uuid");
|
|
17
19
|
class ServerInitializerFastify {
|
|
18
20
|
initialize(serviceName) {
|
|
19
21
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
22
|
try {
|
|
21
|
-
this.server = (0, fastify_1.default)({
|
|
23
|
+
this.server = (0, fastify_1.default)({
|
|
24
|
+
logger: logger_1.default,
|
|
25
|
+
requestIdLogLabel: "reqId",
|
|
26
|
+
genReqId(req) {
|
|
27
|
+
// you get access to the req here if you need it - must be a synchronous function
|
|
28
|
+
return (0, uuid_1.v4)();
|
|
29
|
+
},
|
|
30
|
+
});
|
|
22
31
|
this.server.register(require("@fastify/cors"), {
|
|
23
32
|
origin: ["http://localhost:8080", "http://127.0.0.1:8080", "http://localhost:3000"],
|
|
24
33
|
methods: ["GET", "PUT", "PATCH", "POST", "DELETE"],
|
|
25
34
|
});
|
|
35
|
+
this.server.register(request_context_1.default, {
|
|
36
|
+
hook: "preValidation",
|
|
37
|
+
});
|
|
38
|
+
// this.server.addHook("preHandler", async (request: any) => {
|
|
39
|
+
// requestContext.set("user", request.user);
|
|
40
|
+
// logger.info(`1user:${requestContext.get("user")}`);
|
|
41
|
+
// });
|
|
26
42
|
return this.server;
|
|
27
43
|
}
|
|
28
44
|
catch (err) {
|
package/dist/src/fastify.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fastify.js","sourceRoot":"","sources":["../../src/fastify.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"fastify.js","sourceRoot":"","sources":["../../src/fastify.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,sDAAkD;AAClD,2DAAmC;AACnC,+EAAmE;AACnE,+BAAkC;AAQlC,MAAM,wBAAwB;IAEtB,UAAU,CAAC,WAAoB;;YACnC,IAAI;gBACF,IAAI,CAAC,MAAM,GAAG,IAAA,iBAAO,EAAC;oBACpB,MAAM,EAAN,gBAAM;oBACN,iBAAiB,EAAE,OAAO;oBAC1B,QAAQ,CAAC,GAAmB;wBAC1B,iFAAiF;wBACjF,OAAO,IAAA,SAAI,GAAE,CAAC;oBAChB,CAAC;iBACF,CAAC,CAAC;gBAEH,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE;oBAC7C,MAAM,EAAE,CAAC,uBAAuB,EAAE,uBAAuB,EAAE,uBAAuB,CAAC;oBACnF,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC;iBACnD,CAAC,CAAC;gBACH,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,yBAA2B,EAAE;oBAChD,IAAI,EAAE,eAAe;iBACtB,CAAC,CAAC;gBACH,8DAA8D;gBAC9D,8CAA8C;gBAC9C,wDAAwD;gBACxD,MAAM;gBAEN,OAAO,IAAI,CAAC,MAAM,CAAC;aACpB;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAC3B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACjB;QACH,CAAC;KAAA;IACK,MAAM,CAAC,IAAa;;YACxB,IAAI;gBACF,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,IAAI,IAAI,EAAE,CAAC,CAAC;gBACjD,OAAO,IAAI,CAAC,MAAM,CAAC;aACpB;YAAC,OAAO,GAAG,EAAE;gBACZ,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;gBAC3B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACjB;QACH,CAAC;KAAA;CACF;AAED,kBAAe,wBAAwB,CAAC"}
|
package/dist/src/index.js
CHANGED
|
@@ -14,6 +14,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const fastify_1 = __importDefault(require("./fastify"));
|
|
16
16
|
const logger_1 = __importDefault(require("@nimee/logger"));
|
|
17
|
+
const error_handler_1 = require("@nimee/error-handler");
|
|
18
|
+
const health_1 = __importDefault(require("@nimee/health"));
|
|
19
|
+
const metrics_1 = require("@nimee/metrics");
|
|
17
20
|
class AppInitializer {
|
|
18
21
|
initialize(server, serverType, serviceName, entryPointsInitializer, connectionString, dbConnection, blackFeatureList, port) {
|
|
19
22
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -21,6 +24,7 @@ class AppInitializer {
|
|
|
21
24
|
let fastify;
|
|
22
25
|
switch (serverType) {
|
|
23
26
|
case "fastify": {
|
|
27
|
+
logger_1.default.info("Initializing Fastify now!");
|
|
24
28
|
fastify = new fastify_1.default();
|
|
25
29
|
this.server = yield fastify.initialize(serviceName);
|
|
26
30
|
break;
|
|
@@ -35,6 +39,9 @@ class AppInitializer {
|
|
|
35
39
|
if (!blackFeatureList.includes("redis")) {
|
|
36
40
|
// await redisService.initRedisConnection(this.serviceName, `${this.serviceName}_`);
|
|
37
41
|
}
|
|
42
|
+
error_handler_1.errorHandler.registerAndHandleAllErrors(this.server);
|
|
43
|
+
yield new health_1.default(this.server, undefined).start(this.serviceName);
|
|
44
|
+
metrics_1.defaultMetricsExporter.collectDefaultMetricsAndExport(this.server, "/metrics", 2000, process.env.logLevel);
|
|
38
45
|
if (entryPointsInitializer) {
|
|
39
46
|
const allEntryPointInitializers = [entryPointsInitializer];
|
|
40
47
|
allEntryPointInitializers.forEach((entryPointInitializer) => {
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,wDAAiD;AACjD,2DAAmC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,wDAAiD;AACjD,2DAAmC;AACnC,wDAAoD;AACpD,2DAA0C;AAC1C,4CAAwD;AACxD,MAAM,cAAc;IAKZ,UAAU,CACd,MAAW,EACX,UAAkB,EAClB,WAAmB,EACnB,sBAA2B,EAC3B,gBAAwB,EACxB,YAAiB,EACjB,gBAA+B,EAC/B,IAAY;;YAEZ,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;YAC/B,IAAI,OAAO,CAAC;YACZ,QAAQ,UAAU,EAAE;gBAClB,KAAK,SAAS,CAAC,CAAC;oBACd,gBAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;oBACzC,OAAO,GAAG,IAAI,iBAAwB,EAAE,CAAC;oBACzC,IAAI,CAAC,MAAM,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC;oBACpD,MAAM;iBACP;gBACD,KAAK,SAAS,CAAC,C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
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const
|
|
15
|
+
const error_handler_1 = require("@nimee/error-handler");
|
|
16
16
|
const mongoose_1 = __importDefault(require("mongoose"));
|
|
17
17
|
const logger_1 = __importDefault(require("@nimee/logger"));
|
|
18
18
|
let connectionIntance;
|
|
@@ -31,13 +31,13 @@ class MongooseConnection {
|
|
|
31
31
|
return this.db;
|
|
32
32
|
}
|
|
33
33
|
yield mongoose_1.default.connect(connectionString, { autoIndex: true, autoCreate: true }).catch(function (error) {
|
|
34
|
-
|
|
34
|
+
error_handler_1.errorHandler.handleError(error);
|
|
35
35
|
});
|
|
36
36
|
logger_1.default.info(`Connected successfully to mongo server from ${serviceName}`);
|
|
37
37
|
this.db = mongoose_1.default.connection;
|
|
38
38
|
}
|
|
39
39
|
catch (error) {
|
|
40
|
-
throw new
|
|
40
|
+
throw new error_handler_1.CustomError(`${serviceName}_mongoose_connection_failed`, 400, `connectToDb from ${serviceName} with error ${error.message}`, error);
|
|
41
41
|
}
|
|
42
42
|
});
|
|
43
43
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongooseConnection.js","sourceRoot":"","sources":["../../src/mongooseConnection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"mongooseConnection.js","sourceRoot":"","sources":["../../src/mongooseConnection.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,wDAAiE;AACjE,wDAAgD;AAChD,2DAAmC;AACnC,IAAI,iBAAsB,CAAC;AAE3B,MAAM,kBAAkB;IAEtB,MAAM,CAAC,kBAAkB;QACvB,IAAI,CAAC,iBAAiB,EAAE;YACtB,iBAAiB,GAAG,IAAI,kBAAkB,EAAE,CAAC;SAC9C;QAED,OAAO,iBAAiB,CAAC;IAC3B,CAAC;IACK,WAAW,CAAC,gBAAwB,EAAE,WAAmB;;YAC7D,IAAI;gBACF,IAAI,IAAI,CAAC,EAAE,EAAE;oBACX,gBAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;oBACvC,OAAO,IAAI,CAAC,EAAE,CAAC;iBAChB;gBAED,MAAM,kBAAQ,CAAC,OAAO,CAAC,gBAAgB,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,UAAU,KAAK;oBACnG,4BAAY,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;gBAClC,CAAC,CAAC,CAAC;gBACH,gBAAM,CAAC,IAAI,CAAC,+CAA+C,WAAW,EAAE,CAAC,CAAC;gBAE1E,IAAI,CAAC,EAAE,GAAG,kBAAQ,CAAC,UAAU,CAAC;aAC/B;YAAC,OAAO,KAAK,EAAE;gBACd,MAAM,IAAI,2BAAW,CACnB,GAAG,WAAW,6BAA6B,EAC3C,GAAG,EACH,oBAAoB,WAAW,eAAe,KAAK,CAAC,OAAO,EAAE,EAC7D,KAAK,CACN,CAAC;aACH;QACH,CAAC;KAAA;CACF;AACD,kBAAe,kBAAkB,CAAC,kBAAkB,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nimee/initialize-ms",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "init behavior for each ms",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"author": "dan goldberg",
|
|
@@ -10,10 +10,11 @@
|
|
|
10
10
|
"watch-ts": "tsc -w",
|
|
11
11
|
"build": "npm run build-ts",
|
|
12
12
|
"lint": "eslint -c ../../.eslintrc.js --ext .ts src/**",
|
|
13
|
-
"
|
|
14
|
-
"
|
|
13
|
+
"prettier-format": "prettier --config .prettierrc 'src/**/*.ts' --write",
|
|
14
|
+
"publish-it": "npm run build && npm publish --scope=nimee --access public"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
+
"@types/uuid": "^9.0.0",
|
|
17
18
|
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
18
19
|
"@typescript-eslint/eslint-plugin-tslint": "^5.18.0",
|
|
19
20
|
"@typescript-eslint/parser": "^5.18.0",
|
|
@@ -21,14 +22,16 @@
|
|
|
21
22
|
"eslint-plugin-jsdoc": "^38.1.6",
|
|
22
23
|
"eslint-plugin-no-null": "^1.0.2",
|
|
23
24
|
"nodemon": "^1.19.1",
|
|
24
|
-
"typescript": "4.
|
|
25
|
+
"typescript": "^4.8.3"
|
|
25
26
|
},
|
|
26
27
|
"types": "dist/src/index.d.ts",
|
|
27
28
|
"dependencies": {
|
|
28
29
|
"@fastify/cors": "^8.1.1",
|
|
30
|
+
"@fastify/request-context": "^4.2.0",
|
|
29
31
|
"@fastify/swagger": "^8.1.0",
|
|
30
|
-
"@nimee/
|
|
32
|
+
"@nimee/error-handler": "0.0.3",
|
|
33
|
+
"@nimee/logger": "1.0.21",
|
|
31
34
|
"fastify": "^4.6.0",
|
|
32
35
|
"mongoose": "6.2.10"
|
|
33
36
|
}
|
|
34
|
-
}
|
|
37
|
+
}
|
package/src/fastify.ts
CHANGED
|
@@ -1,15 +1,38 @@
|
|
|
1
|
-
import Fastify, {
|
|
1
|
+
import Fastify, { FastifyRequest } from "fastify";
|
|
2
2
|
import logger from "@nimee/logger";
|
|
3
|
+
import fastifyRequestContextPlugin from "@fastify/request-context";
|
|
4
|
+
import { v4 as uuid } from "uuid";
|
|
5
|
+
|
|
6
|
+
declare module "@fastify/request-context" {
|
|
7
|
+
interface RequestContextData {
|
|
8
|
+
foo: string;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
3
12
|
class ServerInitializerFastify {
|
|
4
13
|
server: any;
|
|
5
14
|
async initialize(serviceName?: string) {
|
|
6
15
|
try {
|
|
7
|
-
this.server = Fastify({
|
|
16
|
+
this.server = Fastify({
|
|
17
|
+
logger,
|
|
18
|
+
requestIdLogLabel: "reqId",
|
|
19
|
+
genReqId(req: FastifyRequest) {
|
|
20
|
+
// you get access to the req here if you need it - must be a synchronous function
|
|
21
|
+
return uuid();
|
|
22
|
+
},
|
|
23
|
+
});
|
|
8
24
|
|
|
9
25
|
this.server.register(require("@fastify/cors"), {
|
|
10
26
|
origin: ["http://localhost:8080", "http://127.0.0.1:8080", "http://localhost:3000"],
|
|
11
27
|
methods: ["GET", "PUT", "PATCH", "POST", "DELETE"],
|
|
12
28
|
});
|
|
29
|
+
this.server.register(fastifyRequestContextPlugin, {
|
|
30
|
+
hook: "preValidation",
|
|
31
|
+
});
|
|
32
|
+
// this.server.addHook("preHandler", async (request: any) => {
|
|
33
|
+
// requestContext.set("user", request.user);
|
|
34
|
+
// logger.info(`1user:${requestContext.get("user")}`);
|
|
35
|
+
// });
|
|
13
36
|
|
|
14
37
|
return this.server;
|
|
15
38
|
} catch (err) {
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import ServerInitializerFastify from "./fastify";
|
|
2
2
|
import logger from "@nimee/logger";
|
|
3
|
-
|
|
3
|
+
import { errorHandler } from "@nimee/error-handler";
|
|
4
|
+
import HealthService from "@nimee/health";
|
|
5
|
+
import { defaultMetricsExporter } from "@nimee/metrics";
|
|
4
6
|
class AppInitializer {
|
|
5
7
|
public httpServer: any;
|
|
6
8
|
public port: number;
|
|
@@ -20,6 +22,7 @@ class AppInitializer {
|
|
|
20
22
|
let fastify;
|
|
21
23
|
switch (serverType) {
|
|
22
24
|
case "fastify": {
|
|
25
|
+
logger.info("Initializing Fastify now!");
|
|
23
26
|
fastify = new ServerInitializerFastify();
|
|
24
27
|
this.server = await fastify.initialize(serviceName);
|
|
25
28
|
break;
|
|
@@ -33,6 +36,9 @@ class AppInitializer {
|
|
|
33
36
|
if (!blackFeatureList.includes("redis")) {
|
|
34
37
|
// await redisService.initRedisConnection(this.serviceName, `${this.serviceName}_`);
|
|
35
38
|
}
|
|
39
|
+
errorHandler.registerAndHandleAllErrors(this.server);
|
|
40
|
+
await new HealthService(this.server, undefined).start(this.serviceName);
|
|
41
|
+
defaultMetricsExporter.collectDefaultMetricsAndExport(this.server, "/metrics", 2000, process.env.logLevel);
|
|
36
42
|
|
|
37
43
|
if (entryPointsInitializer) {
|
|
38
44
|
const allEntryPointInitializers = [entryPointsInitializer];
|