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

Improve return types for render, renderAsync, renderFile and renderFileAsync #199

Merged
merged 6 commits into from Jan 10, 2023

Conversation

nhaef
Copy link
Contributor

@nhaef nhaef commented Dec 8, 2022

Hey, I really like eta and I have been using it for a few projects already. I would love to contribute to this project. ❤

As already explained in this issue, the types of the render functions can be improved by overloading them. There are essentially three branches and every branch defines their respective return type:

export default function render(
    template: string | TemplateFunction,
    data: object,
    config?: PartialConfig,
    cb?: CallbackFn
): string | Promise<string> | void {
    if (options.async) {
        if (cb) {
            // user provides callback function
            // return type is void
        } else {
            // user sets async option
            // return type is Promise<string>
        }
    } else {
        // return type is string
    }
}

The idea would be to define one function overload per return type so that the function can be consumed easily, without the need of any type assertion. The implementation signature is provided as well in order to prevent breaking changes.

I appreciate any feedback, ideas and suggestions.

This PR resolves #189.

@nhaef nhaef changed the title Improve return types for render, `` Improve return types for render, renderAsync, renderFile and renderFileAsync Dec 8, 2022
@nebrelbug
Copy link
Collaborator

@nhaef thanks for working on this! I think this change will be really helpful. Is it ready for review yet?

@nhaef
Copy link
Contributor Author

nhaef commented Dec 15, 2022

Hi @nebrelbug, thanks for the feedback. It isn't ready for review yet, but it is almost there :-) I will update this PR later today

@nhaef
Copy link
Contributor Author

nhaef commented Dec 15, 2022

Okay, function overloads for renderFile and renderFileAsync have been added as well. The return type of both functions depends on the return type of tryHandleCache. This method is pretty much structured like this:

function tryHandleCache(data: object, options: EtaConfigWithFilename, cb: CallbackFn | undefined) {
    if (cb) {
        // return type is void
    } else {
        // return type is Promise<string>
    }
}

The function overloads of renderFile and renderFileAsync are specified so that the return type is void if the user provides a CallbackFn, otherwise the return type is Promise<string>. Similar to the previous changes, I did not remove the existing function overloads with the optional CallbackFn so that this PR does not introduce any breaking changes.

@nhaef
Copy link
Contributor Author

nhaef commented Dec 15, 2022

Here is a preview of the inferred types.

click me

Eta.render

Eta.render(templateFn, data); // string
Eta.render(templateFn, data, config); // string
Eta.render(templateFn, data, { ...config, async: true }); // Promise<string>
Eta.render(templateFn, data, { ...config, async: true }, callbackFn); // void

Eta.renderAsync

Eta.renderAsync(templateFn, data); // Promise<string>
Eta.renderAsync(templateFn, data, config); // Promise<string>
Eta.renderAsync(templateFn, data, { ...config, async: true }); // Promise<string>
Eta.renderAsync(templateFn, data, { ...config, async: true }, callbackFn); // void

Eta.renderFile

Eta.renderFile(fileName, data, config); // Promise<string>
Eta.renderFile(fileName, data, config, callbackFn); // void
Eta.renderFile(fileName, { ...data, ...config }); // Promise<string>
Eta.renderFile(fileName, { ...data, ...config }, callbackFn); // void

Eta.renderFileAsync

Eta.renderFileAsync(fileName, data, config); // Promise<string>
Eta.renderFileAsync(fileName, data, config, callbackFn); // void
Eta.renderFileAsync(fileName, { ...data, ...config }); // Promise<string>
Eta.renderFileAsync(fileName, { ...data, ...config }, callbackFn); // void

@nhaef nhaef marked this pull request as ready for review December 15, 2022 21:37
@nhaef
Copy link
Contributor Author

nhaef commented Dec 15, 2022

The changes are now ready for review. I would especially be interested if I should add JSDoc annotations for each function overload or not. Looking forward to your feedback 🙂

src/file-handlers.ts Outdated Show resolved Hide resolved
src/file-handlers.ts Outdated Show resolved Hide resolved
@nebrelbug
Copy link
Collaborator

nebrelbug commented Jan 1, 2023

@nhaef thanks so much for your work here! Sorry I've just gotten to reviewing this now. As I commented above, it seems to me that we should be able to remove some redundant function overloads. Is that correct?

@nhaef
Copy link
Contributor Author

nhaef commented Jan 9, 2023

@nhaef thanks so much for your work here! Sorry I've just gotten to reviewing this now. As I commented above, it seems to me that we should be able to remove some redundant function overloads. Is that correct?

No problem and thanks for the review :) And also sorry from my side as well, I was busy the last few days.

I removed some unnecessary function overloads and updated the conversations. I think some of the conversations which are still open can be summed up with the question "Should we make the implementation signature visible for backwards compatibility?".

@nebrelbug
Copy link
Collaborator

@nhaef I actually didn't realize that implementation signatures were not visible to users. With that knowledge, this PR looks great. I'll merge right now!

@nebrelbug nebrelbug merged commit 242e9fc into eta-dev:master Jan 10, 2023
@nebrelbug
Copy link
Collaborator

@nhaef do you mind if I add you to the contributors section in the README?

@nhaef
Copy link
Contributor Author

nhaef commented Jan 11, 2023

@nhaef do you mind if I add you to the contributors section in the README?

not at all, I'd be honored :D

@nebrelbug
Copy link
Collaborator

@all-contributors please add @nhaef for code

@allcontributors
Copy link
Contributor

@nebrelbug

I've put up a pull request to add @nhaef! 🎉

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.

Improve render functions return types
2 participants