Skip to content

Commit

Permalink
feat: first component lib
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg committed Oct 29, 2024
1 parent 261b66e commit d110189
Show file tree
Hide file tree
Showing 4 changed files with 1,123 additions and 30 deletions.
5 changes: 5 additions & 0 deletions apps/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const models = require('./models');

module.exports = {
models,
};
44 changes: 44 additions & 0 deletions apps/components/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { File } from '@asyncapi/generator-react-sdk';
import { PythonGenerator, JavaGenerator, TypeScriptGenerator, FormatHelpers } from '@asyncapi/modelina';

/**
* @typedef {'python' | 'java' | 'typescript'} Language
* Represents the available programming languages for model generation.
*/

/**
* @typedef RenderArgument
* @type {object}
* @property {AsyncAPIDocument} asyncapi AsyncAPI document object received from the generator after initial parsing with parser-js.
* @property {Language} [language='python'] The target programming language for the generated models.
*/

/**
* Mapping of language strings to Modelina generator classes and file extensions.
*/
const generatorConfig = {
python: { generator: PythonGenerator, extension: 'py' },
java: { generator: JavaGenerator, extension: 'java' },
typescript: { generator: TypeScriptGenerator, extension: 'ts' },
};

/**
* Render all schema models
* @param {RenderArgument} param0
* @returns {Array<File>} Array of File components with generated model content.
*/
export default async function models({ asyncapi, language = 'python' }) {
// Get the selected generator and file extension, defaulting to Python if unknown
const { generator: GeneratorClass, extension } = generatorConfig[language] || generatorConfig.python;
const generator = new GeneratorClass();

const models = await generator.generate(asyncapi);
const files = [];

for (const model of models) {
const modelFileName = `${FormatHelpers.toPascalCase(model.modelName)}.${extension}`;
files.push(<File name={modelFileName}>{model.result}</File>);
}

return files;
}
19 changes: 19 additions & 0 deletions apps/components/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@asyncapi/generator-components",
"version": "1.0.0",
"description": "Package with reusable components for generation using React render engine",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/asyncapi/generator/apps/components"
},
"author": "Lukasz Gornicki",
"license": "Apache-2.0",
"dependencies": {
"@asyncapi/generator-react-sdk": "^1.1.2",
"@asyncapi/modelina": "^4.0.0-next.62"
}
}
Loading

0 comments on commit d110189

Please sign in to comment.