| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
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;
Sorry, something went wrong.
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.
Sorry, something went wrong.
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.
Sorry, something went wrong.
| 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
Sorry, something went wrong.
There was a problem hiding this comment.
Will do, thank you!
Sorry, something went wrong.
…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? |
Sorry, something went wrong.
|
Any update on this? Bump! |
Sorry, something went wrong.
|
any updates? |
Sorry, something went wrong.
|
@ng-state this package is no longer maintained, see README |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
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
{ "scripts": { "generate": "openapi --input ./spec.json --output ./generated --templateOverrides index:\"Hello World\" exportService:./templates/service.hbs" }, }NodeJs