@asyncapi/generator-model-sdk
v0.7.0
Published
The Model SDK for generating data models
Downloads
164
Readme
AsyncAPI Model SDK is a set of classes/functions for generating data models from JSON Schema and AsyncAPI spec.
:loudspeaker: ATTENTION:
This package is under development and it has not reached version 1.0.0 yet, which means its API might get breaking changes without prior notice. Once it reaches its first stable version, we'll follow semantic versioning.
Installation
Run this command to install the SDK in your project:
npm install --save @asyncapi/generator-model-sdk
How it works
The process of creating data models from input data consists of 2 processes, the input and generation process.
The input process
The input process ensures that any supported input is handled correctly, the basics are that any input needs to be converted into our internal model representation CommonModel
. The following inputs are supported:
- JSON Schema Draft 7, this is the default inferred input if we cannot find another input processor.
- AsyncAPI version 2.0.0
Read more about the input process here.
The generation process
The generation process uses the predefined CommonModel
s from the input stage to easily generate models regardless of input. The package supports the following output languages:
- JavaScript
- TypeScript
- Java
Check out the example to see how to use the library and generators document for more info.
NOTE: Each implemented language has different options, dictated by the nature of the language. Keep this in mind when selecting a language.
Example
import { TypeScriptGenerator } from '@asyncapi/generator-model-sdk';
const generator = new TypeScriptGenerator({ modelType: 'interface' });
const doc = {
$id: "Address",
type: "object",
properties: {
street_name: { type: "string" },
city: { type: "string", description: "City description" },
state: { type: "string" },
house_number: { type: "number" },
marriage: { type: "boolean", description: "Status if marriage live in given house" },
pet_names: { type: "array", items: { type: "string" } },
state: { type: "string", enum: ["Texas", "Alabama", "California", "other"] },
},
required: ["street_name", "city", "state", "house_number", "state"],
};
const interfaceModel = await generator.generate(doc);
// interfaceModel[0] should have the shape:
interface Address {
streetName: string;
city: string;
state: string;
houseNumber: number;
marriage?: boolean;
petNames?: Array<string>;
state?: "Texas" | "Alabama" | "California" | "other";
}
Supported input
These are the supported inputs.
AsyncAPI input
The library expects the asyncapi
property for the document to be sat in order to understand the input format.
- Generate from a parsed AsyncAPI document:
const parser = require('@asyncapi/parser');
const doc = await parser.parse(`{asyncapi: '2.0.0'}`);
generator.generate(doc);
- Generate from a pure JS object:
generator.generate({asyncapi: '2.0.0'});
JSON Schema input
- Generate from a pure JS object:
generator.generate({$schema: 'http://json-schema.org/draft-07/schema#'});
Customization
There are multiple ways to customize the library both in terms of processing, logging and output generation, check the customization document.
Development
To setup your development environment please read the development document.
Contributing
Read CONTRIBUTING guide.
Contributors ✨
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!