Add a "layout" for endpoints
See original GitHub issueDescribe the problem
If I currently have an endpoint that should only be available for logged in users. I can do the following:
export async function get({ locals }) {
if (!locals.user) {
return {
status: 401
}
}
// otherwise proceed and return the data
return {
status: 200,
body: data
}
}
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.
export async function get({ locals }) {
if (!locals.user) {
return {
status: 401
}
}
// No return value as we want the normal endpoints to kick in.
}
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
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Design Your API Endpoints - OpenClassrooms
For this part, we'll be thinking about how to design an API for a photo-sharing app called InstaPhoto. We want users to be...
Read more >Cisco Webex Video Endpoints Video Layouts Demo - YouTube
In this video I demo the different video layout on the Cisco Webex Room video systems inclusive of the Webex Board Pro, Room...
Read more >Best practices for REST API design - Stack Overflow Blog
Learn how to design REST APIs to be easy to understand for anyone, future-proof, secure, and fast since they serve data to clients...
Read more >Pexip Layout Controls for Cisco endpoints
You can enhance your meeting room experience with the Pexip Layout Controls feature, which allows you to switch between different conference layouts using...
Read more >create your own layout in an MCU 5320 - Cisco Community
In addition, you can set the layout individually, I mean, each endpoint in the conference can have his own layout, as well as...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I think this sounds like a neat idea. I’d probably lean towards calling it something like
__middleware
though.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.