some change
This commit is contained in:
parent
d4a1492d56
commit
313f1def49
38 changed files with 475 additions and 401 deletions
29
scripts/generate-client.mjs
Normal file
29
scripts/generate-client.mjs
Normal file
|
@ -0,0 +1,29 @@
|
|||
import openapiTS, { astToString } from 'openapi-typescript'
|
||||
import prettier from 'prettier'
|
||||
import path from 'path'
|
||||
import fs from 'node:fs/promises'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const { format, resolveConfig } = prettier
|
||||
/**
|
||||
* @param openapiUrl {string}
|
||||
* @param outputFilePath {string}
|
||||
* @param pathToPrettierRc {string}
|
||||
*/
|
||||
export async function generateApiClient(openapiUrl, outputFilePath, pathToPrettierRc) {
|
||||
const request = new Request(openapiUrl)
|
||||
const response = await fetch(request)
|
||||
const json = await response.text()
|
||||
const ast = await openapiTS(json, {})
|
||||
const prettierConfig = await resolveConfig(pathToPrettierRc, {
|
||||
useCache: true,
|
||||
})
|
||||
let schemaCode = astToString(ast)
|
||||
schemaCode = await format(schemaCode, { parser: 'typescript', ...prettierConfig })
|
||||
await fs.writeFile(path.join(outputFilePath), schemaCode)
|
||||
}
|
||||
|
||||
if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
||||
if (!process.env.OPENAPI_URL) throw new Error('OPENAPI_URL is not defined')
|
||||
await generateApiClient(process.env.OPENAPI_URL, './src/app/api/schema.ts', './.prettierrc')
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue