npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-openapi-form-generator

v1.1.1

Published

Generates Angular ReactiveForm factories based on a Swagger or OpenAPI definition

Downloads

18

Readme

Angular Form Generator

Generates an Angular ReactiveForm from a Swagger or OpenAPI definition.

Based on verizonconnect/ngx-form-generator repository

Install

# install locally as development dependency in project (recommended)
npm i ngx-openapi-form-generator --save-dev

# install globally on your system (not recommended)
npm i ngx-openapi-form-generator -g

Usage

CLI

# TODO CLI usage

| Option | Alias | Comment | Required | | ------------ | ---------------- | ------------------------------------------------ | -------- | | --version | | Show version number | | | --input-spec | -i, --swaggerUrl | Location of the OpenAPI spec as URL or file path | ✓ | | --output | -o, --outDir | Folder where to write the generated files | | | --help | -h | Show help | |

Library Usage

const {FormOpenapiGenerator, DefaultOutputFormatter} = require("ngx-openapi-form-generator");

// Basebath or file url to your OpenAPI definition
let specFileOrUrlPath = "https://localhost:5001/swagger/v1/swagger.json";

async function main() {
    return FormOpenapiGenerator.from({
        specFileOrUrlPath: specFileOrUrlPath,
        output: new DefaultOutputFormatter({
            outputFolder: 'src/my-api/form/'
        })
    });
}
main();

Run this fule using node command line.

Usage

Generation output

templateProperty.ts

export interface TemplateProperty {
    isRequired?: boolean;
    maxLength?: number;
}

candidateProfileModel.ts

import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { TemplateProperty } from './templateProperty';

export interface CandidateProfileEditModelTemplate {
    description: TemplateProperty;
    lastName: TemplateProperty;
    firstName: TemplateProperty;
    birthdate: TemplateProperty;
    placesDetailsResponse: TemplateProperty;
}

export const candidateProfileEditModelTemplate: CandidateProfileEditModelTemplate = {
    description: {
        maxLength: 2500
    },
    lastName: {
        maxLength: 150
    },
    firstName: {
        maxLength: 150
    },
    birthdate: {},
    placesDetailsResponse: {}
};

export class CandidateProfileEditModelFactory {
    private readonly _fields = {
        description: [Validators.maxLength(2500)],
        lastName: [Validators.maxLength(150)],
        firstName: [Validators.maxLength(150)],
        birthdate: [],
        placesDetailsResponse: []
    };

    constructor(private readonly _formBuilder: FormBuilder) {}

    public fillForm(form: FormGroup): void {
        Object.keys(this._fields).forEach(fieldKey => {
            form.addControl(
                fieldKey,
                this._formBuilder.control(null, this._fields[fieldKey])
            );
        });
    }
}

index.ts

export {
    candidateProfileEditModelTemplate,
    CandidateProfileEditModelTemplate,
    CandidateProfileEditModelFactory
} from './candidateProfileEditModel';
export { TemplateProperty } from './templateProperty';

Usage

app.component.ts

import {
    CandidateProfileEditModel,
    CandidateProfileEditModelFactory,
    candidateProfileEditModelTemplate
} from './my-api/form';
// {...}

@Component({
    selector   : 'app-root',
    templateUrl: './app.component.html',
    styleUrls  : ['./app.component.scss']
})
export class AppComponent implements OnDestroy, OnInit {
    
    // {...}

    public readonly form: FormGroup;
    public readonly template: CandidateProfileEditModelTemplate = candidateProfileEditModelTemplate;

    constructor(private readonly _formBuilder: FormBuilder) {
        const formFactory = new CandidateProfileEditModelFactory(this._formBuilder);
        this.form = this._formBuilder.group({});
        formFactory.fillForm(this.form);
    }
    
    // {...}
}

app.component.html

<form [formGroup]="form">
    <input [formControlName]="'lastName'"
           [maxlength]="template.lastName.maxLength">
</form>

Advanced usage

This is recommended to use a directive automatically applying properties to your html fields. A very basic implementation could be:

import {Directive, ElementRef, Input} from '@angular/core';
import {TemplateProperty} from './my-api/form';

@Directive({
    selector: '[validationModel]'
})
export class ValidationModelDirective {

    @Input()
    public set validationModel(val: TemplateProperty) {
        const input: HTMLInputElement|HTMLTextAreaElement = this.el.nativeElement;
        if (val.maxLength != null) {
            input.maxLength = val.maxLength;
        }
        if (val.isRequired != null) {
            input.required = val.isRequired;
        }
    }

    constructor(private readonly el: ElementRef) {
    }

}

And usage would be

<form [formGroup]="form">
    <input [formControlName]="'lastName'"
           [validationModel]="template.lastName">
</form>

Reference