Skip to content
This repository was archived by the owner on Dec 20, 2025. It is now read-only.
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
7 changes: 7 additions & 0 deletions .changeset/tame-schools-kick.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 7 additions & 8 deletions packages/google-sr/src/results/organic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Importing the CSS Selectors from google-sr-selectors
import { GeneralSelector, OrganicSearchSelector } from "google-sr-selectors";
import {
type ResultParser,
Expand All @@ -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;
Expand Down Expand Up @@ -46,7 +45,7 @@ export const OrganicResult: ResultParser<OrganicResultNode> = (
// 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<OrganicResultNode>[] = [];
const organicSearchBlocks = $(GeneralSelector.block).get();

Expand All @@ -64,11 +63,6 @@ export const OrganicResult: ResultParser<OrganicResultNode> = (
);
if (noPartialResults && !link) continue;

const description = coerceToStringOrUndefined(
$el.find(OrganicSearchSelector.description).text(),
);
if (noPartialResults && !description) continue;

const title = coerceToStringOrUndefined(
$el.find(OrganicSearchSelector.title).text(),
);
Expand All @@ -86,6 +80,11 @@ export const OrganicResult: ResultParser<OrganicResultNode> = (
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,
Expand Down
5 changes: 4 additions & 1 deletion packages/google-sr/tests/results.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
});

Expand Down