@dmptool/utils 1.0.5 → 1.0.7

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.
@@ -2,46 +2,60 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.putEvent = void 0;
4
4
  const client_eventbridge_1 = require("@aws-sdk/client-eventbridge");
5
+ const general_1 = require("./general");
5
6
  /**
6
7
  * Publishes an event to EventBridge.
7
8
  *
9
+ * @param logger The logger to use for logging.
8
10
  * @param source The name of the caller (e.g. the Lambda Function or Application Function)
9
11
  * @param detailType The type of event (resources typically watch for specific types of events)
10
12
  * @param detail The payload of the event (will be accessible to the invoked resource)
11
13
  */
12
- const putEvent = async (source, detailType, details) => {
13
- var _a, _b, _c, _d, _e;
14
- const busName = process.env.EVENTBRIDGE_BUS_NAME;
15
- if (busName) {
14
+ const putEvent = async (logger, busName, source, detailType, details, region = 'us-west-2') => {
15
+ var _a, _b, _c, _d;
16
+ let errMsg = '';
17
+ if (logger && busName) {
16
18
  // Create a new EventBridge client instance
17
- const client = new client_eventbridge_1.EventBridgeClient({
18
- region: (_a = process.env.AWS_REGION) !== null && _a !== void 0 ? _a : "us-west-2",
19
- });
20
- // Publish the event
21
- const response = await client.send(new client_eventbridge_1.PutEventsCommand({
22
- Entries: [
23
- {
24
- EventBusName: busName,
25
- Detail: details ? JSON.stringify(details) : undefined,
26
- DetailType: detailType,
27
- Source: source,
28
- },
29
- ],
30
- }));
31
- if (response) {
32
- const statusCode = (_c = (_b = response.$metadata) === null || _b === void 0 ? void 0 : _b.httpStatusCode) !== null && _c !== void 0 ? _c : 500;
33
- // We got a response, so return it.
34
- return {
35
- status: statusCode,
36
- message: statusCode >= 200 && statusCode <= 300 ? "Ok" : "Failure",
37
- eventId: (_e = (_d = response.Entries) === null || _d === void 0 ? void 0 : _d[0]) === null || _e === void 0 ? void 0 : _e.EventId
38
- };
19
+ const client = new client_eventbridge_1.EventBridgeClient({ region });
20
+ logger.debug({ busName, source, detailType, details }, 'Publishing event');
21
+ try {
22
+ // Publish the event
23
+ const response = await client.send(new client_eventbridge_1.PutEventsCommand({
24
+ Entries: [
25
+ {
26
+ EventBusName: busName,
27
+ Detail: details ? JSON.stringify(details) : undefined,
28
+ DetailType: detailType,
29
+ Source: source,
30
+ },
31
+ ],
32
+ }));
33
+ if (response) {
34
+ const statusCode = (_b = (_a = response.$metadata) === null || _a === void 0 ? void 0 : _a.httpStatusCode) !== null && _b !== void 0 ? _b : 500;
35
+ // We got a response, so return it.
36
+ return {
37
+ status: statusCode,
38
+ message: statusCode >= 200 && statusCode <= 300 ? "Ok" : "Failure",
39
+ eventId: (_d = (_c = response.Entries) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.EventId
40
+ };
41
+ }
42
+ else {
43
+ logger.error({ busName, source, detailType, details }, 'No response from EventBridge');
44
+ errMsg = 'No response from EventBridge';
45
+ }
39
46
  }
47
+ catch (error) {
48
+ logger.fatal({ busName, source, detailType, details, error }, 'Error publishing event');
49
+ errMsg = `Error publishing event: ${(0, general_1.toErrorMessage)(error)}`;
50
+ }
51
+ }
52
+ else {
53
+ errMsg = 'Missing logger or busName args!';
40
54
  }
41
55
  // The busName was not available or the response was undefined
42
56
  return {
43
57
  status: 500,
44
- message: `Failure: ${busName ? 'Unable to put event.' : 'Missing EVENTBRIDGE_BUS_NAME variable!'}`,
58
+ message: errMsg,
45
59
  eventId: undefined
46
60
  };
47
61
  };
package/dist/maDMP.d.ts CHANGED
@@ -1,22 +1,27 @@
1
+ import { Logger } from 'pino';
2
+ import { ConnectionParams } from './rds';
3
+ import { EnvironmentEnum } from "./general";
1
4
  import { DMPToolDMPType, DMPToolExtensionType } from "@dmptool/types";
2
5
  /**
3
6
  * Validate the specified DMP metadata record against the RDA Common Standard
4
7
  * and DMP Tool extensions schema
5
8
  *
9
+ * @param logger the logger to use for logging
6
10
  * @param dmp The DMP metadata record to validate
7
11
  * @returns the DMP metadata record if it is valid
8
12
  * @throws DMPValidationError if the record is invalid with the error message(s)
9
13
  */
10
- export declare const validateRDACommonStandard: (dmp: DMPToolDMPType) => DMPToolDMPType;
14
+ export declare const validateRDACommonStandard: (logger: Logger, dmp: DMPToolDMPType) => DMPToolDMPType;
11
15
  /**
12
16
  * Validate the specified DMP metadata record against the RDA Common Standard
13
17
  * and DMP Tool extensions schema
14
18
  *
19
+ * @param logger the logger to use for logging
15
20
  * @param dmp The DMP metadata record to validate
16
21
  * @returns the DMP metadata record if it is valid
17
22
  * @throws DMPValidationError if the record is invalid with the error message(s)
18
23
  */
19
- export declare const validateDMPToolExtensions: (dmp: DMPToolExtensionType) => DMPToolExtensionType;
24
+ export declare const validateDMPToolExtensions: (logger: Logger, dmpId: string, dmp: DMPToolExtensionType) => DMPToolExtensionType;
20
25
  /**
21
26
  *
22
27
  * Generate a JSON representation for a DMP that confirms to the RDA Common Metadata
@@ -35,7 +40,11 @@ export declare const validateDMPToolExtensions: (dmp: DMPToolExtensionType) => D
35
40
  * - The `registered` indicates whether the DMP is published/registered with DataCite/EZID
36
41
  * - The `tombstoned` indicates that it was published/registered but is now removed
37
42
  *
43
+ * @param rdsConnectionParams the connection parameters for the RDS instance
44
+ * @param applicationName the name of the application/service
45
+ * @param domainName the domain name of the application/service website
38
46
  * @param planId the ID of the plan to generate the DMP for
47
+ * @param env The environment from EnvironmentEnum (defaults to EnvironmentEnum.DEV)
39
48
  * @returns a JSON representation of the DMP
40
49
  */
41
- export declare function planToDMPCommonStandard(planId: number): Promise<DMPToolDMPType | undefined>;
50
+ export declare function planToDMPCommonStandard(rdsConnectionParams: ConnectionParams, applicationName: string, domainName: string, env: EnvironmentEnum | undefined, planId: number): Promise<DMPToolDMPType | undefined>;