UNPKG

auto-typegen

Version:

Automatically generate TypeScript interfaces from JSON data, API responses, or database schemas. Perfect for Mongoose, Sequelize, and raw data. Simplify your workflow and ensure type safety with just one function call!

38 lines (37 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createTypedFetch = createTypedFetch; const generator_1 = require("./generator"); const normalizeData_1 = require("./utils/normalizeData"); async function createTypedFetch(options) { var _a; const generator = new generator_1.TypeGenerator(); const normalizedData = (0, normalizeData_1.normalizeData)(options.data); const interfaces = generator.generate(normalizedData, options.interfaceName); if (typeof process !== "undefined" && ((_a = process.versions) === null || _a === void 0 ? void 0 : _a.node)) { // Node.js environment const fs = require("fs"); const path = require("path"); const fullPath = path.resolve(options.outputPath); fs.mkdirSync(path.dirname(fullPath), { recursive: true }); fs.writeFileSync(fullPath, interfaces); return { success: true, path: fullPath }; } // Browser environment if (typeof window !== "undefined") { try { const blob = new Blob([interfaces], { type: "text/plain" }); const link = document.createElement("a"); link.href = URL.createObjectURL(blob); link.download = options.outputPath.split("/").pop() || "types.ts"; document.body.appendChild(link); link.click(); document.body.removeChild(link); return { success: true }; } catch (error) { return { success: false, error }; } } throw new Error("Unsupported environment"); }