Skip to content

Commit 2863fe0

Browse files
authored
Use resolvePublishedContentByUrl instead of the deprecated resolution endpoint (#3310)
1 parent 8721806 commit 2863fe0

File tree

2 files changed

+24
-93
lines changed

2 files changed

+24
-93
lines changed

packages/gitbook-v2/src/lib/data/lookup.ts

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { race, tryCatch } from '@/lib/async';
22
import { joinPath, joinPathWithBaseURL } from '@/lib/paths';
33
import { trace } from '@/lib/tracing';
4-
import type { GitBookAPI, PublishedSiteContentLookup, SiteVisitorPayload } from '@gitbook/api';
4+
import type { PublishedSiteContentLookup, SiteVisitorPayload } from '@gitbook/api';
55
import { apiClient } from './api';
66
import { getExposableError } from './errors';
77
import type { DataFetcherResponse } from './types';
@@ -18,85 +18,32 @@ interface LookupPublishedContentByUrlInput {
1818
* Lookup a content by its URL using the GitBook resolvePublishedContentByUrl API endpoint.
1919
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
2020
*/
21-
export async function resolvePublishedContentByUrl(input: LookupPublishedContentByUrlInput) {
22-
return lookupPublishedContentByUrl({
23-
url: input.url,
24-
fetchLookupAPIResult: ({ url, signal }) => {
25-
const api = apiClient({ apiToken: input.apiToken });
26-
return trace(
27-
{
28-
operation: 'resolvePublishedContentByUrl',
29-
name: url,
30-
},
31-
() =>
32-
tryCatch(
33-
api.urls.resolvePublishedContentByUrl(
34-
{
35-
url,
36-
...(input.visitorPayload ? { visitor: input.visitorPayload } : {}),
37-
redirectOnError: input.redirectOnError,
38-
},
39-
{ signal }
40-
)
41-
)
42-
);
43-
},
44-
});
45-
}
46-
47-
/**
48-
* Lookup a content by its URL using the GitBook getPublishedContentByUrl API endpoint.
49-
* To optimize caching, we try multiple lookup alternatives and return the first one that matches.
50-
*
51-
* @deprecated use resolvePublishedContentByUrl.
52-
*
53-
*/
54-
export async function getPublishedContentByURL(input: LookupPublishedContentByUrlInput) {
55-
return lookupPublishedContentByUrl({
56-
url: input.url,
57-
fetchLookupAPIResult: ({ url, signal }) => {
58-
const api = apiClient({ apiToken: input.apiToken });
59-
return trace(
60-
{
61-
operation: 'getPublishedContentByURL',
62-
name: url,
63-
},
64-
() =>
65-
tryCatch(
66-
api.urls.getPublishedContentByUrl(
67-
{
68-
url,
69-
visitorAuthToken: input.visitorPayload.jwtToken ?? undefined,
70-
redirectOnError: input.redirectOnError,
71-
// @ts-expect-error - cacheVersion is not a real query param
72-
cacheVersion: 'v2',
73-
},
74-
{ signal }
75-
)
76-
)
77-
);
78-
},
79-
});
80-
}
81-
82-
type TryCatch<T> = ReturnType<typeof tryCatch<T>>;
83-
84-
async function lookupPublishedContentByUrl(input: {
85-
url: string;
86-
fetchLookupAPIResult: (args: {
87-
url: string;
88-
signal: AbortSignal;
89-
}) => TryCatch<Awaited<ReturnType<GitBookAPI['urls']['resolvePublishedContentByUrl']>>>;
90-
}): Promise<DataFetcherResponse<PublishedSiteContentLookup>> {
21+
export async function lookupPublishedContentByUrl(
22+
input: LookupPublishedContentByUrlInput
23+
): Promise<DataFetcherResponse<PublishedSiteContentLookup>> {
9124
const lookupURL = new URL(input.url);
9225
const url = stripURLSearch(lookupURL);
9326
const lookup = getURLLookupAlternatives(url);
9427

9528
const result = await race(lookup.urls, async (alternative, { signal }) => {
96-
const callResult = await input.fetchLookupAPIResult({
97-
url: alternative.url,
98-
signal,
99-
});
29+
const api = apiClient({ apiToken: input.apiToken });
30+
const callResult = await trace(
31+
{
32+
operation: 'resolvePublishedContentByUrl',
33+
name: alternative.url,
34+
},
35+
() =>
36+
tryCatch(
37+
api.urls.resolvePublishedContentByUrl(
38+
{
39+
url: alternative.url,
40+
...(input.visitorPayload ? { visitor: input.visitorPayload } : {}),
41+
redirectOnError: input.redirectOnError,
42+
},
43+
{ signal }
44+
)
45+
)
46+
);
10047

10148
if (callResult.error) {
10249
if (alternative.primary) {

packages/gitbook-v2/src/middleware.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ import {
1616
import { serveResizedImage } from '@/routes/image';
1717
import {
1818
DataFetcherError,
19-
getPublishedContentByURL,
2019
getVisitorAuthBasePath,
20+
lookupPublishedContentByUrl,
2121
normalizeURL,
22-
resolvePublishedContentByUrl,
2322
throwIfDataError,
2423
} from '@v2/lib/data';
2524
import { isGitBookAssetsHostURL, isGitBookHostURL } from '@v2/lib/env';
@@ -34,18 +33,6 @@ export const config = {
3433

3534
type URLWithMode = { url: URL; mode: 'url' | 'url-host' };
3635

37-
/**
38-
* Temporary list of hosts to test adaptive content using the new resolution API.
39-
*/
40-
const ADAPTIVE_CONTENT_HOSTS = [
41-
'docs.gitbook.com',
42-
'paypal.gitbook.com',
43-
'adaptive-docs.gitbook-staging.com',
44-
'enriched-content-playground.gitbook-staging.io',
45-
'docs.testgitbook.com',
46-
'launchdarkly-site.gitbook.education',
47-
];
48-
4936
export async function middleware(request: NextRequest) {
5037
try {
5138
const requestURL = new URL(request.url);
@@ -104,11 +91,8 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
10491
});
10592

10693
const withAPIToken = async (apiToken: string | null) => {
107-
const resolve = ADAPTIVE_CONTENT_HOSTS.includes(siteRequestURL.hostname)
108-
? resolvePublishedContentByUrl
109-
: getPublishedContentByURL;
11094
const siteURLData = await throwIfDataError(
111-
resolve({
95+
lookupPublishedContentByUrl({
11296
url: siteRequestURL.toString(),
11397
visitorPayload: {
11498
jwtToken: visitorToken?.token ?? undefined,

0 commit comments

Comments
 (0)