Next.js Discord

Discord Forum

API/Service Layer shared between client/server

Unanswered
Pavement ant posted this in #help-forum
Open in Discord
Pavement antOP
I just got assigned to a team that is using Next.js. I can see that they are not too proficient, and neither am I (in using next.js).

Take this example (app is littered with calls like that)
const authToken = await getAuthToken();
const project = await api('project/create', [], { options: { body, authToken } });

getToken is exposed from a 'use server'
api is some over-engineered abstraction of fetch (not looking too good either)
the api calls usually go to our non next.js server/backend

Im just wondering, is there's anything stopping me/us from creating a service layer (usable within server/client) where I will expose all api calls? (worst case just c/p-ing the two lines in a separate file so its more reusable... im used to having some sort of api/service layer where i expose predefined api calls etc.)
Or generally any tips from your architecture/project structure when it comes to service layer/fetch interceptors to attach Authentication etc..?

P.S. Not too relevant to the question above, just my thoughts. This looks extra weird because for the request above I first await for token from next use-server, and then ping another backend to get the data...

0 Replies