Wondering what’s next for npm?Check out our public roadmap! »

    @asyncapi/generator-model-sdk
    TypeScript icon, indicating that this package has built-in type declarations

    0.7.0 • Public • Published

    AsyncAPI logo
    AsyncAPI Model SDK

    The official SDK for generating data models from JSON Schema and AsyncAPI spec

    Coverage Status

    All Contributors

    AsyncAPI Model SDK is a set of classes/functions for generating data models from JSON Schema and AsyncAPI spec.


    📢 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:

    Read more about the input process here.

    The generation process

    The generation process uses the predefined CommonModels 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.

    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):


    Maciej Urbańczyk

    🐛 💻 📖 🤔 🚧 💬 ⚠️ 👀

    czlowiek488

    🐛 👀 🤔

    Sergio Moya

    🐛

    Jonas Lagoni

    🐛 💻 📖 🤔 🚧 💬 ⚠️ 👀

    Lukasz Gornicki

    👀

    Arjun Garg

    💻

    Fran Méndez

    👀

    This project follows the all-contributors specification. Contributions of any kind welcome!

    Install

    npm i @asyncapi/generator-model-sdk

    DownloadsWeekly Downloads

    491

    Version

    0.7.0

    License

    Apache-2.0

    Unpacked Size

    289 kB

    Total Files

    174

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar