-
-
Notifications
You must be signed in to change notification settings - Fork 528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option to pass in custom handlebars templates #1268 #1995
base: main
Are you sure you want to change the base?
Conversation
The parameter `templateOverrides` allows the override of any template or partial with a custom implementation. ## Example: package.json ```json { "scripts": { "generate": "openapi --input ./spec.json --output ./generated --templateOverrides index:\"Hello World\" service:./templates/service.hbs" }, } ``` ```typescript const OpenAPI = require('openapi-typescript-codegen'); OpenAPI.generate({ input: './spec.json', output: './generated', templateOverrides: { index: 'templates/index.hsb', service: 'templates/service.hsb', }, }); ```
}, | ||
}); | ||
|
||
return eval(`(function(){return ${templateSpec} }());`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eval
seems safe enough, although I suspect code quality tools will complain.
Here is an another (extremely hacky) alternative using a temp file which gets resolved
import { readFileSync, writeFileSync, mkdtempSync } from 'fs';
...
const tempDir = mkdtempSync('template-');
const tempFilePath = join(tempDir, 'template.js');
writeFileSync(tempFilePath, `module.exports = ${compiled};`);
const module = require(resolve(tempFilePath));
execSync(`rm -rf ${tempDir}`);
return module;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the observation. Yeah, code quality tools will warn about the safety of this to prevent developers from shipping code that could be exploited. Seeing this is a library and the provider of the template to be evaluated is the developer using the library, I think this is a safe implementation of the eval function — and the cleanest solution I could find. Writing and reading to disk on every precompilation would significantly affect performance and might encounter problems with disk access permissions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same thoughts. I wish we could use some other api besides handlebars.precompile
. But I could not find any that works.
import { registerHandlebarHelpers } from './registerHandlebarHelpers'; | ||
|
||
export type TemplateOverrideNames = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this type can be added to the Options
type for generate
api in types/index.d.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do, thank you!
…partial" schema. - Add `templateOverrides` type to the Options type for generate api in types/index.d.ts - Update README.md file
Add option to pass in custom handlebars templates ferdikoomen#1268 ferdikoomen#1995
can we assist with getting this PR reviewed and merged? |
Any update on this? Bump! |
any updates? |
@ng-state this package is no longer maintained, see README |
Introducing a simple, yet dynamic solution to conquer issue #1268 with unparalleled flexibility! 🚀🌟
Overriding Built-In Templates
The parameter
templateOverrides
allows the override of any template or partial with a custom implementation.Example:
package.json
NodeJs