@@ -6,6 +6,7 @@ import type { GitBookAnyContext } from '@v2/lib/context';
6
6
import { type CacheFunctionOptions , cache , noCacheFetchOptions } from '@/lib/cache' ;
7
7
8
8
import { resolveContentRef } from '../references' ;
9
+ import { isV2 } from '../v2' ;
9
10
import { enrichFilesystem } from './enrich' ;
10
11
11
12
const weakmap = new WeakMap < DocumentBlockOpenAPI , ResolveOpenAPIBlockResult > ( ) ;
@@ -65,32 +66,18 @@ async function baseResolveOpenAPIBlock(args: ResolveOpenAPIBlockArgs): ResolveOp
65
66
}
66
67
}
67
68
68
- const fetchFilesystem = cache ( {
69
- name : 'openapi.fetch.v6' ,
70
- get : async ( url : string , options : CacheFunctionOptions ) => {
71
- // Wrap the raw string to prevent invalid URLs from being passed to fetch.
72
- // This can happen if the URL has whitespace, which is currently handled differently by Cloudflare's implementation of fetch:
73
- // https://github.com/cloudflare/workerd/issues/1957
74
- const response = await fetch ( new URL ( url ) , {
75
- ...noCacheFetchOptions ,
76
- signal : options . signal ,
77
- } ) ;
69
+ function fetchFilesystem ( url : string ) {
70
+ if ( isV2 ( ) ) {
71
+ return fetchFilesystemV2 ( url ) ;
72
+ }
78
73
79
- if ( ! response . ok ) {
80
- throw new Error (
81
- `Failed to fetch OpenAPI file: ${ response . status } ${ response . statusText } `
82
- ) ;
83
- }
74
+ return fetchFilesystemV1 ( url ) ;
75
+ }
84
76
85
- const text = await response . text ( ) ;
86
- const filesystem = await parseOpenAPI ( {
87
- value : text ,
88
- rootURL : url ,
89
- // If we fetch the OpenAPI specification
90
- // it's the legacy system, it means the spec can be trusted here.
91
- trust : true ,
92
- } ) ;
93
- const richFilesystem = await enrichFilesystem ( filesystem ) ;
77
+ const fetchFilesystemV1 = cache ( {
78
+ name : 'openapi.fetch.v6' ,
79
+ get : async ( url : string , options : CacheFunctionOptions ) => {
80
+ const richFilesystem = await fetchFilesystemUncached ( url , options ) ;
94
81
return {
95
82
// Cache for 4 hours
96
83
ttl : 24 * 60 * 60 ,
@@ -100,3 +87,44 @@ const fetchFilesystem = cache({
100
87
} ;
101
88
} ,
102
89
} ) ;
90
+
91
+ async function fetchFilesystemV2 ( url : string ) {
92
+ 'use cache' ;
93
+
94
+ // TODO: add cache lifetime once we can use next.js 15 code here
95
+
96
+ const response = await fetchFilesystemUncached ( url ) ;
97
+
98
+ return response ;
99
+ }
100
+
101
+ async function fetchFilesystemUncached (
102
+ url : string ,
103
+ options ?: {
104
+ signal ?: AbortSignal ;
105
+ }
106
+ ) {
107
+ // Wrap the raw string to prevent invalid URLs from being passed to fetch.
108
+ // This can happen if the URL has whitespace, which is currently handled differently by Cloudflare's implementation of fetch:
109
+ // https://github.com/cloudflare/workerd/issues/1957
110
+ const response = await fetch ( new URL ( url ) , {
111
+ ...noCacheFetchOptions ,
112
+ signal : options ?. signal ,
113
+ } ) ;
114
+
115
+ if ( ! response . ok ) {
116
+ throw new Error ( `Failed to fetch OpenAPI file: ${ response . status } ${ response . statusText } ` ) ;
117
+ }
118
+
119
+ const text = await response . text ( ) ;
120
+ const filesystem = await parseOpenAPI ( {
121
+ value : text ,
122
+ rootURL : url ,
123
+ // If we fetch the OpenAPI specification
124
+ // it's the legacy system, it means the spec can be trusted here.
125
+ trust : true ,
126
+ } ) ;
127
+ const richFilesystem = await enrichFilesystem ( filesystem ) ;
128
+
129
+ return richFilesystem ;
130
+ }
0 commit comments