UNPKG

@infinite-debugger/swagger-to-ts

Version:

swagger-to-ts is a powerful library that allows you to generate TypeScript code from Swagger documentation or OpenAPI specifications. It simplifies the process of integrating API definitions into your TypeScript projects, saving you time and effort.

114 lines (107 loc) 8.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.cleanEmptyFoldersRecursively = exports.getHelpText = exports.getGeneratedFileWarningComment = exports.getImportsCode = exports.addModuleImport = void 0; const path_1 = require("path"); const fs_extra_1 = require("fs-extra"); const config_1 = require("../config"); const addModuleImport = ({ imports, importFilePath, importName, }) => { if (!imports[importFilePath]) { imports[importFilePath] = []; } if (!imports[importFilePath].includes(importName)) { imports[importFilePath].push(importName); } }; exports.addModuleImport = addModuleImport; const getImportsCode = ({ imports }) => { if (imports) { return Object.entries(imports) .filter(([, importFilePath]) => { return importFilePath.length > 0; }) .map(([importFilePath, importNames]) => { return `import { ${importNames.join(', ')} } from '${importFilePath}';`; }); } return []; }; exports.getImportsCode = getImportsCode; //#endregion //#region Get auto-generated file warning comment const getGeneratedFileWarningComment = () => { return ` /** * AUTO-GENERATED FILE * * WARNING: DO NOT MODIFY THIS FILE MANUALLY * * This file has been automatically generated by the ${config_1.pkg.name} library. * Any manual changes made to this file will be overwritten the next time the library is run. * If you need to make changes to the TypeScript definitions, modify the OpenAPI specification file * and regenerate the TypeScript code using the ${config_1.pkg.name} library. */ `.trimIndent(); }; exports.getGeneratedFileWarningComment = getGeneratedFileWarningComment; //#endregion //#region Get help text const getHelpText = () => { return ` ${config_1.pkg.name} Description: ${config_1.pkg.name} is a powerful tool that allows you to effortlessly generate TypeScript code from Swagger documents or OpenAPI specifications. This tool streamlines the process of creating strongly typed models, API clients, and request/response objects in TypeScript, saving you valuable development time. Usage: swagger-to-ts [options] Options: -o, --output <directory>: Specifies the output directory for the generated TypeScript code. If not provided, the code will be generated in the current directory. -f, --file <file>: Specifies the Swagger document or OpenAPI specification file to use for generating the TypeScript code. If not provided, the application will look for a file named swagger.json in the current directory. -h, --help: Displays the help text and usage instructions for the command line application. -v, --version: Displays the version information of the swagger-to-ts command line application. -wIS, --write-internal-state: Outputs the internal state of the application to the output directory. This is useful for debugging purposes. -rONS, --request-operation-name-source: Specifies the source for the request operation name. Valid values are 'path' and 'operationId'. If not provided, the default value is 'path'. -gTC, --generate-tsed-controllers: Generates controllers for the Ts.ED framework. If not provided, the default value is 'false'. -tADIP, --tsed-authenticate-decorator-import-path: Specifies the import path for the Ts.ED @Authenticate decorator. If not provided, the default value is '@tsed/common'. -tAADIP, --tsed-authorize-decorator-import-path: Specifies the import path for the Ts.ED @Authorize decorator. If not provided, the Authorize decorator will not be added to controller methods. -tCNP, --tsed-controller-name-prefix: Specifies the prefix to use for the Ts.ED controller names. -tCNS, --tsed-controller-name-suffix: Specifies the suffix to use for the Ts.ED controller names. -niTFVS, --no-infer-type-from-validation-schema: Disables the automatic inference of TypeScript types from the validation schema. If not provided, the default value is 'false'. -pRH, --propagate-request-headers: Propagates the request headers to the generated API client classes. If not provided, the default value is 'false'. -sN, --scope-name: Specifies the name of the scope to use for the generated TypeScript code. Example: To generate TypeScript code from a Swagger document named swagger.json and save it in a directory named generated, you can use the following command: swagger-to-ts -o generated swagger.json Additional Information: - ${config_1.pkg.name} uses the Swagger/OAS specification to generate TypeScript code. - The generated TypeScript code includes interfaces for models, request/response objects, and API client classes. - The application attempts to infer appropriate TypeScript types based on the Swagger document's schema definitions. - If you encounter any issues or need further assistance, please refer to the ${config_1.pkg.name} documentation for troubleshooting and examples. Remember to have the necessary dependencies installed and accessible in your environment before running the ${config_1.pkg.name} command line application. For more information, please visit the ${config_1.pkg.name} GitHub repository or the official Swagger documentation. `.trimIndent(); }; exports.getHelpText = getHelpText; //#endregion //#region Clean empty folder recursively const cleanEmptyFoldersRecursively = (folder) => { const isDir = (0, fs_extra_1.statSync)(folder).isDirectory(); if (!isDir) { return; } let files = (0, fs_extra_1.readdirSync)(folder); if (files.length > 0) { files.forEach((file) => { const fullPath = (0, path_1.join)(folder, file); (0, exports.cleanEmptyFoldersRecursively)(fullPath); }); // re-evaluate files; after deleting subfolder // we may have parent folder empty now files = (0, fs_extra_1.readdirSync)(folder); } if (files.length == 0) { (0, fs_extra_1.rmdirSync)(folder); return; } }; exports.cleanEmptyFoldersRecursively = cleanEmptyFoldersRecursively; //#endregion //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiVXRpbHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvYXBpL1V0aWxzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLCtCQUE0QjtBQUU1Qix1Q0FBNEQ7QUFFNUQsc0NBQWdDO0FBVXpCLE1BQU0sZUFBZSxHQUFHLENBQUMsRUFDOUIsT0FBTyxFQUNQLGNBQWMsRUFDZCxVQUFVLEdBQ2EsRUFBRSxFQUFFO0lBQzNCLElBQUksQ0FBQyxPQUFPLENBQUMsY0FBYyxDQUFDLEVBQUU7UUFDNUIsT0FBTyxDQUFDLGNBQWMsQ0FBQyxHQUFHLEVBQUUsQ0FBQztLQUM5QjtJQUNELElBQUksQ0FBQyxPQUFPLENBQUMsY0FBYyxDQUFDLENBQUMsUUFBUSxDQUFDLFVBQVUsQ0FBQyxFQUFFO1FBQ2pELE9BQU8sQ0FBQyxjQUFjLENBQUMsQ0FBQyxJQUFJLENBQUMsVUFBVSxDQUFDLENBQUM7S0FDMUM7QUFDSCxDQUFDLENBQUM7QUFYVyxRQUFBLGVBQWUsbUJBVzFCO0FBT0ssTUFBTSxjQUFjLEdBQUcsQ0FBQyxFQUFFLE9BQU8sRUFBeUIsRUFBRSxFQUFFO0lBQ25FLElBQUksT0FBTyxFQUFFO1FBQ1gsT0FBTyxNQUFNLENBQUMsT0FBTyxDQUFDLE9BQVEsQ0FBQzthQUM1QixNQUFNLENBQUMsQ0FBQyxDQUFDLEVBQUUsY0FBYyxDQUFDLEVBQUUsRUFBRTtZQUM3QixPQUFP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