UNPKG

template-nestjs-generator

Version:

A custom NestJS CRUD generator

90 lines (80 loc) 2.69 kB
import * as Handlebars from "handlebars" export function configHandlebars() { Handlebars.registerHelper("toLowerCase", function (value) { return value && typeof value === "string" ? value.toLowerCase() : "" }) Handlebars.registerHelper("newLine", () => "\n") // Helper para negación Handlebars.registerHelper("not", function (value) { return !value }) // Helper para condicional 'or' Handlebars.registerHelper("or", function (value1, value2) { return value1 || value2 }) // Helper para stringify Handlebars.registerHelper("json", function (context) { return JSON.stringify(context) }) Handlebars.registerHelper( "checkRequired", function ( this: any, isRequired: any, options: Handlebars.HelperOptions ) { if (isRequired && isRequired !== undefined && isRequired !== null) { return options.fn(this) } else { return options.inverse(this) } } ) Handlebars.registerHelper( "ifDefined", function (this: any, value: any, options: Handlebars.HelperOptions) { if (value !== undefined && value !== null) { return options.fn(this) } else { return options.inverse(this) } } ) Handlebars.registerHelper("typeValidator", function (type) { switch (type) { case "string": return "@IsString()" case "number": return "@IsNumber()" case "boolean": return "@IsBoolean()" case "Date": return "@Type(() => Date) @IsDate()" default: return "" } }) Handlebars.registerHelper("quoteIfString", function (value: any) { if (typeof value === "string") { return `"${value}"` } return value }) Handlebars.registerHelper( "importValidators", function (properties, options) { const validators = new Set() properties?.forEach((prop: { validators: string[] }) => { prop.validators?.forEach((validator) => { validators?.add(validator) }) }) return Array?.from(validators) ?.map( (validator) => `import { ${validator} } from 'class-validator';` ) ?.join("\n") } ) }