-
Notifications
You must be signed in to change notification settings - Fork 3
/
genApiFromSchema.js
35 lines (24 loc) · 1.35 KB
/
genApiFromSchema.js
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
import $RefParser from "@apidevtools/json-schema-ref-parser"
import { compile } from "json-schema-to-typescript"
import fs from "node:fs"
import { writeFile } from "node:fs/promises"
import path from "node:path"
// TODO: Look into avoiding duplication of generated types when multiple top-level schemas
// reference the same common schemas.
// TODO: Use git tags instead of commit hashes
// To use an updated source schema, change this to point to the desired version.
const COMMIT_HASH = "4fe59e1438fad31b79c87568686cf867fd14a41e"
const SCHEMAS = ["eipComponentDef.schema.json", "eipFlow.schema.json"]
const SCHEMAS_BASE_URL = "https://raw.githubusercontent.com"
const GENERATED_SOURCE_DIR = path.resolve("src", "api", "generated")
const buildUrl = (schemaFileName) => (new URL(`OctoConsulting/keip-canvas/${COMMIT_HASH}/schemas/model/json/${schemaFileName}`, SCHEMAS_BASE_URL)).toString()
const generateSources = async (schemaFileName) => {
const bundledSchema = await $RefParser.bundle(buildUrl(schemaFileName))
const generatedCode = await compile(bundledSchema, bundledSchema.title, {
additionalProperties: false,
})
const outputFile = `${schemaFileName.split('.')[0]}.ts`
await writeFile(path.join(GENERATED_SOURCE_DIR, outputFile), generatedCode)
}
fs.mkdirSync(GENERATED_SOURCE_DIR, { recursive: true })
await Promise.all(SCHEMAS.map(generateSources))