Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add support for 'fields' query parameter to getFiles #2521

Merged
merged 13 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@
"pack-n-play": "^2.0.0",
"proxyquire": "^2.1.3",
"sinon": "^18.0.0",
"nise": "6.0.0",
"path-to-regexp": "6.2.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just add a tacking issue to remove this once we upgrade to Node 18 minimum so we don't forget.

"tmp": "^0.2.0",
"typescript": "^5.1.6",
"yargs": "^17.3.1"
Expand Down
6 changes: 6 additions & 0 deletions src/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export interface GetFilesOptions {
startOffset?: string;
userProject?: string;
versions?: boolean;
fields?: string;
}

export interface CombineOptions extends PreconditionOptions {
Expand Down Expand Up @@ -2825,6 +2826,11 @@ class Bucket extends ServiceObject<Bucket, BucketMetadata> {
const files = itemsArray.map((file: FileMetadata) => {
const options = {} as FileOptions;

if (query.fields) {
const fileInstance = file;
return fileInstance;
}

if (query.versions) {
options.generation = file.generation;
}
Expand Down
2 changes: 1 addition & 1 deletion system-test/common.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We adjusted the test placement to address the previous failures.

Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ describe('Common', () => {
assert(err?.message.includes('ECONNREFUSED'));
const timeResponse = Date.now();
assert(timeResponse - timeRequest > minExpectedResponseTime);
done();
}
);
done();
});
});
});
14 changes: 14 additions & 0 deletions system-test/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3249,6 +3249,20 @@ describe('storage', function () {
assert.strictEqual(files!.length, NEW_FILES.length);
});

it('returns file name only when fields is set as name', async () => {
const expected = [
{name: 'CloudLogo1'},
{name: 'CloudLogo2'},
{name: 'CloudLogo3'},
{name: `${DIRECTORY_NAME}/CloudLogo4`},
{name: `${DIRECTORY_NAME}/CloudLogo5`},
{name: `${DIRECTORY_NAME}/inner/CloudLogo6`},
];
const [files] = await bucket.getFiles({fields: 'items(name)'});

assert.deepStrictEqual(files, expected);
});

it('returns folders as prefixes when includeFoldersAsPrefixes is set', async () => {
const expected = [`${DIRECTORY_NAME}/`];
const [, , result] = await bucket.getFiles({
Expand Down
20 changes: 20 additions & 0 deletions test/bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,26 @@ describe('Bucket', () => {
});
});

it('should return Files with specified values if queried for fields', done => {
bucket.request = (
reqOpts: DecorateRequestOptions,
callback: Function
) => {
callback(null, {
items: [{name: 'fake-file-name'}],
});
};

bucket.getFiles(
{fields: 'items(name)'},
(err: Error, files: FakeFile[]) => {
assert.ifError(err);
assert.strictEqual(files[0].name, 'fake-file-name');
done();
}
);
});

it('should return soft-deleted Files if queried for softDeleted', done => {
const softDeletedTime = new Date('1/1/2024').toISOString();
bucket.request = (
Expand Down
Loading