Skip to content

Commit a890374

Browse files
committed
feat: parse request body parameters recursively and add checkmark before added property
1 parent 7f2bd94 commit a890374

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/utils/format-single-api-endpoint-as-markdown.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { CustomPathDiffItem } from './generate-markdown-diff'
22
import { responseFormatter } from '@/utils/single-api-endpoint-formatter.ts/response-formatter'
33
import { requestParametersFormatter } from '@/utils/single-api-endpoint-formatter.ts/request-parameters-formatter'
4-
import { requestBodyFormatter } from '@/utils/single-api-endpoint-formatter.ts/request-body-formatter'
4+
import {
5+
CustomRequestBodyObject,
6+
requestBodyFormatter
7+
} from '@/utils/single-api-endpoint-formatter.ts/request-body-formatter'
58

69
type FormatSingleApiEndpointAsMarkdown = (
710
endpoint: CustomPathDiffItem,
@@ -30,7 +33,8 @@ export const formatSingleApiEndpointAsMarkdown: FormatSingleApiEndpointAsMarkdow
3033
shouldCheckForChanges
3134
})
3235
const requestBodyMarkdown = requestBodyFormatter({
33-
endpointDetailData
36+
endpointDetailData,
37+
baseRequestBody: baseApiEndpoint?.requestBody as CustomRequestBodyObject
3438
})
3539
const responseMarkdown = responseFormatter(
3640
responses,

src/utils/single-api-endpoint-formatter.ts/request-body-formatter.ts

+63-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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'
14
import { SchemaObject } from '@/utils/json-to-markdown'
2-
import { jsonToMarkdownTable } from '@/utils/json-to-markdown-table'
5+
import { Markdown } from '@/utils/markdown/markdown'
36
import { OpenAPIV3 } from 'openapi-types'
47

58
export type RequestBodyFormatter = (args: {
69
endpointDetailData: OpenAPIV3.OperationObject
10+
baseRequestBody?: CustomRequestBodyObject
711
}) => string
812
interface CustomMediaTypeObject {
913
schema?: SchemaObject
@@ -17,7 +21,7 @@ interface CustomMediaTypeObject {
1721
}
1822
}
1923

20-
interface CustomRequestBodyObject {
24+
export interface CustomRequestBodyObject {
2125
description?: string
2226
content: {
2327
[media: string]: CustomMediaTypeObject
@@ -26,9 +30,30 @@ interface CustomRequestBodyObject {
2630
}
2731

2832
export const requestBodyFormatter: RequestBodyFormatter = ({
29-
endpointDetailData
33+
endpointDetailData,
34+
baseRequestBody // only used for modified endpoints
3035
}) => {
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+
3153
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+
: []
3257
const doesHaveRequestBody = requestBody !== undefined
3358

3459
if (!doesHaveRequestBody) {
@@ -43,10 +68,42 @@ export const requestBodyFormatter: RequestBodyFormatter = ({
4368
- Content Required: ${requestBody?.required ?? 'Not Provided'}
4469
`
4570

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+
46105
const requestBodyMarkdown = requestBody?.content?.['application/json']?.schema
47-
? jsonToMarkdownTable({
48-
schema: requestBody?.content?.['application/json']?.schema
49-
})
106+
? mdc.toString()
50107
: ''
51108

52109
return requestBodyAdditionalInfo + requestBodyMarkdown

0 commit comments

Comments
 (0)