UNPKG

@elliots/openapi-ts-backend

Version:

Enables easy implementions of OpenAPI REST APIs in TypeScript with full typings of schemas and operations.

84 lines 4.03 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const yaml = __importStar(require("js-yaml")); const openapi_typescript_1 = __importDefault(require("openapi-typescript")); const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const parser_1 = require("./parser"); const templates = __importStar(require("./templates")); const YAML_EXTENSION = /\.ya?ml$/; function write(dirName, fileName, data) { const outputPath = path_1.default.resolve(dirName, fileName); fs_1.writeFileSync(outputPath, data); return outputPath; } function createSpecTypes(specPath, outputDir) { return __awaiter(this, void 0, void 0, function* () { const raw = fs_1.readFileSync(specPath).toString(); const schema = (YAML_EXTENSION.test(specPath)) ? yaml.load(raw) : JSON.parse(raw); const ts = openapi_typescript_1.default(schema); return write(outputDir, 'spec.ts', ts); }); } function main(program, command, args) { return __awaiter(this, void 0, void 0, function* () { const excludeHandlers = args.includes('--exclude-handlers'); const [inputFile, outputDir] = args.slice(-2); if (!inputFile || !outputDir) { throw new Error(`Usage: ${program} ${command} <path to OpenAPI document> <output directory>`); } if (!fs_1.existsSync(outputDir)) { fs_1.mkdirSync(outputDir); } const specTypesPath = yield createSpecTypes(inputFile, outputDir); const operationIds = parser_1.getApiOperationIds(specTypesPath); const modules = ['requests']; if (!excludeHandlers) { modules.push('handlers'); } write(outputDir, `utils.ts`, templates.utils); if (modules.includes('requests')) { write(outputDir, `requests.ts`, templates.requests); } if (modules.includes('handlers')) { write(outputDir, `handlers.ts`, templates.handlers.replace('$OPERATIONS', operationIds.map(id => ` ${id}: OperationHandler<T, '${id}'>;`).join('\n'))); } write(outputDir, `index.ts`, templates.index + modules .map(module => `export * from './${module}';`) .join('\n')); console.log(`Types written to ${outputDir}`); }); } exports.default = main; //# sourceMappingURL=index.js.map