mirror of
https://github.com/ritz078/transform.git
synced 2026-12-04 13:54:30 +03:00
closes #169
This commit is contained in:
1 parent
16cf173411
commit
fea5053ca7
8 files changed
+154
-1491
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { SchemaGenerator } from "ts-json-schema-generator";
|
||||
import * as ts from "typescript";
|
||||
import { createFormatter } from "ts-json-schema-generator/dist/factory/formatter";
|
||||
import { createParser } from "ts-json-schema-generator/dist/factory/parser";
|
||||
import { createProgram } from "ts-json-schema-generator/dist/factory/program";
|
||||
import { Config } from "ts-json-schema-generator/dist/src/Config";
|
||||
import tempy from "tempy";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default (req: NextApiRequest, res: NextApiResponse) => {
|
||||
try {
|
||||
const filePath = tempy.file({ extension: "ts" });
|
||||
fs.writeFileSync(filePath, req.body);
|
||||
const config: Config = {
|
||||
path: path.resolve(filePath),
|
||||
expose: "all",
|
||||
topRef: true,
|
||||
jsDoc: "extended",
|
||||
type: "*"
|
||||
};
|
||||
const program = createProgram(config);
|
||||
|
||||
const generator: SchemaGenerator = new SchemaGenerator(
|
||||
program,
|
||||
createParser(program, config),
|
||||
createFormatter(config)
|
||||
);
|
||||
const schema = generator.createSchema("*");
|
||||
res.status(200).send(schema);
|
||||
} catch (e) {
|
||||
res.status(500).send(e.message);
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user