UNPKG

@docyrus/tanstack-db-generator

Version:

Code generator utilities for TanStack Query / Database integration with Docyrus API

75 lines (74 loc) 2.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractDataSources = extractDataSources; const DATA_SOURCE_PATH_REGEX = /^\/v1\/apps\/([^\/]+)\/data-sources\/([^\/]+)\/(items)(?:\/\{recordId\})?$/; function extractDataSources(spec) { const dataSourceMap = new Map(); // Process all paths for (const [path, pathItem] of Object.entries(spec.paths)) { const match = path.match(DATA_SOURCE_PATH_REGEX); if (!match) continue; const [, appName, dataSourceName] = match; const key = `${appName}/${dataSourceName}`; // Get or create data source info let dataSource = dataSourceMap.get(key); if (!dataSource) { dataSource = { appName, dataSourceName, entityName: getEntityName(appName, dataSourceName), endpoints: {} }; dataSourceMap.set(key, dataSource); } // Process each method if (pathItem.get) { if (path.includes('{recordId}')) { dataSource.endpoints.get = createOperationInfo(path, 'get', pathItem.get); } else { dataSource.endpoints.list = createOperationInfo(path, 'get', pathItem.get); } } if (pathItem.post) { dataSource.endpoints.create = createOperationInfo(path, 'post', pathItem.post); } if (pathItem.patch) { dataSource.endpoints.update = createOperationInfo(path, 'patch', pathItem.patch); } if (pathItem.delete) { if (path.includes('{recordId}')) { dataSource.endpoints.delete = createOperationInfo(path, 'delete', pathItem.delete); } else { dataSource.endpoints.deleteMany = createOperationInfo(path, 'delete', pathItem.delete); } } } return Array.from(dataSourceMap.values()); } function createOperationInfo(path, method, operation) { return { path, method, operationId: operation.operationId, parameters: operation.parameters, requestBody: operation.requestBody, responseSchema: extractResponseSchema(operation) }; } function extractResponseSchema(operation) { const successResponse = operation.responses?.['200'] || operation.responses?.['201']; if (!successResponse?.content?.['application/json']?.schema) { return null; } return successResponse.content['application/json'].schema; } function getEntityName(appName, dataSourceName) { // Convert to PascalCase const toPascalCase = (str) => str.split(/[_-]/) .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(''); return `${toPascalCase(appName)}${toPascalCase(dataSourceName)}Entity`; }