1
+ import { tableFromObject } from '@/formatters/table-from-object'
2
+ import { FlattenedSchemaPropertyItem } from '@/types/flattened-schema-property-item'
3
+ import { getflattenedSchema } from '@/utils/get-flattened-schema'
1
4
import { SchemaObject } from '@/utils/json-to-markdown'
2
- import { jsonToMarkdownTable } from '@/utils/json-to- markdown-table '
5
+ import { Markdown } from '@/utils/markdown/markdown '
3
6
import { OpenAPIV3 } from 'openapi-types'
4
7
5
8
export type RequestBodyFormatter = ( args : {
6
9
endpointDetailData : OpenAPIV3 . OperationObject
10
+ baseRequestBody ?: CustomRequestBodyObject
7
11
} ) => string
8
12
interface CustomMediaTypeObject {
9
13
schema ?: SchemaObject
@@ -17,7 +21,7 @@ interface CustomMediaTypeObject {
17
21
}
18
22
}
19
23
20
- interface CustomRequestBodyObject {
24
+ export interface CustomRequestBodyObject {
21
25
description ?: string
22
26
content : {
23
27
[ media : string ] : CustomMediaTypeObject
@@ -26,9 +30,30 @@ interface CustomRequestBodyObject {
26
30
}
27
31
28
32
export const requestBodyFormatter : RequestBodyFormatter = ( {
29
- endpointDetailData
33
+ endpointDetailData,
34
+ baseRequestBody // only used for modified endpoints
30
35
} ) => {
36
+ let flattenedRequestBody : FlattenedSchemaPropertyItem [ ] = [ ]
37
+ let flattenedBaseRequestBody : FlattenedSchemaPropertyItem [ ] = [ ]
38
+
39
+ if ( baseRequestBody ) {
40
+ const resolvedBaseRequestBody =
41
+ baseRequestBody as unknown as CustomRequestBodyObject
42
+
43
+ const baseRequestBodyContent =
44
+ resolvedBaseRequestBody ?. content ?. [ 'application/json' ] ??
45
+ resolvedBaseRequestBody ?. content ?. [ 'text/plain' ]
46
+
47
+ const baseRequestBodyContentSchema =
48
+ baseRequestBodyContent ?. schema as SchemaObject
49
+
50
+ flattenedBaseRequestBody = getflattenedSchema ( baseRequestBodyContentSchema )
51
+ }
52
+
31
53
const requestBody = endpointDetailData . requestBody as CustomRequestBodyObject // all refs have been resolved in main.ts
54
+ flattenedRequestBody = requestBody . content [ 'application/json' ] . schema
55
+ ? getflattenedSchema ( requestBody . content [ 'application/json' ] . schema )
56
+ : [ ]
32
57
const doesHaveRequestBody = requestBody !== undefined
33
58
34
59
if ( ! doesHaveRequestBody ) {
@@ -43,10 +68,42 @@ export const requestBodyFormatter: RequestBodyFormatter = ({
43
68
- Content Required: ${ requestBody ?. required ?? 'Not Provided' }
44
69
`
45
70
71
+ const mdc = new Markdown ( )
72
+
73
+ const headers = [ 'Property' , 'Type' , 'Required' , 'Description' , 'Example' ]
74
+
75
+ // const rows = Object.entries(obj.schema.properties ?? {}).map(
76
+ // ([propertyName, propertyMetadata]) => [
77
+ // propertyName,
78
+ // propertyMetadata.type,
79
+ // obj.schema.required?.includes(propertyName) ? 'Yes' : 'No',
80
+ // propertyMetadata.description ?? '',
81
+ // propertyMetadata.example ?? ''
82
+ // ]
83
+ // )
84
+ const baseFlattenedSchemaPropertyList = flattenedBaseRequestBody ?. map (
85
+ row => row . property
86
+ )
87
+ const rowsFromNewBody = flattenedRequestBody . map ( row => {
88
+ const hasBeenAdded = ! baseFlattenedSchemaPropertyList . includes ( row . property )
89
+
90
+ return {
91
+ property : `${ hasBeenAdded ? '✅' : '' } ${ row . property } ` ,
92
+ type : row . type ,
93
+ required : row . required ,
94
+ description : row . description ,
95
+ example : row . example
96
+ }
97
+ } )
98
+ const rows = [ ...rowsFromNewBody ]
99
+ const dataIndex = headers . map ( header => header . toLowerCase ( ) )
100
+
101
+ const tableMarkdown = tableFromObject ( { headers, rows, dataIndex } )
102
+
103
+ mdc . appendToNewLine ( tableMarkdown )
104
+
46
105
const requestBodyMarkdown = requestBody ?. content ?. [ 'application/json' ] ?. schema
47
- ? jsonToMarkdownTable ( {
48
- schema : requestBody ?. content ?. [ 'application/json' ] ?. schema
49
- } )
106
+ ? mdc . toString ( )
50
107
: ''
51
108
52
109
return requestBodyAdditionalInfo + requestBodyMarkdown
0 commit comments