@@ -19,25 +19,29 @@ export type AnyOpenAPIOperationBlock = DocumentBlockOpenAPI | DocumentBlockOpenA
19
19
20
20
export type OpenAPIBlockType = 'operation' | 'models' ;
21
21
22
- export type ResolveOpenAPIOperationBlockResult = {
23
- error : undefined ;
22
+ export type OpenAPIOperationResult = {
24
23
data : OpenAPIOperationData | null ;
24
+ error : undefined ;
25
25
specUrl : string | null ;
26
26
} ;
27
27
28
- export type ResolveOpenAPIModelsBlockResult = {
29
- error : undefined ;
28
+ export type OpenAPIModelsResult = {
30
29
data : OpenAPIModelsData | null ;
30
+ error : undefined ;
31
31
specUrl : string | null ;
32
32
} ;
33
33
34
- export type ResolveOpenAPIBlockResult < T extends OpenAPIBlockType = OpenAPIBlockType > =
35
- | ( T extends 'operation' ? ResolveOpenAPIOperationBlockResult : ResolveOpenAPIModelsBlockResult )
36
- | ResolveOpenAPIBlockError ;
34
+ export type OpenAPIErrorResult = {
35
+ data ?: undefined ;
36
+ specUrl ?: undefined ;
37
+ error : OpenAPIParseError ;
38
+ } ;
37
39
38
- type ResolveOpenAPIBlockError = { error : OpenAPIParseError ; data ?: undefined ; specUrl ?: undefined } ;
40
+ export type OpenAPIBlockResult < T extends OpenAPIBlockType = 'operation' > =
41
+ | ( T extends 'operation' ? OpenAPIOperationResult : OpenAPIModelsResult )
42
+ | OpenAPIErrorResult ;
39
43
40
- type ResolveOpenAPIBlockArgs < T extends OpenAPIBlockType = OpenAPIBlockType > = {
44
+ type ResolveOpenAPIBlockArgs < T = OpenAPIBlockType > = {
41
45
block : AnyOpenAPIOperationBlock ;
42
46
context : GitBookAnyContext ;
43
47
/**
@@ -48,21 +52,21 @@ type ResolveOpenAPIBlockArgs<T extends OpenAPIBlockType = OpenAPIBlockType> = {
48
52
type ?: T | OpenAPIBlockType ;
49
53
} ;
50
54
51
- const weakmap = new WeakMap < AnyOpenAPIOperationBlock , Promise < ResolveOpenAPIBlockResult > > ( ) ;
55
+ const weakmap = new WeakMap < AnyOpenAPIOperationBlock , Promise < OpenAPIBlockResult > > ( ) ;
52
56
53
57
/**
54
58
* Cache the result of resolving an OpenAPI block.
55
59
* It is important because the resolve is called in sections and in the block itself.
56
60
*/
57
61
export function resolveOpenAPIBlock < T extends OpenAPIBlockType = 'operation' > (
58
62
args : ResolveOpenAPIBlockArgs < T >
59
- ) : Promise < ResolveOpenAPIBlockResult < T > > {
63
+ ) : Promise < OpenAPIBlockResult < T > > {
60
64
if ( weakmap . has ( args . block ) ) {
61
- return weakmap . get ( args . block ) as Promise < ResolveOpenAPIBlockResult < T > > ;
65
+ return weakmap . get ( args . block ) ;
62
66
}
63
67
64
68
const result = baseResolveOpenAPIBlock ( args ) ;
65
- weakmap . set ( args . block , result as Promise < ResolveOpenAPIBlockResult < T > > ) ;
69
+ weakmap . set ( args . block , result as Promise < OpenAPIBlockResult > ) ;
66
70
return result ;
67
71
}
68
72
@@ -71,17 +75,17 @@ export function resolveOpenAPIBlock<T extends OpenAPIBlockType = 'operation'>(
71
75
*/
72
76
async function baseResolveOpenAPIBlock < T extends OpenAPIBlockType = 'operation' > (
73
77
args : ResolveOpenAPIBlockArgs < T >
74
- ) : Promise < ResolveOpenAPIBlockResult < T > > {
78
+ ) : Promise < OpenAPIBlockResult < T > > {
75
79
const { context, block, type = 'operation' } = args ;
76
80
if ( ! block . data . path || ! block . data . method ) {
77
- return { data : null , specUrl : null } as ResolveOpenAPIBlockResult < T > ;
81
+ return createResults ( null ) ;
78
82
}
79
83
80
84
const ref = block . data . ref ;
81
85
const resolved = ref ? await resolveContentRef ( ref , context ) : null ;
82
86
83
87
if ( ! resolved ) {
84
- return { data : null , specUrl : null } as ResolveOpenAPIBlockResult < T > ;
88
+ return createResults ( null ) ;
85
89
}
86
90
87
91
try {
@@ -93,17 +97,17 @@ async function baseResolveOpenAPIBlock<T extends OpenAPIBlockType = 'operation'>
93
97
return fetchFilesystem ( resolved . href ) ;
94
98
} ) ( ) ;
95
99
100
+ let data : OpenAPIOperationData | OpenAPIModelsData | null = null ;
96
101
if ( type === 'models' ) {
97
- const data = await resolveOpenAPIModels ( filesystem ) ;
98
- return { data : data , specUrl : resolved . href } as ResolveOpenAPIBlockResult < T > ;
102
+ data = await resolveOpenAPIModels ( filesystem ) ;
103
+ } else {
104
+ data = await resolveOpenAPIOperation ( filesystem , {
105
+ path : block . data . path ,
106
+ method : block . data . method ,
107
+ } ) ;
99
108
}
100
109
101
- const data = await resolveOpenAPIOperation ( filesystem , {
102
- path : block . data . path ,
103
- method : block . data . method ,
104
- } ) ;
105
-
106
- return { data, specUrl : resolved . href } as ResolveOpenAPIBlockResult < T > ;
110
+ return createResults ( data , resolved . href ) ;
107
111
} catch ( error ) {
108
112
if ( error instanceof OpenAPIParseError ) {
109
113
return { error } ;
@@ -175,3 +179,17 @@ async function fetchFilesystemUncached(
175
179
176
180
return richFilesystem ;
177
181
}
182
+
183
+ /**
184
+ * Create a result for OpenAPI based on the type.
185
+ */
186
+ function createResults < T extends OpenAPIOperationData | OpenAPIModelsData > (
187
+ data : T | null ,
188
+ specUrl ?: string
189
+ ) : OpenAPIBlockResult < T > {
190
+ return {
191
+ error : undefined ,
192
+ data,
193
+ specUrl,
194
+ } ;
195
+ }
0 commit comments