Replies: 3 comments 3 replies
-
Using server hooks + a switch statement (in api/+server.ts) should get the job done.
import { error, json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { doAdminStuff, doUserStuff } from './whatever-this-file-is';
export const GET = ((event) => {
switch (event.locals.userRole) {
case 'admin':
return doAdminStuff(event);
case 'user':
return doUserStuff(event);
default:
throw error(401, 'who are you');
}
}) satisfies RequestHandler; |
Beta Was this translation helpful? Give feedback.
-
I do something like this in my app. Although it feels like more of a hack than a good practice. More info - #6315 (comment) |
Beta Was this translation helpful? Give feedback.
-
To expand a little bit, I see a few ways for people to handle some of what I'm going to call "starter logic". Auth logic is probably the easiest to describe, so I'll use that in this example. Each file starts with starter logicYou could directly write the logic for each endpoint in each endpoint file. Obviously you'll end up extracting this out into a utils-like collection of endpoint starter logic functions, but this isn't "proper" middleware in my opinion. Additionally, it does nothing to visually show you what logic is applying to what endpoints without physically going into each file and looking at which function you've implemented for each endpoint. hooks.server.tsYou could write an overly complicated switch statement containing every route and flow all your requests through this, but this file fires on EVERY request. You might not need logic to run on every request, which means that many requests have needless extra cost associated to them. Additionally, as the complexity of your app grows the complexity of this logic can get out of hand really quickly. GroupingI just think this is the best option. You can visually see what your groups look like. You can easily update logic to apply across several endpoints at once. You are only running the logic that is relevant to each groups of endpoints. Your logic is simple, clean, readable, maintainable, and easily expandable. I'd love to get more opinions on this topic. |
Beta Was this translation helpful? Give feedback.
-
The docs refer to grouping as "layout grouping" specifically, but I'm curious to know if I can add
(group)
grouping folders within API routes to run middleware functions. If not, I think it's a pretty cool idea. Consider the following:Not only would this provide the visual benefit of seeing what api endpoints are accessible by which user types, but it would greatly reduce the amount of code I have to write. I could, of course, write a reusable set of auth check functions, import them, etc - but even this would be lines of needless import statements and function calls if it works the way I've described above.
Someone let me know if this already works the way I think it should because I'm not clear on the implementation if that's the case.
Beta Was this translation helpful? Give feedback.
All reactions