UNPKG

bridgets

Version:

<p align="center"> <a href="https://bridgets.co"> <img src="http://bridgets.co/assets/logo-short.svg" height="48" /> <h1 align="center">BridgeTS</h1> </a> </p>

160 lines 7.87 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeController = void 0; const Utilities_1 = require("../Utilities"); const fs_1 = require("./fs"); const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema")); const json_schema_to_zod_1 = require("json-schema-to-zod"); const dataValidator_1 = require("../Handler/Validators/dataValidator"); const hasHeaders = (handler) => { if (handler.headersSchema) return true; if (handler.middlewares) for (const mid of handler.middlewares) if (hasHeaders(mid)) return true; return false; }; const hasBody = (handler) => { if (handler.bodySchema) return true; if (handler.middlewares) for (const mid of handler.middlewares) if (hasBody(mid)) return true; return false; }; const hasQuery = (handler) => { if (handler.querySchema) return true; if (handler.middlewares) for (const mid of handler.middlewares) if (hasQuery(mid)) return true; return false; }; const writeController = (controller, pathArray, sdkLocation, typeLocation, sdkTypeName) => { const controllersInside = []; //////////////////////////////////////////////////////////////////////////////////////////////////////////// // IMPORTS & CLASS TYPE //////////////////////////////////////////////////////////////////////////////////////////////////////////// Object.entries(controller).forEach(([name, controller]) => { if (!(0, Utilities_1.isController)(controller)) return; controllersInside.push([name, controller]); }); // writing the import of type from dts file // It handles index files and first stage file let file = `import { ${sdkTypeName} } from '${controllersInside.length === 0 && pathArray.length === 1 ? './' : '../'.repeat(controllersInside.length !== 0 ? pathArray.length : pathArray.length - 1)}dts/${typeLocation.replace('.ts', '').replace(/^.\//, '')}';\n`; // HAVE TO CHECK IF ZOD NEEDED file += `import { z } from 'zod';\n`; file += '\n'; if (controllersInside.length !== 0) { (0, fs_1.createFolder)((0, Utilities_1.pathArrayToPath)(pathArray, sdkLocation)); controllersInside.forEach(([ctrlName, ctrl]) => { file += `import { ${ctrl.constructor.name} } from './${ctrlName.toLocaleLowerCase()}';\n`; (0, exports.writeController)(ctrl, [...pathArray, ctrlName], sdkLocation, typeLocation, sdkTypeName); }); file += '\n'; } const className = controller.constructor.name; const typeVar = className + 'T'; file += `type ${typeVar} = ${sdkTypeName}${pathArray.map((p) => `['${p}']`).reduce((a, b) => a + b)};\n\n`; //////////////////////////////////////////////////////////////////////////////////////////////////////////// // EXPORT CLASS //////////////////////////////////////////////////////////////////////////////////////////////////////////// file += `export class ${className} {\n`; controllersInside.forEach(([ctrlName, ctrl]) => { file += ` public ${ctrlName.toLocaleLowerCase()}: ${ctrl.constructor.name};\n`; }); file += ` constructor(private Fetch: any) {`; if (controllersInside.length === 0) file += '}\n'; else { file += '\n'; controllersInside.forEach(([ctrlName, ctrl]) => { file += ` this.${ctrlName.toLocaleLowerCase()} = new ${ctrl.constructor.name}(this.Fetch);\n`; }); file += ' }\n'; } //////////////////////////////////////////////////////////////////////////////////////////////////////////// // ZOD OBBJECTS IF ANY //////////////////////////////////////////////////////////////////////////////////////////////////////////// const zodObjects = {}; Object.entries(controller).forEach(([name, handler]) => { if (!(0, Utilities_1.isBridgeHandler)(handler)) return; if ((0, dataValidator_1.isZodParser)(handler.bodySchema) || (0, dataValidator_1.isZodParser)(handler.querySchema) || (0, dataValidator_1.isZodParser)(handler.headersSchema)) zodObjects[name] = {}; else return; try { if ((0, dataValidator_1.isZodParser)(handler.bodySchema)) zodObjects[name].body = (0, json_schema_to_zod_1.jsonSchemaToZod)((0, zod_to_json_schema_1.default)(handler.bodySchema), 'zodObject', false) .replace('const zodObject = ', '') .slice(0, -2); if ((0, dataValidator_1.isZodParser)(handler.querySchema)) zodObjects[name].query = (0, json_schema_to_zod_1.jsonSchemaToZod)((0, zod_to_json_schema_1.default)(handler.querySchema), 'zodObject', false) .replace('const zodObject = ', '') .slice(0, -2); if ((0, dataValidator_1.isZodParser)(handler.headersSchema)) zodObjects[name].query = (0, json_schema_to_zod_1.jsonSchemaToZod)((0, zod_to_json_schema_1.default)(handler.headersSchema), 'zodObject', false) .replace('const zodObject = ', '') .slice(0, -2); } catch (err) { console.log('Error in zod compilation'); } }); if (Object.keys(zodObjects).length > 0) { file += '\n public zodSchemas = {\n'; Object.entries(zodObjects).forEach(([name, z]) => { file += ` ${name}: {\n`; Object.entries(z).forEach(([type, zodString]) => { file += ` ${type}: ${zodString},\n`; }); file += ' },\n'; }); file += ' };\n'; } //////////////////////////////////////////////////////////////////////////////////////////////////////////// // CLASS METHODS //////////////////////////////////////////////////////////////////////////////////////////////////////////// Object.entries(controller).forEach(([name, handler]) => { if (!(0, Utilities_1.isBridgeHandler)(handler)) return; if (handler.description) file += `\n /** ${handler.description}*/`; const paramsString = []; if (hasBody(handler)) paramsString.push(`body: ${typeVar}['${name}']['body']`); if (hasQuery(handler)) paramsString.push(`query: ${typeVar}['${name}']['query']`); if (hasHeaders(handler)) paramsString.push(`headers: ${typeVar}['${name}']['headers']`); if (handler.filesConfig) { if (handler.filesConfig === 'any') paramsString.push('files: Record<string, File>'); else paramsString.push(`files: {${handler.filesConfig .map((f) => ` ${f}: File;`) .reduce((a, b) => a + b) .slice(0, -1)} }`, ''); } const hasParams = paramsString.length > 0; file += `\n public ${name} = (${hasParams ? `p: ${(0, Utilities_1.getParamsObjectString)(paramsString)}` : ''}): Promise<${typeVar}['${name}']['return']> => {\n return this.Fetch({ path: '${[...pathArray, name] .map((p) => `/${p}`) .reduce((a, b) => a + b)}', method: '${handler.method || 'POST'}'${hasParams ? ', ...p ' : ''}});\n };\n`; }); file += `}\n`; (0, fs_1.writeFile)(controllersInside.length !== 0 ? (0, Utilities_1.pathArrayToPath)([...pathArray, 'index'], sdkLocation) : (0, Utilities_1.pathArrayToPath)(pathArray, sdkLocation), file); }; exports.writeController = writeController; //# sourceMappingURL=writeControllers.js.map