The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Centralize partner search validation and remove mock provider
Rafi5b30e4ccb93b
diff --git a/apps/web/lib/api/partners/search/build-search-query.ts b/apps/web/lib/api/partners/search/build-search-query.tsindex 632fb96134..f945ab4795 100644--- a/apps/web/lib/api/partners/search/build-search-query.ts+++ b/apps/web/lib/api/partners/search/build-search-query.ts@@ -1,7 +1,9 @@import { getPartnersQuerySchemaExtended } from "@/lib/zod/schemas/partners";import * as z from "zod/v4";-import { PARTNER_SEARCH_CANDIDATE_LIMIT } from "./constants";-import { PartnerSearchCandidateQuery } from "./types";+import {+ PARTNER_SEARCH_CANDIDATE_LIMIT,+ PartnerSearchCandidateQuery,+} from "./types";export type PartnerSearchQueryInput = z.infer<typeof getPartnersQuerySchemaExtendeddiff --git a/apps/web/lib/api/partners/search/constants.ts b/apps/web/lib/api/partners/search/constants.tsdeleted file mode 100644index 417e2bb4f1..0000000000--- a/apps/web/lib/api/partners/search/constants.ts+++ /dev/null@@ -1,13 +0,0 @@-export const PARTNER_SEARCH_CANDIDATE_LIMIT = 100;--export function validatePartnerSearchCandidateLimit(limit: number) {- if (- !Number.isSafeInteger(limit) ||- limit <= 0 ||- limit > PARTNER_SEARCH_CANDIDATE_LIMIT- ) {- throw new Error(- `Partner search candidate limit must be between 1 and ${PARTNER_SEARCH_CANDIDATE_LIMIT}.`,- );- }-}diff --git a/apps/web/lib/api/partners/search/index.ts b/apps/web/lib/api/partners/search/index.tsindex 21101a1bbd..bf35146f04 100644--- a/apps/web/lib/api/partners/search/index.ts+++ b/apps/web/lib/api/partners/search/index.ts@@ -1,9 +1,7 @@export * from "./backfill";export * from "./build-search-query";-export * from "./constants";export * from "./order-search-results";export * from "./provider";-export * from "./providers/mock";export * from "./providers/upstash-redis";export * from "./providers/upstash-search";export * from "./searchable-values";diff --git a/apps/web/lib/api/partners/search/providers/mock.ts b/apps/web/lib/api/partners/search/providers/mock.tsdeleted file mode 100644index d1c1282ada..0000000000--- a/apps/web/lib/api/partners/search/providers/mock.ts+++ /dev/null@@ -1,72 +0,0 @@-import { validatePartnerSearchCandidateLimit } from "../constants";-import {- getPartnerSearchableValues,- normalizePartnerSearchQuery,-} from "../searchable-values";-import {- PartnerSearchCandidateQuery,- PartnerSearchDocument,- PartnerSearchProvider,-} from "../types";--function findMatchingDocuments(- documents: Iterable<PartnerSearchDocument>,- { programId, query }: PartnerSearchCandidateQuery,-): PartnerSearchDocument[] {- const normalizedQuery = normalizePartnerSearchQuery(query);-- return Array.from(documents).filter(- (document) =>- document.programId === programId &&- getPartnerSearchableValues(document).some((value) =>- normalizePartnerSearchQuery(value).includes(normalizedQuery),- ),- );-}--/**- * In-memory search used for unit tests and API plumbing. It is not- * intended for production or performance testing.- */-export function createMockPartnerSearchProvider(- initialDocuments: PartnerSearchDocument[] = [],-): PartnerSearchProvider {- const documents = new Map(- initialDocuments.map((document) => [document.id, document]),- );-- return {- async searchCandidates({- programId,- query,- limit,- }: PartnerSearchCandidateQuery) {- validatePartnerSearchCandidateLimit(limit);- const matches = findMatchingDocuments(documents.values(), {- programId,- query,- limit,- });-- return {- hits: matches.slice(0, limit).map((document) => ({- id: document.id,- })),- };- },-- async waitForIndexing() {},-- async upsert(updatedDocuments) {- for (const document of updatedDocuments) {- documents.set(document.id, document);- }- },-- async delete(documentIds) {- for (const documentId of documentIds) {- documents.delete(documentId);- }- },- };-}diff --git a/apps/web/lib/api/partners/search/providers/upstash-redis.ts b/apps/web/lib/api/partners/search/providers/upstash-redis.tsindex c736ba1729..0836292279 100644--- a/apps/web/lib/api/partners/search/providers/upstash-redis.ts+++ b/apps/web/lib/api/partners/search/providers/upstash-redis.ts@@ -5,7 +5,6 @@ import {type InferFilterFromSchema,type SearchIndex,} from "@upstash/redis";-import { validatePartnerSearchCandidateLimit } from "../constants";import {getPartnerSearchableValues,normalizePartnerSearchQuery,@@ -15,6 +14,7 @@ import type {PartnerSearchDocument,PartnerSearchProvider,} from "../types";+import { validatePartnerSearchCandidateLimit } from "../types";const DEFAULT_INDEX_NAME = "partner-search-v1";const TRANSIENT_RETRY_ATTEMPTS = 2;diff --git a/apps/web/lib/api/partners/search/providers/upstash-search.ts b/apps/web/lib/api/partners/search/providers/upstash-search.tsindex fb7a600564..5f036dfeb1 100644--- a/apps/web/lib/api/partners/search/providers/upstash-search.ts+++ b/apps/web/lib/api/partners/search/providers/upstash-search.ts@@ -1,12 +1,12 @@import { chunk } from "@dub/utils";import { Search } from "@upstash/search";-import { validatePartnerSearchCandidateLimit } from "../constants";import { normalizePartnerSearchQuery } from "../searchable-values";import type {PartnerSearchCandidateQuery,PartnerSearchDocument,PartnerSearchProvider,} from "../types";+import { validatePartnerSearchCandidateLimit } from "../types";const DEFAULT_INDEX_NAME = "partner-search-v1";const WRITE_BATCH_SIZE = 100;diff --git a/apps/web/lib/api/partners/search/types.ts b/apps/web/lib/api/partners/search/types.tsindex 41a63b67a4..2ed88da1e2 100644--- a/apps/web/lib/api/partners/search/types.ts+++ b/apps/web/lib/api/partners/search/types.ts@@ -1,5 +1,19 @@import { PlatformType } from "@prisma/client";+export const PARTNER_SEARCH_CANDIDATE_LIMIT = 100;++export function validatePartnerSearchCandidateLimit(limit: number) {+ if (+ !Number.isSafeInteger(limit) ||+ limit <= 0 ||+ limit > PARTNER_SEARCH_CANDIDATE_LIMIT+ ) {+ throw new Error(+ `Partner search candidate limit must be between 1 and ${PARTNER_SEARCH_CANDIDATE_LIMIT}.`,+ );+ }+}+/*** Program enrollment data stored in the partner search index.* The database remains the source of truth, so every field must be rebuildable.diff --git a/apps/web/tests/partners/partner-search-provider.test.ts b/apps/web/tests/partners/partner-search-provider.test.tsdeleted file mode 100644index 993f4ab545..0000000000--- a/apps/web/tests/partners/partner-search-provider.test.ts+++ /dev/null@@ -1,86 +0,0 @@-import {- createMockPartnerSearchProvider,- PARTNER_SEARCH_CANDIDATE_LIMIT,- PartnerSearchDocument,-} from "@/lib/api/partners/search";-import { describe, expect, it } from "vitest";--const partnerDocument: PartnerSearchDocument = {- id: "pge_test",- programId: "prog_test",- partnerId: "pn_test",- name: "Rafi Hasan",- email: "[email protected]",- companyName: "Dub Partners",- description: "Developer tools educator",- platformTypes: ["website", "youtube", "twitter"],- platformIdentifiers: ["rafi.dev", "@rafi-youtube", "@rafi-on-x"],- linkDomains: ["dub.sh"],- linkKeys: ["rafi"],- shortLinks: ["https://dub.sh/rafi"],- destinationUrls: ["https://example.com/referrals/rafi"],-};--describe("partner search provider contract", () => {- it.each([- ["name", "rafi"],- ["partner ID", "pn_test"],- ["partial email", "examp"],- ["company name", "dub partners"],- ["description", "educator"],- ["platform type", "twitter"],- ["platform identifier", "rafi-on-x"],- ["link domain", "dub.sh"],- ["link key", "rafi"],- ["short link", "dub.sh/rafi"],- ["link destination", "referrals/rafi"],- ])("searches by %s", async (_field, query) => {- const provider = createMockPartnerSearchProvider([partnerDocument]);-- const result = await provider.searchCandidates({- programId: partnerDocument.programId,- query,- limit: PARTNER_SEARCH_CANDIDATE_LIMIT,- });- expect(result.hits).toEqual([{ id: partnerDocument.id }]);- });-- it("keeps search results scoped to a program", async () => {- const provider = createMockPartnerSearchProvider([partnerDocument]);-- await expect(- provider.searchCandidates({- programId: "prog_other",- query: "rafi",- limit: PARTNER_SEARCH_CANDIDATE_LIMIT,- }),- ).resolves.toEqual({ hits: [] });- });-- it("bounds relevance candidates to the shared provider limit", async () => {- const documents = Array.from(- { length: PARTNER_SEARCH_CANDIDATE_LIMIT + 1 },- (_, index) => ({- ...partnerDocument,- id: `pge_${index}`,- partnerId: `pn_${index}`,- }),- );- const provider = createMockPartnerSearchProvider(documents);-- const result = await provider.searchCandidates({- programId: partnerDocument.programId,- query: "rafi",- limit: PARTNER_SEARCH_CANDIDATE_LIMIT,- });- expect(result.hits).toHaveLength(PARTNER_SEARCH_CANDIDATE_LIMIT);- expect(result.hits[0]).toEqual(expect.objectContaining({ id: "pge_0" }));- await expect(- provider.searchCandidates({- programId: partnerDocument.programId,- query: "rafi",- limit: PARTNER_SEARCH_CANDIDATE_LIMIT + 1,- }),- ).rejects.toThrow("must be between 1 and 100");- });-});