fix: use backlinks history for default trends (#69)

* fix: use backlinks history for default trends

* simplify backlinks: remove filters, always use history endpoint

Remove the filter UI (status, subdomains, indirect links, exclude internal)
and hardcode defaults across the stack. Replace the conditional
timeseries_summary + timeseries_new_lost_summary fallback with a single
backlinks/history/live call for trend data. This reduces the overview from
5 parallel API calls to 3 and removes all conditional branching.

* improve charts

* fix: refresh backlinks cost docs
This commit is contained in:
Ben Senescu 2026-04-04 18:21:42 -04:00 committed by Ben Senescu
parent fdb2d453e4
commit d773105e8d
21 changed files with 135 additions and 399 deletions

View File

@ -267,7 +267,7 @@ That means you can try OpenSEO for free with the starter credit, then decide if/
### 4) Backlinks search
- Backlinks search costs about `$0.08` for a domain or `$0.04` for a page.
- Backlinks search costs about `$0.06` for a domain or `$0.04` for a page.
- Opening extra tabs like `Referring Domains` or `Top Pages` adds about `+$0.02` each.
- Exact cost can vary slightly based on returned rows and DataForSEO pricing.
@ -276,7 +276,7 @@ That means you can try OpenSEO for free with the starter credit, then decide if/
- 100 keyword research requests at the default 150 results: `$3.50`
- 100 keyword research requests at 500 results each: `$7.00`
- 100 domain overviews (200 ranked keywords each): `$4.01`
- 100 backlinks domain searches at current defaults before opening extra tabs: about `$8.38`
- 100 backlinks domain searches at current defaults before opening extra tabs: about `$6.34`
- 100 backlinks page searches at current defaults before opening extra tabs: about `$4.30`
- 100 fully explored backlinks domain searches: about `$12.98`
- 100 fully explored backlinks domain searches: about `$10.94`
- 100 fully explored backlinks page searches: about `$8.61`

View File

@ -2,7 +2,10 @@ import { existsSync, readFileSync } from "node:fs";
import process from "node:process";
import { createBacklinksService } from "@/server/features/backlinks/services/BacklinksService";
import type { BillingCustomerContext } from "@/server/billing/subscription";
import type { BacklinksLookupInput } from "@/types/schemas/backlinks";
import type {
BacklinksLookupInput,
BacklinksTargetScope,
} from "@/types/schemas/backlinks";
loadLocalEnv();
@ -84,6 +87,7 @@ async function main() {
function buildInput(cliArgs: Record<string, string>): BacklinksLookupInput {
const target = cliArgs.target;
const scope = parseScope(cliArgs.scope);
if (!target) {
printUsageAndExit("Missing target.");
}
@ -93,10 +97,7 @@ function buildInput(cliArgs: Record<string, string>): BacklinksLookupInput {
return {
target,
includeSubdomains: parseBoolean(cliArgs.subdomains, true),
includeIndirectLinks: parseBoolean(cliArgs.indirect, true),
excludeInternalBacklinks: parseBoolean(cliArgs.excludeInternal, true),
status: parseStatus(cliArgs.status),
scope,
};
}
@ -147,13 +148,12 @@ function parseBoolean(value: string | undefined, fallback: boolean) {
return value === "true";
}
function parseStatus(
function parseScope(
value: string | undefined,
): BacklinksLookupInput["status"] {
if (value === "live" || value === "lost" || value === "all") {
return value;
}
return "live";
): BacklinksTargetScope | undefined {
if (!value) return undefined;
if (value === "domain" || value === "page") return value;
printUsageAndExit(`Invalid scope: ${value}. Expected domain or page.`);
}
function parsePositiveInteger(value: string | undefined, fallback: number) {
@ -185,7 +185,7 @@ function loadLocalEnv() {
function printUsageAndExit(message: string): never {
console.error(message);
console.error(
"Usage: pnpm billing:backlinks --target=example.com --confirmLive=true [--status=live|lost|all] [--subdomains=true|false] [--indirect=true|false] [--excludeInternal=true|false] [--repeat=1] [--includeTabs=true|false] [--allowCi=true]",
"Usage: pnpm billing:backlinks --target=example.com --confirmLive=true [--scope=domain|page] [--repeat=1] [--includeTabs=true|false] [--allowCi=true]",
);
process.exit(1);
}

View File

@ -49,13 +49,20 @@ export function BacklinksTrendChart({
tickFormatter={formatChartTick}
minTickGap={24}
/>
<YAxis />
<YAxis yAxisId="left" tickFormatter={formatAxisValue} width={60} />
<YAxis
yAxisId="right"
orientation="right"
tickFormatter={formatAxisValue}
width={60}
/>
<Tooltip
formatter={formatTooltipValue}
labelFormatter={formatChartLabel}
/>
<Legend />
<Line
yAxisId="left"
type="monotone"
dataKey="backlinks"
stroke="#2563eb"
@ -64,6 +71,7 @@ export function BacklinksTrendChart({
name="Backlinks"
/>
<Line
yAxisId="right"
type="monotone"
dataKey="referringDomains"
stroke="#14b8a6"
@ -111,12 +119,20 @@ export function BacklinksNewLostChart({
tickFormatter={formatChartTick}
minTickGap={24}
/>
<YAxis />
<YAxis tickFormatter={formatAxisValue} width={60} />
<Tooltip
formatter={formatTooltipValue}
labelFormatter={formatChartLabel}
/>
<Legend />
<Line
type="monotone"
dataKey="lostBacklinks"
stroke="#ef4444"
strokeWidth={2}
dot={false}
name="Lost backlinks"
/>
<Line
type="monotone"
dataKey="newBacklinks"
@ -125,14 +141,6 @@ export function BacklinksNewLostChart({
dot={false}
name="New backlinks"
/>
<Line
type="monotone"
dataKey="lostBacklinks"
stroke="#dc2626"
strokeWidth={2}
dot={false}
name="Lost backlinks"
/>
</LineChart>
) : null}
</div>
@ -174,6 +182,13 @@ function EmptyChartState() {
);
}
function formatAxisValue(value: unknown) {
if (typeof value !== "number") return "";
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(1)}M`;
if (value >= 1_000) return `${(value / 1_000).toFixed(0)}K`;
return String(value);
}
function formatChartTick(value: unknown) {
return typeof value === "string" ? formatMonthLabel(value) : "";
}

View File

@ -129,7 +129,7 @@ function BacklinksContent({
useEffect(() => {
setFilterText("");
}, [searchState.target, searchState.status, searchState.tab]);
}, [searchState.target, searchState.tab]);
const mergedData = useMemo(
() => mergeTabData(data, referringDomains, topPages),

View File

@ -9,10 +9,7 @@ import {
import type { BacklinksSearchState } from "./backlinksPageTypes";
import { resolveBacklinksSearchScope } from "./backlinksSearchScope";
type SearchDraft = Pick<
BacklinksSearchState,
"target" | "scope" | "subdomains" | "indirect" | "excludeInternal" | "status"
>;
type SearchDraft = Pick<BacklinksSearchState, "target" | "scope">;
function getBacklinksValidationErrors(
value: SearchDraft,
@ -44,7 +41,6 @@ export function BacklinksSearchCard({
isFetching: boolean;
onSubmit: (values: SearchDraft) => void;
}) {
const [showAdvanced, setShowAdvanced] = useState(false);
const [userSelectedScope, setUserSelectedScope] = useState(false);
const form = useForm({
defaultValues: initialValues,
@ -93,7 +89,7 @@ export function BacklinksSearchCard({
return (
<label
className={`input input-bordered lg:col-span-8 flex items-center gap-2 ${targetError ? "input-error" : ""}`}
className={`input input-bordered lg:col-span-10 flex items-center gap-2 ${targetError ? "input-error" : ""}`}
>
<Search className="size-4 text-base-content/60" />
<input
@ -119,28 +115,6 @@ export function BacklinksSearchCard({
}}
</form.Field>
<form.Field name="status">
{(field) => (
<select
className="select select-bordered lg:col-span-2"
value={field.state.value}
onChange={(event) =>
field.handleChange(
event.target.value === "lost"
? "lost"
: event.target.value === "all"
? "all"
: "live",
)
}
>
<option value="live">Live links</option>
<option value="lost">Lost links</option>
<option value="all">All links</option>
</select>
)}
</form.Field>
<form.Subscribe selector={(state) => state.isSubmitting}>
{(isSubmitting) => (
<button
@ -193,70 +167,6 @@ export function BacklinksSearchCard({
</form.Field>
</div>
</div>
<div className="flex flex-wrap items-center gap-3">
<form.Field name="subdomains">
{(field) => (
<label className="label cursor-pointer gap-2 py-0">
<input
type="checkbox"
className="checkbox checkbox-sm"
checked={field.state.value}
onChange={(event) =>
field.handleChange(event.target.checked)
}
/>
<span className="label-text">Include subdomains</span>
</label>
)}
</form.Field>
<button
type="button"
className="btn btn-ghost btn-sm"
onClick={() => setShowAdvanced((current) => !current)}
>
{showAdvanced ? "Hide advanced" : "Show advanced"}
</button>
</div>
{showAdvanced ? (
<div className="grid grid-cols-1 gap-3 rounded-xl border border-base-300 bg-base-200/40 p-4 text-sm md:grid-cols-2">
<form.Field name="indirect">
{(field) => (
<label className="label cursor-pointer justify-start gap-3 py-0">
<input
type="checkbox"
className="checkbox checkbox-sm"
checked={field.state.value}
onChange={(event) =>
field.handleChange(event.target.checked)
}
/>
<span className="label-text">Include indirect links</span>
</label>
)}
</form.Field>
<form.Field name="excludeInternal">
{(field) => (
<label className="label cursor-pointer justify-start gap-3 py-0">
<input
type="checkbox"
className="checkbox checkbox-sm"
checked={field.state.value}
onChange={(event) =>
field.handleChange(event.target.checked)
}
/>
<span className="label-text">
Exclude internal backlinks
</span>
</label>
)}
</form.Field>
</div>
) : null}
</form>
{errorMessage ? (

View File

@ -1,5 +1,4 @@
import type {
BacklinksStatus,
BacklinksTab,
BacklinksTargetScope,
} from "@/types/schemas/backlinks";
@ -26,10 +25,6 @@ export type BacklinksTopPagesData = Awaited<
export type BacklinksSearchState = {
target: string;
scope: BacklinksTargetScope;
subdomains: boolean;
indirect: boolean;
excludeInternal: boolean;
status: BacklinksStatus;
tab: BacklinksTab;
};

View File

@ -60,19 +60,8 @@ export function useBacklinksPageData({
() => ({
target: searchState.target,
scope: searchState.scope,
subdomains: searchState.subdomains,
indirect: searchState.indirect,
excludeInternal: searchState.excludeInternal,
status: searchState.status,
}),
[
searchState.excludeInternal,
searchState.indirect,
searchState.scope,
searchState.status,
searchState.subdomains,
searchState.target,
],
[searchState.scope, searchState.target],
);
const testAccessMutation = useMutation({
@ -86,10 +75,6 @@ export function useBacklinksPageData({
projectId,
searchState.scope,
searchState.target,
searchState.subdomains,
searchState.indirect,
searchState.excludeInternal,
searchState.status,
] as const;
const overviewQuery = useQuery({
@ -166,25 +151,13 @@ export function useBacklinksPageData({
export function navigateToBacklinksSearch(
navigate: BacklinksPageProps["navigate"],
values: Pick<
BacklinksSearchState,
| "target"
| "scope"
| "subdomains"
| "indirect"
| "excludeInternal"
| "status"
>,
values: Pick<BacklinksSearchState, "target" | "scope">,
) {
navigate({
search: (prev) => ({
...prev,
target: values.target,
scope: getPersistedBacklinksSearchScope(values.target, values.scope),
subdomains: values.subdomains ? undefined : false,
indirect: values.indirect ? undefined : false,
excludeInternal: values.excludeInternal ? undefined : false,
status: values.status === "live" ? undefined : values.status,
tab: undefined,
}),
replace: true,
@ -212,10 +185,6 @@ function buildBacklinksRequestInput(
projectId,
target: searchState.target,
scope: searchState.scope,
includeSubdomains: searchState.subdomains,
includeIndirectLinks: searchState.indirect,
excludeInternalBacklinks: searchState.excludeInternal,
status: searchState.status,
};
}

View File

@ -11,15 +11,7 @@ export const Route = createFileRoute("/_project/p/$projectId/backlinks")({
function BacklinksRoute() {
const { projectId } = Route.useParams();
const navigate = useNavigate({ from: Route.fullPath });
const {
target = "",
scope: rawScope,
subdomains = true,
indirect = true,
excludeInternal = true,
status = "live",
tab = "backlinks",
} = Route.useSearch();
const { target = "", scope: rawScope, tab = "backlinks" } = Route.useSearch();
const scope = rawScope ?? inferBacklinksSearchScopeFromTarget(target);
return (
@ -29,10 +21,6 @@ function BacklinksRoute() {
searchState={{
target,
scope,
subdomains,
indirect,
excludeInternal,
status,
tab,
}}
/>

View File

@ -4,8 +4,7 @@ const backlinksSummaryMock = vi.fn();
const backlinksRowsMock = vi.fn();
const referringDomainsMock = vi.fn();
const domainPagesMock = vi.fn();
const timeseriesSummaryMock = vi.fn();
const newLostTimeseriesMock = vi.fn();
const backlinksHistoryMock = vi.fn();
vi.mock("@/server/lib/r2-cache", () => ({
buildCacheKey: vi.fn(
@ -27,8 +26,7 @@ vi.mock("@/server/lib/dataforseoClient", () => ({
rows: backlinksRowsMock,
referringDomains: referringDomainsMock,
domainPages: domainPagesMock,
timeseriesSummary: timeseriesSummaryMock,
newLostTimeseries: newLostTimeseriesMock,
history: backlinksHistoryMock,
},
})),
}));
@ -98,17 +96,12 @@ it("profiles only the initial overview calls and reuses cache on repeat", async
rel_attributes: ["noopener"],
},
]);
timeseriesSummaryMock.mockResolvedValue([
backlinksHistoryMock.mockResolvedValue([
{
date: "2026-02-01",
backlinks: 1100,
referring_domains: 300,
rank: 40,
},
]);
newLostTimeseriesMock.mockResolvedValue([
{
date: "2026-02-01",
new_backlinks: 20,
lost_backlinks: 5,
new_referring_domains: 3,
@ -117,23 +110,11 @@ it("profiles only the initial overview calls and reuses cache on repeat", async
]);
const first = await service.profileOverview(
{
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
{ target: "example.com" },
billingCustomer,
);
const second = await service.profileOverview(
{
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
{ target: "example.com" },
billingCustomer,
);
@ -142,6 +123,7 @@ it("profiles only the initial overview calls and reuses cache on repeat", async
expect(referringDomainsMock).not.toHaveBeenCalled();
expect(domainPagesMock).not.toHaveBeenCalled();
expect(backlinksSummaryMock).toHaveBeenCalledOnce();
expect(backlinksHistoryMock).toHaveBeenCalledOnce();
expect(second).toEqual(first);
});
@ -175,23 +157,11 @@ it("profiles referring domains and top pages separately", async () => {
]);
const domains = await service.profileReferringDomains(
{
target: "https://example.com/foo",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
{ target: "https://example.com/foo" },
billingCustomer,
);
const pages = await service.profileTopPages(
{
target: "https://example.com/foo",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
{ target: "https://example.com/foo" },
billingCustomer,
);
@ -221,13 +191,7 @@ it("does not fall back to target spam score for referring domains", async () =>
]);
const domains = await service.profileReferringDomains(
{
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
},
{ target: "example.com" },
billingCustomer,
);
@ -256,16 +220,9 @@ it("keeps cache entries isolated per organization", async () => {
lost_referring_domains: 2,
});
backlinksRowsMock.mockResolvedValue([]);
timeseriesSummaryMock.mockResolvedValue([]);
newLostTimeseriesMock.mockResolvedValue([]);
backlinksHistoryMock.mockResolvedValue([]);
const input = {
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live" as const,
};
const input = { target: "example.com" };
await service.profileOverview(input, billingCustomer);
await service.profileOverview(input, {

View File

@ -67,10 +67,6 @@ async function buildOverviewCacheKey(
organizationId: billingCustomer.organizationId,
target: normalizedTarget.apiTarget,
scope: normalizedTarget.scope,
includeSubdomains: input.includeSubdomains,
includeIndirectLinks: input.includeIndirectLinks,
excludeInternalBacklinks: input.excludeInternalBacklinks,
status: input.status,
});
}
@ -86,10 +82,6 @@ async function buildTabCacheKey(
organizationId: billingCustomer.organizationId,
target: normalizedTarget.apiTarget,
scope: normalizedTarget.scope,
includeSubdomains: input.includeSubdomains,
includeIndirectLinks: input.includeIndirectLinks,
excludeInternalBacklinks: input.excludeInternalBacklinks,
status: input.status,
});
}

View File

@ -57,10 +57,6 @@ export const backlinksOverviewSchema = z.object({
target: z.string(),
displayTarget: z.string(),
scope: z.enum(["domain", "page"]),
includeSubdomains: z.boolean(),
includeIndirectLinks: z.boolean(),
excludeInternalBacklinks: z.boolean(),
status: z.enum(["live", "lost", "all"]),
summary: z.object({
rank: z.number().nullable(),
backlinks: z.number().nullable(),

View File

@ -2,12 +2,11 @@ import { z } from "zod";
import type { BillingCustomerContext } from "@/server/billing/subscription";
import {
type BacklinksRequest,
type fetchBacklinksHistoryRaw,
type fetchBacklinksRowsRaw,
type fetchBacklinksSummaryRaw,
type fetchDomainPagesSummaryRaw,
type fetchNewLostTimeseriesRaw,
type fetchReferringDomainsRaw,
type fetchTimeseriesSummaryRaw,
normalizeBacklinksTarget,
} from "@/server/lib/dataforseoBacklinks";
import { createDataforseoClient } from "@/server/lib/dataforseoClient";
@ -74,28 +73,26 @@ export async function profileBacklinksOverview(
const normalizedTarget = normalizeBacklinksTarget(input.target, {
scope: input.scope,
});
const request = buildBacklinksRequest(input, normalizedTarget.apiTarget);
const request = buildBacklinksRequest(normalizedTarget.apiTarget);
const dateRange = buildBacklinksDateRange(now);
const [summary, backlinks, trends, newLostTrends] = await Promise.all([
const [summary, backlinks, history] = await Promise.all([
dataforseo.backlinks.summary(request),
dataforseo.backlinks.rows({ ...request, limit: 100 }),
normalizedTarget.scope === "domain"
? dataforseo.backlinks.timeseriesSummary({ ...request, ...dateRange })
: Promise.resolve([]),
normalizedTarget.scope === "domain"
? dataforseo.backlinks.newLostTimeseries({ ...request, ...dateRange })
? dataforseo.backlinks.history({
target: normalizedTarget.apiTarget,
...dateRange,
})
: Promise.resolve([]),
]);
const overview = buildOverviewResult({
input,
normalizedTarget,
now,
summary,
backlinks,
trends,
newLostTrends,
history,
});
await cacheValue(
cache,
@ -124,7 +121,6 @@ export async function profileReferringDomainsRows(
const dataforseo = createDataforseoClient(billingCustomer);
const request = buildBacklinksRequest(
input,
normalizeBacklinksTarget(input.target, { scope: input.scope }).apiTarget,
);
const response = await dataforseo.backlinks.referringDomains({
@ -155,7 +151,6 @@ export async function profileTopPagesRows(
const dataforseo = createDataforseoClient(billingCustomer);
const request = buildBacklinksRequest(
input,
normalizeBacklinksTarget(input.target, { scope: input.scope }).apiTarget,
);
const response = await dataforseo.backlinks.domainPages({
@ -169,17 +164,8 @@ export async function profileTopPagesRows(
return { rows };
}
function buildBacklinksRequest(
input: BacklinksLookupInput,
target: string,
): BacklinksRequest {
return {
target,
includeSubdomains: input.includeSubdomains,
includeIndirectLinks: input.includeIndirectLinks,
excludeInternalBacklinks: input.excludeInternalBacklinks,
status: input.status,
};
function buildBacklinksRequest(target: string): BacklinksRequest {
return { target };
}
function buildBacklinksDateRange(now: Date): BacklinksDateRange {
@ -199,22 +185,37 @@ function buildBacklinksDateRange(now: Date): BacklinksDateRange {
}
function buildOverviewResult(args: {
input: BacklinksLookupInput;
normalizedTarget: ReturnType<typeof normalizeBacklinksTarget>;
now: Date;
summary: Awaited<ReturnType<typeof fetchBacklinksSummaryRaw>>["data"];
backlinks: Awaited<ReturnType<typeof fetchBacklinksRowsRaw>>["data"];
trends: Awaited<ReturnType<typeof fetchTimeseriesSummaryRaw>>["data"];
newLostTrends: Awaited<ReturnType<typeof fetchNewLostTimeseriesRaw>>["data"];
history: Awaited<ReturnType<typeof fetchBacklinksHistoryRaw>>["data"];
}): BacklinksOverviewResult {
const historyRows = args.history
.map((item) => ({
date: normalizeHistoryDate(item.date),
backlinks: item.backlinks ?? null,
referringDomains: item.referring_domains ?? null,
rank: item.rank ?? null,
newBacklinks: item.new_backlinks ?? null,
lostBacklinks: item.lost_backlinks ?? null,
newReferringDomains:
item.new_referring_domains ?? item.new_reffering_domains ?? null,
lostReferringDomains:
item.lost_referring_domains ?? item.lost_reffering_domains ?? null,
}))
.filter(
(
item,
): item is typeof item & {
date: string;
} => item.date !== null,
);
return {
target: args.normalizedTarget.apiTarget,
displayTarget: args.normalizedTarget.displayTarget,
scope: args.normalizedTarget.scope,
includeSubdomains: args.input.includeSubdomains,
includeIndirectLinks: args.input.includeIndirectLinks,
excludeInternalBacklinks: args.input.excludeInternalBacklinks,
status: args.input.status,
summary: {
rank: args.summary.rank ?? null,
backlinks: args.summary.backlinks ?? null,
@ -238,29 +239,27 @@ function buildOverviewResult(args: {
backlinks: mapBacklinksRows(args.backlinks),
referringDomains: [],
topPages: [],
trends: args.trends
.filter((item) => Boolean(item.date))
.map((item) => ({
date: item.date ?? "",
backlinks: item.backlinks ?? null,
referringDomains: item.referring_domains ?? null,
rank: item.rank ?? null,
trends: historyRows.map((item) => ({
date: item.date,
backlinks: item.backlinks,
referringDomains: item.referringDomains,
rank: item.rank,
})),
newLostTrends: args.newLostTrends
.filter((item) => Boolean(item.date))
.map((item) => ({
date: item.date ?? "",
newBacklinks: item.new_backlinks ?? null,
lostBacklinks: item.lost_backlinks ?? null,
newReferringDomains:
item.new_referring_domains ?? item.new_reffering_domains ?? null,
lostReferringDomains:
item.lost_referring_domains ?? item.lost_reffering_domains ?? null,
newLostTrends: historyRows.map((item) => ({
date: item.date,
newBacklinks: item.newBacklinks,
lostBacklinks: item.lostBacklinks,
newReferringDomains: item.newReferringDomains,
lostReferringDomains: item.lostReferringDomains,
})),
fetchedAt: args.now.toISOString(),
};
}
function normalizeHistoryDate(value: string | null | undefined) {
return value ? value.slice(0, 10) : null;
}
function mapBacklinksRows(
rows: Awaited<ReturnType<typeof fetchBacklinksRowsRaw>>["data"],
) {

View File

@ -135,10 +135,6 @@ describe("fetchBacklinksSummaryRaw", () => {
await expect(
fetchBacklinksSummaryRaw({
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
}),
).rejects.toMatchObject({ code: "BACKLINKS_NOT_ENABLED" });
@ -171,10 +167,6 @@ describe("fetchBacklinksSummaryRaw", () => {
await expect(
fetchBacklinksSummaryRaw({
target: "not-a-real-input.example",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
}),
).rejects.toMatchObject({ code: "VALIDATION_ERROR" });
});

View File

@ -4,24 +4,22 @@ import type {
DataforseoApiResponse,
} from "@/server/lib/dataforseoCost";
import { getRequiredEnvValue } from "@/server/lib/runtime-env";
import type { BacklinksLookupInput } from "@/types/schemas/backlinks";
import {
backlinksHistoryItemSchema,
backlinksItemSchema,
backlinksSummaryItemSchema,
domainPageSummaryItemSchema,
newLostTimeseriesItemSchema,
parseFirstResult,
parseItems,
referringDomainItemSchema,
responseSchema,
timeseriesSummaryItemSchema,
} from "@/server/lib/dataforseoBacklinksSupport";
import { classifyBacklinksErrorWithAccountState } from "@/server/lib/dataforseoBacklinksAccount";
export { normalizeBacklinksTarget } from "@/server/lib/dataforseoBacklinksTarget";
const API_BASE = "https://api.dataforseo.com";
export type BacklinksRequest = BacklinksLookupInput & {
export type BacklinksRequest = {
target: string;
};
@ -29,7 +27,8 @@ export type BacklinksListRequest = BacklinksRequest & {
limit?: number;
};
export type BacklinksTimeseriesRequest = BacklinksRequest & {
export type BacklinksTimeseriesRequest = {
target: string;
dateFrom: string;
dateTo: string;
};
@ -160,10 +159,10 @@ async function postBacklinks(path: string, payload: unknown) {
function buildCommonPayload(input: BacklinksRequest) {
return {
target: input.target,
include_subdomains: input.includeSubdomains,
include_indirect_links: input.includeIndirectLinks,
exclude_internal_backlinks: input.excludeInternalBacklinks,
backlinks_status_type: input.status,
include_subdomains: true,
include_indirect_links: true,
exclude_internal_backlinks: true,
backlinks_status_type: "live",
rank_scale: "one_hundred",
};
}
@ -243,49 +242,21 @@ export async function fetchDomainPagesSummaryRaw(input: BacklinksListRequest) {
} satisfies DataforseoApiResponse<typeof data>;
}
export async function fetchTimeseriesSummaryRaw(
export async function fetchBacklinksHistoryRaw(
input: BacklinksTimeseriesRequest,
) {
const response = await postBacklinks(
"/v3/backlinks/timeseries_summary/live",
[
const response = await postBacklinks("/v3/backlinks/history/live", [
{
...buildCommonPayload(input),
target: input.target,
date_from: input.dateFrom,
date_to: input.dateTo,
group_range: "month",
rank_scale: "one_hundred",
},
],
);
]);
const data = parseItems(
"timeseries-summary-live",
"backlinks-history-live",
response.results,
timeseriesSummaryItemSchema,
);
return {
data,
billing: response.billing,
} satisfies DataforseoApiResponse<typeof data>;
}
export async function fetchNewLostTimeseriesRaw(
input: BacklinksTimeseriesRequest,
) {
const response = await postBacklinks(
"/v3/backlinks/timeseries_new_lost_summary/live",
[
{
...buildCommonPayload(input),
date_from: input.dateFrom,
date_to: input.dateTo,
group_range: "month",
},
],
);
const data = parseItems(
"timeseries-new-lost-summary-live",
response.results,
newLostTimeseriesItemSchema,
backlinksHistoryItemSchema,
);
return {
data,

View File

@ -97,18 +97,12 @@ export const domainPageSummaryItemSchema = z
})
.passthrough();
export const timeseriesSummaryItemSchema = z
export const backlinksHistoryItemSchema = z
.object({
date: z.string().nullable().optional(),
rank: z.number().nullable().optional(),
backlinks: z.number().nullable().optional(),
referring_domains: z.number().nullable().optional(),
})
.passthrough();
export const newLostTimeseriesItemSchema = z
.object({
date: z.string().nullable().optional(),
new_backlinks: z.number().nullable().optional(),
lost_backlinks: z.number().nullable().optional(),
new_reffering_domains: z.number().nullable().optional(),

View File

@ -48,12 +48,11 @@ vi.mock("@/server/lib/dataforseoLighthouse", () => ({
}));
vi.mock("@/server/lib/dataforseoBacklinks", () => ({
fetchBacklinksHistoryRaw: vi.fn(),
fetchBacklinksRowsRaw: vi.fn(),
fetchBacklinksSummaryRaw: vi.fn(),
fetchDomainPagesSummaryRaw: vi.fn(),
fetchNewLostTimeseriesRaw: vi.fn(),
fetchReferringDomainsRaw: vi.fn(),
fetchTimeseriesSummaryRaw: vi.fn(),
}));
import { createDataforseoClient } from "./dataforseoClient";
@ -66,10 +65,6 @@ const billingCustomer = {
const backlinksInput = {
target: "example.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live" as const,
};
function setupHostedMode() {

View File

@ -22,12 +22,11 @@ import { fetchDataforseoLighthouseResultRaw } from "@/server/lib/dataforseoLight
import type { LighthouseStrategy } from "@/server/lib/dataforseoLighthousePayload";
import type { StoredLighthousePayload } from "@/server/lib/lighthouseStoredPayload";
import {
fetchBacklinksHistoryRaw,
fetchBacklinksRowsRaw,
fetchBacklinksSummaryRaw,
fetchDomainPagesSummaryRaw,
fetchNewLostTimeseriesRaw,
fetchReferringDomainsRaw,
fetchTimeseriesSummaryRaw,
type BacklinksListRequest,
type BacklinksRequest,
type BacklinksTimeseriesRequest,
@ -62,14 +61,9 @@ export function createDataforseoClient(customer: BillingCustomerContext) {
fetchDomainPagesSummaryRaw(input),
);
},
timeseriesSummary(input: BacklinksTimeseriesRequest) {
history(input: BacklinksTimeseriesRequest) {
return meterDataforseoCall(customer, () =>
fetchTimeseriesSummaryRaw(input),
);
},
newLostTimeseries(input: BacklinksTimeseriesRequest) {
return meterDataforseoCall(customer, () =>
fetchNewLostTimeseriesRaw(input),
fetchBacklinksHistoryRaw(input),
);
},
},

View File

@ -18,10 +18,6 @@ export const getBacklinksOverview = createServerFn({
const input = {
target: data.target,
scope: data.scope,
includeSubdomains: data.includeSubdomains,
includeIndirectLinks: data.includeIndirectLinks,
excludeInternalBacklinks: data.excludeInternalBacklinks,
status: data.status,
};
const profile = await BacklinksService.profileOverview(input, {
organizationId: context.organizationId,
@ -50,10 +46,6 @@ export const getBacklinksReferringDomains = createServerFn({
const input = {
target: data.target,
scope: data.scope,
includeSubdomains: data.includeSubdomains,
includeIndirectLinks: data.includeIndirectLinks,
excludeInternalBacklinks: data.excludeInternalBacklinks,
status: data.status,
};
const profile = await BacklinksService.profileReferringDomains(input, {
organizationId: context.organizationId,
@ -76,10 +68,6 @@ export const getBacklinksTopPages = createServerFn({
const input = {
target: data.target,
scope: data.scope,
includeSubdomains: data.includeSubdomains,
includeIndirectLinks: data.includeIndirectLinks,
excludeInternalBacklinks: data.excludeInternalBacklinks,
status: data.status,
};
const profile = await BacklinksService.profileTopPages(input, {
organizationId: context.organizationId,

View File

@ -46,10 +46,6 @@ export const testBacklinksAccess = createServerFn({
try {
await dataforseo.backlinks.summary({
target: "dataforseo.com",
includeSubdomains: true,
includeIndirectLinks: true,
excludeInternalBacklinks: true,
status: "live",
});
const status = buildVerifiedBacklinksAccessStatus(checkedAt);

View File

@ -1,19 +1,11 @@
import { z } from "zod";
export const backlinksStatusSchema = z.enum(["live", "lost", "all"]);
export const backlinksTabSchema = z.enum(["backlinks", "domains", "pages"]);
export const backlinksTargetScopeSchema = z.enum(["domain", "page"]);
const booleanSearchParamSchema = z
.union([z.boolean(), z.enum(["true", "false"])])
.transform((value) => value === true || value === "true");
export const backlinksLookupSchema = z.object({
target: z.string().min(1, "Target is required").max(2048),
scope: backlinksTargetScopeSchema.optional(),
includeSubdomains: z.boolean().default(true),
includeIndirectLinks: z.boolean().default(true),
excludeInternalBacklinks: z.boolean().default(true),
status: backlinksStatusSchema.default("live"),
});
export const backlinksProjectSchema = z.object({
@ -27,14 +19,9 @@ export const backlinksOverviewInputSchema = backlinksLookupSchema.extend({
export const backlinksSearchSchema = z.object({
target: z.string().optional(),
scope: backlinksTargetScopeSchema.optional(),
subdomains: booleanSearchParamSchema.optional(),
indirect: booleanSearchParamSchema.optional(),
excludeInternal: booleanSearchParamSchema.optional(),
status: backlinksStatusSchema.optional(),
tab: backlinksTabSchema.optional(),
});
export type BacklinksLookupInput = z.infer<typeof backlinksLookupSchema>;
export type BacklinksStatus = z.infer<typeof backlinksStatusSchema>;
export type BacklinksTab = z.infer<typeof backlinksTabSchema>;
export type BacklinksTargetScope = z.infer<typeof backlinksTargetScopeSchema>;

View File

@ -3,17 +3,15 @@ import { backlinksSearchSchema } from "@/types/schemas/backlinks";
import { domainSearchSchema } from "@/types/schemas/domain";
describe("search param boolean parsing", () => {
it("parses explicit false values for backlinks search params", () => {
it("parses backlinks search params with target and tab", () => {
const parsed = backlinksSearchSchema.parse({
subdomains: "false",
indirect: "false",
excludeInternal: "false",
target: "example.com",
tab: "domains",
});
expect(parsed).toEqual({
subdomains: false,
indirect: false,
excludeInternal: false,
target: "example.com",
tab: "domains",
});
});