Deferring Service Initialization Until User Login for Shared HttpClient Instance

is there a way to defer the value of a service? I have a HttpClient layer that grabs some info from localstorage to create an common axios instance thats ultimately shared by all my other api services. however, i need the service to 'wait' until the user logs in and there is a valid entry in local storage. At the moment i have to remove HttpClient and ProductsService from the app context and inject them via Effect.provide, which feels a little wasteful....

const AppContext = Layer.mergeAll(
  EnvService.Layer,
  StorageService.Layer,
  TokenCache.Layer,
  AuthService.Layer,
  HttpClient.Layer, // needs localstorage data thats only available on login
  ProductService.Layer
);

const AppConfigProvider = ConfigProvider.fromJson(import.meta.env);

const AppLayer = Layer.provide(
  AppContext,
  Layer.mergeAll(Layer.setConfigProvider(AppConfigProvider)),
);

export const AppRuntime = ManagedRuntime.make(AppLayer);
Was this page helpful?