Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gersongoulart
Copy link

@gersongoulart gersongoulart commented Jan 23, 2024

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

const OpenAPI = require('openapi-typescript-codegen');

OpenAPI.generate({
    input: './spec.json',
    output: './generated',
    templateOverrides: {
        index: 'Hello World', // the template itself
        exportService: './templates/service.hbs', // or a file path
    },
});

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} }());`);

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;

Copy link
Author

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.

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 =

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

Copy link
Author

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
gersongoulart added a commit to provantagex/openapi-typescript-codegen that referenced this pull request Jan 29, 2024
@salzhrani
Copy link

can we assist with getting this PR reviewed and merged?

@sudhir4ev
Copy link

Any update on this? Bump!

@ng-state
Copy link

any updates?

@mrlubos
Copy link
Collaborator

mrlubos commented Sep 21, 2024

@ng-state this package is no longer maintained, see README

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants