Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

refactor(api): inline the developer category filter

Kris Oeid17d3cc529bb
diff --git a/apps/api/src/controllers/v2/search.ts b/apps/api/src/controllers/v2/search.ts
index a66318367..39b53f093 100644
--- a/apps/api/src/controllers/v2/search.ts
+++ b/apps/api/src/controllers/v2/search.ts
@@ -39,11 +39,7 @@ import {
checkKeyFormatRestriction,
formatTypesOf,
} from "../../lib/key-restriction";
-import {
- isCategoryStringArray,
- stripDeveloperCategory,
- wantsDeveloperCategory,
-} from "../../search/developer";
+import { wantsDeveloperCategory } from "../../search/developer";
import { requestOrigin } from "../../lib/request-origin";
export async function searchController(
@@ -91,20 +87,15 @@ export async function searchController(
logger.info(
"developer category requested without developerBeta flag; dropping",
);
- // Narrow first: req.body.categories is a union of two homogeneous
- // arrays (all-string or all-object), and calling the generic helper
- // directly on that union collapses T to a mixed type that no longer
- // matches either branch on assignment back. Branching on the guard
- // lets each call infer T concretely, so both assignments typecheck
- // with no cast.
- const categories = req.body.categories;
- if (categories !== undefined) {
- if (isCategoryStringArray(categories)) {
- req.body.categories = stripDeveloperCategory(categories);
- } else {
- req.body.categories = stripDeveloperCategory(categories);
- }
- }
+ // filter() widens the union that categories is declared as (either an
+ // all-string or an all-object array), so the result is cast back to
+ // assign it.
+ req.body.categories = (req.body.categories as CategoryOption[]).filter(
+ category =>
+ typeof category === "string"
+ ? category !== "developer"
+ : category.type !== "developer",
+ ) as typeof req.body.categories;
}
}
diff --git a/apps/api/src/search/developer.test.ts b/apps/api/src/search/developer.test.ts
index e2f808fc0..3a8b94281 100644
--- a/apps/api/src/search/developer.test.ts
+++ b/apps/api/src/search/developer.test.ts
@@ -8,11 +8,7 @@ vi.mock("../config", () => ({
}));
import { fetch } from "undici";
-import {
- searchDeveloperCategory,
- stripDeveloperCategory,
- wantsDeveloperCategory,
-} from "./developer";
+import { searchDeveloperCategory, wantsDeveloperCategory } from "./developer";
const fetchMock = vi.mocked(fetch);
@@ -153,23 +149,3 @@ describe("searchDeveloperCategory", () => {
).resolves.toEqual([]);
});
});
-
-describe("stripDeveloperCategory", () => {
- it("removes both the string form and the object form", () => {
- expect(
- stripDeveloperCategory(["github", "developer"] as any),
- ).toEqual(["github"]);
- expect(
- stripDeveloperCategory([{ type: "github" }, { type: "developer" }] as any),
- ).toEqual([{ type: "github" }]);
- });
-
- it("passes undefined through and leaves unrelated categories alone", () => {
- expect(stripDeveloperCategory(undefined)).toBeUndefined();
- expect(stripDeveloperCategory(["github"] as any)).toEqual(["github"]);
- });
-
- it("returns an empty array when developer was the only category", () => {
- expect(stripDeveloperCategory(["developer"] as any)).toEqual([]);
- });
-});
diff --git a/apps/api/src/search/developer.ts b/apps/api/src/search/developer.ts
index 928936ce9..fac7ceef7 100644
--- a/apps/api/src/search/developer.ts
+++ b/apps/api/src/search/developer.ts
@@ -13,39 +13,6 @@ export function wantsDeveloperCategory(categories?: CategoryOption[]): boolean {
);
}
-/**
- * Removes the developer category from a normalized category list. Used by the
- * developerBeta gate: an unentitled team's search proceeds without it rather
- * than erroring.
- */
-export function stripDeveloperCategory<T extends CategoryOption>(
- categories?: T[],
-): T[] | undefined {
- if (categories === undefined) {
- return undefined;
- }
- return categories.filter(category =>
- typeof category === "string"
- ? category !== "developer"
- : category.type !== "developer",
- );
-}
-
-/**
- * The zod-inferred category type on SearchRequest is a union of two
- * *homogeneous* arrays (all-string or all-object), not a single array of a
- * union type. TypeScript's generic inference collapses that union to a mixed
- * `(string | CategoryInput)[]` when `stripDeveloperCategory` is called
- * directly on it, which no longer matches either branch on the way back out.
- * Narrowing with this predicate first lets each call site infer `T` as the
- * concrete branch, so the round trip typechecks with no cast.
- */
-export function isCategoryStringArray(
- categories: CategoryOption[],
-): categories is string[] {
- return categories.every(category => typeof category === "string");
-}
-
export async function searchDeveloperCategory(
options: { query: string; limit: number; teamId: string; timeout: number },
logger: Logger,