Internal API consumption - am I missing something?
Unanswered
Scarlet Ibis posted this in #help-forum
Scarlet IbisOP
I appreciate this has been asked in some form before, but Id like to understand the rationale behind this being considered against best practice.
I have access to 3rd party API. I can consume that API via fetch in server components as it is considered an external resource.
I can implement my own API in express JS and host it somewhere. Again, I can consume that via fetch in server components.
I can implement my own API in a separate NextJS instance and host it somewhere. Again, I can consume that via fetch in server components in server components.
But when I implement an API on the SAME NextJS instance, it is considered bad practice to consume that API? My understanding is that this is because we are calling into ourselves, and that is inherently inefficient to go outside instead of making an internal call, but with the nextJS fetch client, it could quite easily (we already have access to cookies and headers to handle forwarding auth) handle it all internally by calling the route directly instead of calling out?
If we consider this internal consumption a bad practice, what is considered the correct way to implement an API which we can consume on the server, client, and externally without creating an additional surface? Do I really have to implement API on a separate instance if I want to avoid having multiple ways (one for server components, another for client components) to access the same data?
Every option I can think of requires either tight coupling to nextjs or a highly opinionated view of how my data access should be done, when all I want to do is have my API part of the same project and for it to be treated as I would any other API.
I have access to 3rd party API. I can consume that API via fetch in server components as it is considered an external resource.
I can implement my own API in express JS and host it somewhere. Again, I can consume that via fetch in server components.
I can implement my own API in a separate NextJS instance and host it somewhere. Again, I can consume that via fetch in server components in server components.
But when I implement an API on the SAME NextJS instance, it is considered bad practice to consume that API? My understanding is that this is because we are calling into ourselves, and that is inherently inefficient to go outside instead of making an internal call, but with the nextJS fetch client, it could quite easily (we already have access to cookies and headers to handle forwarding auth) handle it all internally by calling the route directly instead of calling out?
If we consider this internal consumption a bad practice, what is considered the correct way to implement an API which we can consume on the server, client, and externally without creating an additional surface? Do I really have to implement API on a separate instance if I want to avoid having multiple ways (one for server components, another for client components) to access the same data?
Every option I can think of requires either tight coupling to nextjs or a highly opinionated view of how my data access should be done, when all I want to do is have my API part of the same project and for it to be treated as I would any other API.