@dmptool/utils 1.0.4 → 1.0.5

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/README.md CHANGED
@@ -764,21 +764,20 @@ if (response) {
764
764
 
765
765
  This code provides a simple `getSSMParameter` function which can be used to fetch an SSM Parameter.
766
766
 
767
- Environment variable requirements:
768
- - `AWS_REGION` - The AWS region where the Lambda Function is running
769
- - `NODE_ENV` - The environment the Lambda Function is running in (e.g. `production`, `staging` or `development`)
770
-
771
767
  The code will use that value to construct the appropriate prefix for the key. For example if you are running in the AWS development environment it will use `/uc3/dmp/tool/dev/` as the prefix.
772
768
 
773
769
  ### Example usage
774
770
  ```typescript
775
- import { getSSMParameter } from '@dmptool/utils';
771
+ import { logger, EnvironmentEnum, getSSMParameter, LogLevelEnum } from '@dmptool/utils';
776
772
 
777
773
  process.env.AWS_REGION = 'us-west-2';
778
774
 
775
+ // Initialize the logger
776
+ const logger: Logger = initializeLogger('exampleSSM', LogLevelEnum.DEBUG);
777
+
779
778
  const paramName = 'RdsDatabase';
780
779
 
781
- const response = await getSSMParameter(paramName);
780
+ const response = await getSSMParameter(logger, paramName, EnvironmentEnum.DEV);
782
781
 
783
782
  if (response) {
784
783
  console.log('SSM Parameter fetched successfully', response);
package/dist/general.d.ts CHANGED
@@ -1,3 +1,11 @@
1
+ /**
2
+ * Possible environments for the application.
3
+ */
4
+ export declare enum EnvironmentEnum {
5
+ DEV = "dev",
6
+ STG = "stg",
7
+ PRD = "prd"
8
+ }
1
9
  /**
2
10
  * Convert an error to a string
3
11
  *
package/dist/general.js CHANGED
@@ -1,6 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.areEqual = exports.removeNullAndUndefinedFromObject = exports.isNullOrUndefined = exports.randomHex = exports.convertMySQLDateTimeToRFC3339 = exports.currentDateAsString = exports.normaliseHttpProtocol = exports.toErrorMessage = void 0;
3
+ exports.areEqual = exports.removeNullAndUndefinedFromObject = exports.isNullOrUndefined = exports.randomHex = exports.convertMySQLDateTimeToRFC3339 = exports.currentDateAsString = exports.normaliseHttpProtocol = exports.toErrorMessage = exports.EnvironmentEnum = void 0;
4
+ /**
5
+ * Possible environments for the application.
6
+ */
7
+ var EnvironmentEnum;
8
+ (function (EnvironmentEnum) {
9
+ EnvironmentEnum["DEV"] = "dev";
10
+ EnvironmentEnum["STG"] = "stg";
11
+ EnvironmentEnum["PRD"] = "prd";
12
+ })(EnvironmentEnum || (exports.EnvironmentEnum = EnvironmentEnum = {}));
4
13
  /**
5
14
  * Convert an error to a string
6
15
  *
package/dist/logger.d.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { Logger } from 'pino';
2
- export declare enum LogLevel {
3
- trace = "trace",
4
- debug = "debug",
5
- info = "info",
6
- warn = "warn",
7
- error = "error",
8
- fatal = "fatal"
2
+ export declare enum LogLevelEnum {
3
+ TRACE = "trace",
4
+ DEBUG = "debug",
5
+ INFO = "info",
6
+ WARN = "warn",
7
+ ERROR = "error",
8
+ FATAL = "fatal"
9
9
  }
10
10
  /**
11
11
  * Initialize a Pino logger with ECS formatting for the Lambda function.
12
12
  *
13
13
  * @param lambdaName Name of the function, used as the module name in the logs
14
- * @param logLevel Log level to use, defaults to 'info'
14
+ * @param logLevel Log level to use, defaults to 'LogLevelEnum.INFO'
15
15
  * @returns A Pino logger instance
16
16
  */
17
- export declare const initializeLogger: (lambdaName: string, logLevel: LogLevel) => Logger;
17
+ export declare const initializeLogger: (lambdaName: string, logLevel?: LogLevelEnum) => Logger;
package/dist/logger.js CHANGED
@@ -3,32 +3,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.initializeLogger = exports.LogLevel = void 0;
6
+ exports.initializeLogger = exports.LogLevelEnum = void 0;
7
7
  const pino_1 = __importDefault(require("pino"));
8
8
  const pino_lambda_1 = require("pino-lambda");
9
9
  const ecs_pino_format_1 = require("@elastic/ecs-pino-format");
10
10
  // The available log levels
11
- var LogLevel;
12
- (function (LogLevel) {
13
- LogLevel["trace"] = "trace";
14
- LogLevel["debug"] = "debug";
15
- LogLevel["info"] = "info";
16
- LogLevel["warn"] = "warn";
17
- LogLevel["error"] = "error";
18
- LogLevel["fatal"] = "fatal";
19
- })(LogLevel || (exports.LogLevel = LogLevel = {}));
11
+ var LogLevelEnum;
12
+ (function (LogLevelEnum) {
13
+ LogLevelEnum["TRACE"] = "trace";
14
+ LogLevelEnum["DEBUG"] = "debug";
15
+ LogLevelEnum["INFO"] = "info";
16
+ LogLevelEnum["WARN"] = "warn";
17
+ LogLevelEnum["ERROR"] = "error";
18
+ LogLevelEnum["FATAL"] = "fatal";
19
+ })(LogLevelEnum || (exports.LogLevelEnum = LogLevelEnum = {}));
20
20
  /**
21
21
  * Initialize a Pino logger with ECS formatting for the Lambda function.
22
22
  *
23
23
  * @param lambdaName Name of the function, used as the module name in the logs
24
- * @param logLevel Log level to use, defaults to 'info'
24
+ * @param logLevel Log level to use, defaults to 'LogLevelEnum.INFO'
25
25
  * @returns A Pino logger instance
26
26
  */
27
- const initializeLogger = (lambdaName, logLevel) => {
27
+ const initializeLogger = (lambdaName, logLevel = LogLevelEnum.INFO) => {
28
28
  const destination = (0, pino_lambda_1.pinoLambdaDestination)();
29
29
  const logger = (0, pino_1.default)(Object.assign({
30
30
  // Set the minimum log level
31
- level: logLevel || 'info' }, ecs_pino_format_1.ecsFormat), destination);
31
+ level: logLevel || LogLevelEnum.INFO }, ecs_pino_format_1.ecsFormat), destination);
32
32
  // Define a standardized module name
33
33
  logger.child({ module: lambdaName });
34
34
  return logger;
package/dist/ssm.d.ts CHANGED
@@ -1,8 +1,12 @@
1
+ import { EnvironmentEnum } from "./general";
2
+ import { Logger } from 'pino';
1
3
  /**
2
4
  * Retrieve a variable from the SSM Parameter store.
3
5
  *
6
+ * @param logger The logger to use for logging.
4
7
  * @param key The name of the variable to retrieve.
8
+ * @param env The environment to retrieve the variable from. Defaults to `EnvironmentEnum.DEV`.
5
9
  * @returns The value of the variable, or undefined if the variable could not be found.
6
10
  * @throws
7
11
  */
8
- export declare const getSSMParameter: (key: string) => Promise<string | undefined>;
12
+ export declare const getSSMParameter: (logger: Logger, key: string, env?: EnvironmentEnum) => Promise<string | undefined>;
package/dist/ssm.js CHANGED
@@ -1,32 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getSSMParameter = void 0;
4
- const client_ssm_1 = require("@aws-sdk/client-ssm");
5
4
  const general_1 = require("./general");
6
- // Create an SSM client
7
- const client = new client_ssm_1.SSMClient();
8
- const ENV = process.env.NODE_ENV === 'production'
9
- ? 'prd' : (process.env.NODE_ENV === 'staging'
10
- ? 'stg' : 'dev');
11
- const KEY_PREFIX = `/uc3/dmp/tool/${ENV}/`;
5
+ const client_ssm_1 = require("@aws-sdk/client-ssm");
12
6
  /**
13
7
  * Retrieve a variable from the SSM Parameter store.
14
8
  *
9
+ * @param logger The logger to use for logging.
15
10
  * @param key The name of the variable to retrieve.
11
+ * @param env The environment to retrieve the variable from. Defaults to `EnvironmentEnum.DEV`.
16
12
  * @returns The value of the variable, or undefined if the variable could not be found.
17
13
  * @throws
18
14
  */
19
- const getSSMParameter = async (key) => {
15
+ const getSSMParameter = async (logger, key, env = general_1.EnvironmentEnum.DEV) => {
20
16
  var _a;
21
- if (key && key.trim() !== '') {
22
- console.log(`Fetching parameter ${KEY_PREFIX}${key}`);
23
- const command = new client_ssm_1.GetParameterCommand({
24
- Name: `${KEY_PREFIX}${key}`,
25
- WithDecryption: true
26
- });
27
- const response = await client.send(command);
28
- if (!(0, general_1.isNullOrUndefined)(response) && !(0, general_1.isNullOrUndefined)(response.Parameter)) {
29
- return (_a = response.Parameter) === null || _a === void 0 ? void 0 : _a.Value;
17
+ if (logger && key && key.trim() !== '') {
18
+ // Create an SSM client
19
+ const client = new client_ssm_1.SSMClient();
20
+ const keyPrefix = `/uc3/dmp/tool/${env.toLowerCase()}/`;
21
+ logger.debug(`Fetching parameter ${keyPrefix}${key}`);
22
+ try {
23
+ const command = new client_ssm_1.GetParameterCommand({
24
+ Name: `${keyPrefix}${key}`,
25
+ WithDecryption: true
26
+ });
27
+ const response = await client.send(command);
28
+ if (!(0, general_1.isNullOrUndefined)(response) && !(0, general_1.isNullOrUndefined)(response.Parameter)) {
29
+ return (_a = response.Parameter) === null || _a === void 0 ? void 0 : _a.Value;
30
+ }
31
+ logger.warn(`Parameter ${keyPrefix}${key} not found.`);
32
+ }
33
+ catch (error) {
34
+ logger.fatal(`Error fetching parameter ${keyPrefix}${key}: ${error}`);
30
35
  }
31
36
  }
32
37
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dmptool/utils",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "Helper/Utility functions for use in the DMP Tool services. Particularly AWS tooling and maDMP serialization",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",