-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.mjs
54 lines (46 loc) · 1.59 KB
/
generate.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import openapiTS, { astToString } from 'openapi-typescript'
import * as fs from 'fs'
import * as yaml from 'yaml'
const schemaObject = yaml.parse(fs.readFileSync('resources/fingerprint-server-api.yaml', 'utf-8'))
// Function to resolve $ref paths
function getObjectByRef(refPath, schema) {
if (!refPath.startsWith('#/')) {
throw new Error('Only local $refs starting with "#/" are supported')
}
// Split the path into parts, e.g., "#/components/schemas/GeolocationCity" -> ["components", "schemas", "GeolocationCity"]
const pathParts = refPath.slice(2).split('/')
// Traverse the schema object based on path parts
let current = schema
for (const part of pathParts) {
if (current[part] !== undefined) {
current = current[part]
} else {
throw new Error(`Path not found: ${refPath}`)
}
}
return current
}
try {
const result = await openapiTS(schemaObject, {
/**
* Enhances generated documentation by propagating it from source object in schema to all properties that use it as $ref.
* */
transform: (schema) => {
if (schema.type === 'object' && Boolean(schema.properties)) {
Object.entries(schema.properties).forEach(([key, value]) => {
if (value.$ref && !value.description) {
const source = getObjectByRef(value.$ref, schemaObject)
schema.properties[key] = {
...value,
description: source?.description,
}
}
})
}
},
})
fs.writeFileSync('./src/generatedApiTypes.ts', astToString(result))
} catch (e) {
console.error(e)
process.exit(1)
}