question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a "layout" for endpoints

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
kevmodromecommented, Feb 7, 2022

I think this sounds like a neat idea. I’d probably lean towards calling it something like __middleware though.

2reactions
jchanes04commented, Nov 22, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found