From 3044d3cfb1b4a24f07fd6ec29e3d20d5818c4ca3 Mon Sep 17 00:00:00 2001 From: Denis DelGrosso <85250797+ddelgrosso1@users.noreply.github.com> Date: Thu, 7 Mar 2024 14:41:22 -0500 Subject: [PATCH] feat: add includeFoldersAsPrefixes for managed folders (#2413) --- src/bucket.ts | 9 +++++++++ system-test/storage.ts | 14 ++++++++++++++ test/bucket.ts | 19 +++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/bucket.ts b/src/bucket.ts index 319a00bd6..6596a59e6 100644 --- a/src/bucket.ts +++ b/src/bucket.ts @@ -162,6 +162,7 @@ export interface GetFilesOptions { autoPaginate?: boolean; delimiter?: string; endOffset?: string; + includeFoldersAsPrefixes?: boolean; includeTrailingDelimiter?: boolean; prefix?: string; matchGlob?: string; @@ -2608,6 +2609,10 @@ class Bucket extends ServiceObject { * @property {string} [endOffset] Filter results to objects whose names are * lexicographically before endOffset. If startOffset is also set, the objects * listed have names between startOffset (inclusive) and endOffset (exclusive). + * @property {boolean} [includeFoldersAsPrefixes] If true, includes folders and + * managed folders in the set of prefixes returned by the query. Only applicable if + * delimiter is set to / and autoPaginate is set to false. + * See: https://cloud.google.com/storage/docs/managed-folders * @property {boolean} [includeTrailingDelimiter] If true, objects that end in * exactly one instance of delimiter have their metadata included in items[] * in addition to the relevant part of the object name appearing in prefixes[]. @@ -2648,6 +2653,10 @@ class Bucket extends ServiceObject { * @param {string} [query.endOffset] Filter results to objects whose names are * lexicographically before endOffset. If startOffset is also set, the objects * listed have names between startOffset (inclusive) and endOffset (exclusive). + * @param {boolean} [query.includeFoldersAsPrefixes] If true, includes folders and + * managed folders in the set of prefixes returned by the query. Only applicable if + * delimiter is set to / and autoPaginate is set to false. + * See: https://cloud.google.com/storage/docs/managed-folders * @param {boolean} [query.includeTrailingDelimiter] If true, objects that end in * exactly one instance of delimiter have their metadata included in items[] * in addition to the relevant part of the object name appearing in prefixes[]. diff --git a/system-test/storage.ts b/system-test/storage.ts index 42c312a47..2e0d3ea70 100644 --- a/system-test/storage.ts +++ b/system-test/storage.ts @@ -3124,6 +3124,20 @@ describe('storage', function () { assert.strictEqual(files!.length, NEW_FILES.length); }); + it('returns folders as prefixes when includeFoldersAsPrefixes is set', async () => { + const expected = [`${DIRECTORY_NAME}/`]; + const [, , result] = await bucket.getFiles({ + delimiter: '/', + includeFoldersAsPrefixes: true, + autoPaginate: false, + }); + + assert.deepStrictEqual( + (result as {prefixes: string[]}).prefixes, + expected + ); + }); + it('should get files as a stream', done => { let numFilesEmitted = 0; diff --git a/test/bucket.ts b/test/bucket.ts index a9dc42af7..e2c45af28 100644 --- a/test/bucket.ts +++ b/test/bucket.ts @@ -1772,10 +1772,25 @@ describe('Bucket', () => { it('should get files with a query', done => { const token = 'next-page-token'; bucket.request = (reqOpts: DecorateRequestOptions) => { - assert.deepStrictEqual(reqOpts.qs, {maxResults: 5, pageToken: token}); + assert.deepStrictEqual(reqOpts.qs, { + maxResults: 5, + pageToken: token, + includeFoldersAsPrefixes: true, + delimiter: '/', + autoPaginate: false, + }); done(); }; - bucket.getFiles({maxResults: 5, pageToken: token}, util.noop); + bucket.getFiles( + { + maxResults: 5, + pageToken: token, + includeFoldersAsPrefixes: true, + delimiter: '/', + autoPaginate: false, + }, + util.noop + ); }); it('should return nextQuery if more results exist', () => {