@defra/grants-config-utils 0.1.2 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @defra/grants-config-utils
2
2
 
3
+ ## 0.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - dabf7f1: feat: Add SNS client
8
+
3
9
  ## 0.1.2
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/grants-config-utils",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "NPM module for providing utils for publishing grants config",
5
5
  "engines": {
6
6
  "node": ">=24",
@@ -31,6 +31,7 @@
31
31
  "exports": {
32
32
  ".": "./src/service/config-store-and-inform.js",
33
33
  "./s3-interactions": "./src/s3/s3-interactions.js",
34
+ "./sns-client": "./src/sns/sns-client.js",
34
35
  "./config": "./src/config/config.js",
35
36
  "./broker": "./src/broker/broker-auth-helper.js"
36
37
  },
@@ -41,6 +42,7 @@
41
42
  "homepage": "https://github.com/DEFRA/grants-config-utils#readme",
42
43
  "dependencies": {
43
44
  "@aws-sdk/client-s3": "3.1047.0",
45
+ "@aws-sdk/client-sns": "3.1047.0",
44
46
  "convict": "6.2.5",
45
47
  "convict-format-with-validator": "6.2.0"
46
48
  },
@@ -39,6 +39,14 @@ export const config = convict({
39
39
  env: "FORCE_PATH_STYLE",
40
40
  },
41
41
  },
42
+ sns: {
43
+ configVersionTopicArn: {
44
+ doc: "ARN of the SNS topic to publish new config version events",
45
+ format: String,
46
+ default: "arn:aws:sns:eu-west-2:000000000000:gfr__sns___config_version",
47
+ env: "GFR__SNS__CONFIG_VERSION_ARN",
48
+ },
49
+ },
42
50
  },
