@diamir/nestjs-process 11.0.0

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/LICENSE ADDED
@@ -0,0 +1,6 @@
1
+ Copyright 2025 WEB&SÖHNE<backend@diamir.io>
2
+
3
+ Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4
+
5
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6
+
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ <p align="center">
2
+ <a href="https://webundsoehne.com" target="blank">
3
+ <img src="https://webundsoehne.com/wp-content/uploads/webundsoehne-logo.png" width="320" alt="Web und Söhne - Logo" />
4
+ </a>
5
+ </p>
6
+ Web & Söhne is Austria's leading expert in programming and implementing complex and large web projects.
7
+
8
+ ---
9
+
10
+ # nestjs-process
11
+
12
+ <!-- TOC -->
13
+ * [nestjs-process](#nestjs-process)
14
+ * [SetApiInfoHeaderMiddleware](#setapiinfoheadermiddleware)
15
+ * [setEnvironmentVariables](#setenvironmentvariables)
16
+ * [requireNodeEnv](#requirenodeenv)
17
+ * [registerExitListeners](#registerexitlisteners)
18
+ <!-- TOC -->
19
+
20
+ ## SetApiInfoHeaderMiddleware
21
+
22
+ NestJs middleware to set
23
+
24
+ - `X-Api-Name`: read from `process.env?.PACKAGE_NAME`
25
+ - `X-Api-Version`: rad from `process.env?.PACKAGE_VERSION`
26
+
27
+ response header. (To be used with [setEnvironmentVariables](#setenvironmentvariables))
28
+
29
+ ## setEnvironmentVariables
30
+
31
+ Writes additional env-vars to the process:
32
+ - `process.env.PACKAGE_NAME`
33
+ - `process.env.PACKAGE_VERSION`
34
+
35
+ Sets the `process.title` to the Package-Name and -Version from `npm_` env vars or reading from the `package.json`
36
+
37
+ ## requireNodeEnv
38
+
39
+ Throw error if `process.env.NODE_ENV` is not set
40
+
41
+ ## registerExitListeners
42
+
43
+ Register `SIGTERM` and `SIGINT` listener
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@diamir/nestjs-process",
3
+ "version": "11.0.0",
4
+ "description": "",
5
+ "author": "Backend <backend@diamir.io>",
6
+ "repository": "https://github.com/tailoredmedia/backend-nx-skeleton",
7
+ "license": "ISC",
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "keywords": [],
12
+ "main": "./src/index.js",
13
+ "dependencies": {},
14
+ "peerDependencies": {
15
+ "@nestjs/common": ">= 11.0.0"
16
+ },
17
+ "peerDependenciesMeta": {},
18
+ "engines": {
19
+ "node": ">= 20"
20
+ },
21
+ "types": "./src/index.d.ts",
22
+ "type": "commonjs"
23
+ }
@@ -0,0 +1,2 @@
1
+ export declare function setEnvironmentVariables(packageFile?: string): Promise<void>;
2
+ export declare function requireNodeEnv(errorMessage?: string): void;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setEnvironmentVariables = setEnvironmentVariables;
4
+ exports.requireNodeEnv = requireNodeEnv;
5
+ const promises_1 = require("node:fs/promises");
6
+ const node_path_1 = require("node:path");
7
+ async function setEnvironmentVariables(packageFile = (0, node_path_1.join)(process.cwd(), 'package.json')) {
8
+ const name = process.env.PACKAGE_NAME ??
9
+ process.env.npm_package_name ??
10
+ ((0, node_path_1.basename)(process.env.PWD ?? '').length ? (0, node_path_1.basename)(process.env.PWD) : undefined);
11
+ const version = process.env.PACKAGE_VERSION ?? process.env.npm_package_version;
12
+ let packageJson;
13
+ if (!(name && version)) {
14
+ packageJson = JSON.parse(await (0, promises_1.readFile)(packageFile, { encoding: 'utf8' }));
15
+ }
16
+ process.env.PACKAGE_NAME = name ?? packageJson?.name ?? 'nestjs';
17
+ process.env.PACKAGE_VERSION = version ?? packageJson?.version ?? '0.0.0';
18
+ process.title = `${process.env.PACKAGE_NAME}-${process.env.PACKAGE_VERSION}`;
19
+ }
20
+ function requireNodeEnv(errorMessage = 'NODE_ENV environment variable is not set.') {
21
+ if (!process.env.NODE_ENV) {
22
+ throw new Error(errorMessage);
23
+ }
24
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './environment';
2
+ export * from './info-header';
3
+ export * from './process';
package/src/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./environment"), exports);
18
+ __exportStar(require("./info-header"), exports);
19
+ __exportStar(require("./process"), exports);
@@ -0,0 +1,5 @@
1
+ import type { NestMiddleware, OnModuleInit } from '@nestjs/common';
2
+ export declare class SetApiInfoHeaderMiddleware implements NestMiddleware, OnModuleInit {
3
+ onModuleInit(): Promise<void>;
4
+ use(_req: never, res: any, next: () => unknown): void;
5
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SetApiInfoHeaderMiddleware = void 0;
4
+ const environment_1 = require("./environment");
5
+ class SetApiInfoHeaderMiddleware {
6
+ async onModuleInit() {
7
+ await (0, environment_1.setEnvironmentVariables)();
8
+ }
9
+ use(_req, res, next) {
10
+ res.setHeader('X-Api-Name', process.env?.PACKAGE_NAME);
11
+ res.setHeader('X-Api-Version', process.env?.PACKAGE_VERSION ?? process.env?.npm_package_version ?? '0.0.0');
12
+ next();
13
+ }
14
+ }
15
+ exports.SetApiInfoHeaderMiddleware = SetApiInfoHeaderMiddleware;
@@ -0,0 +1 @@
1
+ export declare function registerExitListeners(name?: string): void;
package/src/process.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerExitListeners = registerExitListeners;
4
+ const common_1 = require("@nestjs/common");
5
+ function registerExitListeners(name = 'module') {
6
+ const logger = new common_1.Logger('ExitListener');
7
+ function getListener(sig) {
8
+ return () => {
9
+ logger.verbose(`---- ${name} exiting via ${sig} ----`);
10
+ process.exit();
11
+ };
12
+ }
13
+ process.on('SIGTERM', getListener('SIGTERM'));
14
+ process.on('SIGINT', getListener('SIGINT'));
15
+ process.on('exit', (code) => {
16
+ logger.log(`++++ ${name} exiting with code ${code} ++++`);
17
+ });
18
+ logger.debug('Exit listeners registered.');
19
+ }