Add option to pass in custom handlebars templates #1268#1995
Add option to pass in custom handlebars templates #1268#1995gersongoulart wants to merge 2 commits intoferdikoomen:mainfrom
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.
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.
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.
Same thoughts. I wish we could use some other api besides handlebars.precompile. But I could not find any that works.
| import { preCompileTemplate } from './preCompileTemplate'; | ||
| import { registerHandlebarHelpers } from './registerHandlebarHelpers'; | ||
|
|
||
| export type TemplateOverrideNames = |
There was a problem hiding this comment.
Perhaps this type can be added to the Options type for generate api in types/index.d.ts
…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
templateOverridesallows the override of any template or partial with a custom implementation.Example:
package.json
{ "scripts": { "generate": "openapi --input ./spec.json --output ./generated --templateOverrides index:\"Hello World\" exportService:./templates/service.hbs" }, }NodeJs