Skip to content

fix(retail): add region tag for nodejs - Update search with query expansion #4119

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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

'use strict';

// [START retail_search_for_products_with_query_expansion_specification]
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Now that this code block is being exposed as a sample, it would be beneficial to refactor it for clarity and to align with common JavaScript best practices.

Here are a few suggestions:

  1. Hoist require statements: The require('@google-cloud/retail') call on line 19 should be at the top level of the module, outside the main function. This is standard Node.js practice for better readability and dependency management.

  2. Simplify function structure: The main function defines and immediately calls an inner async function callSearch. This wrapper is redundant and can be removed by moving its logic directly into main, simplifying the control flow.

  3. Improve error handling: The process.on('unhandledRejection', ...) can be replaced with a more explicit .catch() on the main() call. This would also allow the error handling logic to be moved outside the sample's region tags, which is a common and recommended pattern for samples.

Here is an example of a refactored structure:

'use strict';

const {SearchServiceClient} = require('@google-cloud/retail');

// [START retail_search_for_products_with_query_expansion_specification]
async function main() {
  const retailClient = new SearchServiceClient();
  // ... all logic from callSearch ...
}
// [END retail_search_for_products_with_query_expansion_specification]

main().catch(err => {
  console.error(err.message);
  process.exitCode = 1;
});

These changes would significantly improve the readability and maintainability of the sample.


async function main() {
// Imports the Google Cloud client library.
const {SearchServiceClient} = require('@google-cloud/retail');
Expand Down Expand Up @@ -82,3 +84,5 @@ process.on('unhandledRejection', err => {
});

main();

// [END retail_search_for_products_with_query_expansion_specification]
Loading