sveltesveltekithexagonal-architecture

Access cookies from another service


I'm trying to work with a clean and hexagonal architecture for my sveltekit frontend, connecting to an external API. When I want to load some data for a page, inside my load function I instantiate a simple command/DTO that I pass to a usecase class, which passes it on to a handler class, which then calls the external API to get the data via a gateway class. Now architecture wise, the load function is inside the Infrastructure layer, then command/usecase/handler are all inside the Application layer, then the gateway is in the Infrastructure layer but I instantiate it inside my handler via dependency injection on a Domain interface so I'm good on that.

Problem is, I need access to the cookies in my gateway, so I can attach them to my outgoing request and authenticate on my external API. But those cookies are only accessible inside my load function as far as I know, and I don't want to drill either the whole original request or the cookies through my whole usecase because the Application layer shouldn't care about that.

I would like to get access to existing cookies for the current original request inside my gateway directly, is there any way I could do that in SvelteKit while still being careful about not sharing data between users on the node server?


Solution

  • The solution was to create a new DI injector in each load function instead of one for the application. That way I can pass the request to the injector factory and have it injected directly into the gateway class (or into a RequestContextProvider class that is then injected into the gateway, whatever works best for your case).