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

Implement res.locals #355

Closed
06000208 opened this issue Apr 22, 2022 · 1 comment · Fixed by #356
Closed

Implement res.locals #355

06000208 opened this issue Apr 22, 2022 · 1 comment · Fixed by #356
Labels
enhancement New feature or request

Comments

@06000208
Copy link
Contributor

06000208 commented Apr 22, 2022

Is your feature request related to a problem? Please describe.
Unsure if this is a bug report or a feature request, I've gone with the latter as this seems to be unimplemented behavior.

In express, res.locals are a per-request equivalent to app.locals, and the property is mentioned here in tinyhttp:

locals?: Record<string, any>

However, res.locals's behavior isn't present, as it doesn't seem to be used in renderTemplate, and they won't be available within templates:

import { App } from "@tinyhttp/app";
import { renderFile as eta } from "eta";
const app = new App();
app.engine("eta", eta);
app.use(function(req, res, next) {
    res.locals.hello = "hello world!";
    next();
});
app.get((req, res) => res.render("index.eta"));
app.listen(3000, () => console.log("Listening on port 3000"));

views/index.eta

<body>
  <p><%= it.hello %></p>
</body>

Describe the solution you'd like
Matching express's res.locals behavior, described here: https://expressjs.com/en/api.html#res.locals

Describe alternatives you've considered
I could manually use res.locals with res.render or use a function to automate this, but it would be more convienent to have res.locals's expected behavior, as doing so somewhat defeats the point of res.locals

app.get((req, res) => res.render("index.eta", { hello: res.locals.hello }));
@06000208 06000208 added the enhancement New feature or request label Apr 22, 2022
@06000208 06000208 changed the title Utilize res.locals as expected Implement res.locals Apr 22, 2022
@talentlessguy
Copy link
Member

could you please make a PR with the fix?

talentlessguy pushed a commit that referenced this issue Jun 6, 2022
* feat: Implement res.locals behavior (#355)

* fix: default to empty res.locals object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants