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 #2519

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-storage/tre
| List Files | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/listFiles.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFiles.js,samples/README.md) |
| List Files By Prefix | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesByPrefix.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesByPrefix.js,samples/README.md) |
| List Files Paginate | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesPaginate.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesPaginate.js,samples/README.md) |
| List Files With Fields | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithFields.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithFields.js,samples/README.md) |
| List Files with Old Versions. | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithOldVersions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithOldVersions.js,samples/README.md) |
| List Notifications | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/listNotifications.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listNotifications.js,samples/README.md) |
| Lock Retention Policy | [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/lockRetentionPolicy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/lockRetentionPolicy.js,samples/README.md) |
Expand Down
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",
"tmp": "^0.2.0",
"typescript": "^5.1.6",
"yargs": "^17.3.1"
Expand Down
18 changes: 18 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ objects to users via direct download.
* [List Files](#list-files)
* [List Files By Prefix](#list-files-by-prefix)
* [List Files Paginate](#list-files-paginate)
* [List Files With Fields](#list-files-with-fields)
* [List Files with Old Versions.](#list-files-with-old-versions.)
* [List Notifications](#list-notifications)
* [Lock Retention Policy](#lock-retention-policy)
Expand Down Expand Up @@ -1451,6 +1452,23 @@ __Usage:__



### List Files With Fields

View the [source code](https://github.com/googleapis/nodejs-storage/blob/main/samples/listFilesWithFields.js).

[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-storage&page=editor&open_in_editor=samples/listFilesWithFields.js,samples/README.md)

__Usage:__


`node samples/listFilesWithFields.js`


-----




### List Files with Old Versions.

List Files with Old Versions.
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
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