11import { race , tryCatch } from '@/lib/async' ;
22import { joinPath , joinPathWithBaseURL } from '@/lib/paths' ;
33import { trace } from '@/lib/tracing' ;
4- import type { GitBookAPI , PublishedSiteContentLookup , SiteVisitorPayload } from '@gitbook/api' ;
4+ import type { PublishedSiteContentLookup , SiteVisitorPayload } from '@gitbook/api' ;
55import { apiClient } from './api' ;
66import { getExposableError } from './errors' ;
77import 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 ) {
0 commit comments