@defra/grants-config-utils 0.5.0 → 0.6.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 +6 -0
- package/package.json +1 -1
- package/src/sns/sns-client.js +15 -7
- package/src/sns/sns-client.test.js +75 -0
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/sns/sns-client.js
CHANGED
|
@@ -78,20 +78,28 @@ export const publishFIFOMessage = async (
|
|
|
78
78
|
};
|
|
79
79
|
|
|
80
80
|
/* Converts a simple key-value object into the format required by SNS for message attributes.
|
|
81
|
-
Note only supports string values currently
|
|
82
|
-
Example input: { key1: "value1", key2: "value2" }
|
|
81
|
+
Note only supports string and array of strings values currently
|
|
82
|
+
Example input: { key1: "value1", key2: ["value2", "value3"] }
|
|
83
83
|
Example output: {
|
|
84
84
|
key1: { DataType: "String", StringValue: "value1" },
|
|
85
|
-
key2: { DataType: "String", StringValue: "value2" }
|
|
85
|
+
key2: { DataType: "String.Array", StringValue: '["value2","value3"]' }
|
|
86
86
|
}
|
|
87
87
|
*/
|
|
88
88
|
const convertMessageAttributes = (attributes) => {
|
|
89
89
|
const convertedAttributes = {};
|
|
90
90
|
for (const key in attributes) {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
91
|
+
const value = attributes[key];
|
|
92
|
+
if (Array.isArray(value)) {
|
|
93
|
+
convertedAttributes[key] = {
|
|
94
|
+
DataType: "String.Array",
|
|
95
|
+
StringValue: JSON.stringify(value),
|
|
96
|
+
};
|
|
97
|
+
} else {
|
|
98
|
+
convertedAttributes[key] = {
|
|
99
|
+
DataType: "String",
|
|
100
|
+
StringValue: value,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
95
103
|
}
|
|
96
104
|
return convertedAttributes;
|
|
97
105
|
};
|
|
@@ -121,6 +121,44 @@ describe("publish", () => {
|
|
|
121
121
|
});
|
|
122
122
|
});
|
|
123
123
|
|
|
124
|
+
it("publishes a FIFO message to a topic with array message attributes", async () => {
|
|
125
|
+
const message = {
|
|
126
|
+
key: "value",
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const send = vi.fn();
|
|
130
|
+
|
|
131
|
+
SNSClient.mockImplementation(function () {
|
|
132
|
+
return { send };
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
PublishCommand.mockImplementation(function (params) {
|
|
136
|
+
return params;
|
|
137
|
+
});
|
|
138
|
+
setupClient(mockLogger, {
|
|
139
|
+
region: "us-east-1",
|
|
140
|
+
awsEndpointUrl: "http://localhost:4566",
|
|
141
|
+
publishToTopic: topicArn,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
await publishFIFOMessage(message, "MY_GROUP", "MY_DEDUPLICATION_ID", {
|
|
145
|
+
customAttribute: ["value1", "value2"],
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
expect(PublishCommand).toHaveBeenCalledWith({
|
|
149
|
+
TopicArn: topicArn,
|
|
150
|
+
Message: '{"key":"value"}',
|
|
151
|
+
MessageAttributes: {
|
|
152
|
+
customAttribute: {
|
|
153
|
+
DataType: "String.Array",
|
|
154
|
+
StringValue: '["value1","value2"]',
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
MessageGroupId: "MY_GROUP",
|
|
158
|
+
MessageDeduplicationId: "MY_DEDUPLICATION_ID",
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
124
162
|
it("error thrown if missing FIFO attributes when sending FIFO message", async () => {
|
|
125
163
|
const message = {
|
|
126
164
|
key: "value",
|
|
@@ -195,6 +233,43 @@ describe("publish", () => {
|
|
|
195
233
|
});
|
|
196
234
|
});
|
|
197
235
|
|
|
236
|
+
it("publishes a message including custom message attribute with array value", async () => {
|
|
237
|
+
const message = {
|
|
238
|
+
key: "value",
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const send = vi.fn();
|
|
242
|
+
|
|
243
|
+
SNSClient.mockImplementation(function () {
|
|
244
|
+
return { send };
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
PublishCommand.mockImplementation(function (params) {
|
|
248
|
+
return params;
|
|
249
|
+
});
|
|
250
|
+
setupClient(mockLogger, {
|
|
251
|
+
region: "us-east-1",
|
|
252
|
+
awsEndpointUrl: "http://localhost:4566",
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
await publishMessage(
|
|
256
|
+
message,
|
|
257
|
+
{ customAttribute: ["value1", "value2"] },
|
|
258
|
+
"specialTopicArn",
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
expect(PublishCommand).toHaveBeenCalledWith({
|
|
262
|
+
TopicArn: "specialTopicArn",
|
|
263
|
+
Message: '{"key":"value"}',
|
|
264
|
+
MessageAttributes: {
|
|
265
|
+
customAttribute: {
|
|
266
|
+
DataType: "String.Array",
|
|
267
|
+
StringValue: '["value1","value2"]',
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
198
273
|
it("sets up client with defaults if config object is not supplied", async () => {
|
|
199
274
|
const send = vi.fn();
|
|
200
275
|
SNSClient.mockImplementation(function () {
|