Skip to content

Commit 330917c

Browse files
committed
chore: update dependencies and enhance parseMethodMetadata function to accept interfaces
1 parent 596039a commit 330917c

15 files changed

Lines changed: 92 additions & 36 deletions

File tree

packages/parser/src/parses/method.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function parseMethodParameters({ method, parameters, path }: PathMethod,
7979
return config
8080
}
8181

82-
export function parseMethodMetadata({ method, path, responses, options: meta }: PathMethod) {
82+
export function parseMethodMetadata({ method, path, responses, options: meta }: PathMethod, interfaces?: StatementInterface[]) {
8383
const comments = [
8484
meta.summary && `@summary ${meta.summary}`,
8585
meta.description && `@description ${meta.description}`,
@@ -89,14 +89,16 @@ export function parseMethodMetadata({ method, path, responses, options: meta }:
8989
]
9090

9191
const name = camelCase(`${method}/${path}`)
92+
9293
const url = `${path.replace(/(\{)/g, '${paths.')}`
9394
const responseSchema
9495
// @ts-expect-error
9596
= responses.default?.content?.['application/json']?.schema
9697
// @ts-expect-error
9798
|| responses['200']?.content?.['application/json']?.schema
99+
|| responses['200']?.schema
98100
|| responses['200']
99-
const responseType = responseSchema ? parseSchemaType(responseSchema) : 'void'
101+
const responseType = responseSchema ? parseSchemaType(responseSchema, interfaces) : 'void'
100102

101103
return { description: comments.filter(Boolean), name, url, responseType, body: [] as string[] }
102104
}

packages/parser/src/parses/schema.ts

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import type { Schema } from 'openapi-specification-types'
2-
import { isArray, uniq } from '@hairy/utils'
1+
import type { StatementInterface } from '@genapi/shared'
2+
import type { StatementField } from '@genapi/shared/dist/index.mjs'
33

4+
import type { Properties, Schema } from 'openapi-specification-types'
5+
import { isArray, uniq } from '@hairy/utils'
46
import { spliceEnumType, useRefMap, varName } from '../utils'
57

68
/**
79
* parse schema to type
810
* @param propertie
911
*/
10-
export function parseSchemaType(propertie: Schema): string {
12+
export function parseSchemaType(propertie: Schema, interfaces: StatementInterface[] = []): string {
1113
if (!propertie)
1214
return 'any'
1315
if (propertie.originalRef)
@@ -16,9 +18,61 @@ export function parseSchemaType(propertie: Schema): string {
1618
if (propertie.$ref)
1719
return varName(useRefMap(propertie.$ref))
1820

21+
// Handle allOf: merge multiple schemas, typically used for generic response types
22+
if (propertie.allOf) {
23+
const fields: Record<string, StatementField> = {}
24+
let base = 'Anonymous'
25+
26+
// Merge all schema attributes in allOf
27+
for (const schema of propertie.allOf) {
28+
// Handling $ref references: merging properties of reference interfaces
29+
assignBaseProperties(schema)
30+
// Processing direct attributes of schema
31+
assignProperties(schema.properties)
32+
}
33+
34+
assignProperties(propertie.properties)
35+
36+
function assignBaseProperties(schema: Schema) {
37+
if (!schema.$ref)
38+
return
39+
const type = parseSchemaType(schema, interfaces)
40+
const interfaceItem = interfaces.find(v => v.name === type)
41+
if (!interfaceItem)
42+
return
43+
for (const property of interfaceItem.properties || []) {
44+
fields[property.name] = property
45+
}
46+
base = interfaceItem.name
47+
}
48+
function assignProperties(properties?: Properties) {
49+
for (const [field, item] of Object.entries(properties || {})) {
50+
const type = parseSchemaType(item, interfaces)
51+
if (item.$ref)
52+
base = type
53+
fields[field] = {
54+
type,
55+
required: item.required,
56+
description: item.description,
57+
name: field,
58+
}
59+
}
60+
}
61+
62+
// generate a unique interface name
63+
let name = `AllOf${base}`
64+
let counter = 1
65+
while (interfaces.some(v => v.name === name)) {
66+
name = `AllOf${base}${counter++}`
67+
}
68+
interfaces.push({ name, properties: Object.values(fields), export: true })
69+
return name
70+
}
71+
1972
if (propertie.schema)
20-
return parseSchemaType(propertie.schema)
73+
return parseSchemaType(propertie.schema, interfaces)
2174

75+
// TODO: handle additionalProperties
2276
if (propertie.additionalProperties)
2377
return `Record<string, ${parseSchemaType(propertie.additionalProperties)}>`
2478

packages/presets/src/axios/js/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
5757
body: 'data',
5858
query: 'params',
5959
})
60-
let { name, description, url, responseType } = parseMethodMetadata(config)
60+
let { name, description, url, responseType } = parseMethodMetadata(config, interfaces)
6161

6262
options.push(['...', 'config'])
6363
interfaces.push(...attachInters)

packages/presets/src/axios/ts/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
5858
query: 'params',
5959
})
6060

61-
let { name, description, url, responseType } = parseMethodMetadata(config)
61+
let { name, description, url, responseType } = parseMethodMetadata(config, interfaces)
6262

6363
options.push(['...', 'config'])
6464
interfaces.push(...attachInters)

packages/presets/src/fetch/js/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
6060
formData: 'body',
6161
})
6262

63-
let { name, description, url, responseType, body } = parseMethodMetadata(config)
63+
let { name, description, url, responseType, body } = parseMethodMetadata(config, interfaces)
6464

6565
interfaces.push(...attachInters)
6666
parameters.push({

packages/presets/src/fetch/ts/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
5555
* function params/function options/function use interfaces
5656
*/
5757
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config)
58-
let { name, description, url, responseType, body } = parseMethodMetadata(config)
58+
let { name, description, url, responseType, body } = parseMethodMetadata(config, interfaces)
5959

6060
interfaces.push(...attachInters)
6161
parameters.push({

packages/presets/src/got/js/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
5656
* function params/function options/function use interfaces
5757
*/
5858
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config)
59-
let { name, description, url, responseType } = parseMethodMetadata(config)
59+
let { name, description, url, responseType } = parseMethodMetadata(config, interfaces)
6060

6161
interfaces.push(...attachInters)
6262
parameters.push({

packages/presets/src/got/ts/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
5656
* function params/function options/function use interfaces
5757
*/
5858
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config)
59-
let { name, description, url, responseType } = parseMethodMetadata(config)
59+
let { name, description, url, responseType } = parseMethodMetadata(config, interfaces)
6060

6161
interfaces.push(...attachInters)
6262
parameters.push({

packages/presets/src/ky/js/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
5656
* function params/function options/function use interfaces
5757
*/
5858
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config)
59-
let { name, description, url, responseType } = parseMethodMetadata(config)
59+
let { name, description, url, responseType } = parseMethodMetadata(config, interfaces)
6060

6161
interfaces.push(...attachInters)
6262
parameters.push({

packages/presets/src/ky/ts/parser/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function transformPaths(paths: Paths, { configRead, functions, interfaces
5656
* function params/function options/function use interfaces
5757
*/
5858
const { parameters, interfaces: attachInters, options } = parseMethodParameters(config)
59-
let { name, description, url, responseType } = parseMethodMetadata(config)
59+
let { name, description, url, responseType } = parseMethodMetadata(config, interfaces)
6060

6161
interfaces.push(...attachInters)
6262
parameters.push({

0 commit comments

Comments
 (0)