|
1 | | -import type { DocumentBlockOpenAPI, DocumentBlockOpenAPIOperation } from '@gitbook/api'; |
2 | | -import { OpenAPIParseError, parseOpenAPI } from '@gitbook/openapi-parser'; |
3 | | -import { type OpenAPIOperationData, resolveOpenAPIOperation } from '@gitbook/react-openapi'; |
4 | | -import type { GitBookAnyContext } from '@v2/lib/context'; |
| 1 | +import { parseOpenAPI } from '@gitbook/openapi-parser'; |
5 | 2 |
|
6 | 3 | import { type CacheFunctionOptions, cache, noCacheFetchOptions } from '@/lib/cache'; |
7 | | - |
| 4 | +import type { ResolveOpenAPIBlockArgs } from '@/lib/openapi/types'; |
8 | 5 | import { assert } from 'ts-essentials'; |
9 | 6 | import { resolveContentRef } from '../references'; |
10 | 7 | import { isV2 } from '../v2'; |
11 | 8 | import { enrichFilesystem } from './enrich'; |
| 9 | +import type { FetchOpenAPIFilesystemResult } from './types'; |
12 | 10 |
|
13 | | -export type AnyOpenAPIOperationBlock = DocumentBlockOpenAPI | DocumentBlockOpenAPIOperation; |
14 | | - |
15 | | -const weakmap = new WeakMap<AnyOpenAPIOperationBlock, Promise<ResolveOpenAPIBlockResult>>(); |
16 | | - |
17 | | -/** |
18 | | - * Cache the result of resolving an OpenAPI block. |
19 | | - * It is important because the resolve is called in sections and in the block itself. |
20 | | - */ |
21 | | -export function resolveOpenAPIBlock( |
22 | | - args: ResolveOpenAPIBlockArgs |
23 | | -): Promise<ResolveOpenAPIBlockResult> { |
24 | | - if (weakmap.has(args.block)) { |
25 | | - return weakmap.get(args.block)!; |
26 | | - } |
27 | | - |
28 | | - const result = baseResolveOpenAPIBlock(args); |
29 | | - weakmap.set(args.block, result); |
30 | | - return result; |
31 | | -} |
32 | | - |
33 | | -type ResolveOpenAPIBlockArgs = { |
34 | | - block: AnyOpenAPIOperationBlock; |
35 | | - context: GitBookAnyContext; |
36 | | -}; |
37 | | -export type ResolveOpenAPIBlockResult = |
38 | | - | { error?: undefined; data: OpenAPIOperationData | null; specUrl: string | null } |
39 | | - | { error: OpenAPIParseError; data?: undefined; specUrl?: undefined }; |
40 | 11 | /** |
41 | | - * Resolve OpenAPI block. |
| 12 | + * Fetch OpenAPI block. |
42 | 13 | */ |
43 | | -async function baseResolveOpenAPIBlock( |
| 14 | +export async function fetchOpenAPIFilesystem( |
44 | 15 | args: ResolveOpenAPIBlockArgs |
45 | | -): Promise<ResolveOpenAPIBlockResult> { |
| 16 | +): Promise<FetchOpenAPIFilesystemResult> { |
46 | 17 | const { context, block } = args; |
47 | | - if (!block.data.path || !block.data.method) { |
48 | | - return { data: null, specUrl: null }; |
49 | | - } |
50 | 18 |
|
51 | 19 | const ref = block.data.ref; |
52 | 20 | const resolved = ref ? await resolveContentRef(ref, context) : null; |
53 | 21 |
|
54 | 22 | if (!resolved) { |
55 | | - return { data: null, specUrl: null }; |
| 23 | + return { filesystem: null, specUrl: null }; |
56 | 24 | } |
57 | 25 |
|
58 | | - try { |
59 | | - const filesystem = await (() => { |
60 | | - if (ref.kind === 'openapi') { |
61 | | - assert(resolved.openAPIFilesystem); |
62 | | - return resolved.openAPIFilesystem; |
63 | | - } |
64 | | - return fetchFilesystem(resolved.href); |
65 | | - })(); |
66 | | - |
67 | | - const data = await resolveOpenAPIOperation(filesystem, { |
68 | | - path: block.data.path, |
69 | | - method: block.data.method, |
70 | | - }); |
71 | | - |
72 | | - return { data, specUrl: resolved.href }; |
73 | | - } catch (error) { |
74 | | - if (error instanceof OpenAPIParseError) { |
75 | | - return { error }; |
| 26 | + const filesystem = await (() => { |
| 27 | + if (ref.kind === 'openapi') { |
| 28 | + assert(resolved.openAPIFilesystem); |
| 29 | + return resolved.openAPIFilesystem; |
76 | 30 | } |
| 31 | + return fetchFilesystem(resolved.href); |
| 32 | + })(); |
77 | 33 |
|
78 | | - throw error; |
79 | | - } |
| 34 | + return { |
| 35 | + filesystem, |
| 36 | + specUrl: resolved.href, |
| 37 | + }; |
80 | 38 | } |
81 | 39 |
|
82 | 40 | function fetchFilesystem(url: string) { |
|
0 commit comments