Closed fprl closed 9 months ago
I understand where you're coming from and I've also struggled with the API impact.
However, I think it's a bad idea to want to have an external API that the Qwik app also uses internally. You're making things harder for both use cases. Qwik-city is actually awesome because you don't need to worry about API, you can flexibly do what you want.
An external API however needs to be rigid and precise.
Those are two quite different goals, and therefore I think it's better to use another tool for the external API.
Personally, I think graphql is a phenomenal way to provide an external API.
But for a qwik-city app internally, just use the invisible API via loaders and actions instead.
Thanks for your answer @wmertens.
I agree that using other tool may be just better and easier, I could use the entry.fastify.tsx
to handle all API endpoints and at the same time share all the types in the same codebase.
Will do that then, thanks a lot!
Is your feature request related to a problem?
I'm not sure if it's a problem, but since directory-based routing (DBR) isn't really flexible, it might come in handy. One issue I always face with DBR is that declaring routes and API endpoints gets messy as soon as you have more than 5 routes. Although this idea will not solve the issue with regular routes, it may solve API endpoints in a better way.
Describe the solution you'd like
Exposing a
globalLoader$
for declaring global loaders and addingpath
parameter toglobalAction$
. I know that routeLoaders$ works fine for collocation; however, it's difficult to scale with this approach:globalLoader$
andglobalAction$
should a path parameter that registers the loader/action in a specific path provided by the user, this includes accepting dynamic route segments, so you don't even have to follow the DBR convention and their URL is not constructed based on where the file its located.Current
For example, this is how you declare API endpoints now (index hell):
Proposed
How it may look. Please notice that, as they are not regular endpoints and can be declared in any file inside src directory like globalAction$, they don't need to follow DBR conventions like
/path/index.ts
.Where
src/routes/api/users.ts
could look like:Calling it from a page:
TL;DR
globalLoader$
and addpath
parameter toglobalAction$
to construct URL based on it.path
is mandatory to construct URL path:/api/users/123/profile
.Describe alternatives you've considered
globalLoader$
andglobalAction$
path parameter to construct their URL relative to the file where they are located, so you have to follow the DBR convention. For example: Wheresrc/routes/api/users/index.ts
could look like:// path is mandatory to construct URL path:
/api/users
export const useUsers = globalLoader$('/', async (req) => { ... });routeLoader$
has a better DX than a regular endpoint.Additional context
globalAction$
already exists!