@openapi-generator-plus/handlebars-templates
Advanced tools
Comparing version
@@ -805,8 +805,17 @@ "use strict"; | ||
}); | ||
hbs.registerHelper('warn', function (message) { | ||
hbs.registerHelper('warn', function () { | ||
// eslint-disable-next-line prefer-rest-params | ||
const options = arguments[arguments.length - 1]; | ||
if (arguments.length !== 2) { | ||
throw new Error(`warn helper must be called with one argument @ ${sourcePosition(options)}`); | ||
if (arguments.length < 2) { | ||
throw new Error(`warn helper must be called with one or more arguments @ ${sourcePosition(options)}`); | ||
} | ||
let message = ''; | ||
for (let i = 0; i < arguments.length - 1; i++) { | ||
const nextPart = String(arguments[i]); | ||
/* Add a space if both the preceding and following part are text (not punctuation) */ | ||
if (i > 0 && message.match(/[a-zA-Z0-9]$/) && nextPart.match(/^[a-zA-Z0-9]/)) { | ||
message += ' '; | ||
} | ||
message += nextPart; | ||
} | ||
log(types_1.CodegenLogLevel.WARN, `${message} @ ${sourcePosition(options)}`); | ||
@@ -843,2 +852,3 @@ }); | ||
registerPropertyTypeHelper('isFile', types_1.CodegenSchemaType.FILE, hbs); | ||
registerPropertyTypeHelper('isAny', types_1.CodegenSchemaType.ANY, hbs); | ||
/* Content type helpers */ | ||
@@ -974,30 +984,73 @@ hbs.registerHelper('isContentJson', function (value) { | ||
} | ||
const varNames = varName.split('.'); | ||
let context = this; | ||
for (let i = 0; i < varNames.length - 1; i++) { | ||
context = context[varNames[i]]; | ||
} | ||
varName = varNames[varNames.length - 1]; | ||
if (arguments.length === 3) { | ||
this[varName] = value; | ||
context[varName] = value; | ||
} | ||
else { | ||
const result = options.fn(this).trim(); | ||
this[varName] = result; | ||
context[varName] = result; | ||
} | ||
return null; | ||
}); | ||
hbs.registerHelper('unset', function (varName, value) { | ||
// eslint-disable-next-line prefer-rest-params | ||
const options = arguments[arguments.length - 1]; | ||
if (arguments.length !== 2) { | ||
throw new Error(`unset helper must be called with 1 argument @ ${sourcePosition(options)}`); | ||
} | ||
if (!varName) { | ||
throw new Error(`unset helper missing or undefined first argument (varName) @ ${sourcePosition(options)}`); | ||
} | ||
const varNames = varName.split('.'); | ||
let context = this; | ||
for (let i = 0; i < varNames.length - 1; i++) { | ||
context = context[varNames[i]]; | ||
} | ||
varName = varNames[varNames.length - 1]; | ||
delete context[varName]; | ||
return null; | ||
}); | ||
/** | ||
* Join the non-empty lines of the body of the block together with the given separator, and set into the named variable | ||
* Join the non-empty lines of the body of the block together with the given separator, and set into the named variable (if provided) | ||
* in the current scope. | ||
*/ | ||
hbs.registerHelper('join', function (varName, separator) { | ||
hbs.registerHelper('join', function (varNameOrSeparator, separator) { | ||
// eslint-disable-next-line prefer-rest-params | ||
const options = arguments[arguments.length - 1]; | ||
if (arguments.length !== 3) { | ||
throw new Error(`join helper must be called with 2 arguments @ ${sourcePosition(options)}`); | ||
let varName; | ||
if (arguments.length === 2) { | ||
varName = null; | ||
separator = varNameOrSeparator; | ||
} | ||
if (!varName) { | ||
throw new Error(`join helper must be called with 2 arguments; missing or undefined first argument (varName) @ ${sourcePosition(options)}`); | ||
else if (arguments.length === 3) { | ||
varName = varNameOrSeparator; | ||
} | ||
else { | ||
throw new Error(`join helper must be called with 1 or 2 arguments @ ${sourcePosition(options)}`); | ||
} | ||
if (varName && typeof varName !== 'string') { | ||
throw new Error(`join helper called with non-string varName @ ${sourcePosition(options)}`); | ||
} | ||
if (typeof separator !== 'string') { | ||
throw new Error(`join helper must be called with 2 arguments; missing or undefined second argument (separator) @ ${sourcePosition(options)}`); | ||
throw new Error(`join helper called with non-string separator (${typeof separator}) @ ${sourcePosition(options)}`); | ||
} | ||
const result = options.fn(this).split(/\r?\n/).filter(s => s.trim().length > 0).join(separator); | ||
this[varName] = result; | ||
return null; | ||
if (varName) { | ||
const varNames = varName.split('.'); | ||
let context = this; | ||
for (let i = 0; i < varNames.length - 1; i++) { | ||
context = context[varNames[i]]; | ||
} | ||
varName = varNames[varNames.length - 1]; | ||
context[varName] = result; | ||
return null; | ||
} | ||
else { | ||
return result; | ||
} | ||
}); | ||
@@ -1053,2 +1106,14 @@ /** | ||
}); | ||
hbs.registerHelper('isProperty', function (value) { | ||
return (0, types_1.isCodegenProperty)(value); | ||
}); | ||
hbs.registerHelper('isParam', function (value) { | ||
return (0, types_1.isCodegenParameter)(value); | ||
}); | ||
hbs.registerHelper('isArrayValue', function (value) { | ||
return Array.isArray(value); | ||
}); | ||
hbs.registerHelper('isObjectValue', function (value) { | ||
return typeof value === 'object' && !Array.isArray(value) && !(value instanceof Function); | ||
}); | ||
} | ||
@@ -1055,0 +1120,0 @@ exports.registerStandardHelpers = registerStandardHelpers; |
{ | ||
"name": "@openapi-generator-plus/handlebars-templates", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"description": "Utility for openapi-generator-plus generators to use with Handlebars templates", | ||
@@ -19,3 +19,3 @@ "main": "dist/index.js", | ||
"@openapi-generator-plus/indexed-type": "^1.0.0", | ||
"@openapi-generator-plus/types": "^2.16.0", | ||
"@openapi-generator-plus/types": "^2.17.0", | ||
"change-case": "^4.1.2", | ||
@@ -25,3 +25,3 @@ "handlebars": "^4.7.8", | ||
"pluralize": "^8.0.0", | ||
"@openapi-generator-plus/generator-common": "1.5.0" | ||
"@openapi-generator-plus/generator-common": "1.6.0" | ||
}, | ||
@@ -28,0 +28,0 @@ "publishConfig": { |
102
src/index.ts
@@ -5,3 +5,3 @@ import { promises as fs } from 'fs' | ||
import { camelCase, capitalize, pascalCase, uniquePropertiesIncludingInherited, debugStringify, uniquePropertiesIncludingInheritedForParents } from '@openapi-generator-plus/generator-common' | ||
import { CodegenGeneratorContext, CodegenSchemaType, CodegenResponse, CodegenRequestBody, CodegenObjectSchema, CodegenOperation, CodegenVendorExtensions, CodegenExamples, CodegenContent, CodegenLogLevel, CodegenSchemaUsage, CodegenSchema } from '@openapi-generator-plus/types' | ||
import { CodegenGeneratorContext, CodegenSchemaType, CodegenResponse, CodegenRequestBody, CodegenObjectSchema, CodegenOperation, CodegenVendorExtensions, CodegenExamples, CodegenContent, CodegenLogLevel, CodegenSchemaUsage, CodegenSchema, isCodegenParameter, isCodegenProperty } from '@openapi-generator-plus/types' | ||
import { snakeCase, constantCase, sentenceCase, capitalCase } from 'change-case' | ||
@@ -833,8 +833,19 @@ import pluralize from 'pluralize' | ||
hbs.registerHelper('warn', function(message: string) { | ||
hbs.registerHelper('warn', function() { | ||
// eslint-disable-next-line prefer-rest-params | ||
const options = arguments[arguments.length - 1] as ActualHelperOptions | ||
if (arguments.length !== 2) { | ||
throw new Error(`warn helper must be called with one argument @ ${sourcePosition(options)}`) | ||
if (arguments.length < 2) { | ||
throw new Error(`warn helper must be called with one or more arguments @ ${sourcePosition(options)}`) | ||
} | ||
let message = '' | ||
for (let i = 0; i < arguments.length - 1; i++) { | ||
const nextPart = String(arguments[i]) | ||
/* Add a space if both the preceding and following part are text (not punctuation) */ | ||
if (i > 0 && message.match(/[a-zA-Z0-9]$/) && nextPart.match(/^[a-zA-Z0-9]/)) { | ||
message += ' ' | ||
} | ||
message += nextPart | ||
} | ||
log(CodegenLogLevel.WARN, `${message} @ ${sourcePosition(options)}`) | ||
@@ -873,2 +884,3 @@ }) | ||
registerPropertyTypeHelper('isFile', CodegenSchemaType.FILE, hbs) | ||
registerPropertyTypeHelper('isAny', CodegenSchemaType.ANY, hbs) | ||
@@ -1012,7 +1024,14 @@ /* Content type helpers */ | ||
const varNames = varName.split('.') | ||
let context: any = this | ||
for (let i = 0; i < varNames.length - 1; i++) { | ||
context = context[varNames[i]] | ||
} | ||
varName = varNames[varNames.length - 1] | ||
if (arguments.length === 3) { | ||
this[varName] = value | ||
context[varName] = value | ||
} else { | ||
const result = options.fn(this).trim() | ||
this[varName] = result | ||
context[varName] = result | ||
} | ||
@@ -1022,22 +1041,61 @@ return null | ||
hbs.registerHelper('unset', function(this: UnknownObject, varName: string, value?: unknown) { | ||
// eslint-disable-next-line prefer-rest-params | ||
const options = arguments[arguments.length - 1] as ActualHelperOptions | ||
if (arguments.length !== 2) { | ||
throw new Error(`unset helper must be called with 1 argument @ ${sourcePosition(options)}`) | ||
} | ||
if (!varName) { | ||
throw new Error(`unset helper missing or undefined first argument (varName) @ ${sourcePosition(options)}`) | ||
} | ||
const varNames = varName.split('.') | ||
let context: any = this | ||
for (let i = 0; i < varNames.length - 1; i++) { | ||
context = context[varNames[i]] | ||
} | ||
varName = varNames[varNames.length - 1] | ||
delete context[varName] | ||
return null | ||
}) | ||
/** | ||
* Join the non-empty lines of the body of the block together with the given separator, and set into the named variable | ||
* Join the non-empty lines of the body of the block together with the given separator, and set into the named variable (if provided) | ||
* in the current scope. | ||
*/ | ||
hbs.registerHelper('join', function(this: UnknownObject, varName: string, separator: string) { | ||
hbs.registerHelper('join', function(this: UnknownObject, varNameOrSeparator: string, separator?: string) { | ||
// eslint-disable-next-line prefer-rest-params | ||
const options = arguments[arguments.length - 1] as ActualHelperOptions | ||
if (arguments.length !== 3) { | ||
throw new Error(`join helper must be called with 2 arguments @ ${sourcePosition(options)}`) | ||
let varName: string | null | ||
if (arguments.length === 2) { | ||
varName = null | ||
separator = varNameOrSeparator | ||
} else if (arguments.length === 3) { | ||
varName = varNameOrSeparator | ||
} else { | ||
throw new Error(`join helper must be called with 1 or 2 arguments @ ${sourcePosition(options)}`) | ||
} | ||
if (!varName) { | ||
throw new Error(`join helper must be called with 2 arguments; missing or undefined first argument (varName) @ ${sourcePosition(options)}`) | ||
if (varName && typeof varName !== 'string') { | ||
throw new Error(`join helper called with non-string varName @ ${sourcePosition(options)}`) | ||
} | ||
if (typeof separator !== 'string') { | ||
throw new Error(`join helper must be called with 2 arguments; missing or undefined second argument (separator) @ ${sourcePosition(options)}`) | ||
throw new Error(`join helper called with non-string separator (${typeof separator}) @ ${sourcePosition(options)}`) | ||
} | ||
const result = options.fn(this).split(/\r?\n/).filter(s => s.trim().length > 0).join(separator) | ||
this[varName] = result | ||
return null | ||
if (varName) { | ||
const varNames = varName.split('.') | ||
let context: any = this | ||
for (let i = 0; i < varNames.length - 1; i++) { | ||
context = context[varNames[i]] | ||
} | ||
varName = varNames[varNames.length - 1] | ||
context[varName] = result | ||
return null | ||
} else { | ||
return result | ||
} | ||
}) | ||
@@ -1096,2 +1154,16 @@ | ||
}) | ||
hbs.registerHelper('isProperty', function(value: unknown): boolean { | ||
return isCodegenProperty(value) | ||
}) | ||
hbs.registerHelper('isParam', function(value: unknown): boolean { | ||
return isCodegenParameter(value) | ||
}) | ||
hbs.registerHelper('isArrayValue', function(value: unknown): boolean { | ||
return Array.isArray(value) | ||
}) | ||
hbs.registerHelper('isObjectValue', function(value: unknown): boolean { | ||
return typeof value === 'object' && !Array.isArray(value) && !(value instanceof Function) | ||
}) | ||
} | ||
@@ -1098,0 +1170,0 @@ |
113446
4.42%2344
5.73%+ Added
- Removed