@defra/grants-config-utils 1.3.2 → 1.4.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
+ ## 1.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 6229239: Allow listing bucket files to accept a prefix. Fix bug when no files found, to not throw exception
8
+
3
9
  ## 1.3.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": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "NPM module for providing utils for publishing grants config",
5
5
  "engines": {
6
6
  "node": ">=24",
@@ -43,18 +43,19 @@ export const listFiles = async (logger, prefix) => {
43
43
  return result.Contents ?? [];
44
44
  };
45
45
 
46
- export const listAllFiles = async (logger) => {
46
+ export const listAllFiles = async (logger, prefix) => {
47
47
  const client = initialiseClient();
48
- const paginator = paginateListObjectsV2(
49
- { client },
50
- {
51
- Bucket: bucketName,
52
- },
53
- );
48
+ const input = {
49
+ Bucket: bucketName,
50
+ ...(prefix && { Prefix: prefix }),
51
+ };
52
+ const paginator = paginateListObjectsV2({ client }, input);
54
53
 
55
54
  const objectKeys = [];
56
55
  for await (const { Contents } of paginator) {
57
- objectKeys.push(...Contents.map((obj) => ({ Key: obj.Key })));
56
+ if (Contents) {
57
+ objectKeys.push(...Contents.map((obj) => ({ Key: obj.Key })));
58
+ }
58
59
  }
59
60
  logger.info(`Found ${objectKeys.length ?? 0} files in bucket ${bucketName}`);
60
61
  return objectKeys;
@@ -296,6 +296,74 @@ describe("s3-interactions", () => {
296
296
  );
297
297
  });
298
298
 
299
+ it("should return an empty array if Contents is undefined", async () => {
300
+ const mockListResponse = {
301
+ Contents: undefined,
302
+ IsTruncated: false,
303
+ };
304
+
305
+ mockS3Client.send.mockResolvedValueOnce(mockListResponse);
306
+
307
+ const result = await listAllFiles(mockLogger);
308
+
309
+ expect(result).toEqual([]);
310
+ expect(mockLogger.info).toHaveBeenCalledWith(
311
+ `Found 0 files in bucket ${defaultBucketName}`,
312
+ );
313
+ });
314
+
315
+ it("should include Prefix in paginateListObjectsV2 call when prefix is provided", async () => {
316
+ const prefix = "configs/subfolder";
317
+ const mockListResponse = {
318
+ Contents: [{ Key: "configs/subfolder/file.json" }],
319
+ IsTruncated: false,
320
+ };
321
+
322
+ mockS3Client.send.mockResolvedValueOnce(mockListResponse);
323
+
324
+ const result = await listAllFiles(mockLogger, prefix);
325
+
326
+ expect(mockS3Client.send).toHaveBeenCalledWith(
327
+ expect.objectContaining({
328
+ input: {
329
+ Bucket: defaultBucketName,
330
+ Prefix: prefix,
331
+ },
332
+ }),
333
+ );
334
+ expect(result).toEqual([{ Key: "configs/subfolder/file.json" }]);
335
+ expect(mockLogger.info).toHaveBeenCalledWith(
336
+ `Found 1 files in bucket ${defaultBucketName}`,
337
+ );
338
+ });
339
+
340
+ it("should not include Prefix in paginateListObjectsV2 call when prefix is not provided", async () => {
341
+ const mockListResponse = {
342
+ Contents: [{ Key: "file.json" }],
343
+ IsTruncated: false,
344
+ };
345
+
346
+ mockS3Client.send.mockResolvedValueOnce(mockListResponse);
347
+
348
+ const result = await listAllFiles(mockLogger);
349
+
350
+ expect(mockS3Client.send).toHaveBeenCalledWith(
351
+ expect.objectContaining({
352
+ input: {
353
+ Bucket: defaultBucketName,
354
+ },
355
+ }),
356
+ );
357
+ expect(mockS3Client.send).not.toHaveBeenCalledWith(
358
+ expect.objectContaining({
359
+ input: expect.objectContaining({
360
+ Prefix: expect.anything(),
361
+ }),
362
+ }),
363
+ );
364
+ expect(result).toEqual([{ Key: "file.json" }]);
365
+ });
366
+
299
367
  it("should throw an error if listing fails", async () => {
300
368
  const mockError = new Error("List all failed");
301
369