1- import type { AnyObject , OpenAPIV3 } from '@gitbook/openapi-parser' ;
1+ import type { AnyObject , OpenAPIV3 , OpenAPIV3_1 } from '@gitbook/openapi-parser' ;
22
3- export function checkIsReference ( input : unknown ) : input is OpenAPIV3 . ReferenceObject {
3+ export function checkIsReference (
4+ input : unknown ,
5+ ) : input is OpenAPIV3 . ReferenceObject | OpenAPIV3_1 . ReferenceObject {
46 return typeof input === 'object' && ! ! input && '$ref' in input ;
57}
68
@@ -19,3 +21,76 @@ export function resolveDescription(object: AnyObject) {
1921 ? object . description
2022 : undefined ;
2123}
24+
25+ /**
26+ * Extract descriptions from an object.
27+ */
28+ export function extractDescriptions ( object : AnyObject ) {
29+ return {
30+ description : object . description ,
31+ [ 'x-gitbook-description-html' ] :
32+ 'x-gitbook-description-html' in object
33+ ? object [ 'x-gitbook-description-html' ]
34+ : undefined ,
35+ } ;
36+ }
37+
38+ /**
39+ * Resolve the first example from an object.
40+ */
41+ export function resolveFirstExample ( object : AnyObject ) {
42+ if ( 'examples' in object && typeof object . examples === 'object' && object . examples ) {
43+ const keys = Object . keys ( object . examples ) ;
44+ const firstKey = keys [ 0 ] ;
45+ if ( firstKey && object . examples [ firstKey ] ) {
46+ return object . examples [ firstKey ] ;
47+ }
48+ }
49+ if ( 'example' in object && object . example !== undefined ) {
50+ return object . example ;
51+ }
52+ return undefined ;
53+ }
54+
55+ /**
56+ * Resolve the schema of a parameter.
57+ * Extract the description, example and deprecated from parameter.
58+ */
59+ export function resolveParameterSchema (
60+ parameter : OpenAPIV3 . ParameterBaseObject ,
61+ ) : OpenAPIV3 . SchemaObject {
62+ const schema = checkIsReference ( parameter . schema ) ? undefined : parameter . schema ;
63+ return {
64+ // Description of the parameter is defined at the parameter level
65+ // we use display it if the schema doesn't override it
66+ ...extractDescriptions ( parameter ) ,
67+ example : resolveFirstExample ( parameter ) ,
68+ // Deprecated can be defined at the parameter level
69+ deprecated : parameter . deprecated ,
70+ ...schema ,
71+ } ;
72+ }
73+
74+ /**
75+ * Transform a parameter object to a property object.
76+ */
77+ export function parameterToProperty (
78+ parameter : OpenAPIV3 . ParameterObject | OpenAPIV3 . ReferenceObject | OpenAPIV3_1 . ReferenceObject ,
79+ ) : {
80+ propertyName : string | undefined ;
81+ schema : OpenAPIV3 . SchemaObject ;
82+ required : boolean | undefined ;
83+ } {
84+ if ( checkIsReference ( parameter ) ) {
85+ return {
86+ propertyName : parameter . $ref ?? 'Unknown ref' ,
87+ schema : { } ,
88+ required : undefined ,
89+ } ;
90+ }
91+ return {
92+ propertyName : parameter . name ,
93+ schema : resolveParameterSchema ( parameter ) ,
94+ required : parameter . required ,
95+ } ;
96+ }
0 commit comments