-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 a "layout" for endpoints #2847
Comments
Can you use You could check the path prefix if you want to apply some logic only to certain pages |
Yes, you could do so. But if different routes have different rules, or even have differences between get, post, ... this can become complicated. Having the layout.js would keep those rules closer to where they are needed and have a convenient way of differentiating between methods. All in all, this is not a deal breaker, more of a nice to have 😄. |
I agree that adding a feature like this would help to reduce the amount of repeated code and improve readability. Perhaps the name __pre.js could be used for this, as this code would run before the request is passed on to the endpoints. __layout.js doesn't make much sense, since endpoints are usually not responsible for returning HTML and this code would not "append" anything to what was returned from the endpoint like __layout.svelte does. |
It would be also useful if __pre.js (or .ts) can pass data to the next endpoint (or change the locals). |
Now that we have 'shadow endpoints', I think having this opens up another possibility. Currently a If this proposed change gets implemented, we could theoretically make it such that we can not only do an access check like in the first example, but also abstract away some common code that gets exposed to the individual endpoint similar to how |
This looks like a middleware pattern to me. Having the ability to drop in middleware for the endpoints in such a way that it applies to all endpoints in the same folder and below sounds useful. |
I think this sounds like a neat idea. I'd probably lean towards calling it something like |
This is on the roadmap. Closing in favor of the design discussion at #4274. |
Describe the problem
If I currently have an endpoint that should only be available for logged in users. I can do the following:
This works fine, but in some cases can be a bit of a hassle, especially if there are several endpoints and this code has to be copied in all these files.
Describe the proposed solution
Similar to
__layout.svelte
a new file named__layout.js
that runs before the actual endpoints, this allows the developer to group all these access checks in one place. With the fall through rule we already have in place it would mean the above code would be in this new file and not have to be repeated.Alternatives considered
Copying the testing logic to a shared function also works,
But this is not much shorter as we still have to construct and return the return object.
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: