UNPKG

typescript-scaffolder

Version:

![npm version](https://img.shields.io/npm/v/typescript-scaffolder) ### Unit Test Coverage: 97.12%

114 lines (113 loc) 4.82 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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 () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.generateApiRegistry = generateApiRegistry; exports.getApiFunction = getApiFunction; const path = __importStar(require("path")); const fs = __importStar(require("fs")); const file_system_1 = require("../utils/file-system"); const logger_1 = require("../utils/logger"); const client_constructors_1 = require("../utils/client-constructors"); /** * Creates a registry of functions within a file that can be used at runtime. This function will produce * an object in the following shape, where the field name equals the folder name where the api files are * located * * Example: * export const apiRegistry = { * 'source-charlie': { * ...person_api * }, * 'source-delta': { * ...user_api * } * }; * * @param apiRootDir - the parent directory where the Api files are located. Will go into child directories * @param registryFileName - the name of the registry file */ async function generateApiRegistry(apiRootDir, registryFileName = 'registry.ts') { const funcName = 'generateApiRegistry'; logger_1.Logger.debug(funcName, 'Generating registry...'); const importMap = new Map(); // Map<subdirectory, filePaths> (0, file_system_1.walkDirectory)(apiRootDir, (filePath) => { if (filePath.endsWith('.ts') && !filePath.endsWith(registryFileName)) { const subDir = path.relative(apiRootDir, path.dirname(filePath)); const files = importMap.get(subDir) || []; files.push(filePath); importMap.set(subDir, files); } }, '.ts'); if (importMap.size === 0) { logger_1.Logger.warn(funcName, 'No API files found. Registry will not be generated.'); return; } const { importStatements, registryEntries } = (0, client_constructors_1.buildImportMapAndRegistryEntries)(importMap); const output = `${importStatements.join('\n')} export const apiRegistry = { ${registryEntries.join(',\n')} }; `; const registryPath = path.join(apiRootDir, registryFileName); fs.writeFileSync(registryPath, output, 'utf8'); } /** * Dynamically retrieves an API function from the generated registry. * * Note: The returned function's argument signature must be known by the caller. * This function does not enforce parameter validation or ordering. * It is the caller's responsibility to pass the correct arguments. * * @param apiRegistry - A nested record of service -> function name -> API function. * @param service - The top-level key representing a group of API clients (e.g. a namespace). * @param functionName - The exported name of the function within the selected service. * @returns The referenced API function. * @throws If the function or service cannot be found in the registry. */ function getApiFunction(apiRegistry, service, functionName) { const funcName = 'getApiFunction'; logger_1.Logger.debug(funcName, 'Fetching api function...'); const serviceRegistry = apiRegistry?.[service]; if (!serviceRegistry) { logger_1.Logger.warn(funcName, 'No service registry found.'); } const fn = serviceRegistry?.[functionName]; if (!fn || typeof fn !== 'function') { const msg = `Function "${functionName}" not found in service "${service}".`; logger_1.Logger.error(funcName, `Function "${functionName}" not found in service "${service}".`); throw new Error(msg); } return fn; }