feat(developer-knowledge): add Developer Knowledge API samples - #4424
feat(developer-knowledge): add Developer Knowledge API samples#4424llatif wants to merge 5 commits into
Conversation
Add standalone code samples and test suites for Developer Knowledge API: - searchDocumentChunks - getDocument - batchGetDocuments - answerQuery
|
Here is the summary of changes. You are about to add 4 region tags.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces Node.js code samples and tests for the Google Developer Knowledge API, including scripts for answering queries, retrieving documents, batch-fetching documents, and searching document chunks. The review feedback highlights potential TypeError issues across several files (answerQuery.js, getDocument.js, and searchDocumentChunks.js) where properties or methods are accessed on potentially undefined or null fields (such as response.answer, document.content, and chunk.content). It is recommended to use optional chaining or fallback values to ensure robust error handling.
| console.log(`Answer:\n${response.answer.answerText}\n`); | ||
| const citationsCount = response.answer.citations | ||
| ? response.answer.citations.length | ||
| : 0; | ||
| const referencesCount = response.answer.references | ||
| ? response.answer.references.length | ||
| : 0; |
There was a problem hiding this comment.
If response.answer is undefined or null (for example, if no answer could be generated for the query), accessing response.answer.answerText or its other properties directly will throw a TypeError. Use optional chaining or default values to safely handle cases where the answer is missing.
| console.log(`Answer:\n${response.answer.answerText}\n`); | |
| const citationsCount = response.answer.citations | |
| ? response.answer.citations.length | |
| : 0; | |
| const referencesCount = response.answer.references | |
| ? response.answer.references.length | |
| : 0; | |
| const answerText = response.answer?.answerText || ''; | |
| console.log('Answer:\n' + answerText + '\n'); | |
| const citationsCount = response.answer?.citations?.length || 0; | |
| const referencesCount = response.answer?.references?.length || 0; |
| console.log(`URI: ${document.uri}`); | ||
| console.log(`Data Source: ${document.dataSource}`); | ||
| console.log(`Content Length: ${document.contentLengthBytes} bytes`); | ||
| console.log(`Content Preview: ${document.content.substring(0, 150)}...\n`); |
There was a problem hiding this comment.
If document.content is undefined or null, calling .substring() on it will throw a TypeError. Use a fallback empty string or optional chaining to safely handle missing content.
| console.log(`Content Preview: ${document.content.substring(0, 150)}...\n`); | |
| const contentPreview = (document.content || '').substring(0, 150); | |
| console.log('Content Preview: ' + contentPreview + '...\n'); |
| for (const chunk of chunks) { | ||
| console.log(`Parent Document: ${chunk.parent}`); | ||
| console.log(`Chunk ID: ${chunk.id}`); | ||
| console.log(`Content Preview: ${chunk.content.substring(0, 100)}...\n`); |
There was a problem hiding this comment.
Description
Adds Node.js code samples for the Developer Knowledge API using the
@google/developer-knowledgeclient library:answerQuery.js(developerknowledge_answer_query) - Answers queries against a developer knowledge corpus.getDocument.js(developerknowledge_get_document) - Retrieves a document by resource name.batchGetDocuments.js(developerknowledge_batch_get_documents) - Retrieves multiple documents in a single request.searchDocumentChunks.js(developerknowledge_search_document_chunks) - Searches document chunks with pagination limit.Includes comprehensive Mocha unit tests (
test/samples.test.js), ESLint/Prettier configuration with license headers, package metadata, README documentation, and addeddeveloper-knowledgetoCODEOWNERS.Checklist
npm test(see Testing)npm run lint(see Style)developerknowledge.googleapis.com)GoogleCloudPlatform/nodejs-docs-samples. Not a fork.