From d7764ccbddc61c190b33e40a556f69259cd729a7 Mon Sep 17 00:00:00 2001 From: typicalninja <65993466+typicalninja@users.noreply.github.com> Date: Sat, 26 Jul 2025 14:53:35 +0530 Subject: [PATCH 1/3] refactor: make Organic result description optional Some google results do not have the description field populated --- packages/google-sr/src/results/organic.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/google-sr/src/results/organic.ts b/packages/google-sr/src/results/organic.ts index 9301939..6147ddf 100644 --- a/packages/google-sr/src/results/organic.ts +++ b/packages/google-sr/src/results/organic.ts @@ -1,4 +1,3 @@ -// Importing the CSS Selectors from google-sr-selectors import { GeneralSelector, OrganicSearchSelector } from "google-sr-selectors"; import { type ResultParser, @@ -15,7 +14,7 @@ import { export interface OrganicResultNode extends SearchResultNodeLike { type: typeof ResultTypes.OrganicResult; title: string; - description: string; + description?: string; link: string; source: string; isAd: boolean; @@ -46,7 +45,7 @@ export const OrganicResult: ResultParser = ( // Check if the user has called the function directly // Most likely, they have passed the result of calling the function instead of the function itself if (!$) throwNoCheerioError("OrganicResult"); - // + const parsedResults: PartialExceptType[] = []; const organicSearchBlocks = $(GeneralSelector.block).get(); @@ -64,11 +63,6 @@ export const OrganicResult: ResultParser = ( ); if (noPartialResults && !link) continue; - const description = coerceToStringOrUndefined( - $el.find(OrganicSearchSelector.description).text(), - ); - if (noPartialResults && !description) continue; - const title = coerceToStringOrUndefined( $el.find(OrganicSearchSelector.title).text(), ); @@ -86,6 +80,11 @@ export const OrganicResult: ResultParser = ( metaContainer.find(OrganicSearchSelector.metaAd).text(), ); + // Some result do not have the description + const description = coerceToStringOrUndefined( + $el.find(OrganicSearchSelector.description).text(), + ); + parsedResults.push({ type: ResultTypes.OrganicResult, link: link, From 61d3c3a4c0b9047dcdf9abd4f9e9406edff5375a Mon Sep 17 00:00:00 2001 From: typicalninja <65993466+typicalninja@users.noreply.github.com> Date: Sat, 26 Jul 2025 16:13:27 +0530 Subject: [PATCH 2/3] test: make Organic result description optional and add validation for isAd --- packages/google-sr/tests/results.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/google-sr/tests/results.test.ts b/packages/google-sr/tests/results.test.ts index 754d097..3735d97 100644 --- a/packages/google-sr/tests/results.test.ts +++ b/packages/google-sr/tests/results.test.ts @@ -58,8 +58,11 @@ describe( for (const res of results) { expect(res.type).toBe(ResultTypes.OrganicResult); expect(res.link).to.be.a("string").and.not.empty; - expect(res.description).to.be.a("string").and.not.empty; + // description is optional, check if its a string or undefined + expect(["string", "undefined"]).toContain(typeof res.description); + expect(res.source).to.be.a("string").and.not.empty; expect(res.title).to.be.a("string").and.not.empty; + expect(res.isAd).to.be.a("boolean"); } }); From df988cc79b3bc3f4a500a4ff182a6a2b3c70fbc1 Mon Sep 17 00:00:00 2001 From: typicalninja <65993466+typicalninja@users.noreply.github.com> Date: Sat, 26 Jul 2025 16:21:16 +0530 Subject: [PATCH 3/3] chore: add changeset --- .changeset/tame-schools-kick.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/tame-schools-kick.md diff --git a/.changeset/tame-schools-kick.md b/.changeset/tame-schools-kick.md new file mode 100644 index 0000000..88a9a49 --- /dev/null +++ b/.changeset/tame-schools-kick.md @@ -0,0 +1,7 @@ +--- +"google-sr": major +--- + +Make OrganicResult description field optional + +The `description` field in `OrganicResultNode` is now optional (`string | undefined`) to handle cases where search results don't include a description. This is a breaking change as existing code may need to be updated to handle the undefined case. \ No newline at end of file