43
51
  configBroker: {
44
52
  auth: {
@@ -0,0 +1,66 @@
1
+ import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
2
+ import { config } from "../config/config.js";
3
+
4
+ let defaultTopic;
5
+ let loggerInstance;
6
+ let clientSetup = false;
7
+
8
+ let snsClient;
9
+
10
+ export const isClientSetup = () => clientSetup;
11
+
12
+ export const setupClient = (
13
+ logger,
14
+ {
15
+ region = config.get("aws.region"),
16
+ awsEndpointUrl = config.get("aws.endpointUrl"),
17
+ publishToTopic = config.get("aws.sns.configVersionTopicArn"),
18
+ } = {},
19
+ ) => {
20
+ snsClient = new SNSClient({
21
+ region,
22
+ endpoint: awsEndpointUrl,
23
+ });
24
+ loggerInstance = logger;
25
+ defaultTopic = publishToTopic;
26
+ clientSetup = true;
27
+ };
28
+
29
+ export const publishMessage = async (
30
+ data,
31
+ messageAttributes = {},
32
+ topic = defaultTopic,
33
+ ) => {
34
+ if (!clientSetup) {
35
+ throw new Error(
36
+ "SNS client not setup. Call setupClient() before publishing messages.",
37
+ );
38
+ }
39
+ loggerInstance.info(`Publish command ${topic}`);
40
+ await snsClient.send(
41
+ new PublishCommand({
42
+ TopicArn: topic,
43
+ Message: JSON.stringify(data),
44
+ MessageAttributes: convertMessageAttributes(messageAttributes),
45
+ }),
46
+ );
47
+ };
48
+
49
+ /* Converts a simple key-value object into the format required by SNS for message attributes.
50
+ Note only supports string values currently
51
+ Example input: { key1: "value1", key2: "value2" }
52
+ Example output: {
53
+ key1: { DataType: "String", StringValue: "value1" },
54
+ key2: { DataType: "String", StringValue: "value2" }
55
+ }
56
+ */
57
+ const convertMessageAttributes = (attributes) => {
58
+ const convertedAttributes = {};
59
+ for (const key in attributes) {
60
+ convertedAttributes[key] = {
61
+ DataType: "String",
62
+ StringValue: attributes[key],
63
+ };
64
+ }
65
+ return convertedAttributes;
66
+ };
@@ -0,0 +1,154 @@
1
+ import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
2
+ import { isClientSetup, publishMessage, setupClient } from "./sns-client.js";
3
+ import { config } from "../config/config.js";
4
+
5
+ vi.mock("./metrics.js");
6
+ vi.mock("@aws-sdk/client-sns", () => ({
7
+ SNSClient: vi.fn(),
8
+ PublishCommand: vi.fn(),
9
+ }));
10
+
11
+ const mockLogger = {
12
+ info: vi.fn(),
13
+ error: vi.fn(),
14
+ };
15
+
16
+ const topicArn = "arn:aws:sns:us-east-1:123456789012:MyTopic";
17
+
18
+ describe("publish", () => {
19
+ it("error thrown if client not set up", async () => {
20
+ const message = {
21
+ key: "value",
22
+ };
23
+
24
+ const send = vi.fn();
25
+
26
+ SNSClient.mockReturnValue({
27
+ send,
28
+ });
29
+
30
+ expect(isClientSetup()).toBe(false);
31
+
32
+ await expect(publishMessage(message)).rejects.toThrow(
33
+ "SNS client not setup. Call setupClient() before publishing messages.",
34
+ );
35
+
36
+ expect(PublishCommand).toHaveBeenCalledTimes(0);
37
+
38
+ expect(send).toHaveBeenCalledTimes(0);
39
+ });
40
+
41
+ it("publishes a message to a topic", async () => {
42
+ const message = {
43
+ key: "value",
44
+ };
45
+
46
+ const send = vi.fn();
47
+
48
+ SNSClient.mockImplementation(function () {
49
+ return { send };
50
+ });
51
+
52
+ PublishCommand.mockImplementation(function (params) {
53
+ return params;
54
+ });
55
+ setupClient(mockLogger, {
56
+ region: "us-east-1",
57
+ awsEndpointUrl: "http://localhost:4566",
58
+ publishToTopic: topicArn,
59
+ });
60
+
61
+ expect(isClientSetup()).toBe(true);
62
+
63
+ await publishMessage(message);
64
+
65
+ expect(PublishCommand).toHaveBeenCalledWith({
66
+ TopicArn: topicArn,
67
+ Message: '{"key":"value"}',
68
+ MessageAttributes: {},
69
+ });
70
+
71
+ expect(send).toHaveBeenCalledWith({
72
+ TopicArn: topicArn,
73
+ Message: '{"key":"value"}',
74
+ MessageAttributes: {},
75
+ });
76
+ });
77
+
78
+ it("publishes a message including custom message attribute", async () => {
79
+ const message = {
80
+ key: "value",
81
+ };
82
+
83
+ const send = vi.fn();
84
+
85
+ SNSClient.mockImplementation(function () {
86
+ return { send };
87
+ });
88
+
89
+ PublishCommand.mockImplementation(function (params) {
90
+ return params;
91
+ });
92
+ setupClient(mockLogger, {
93
+ region: "us-east-1",
94
+ awsEndpointUrl: "http://localhost:4566",
95
+ });
96
+
97
+ await publishMessage(
98
+ message,
99
+ { customAttribute: "customValue" },
100
+ "specialTopicArn",
101
+ );
102
+
103
+ expect(PublishCommand).toHaveBeenCalledWith({
104
+ TopicArn: "specialTopicArn",
105
+ Message: '{"key":"value"}',
106
+ MessageAttributes: {
107
+ customAttribute: {
108
+ DataType: "String",
109
+ StringValue: "customValue",
110
+ },
111
+ },
112
+ });
113
+
114
+ expect(send).toHaveBeenCalledWith({
115
+ TopicArn: "specialTopicArn",
116
+ Message: '{"key":"value"}',
117
+ MessageAttributes: {
118
+ customAttribute: {
119
+ DataType: "String",
120
+ StringValue: "customValue",
121
+ },
122
+ },
123
+ });
124
+ });
125
+
126
+ it("sets up client with defaults if config object is not supplied", async () => {
127
+ const send = vi.fn();
128
+ SNSClient.mockImplementation(function () {
129
+ return { send };
130
+ });
131
+
132
+ config.set("aws.region", "eu-west-1");
133
+ config.set("aws.endpointUrl", "http://localhost:4566");
134
+ config.set("aws.sns.configVersionTopicArn", "default-topic-arn");
135
+
136
+ setupClient(mockLogger);
137
+
138
+ expect(SNSClient).toHaveBeenCalledWith(
139
+ expect.objectContaining({
140
+ region: "eu-west-1",
141
+ endpoint: "http://localhost:4566",
142
+ }),
143
+ );
144
+
145
+ const message = { key: "value" };
146
+ await publishMessage(message);
147
+
148
+ expect(PublishCommand).toHaveBeenCalledWith(
149
+ expect.objectContaining({
150
+ TopicArn: "default-topic-arn",
151
+ }),
152
+ );
153
+ });
154
+ });