celest-dev / celest

The Flutter cloud platform
https://celest.dev
Other
232 stars 12 forks source link

Dependency Injection in backend #46

Open dnys1 opened 4 months ago

dnys1 commented 4 months ago

Related to #40. Based off a Discord thread.

It is common to have a set of service providers shared between functions on the backend (for example, Sentry). Currently, in order to use the same provider in multiple functions, users are required to initialize the provider at the start of each call, e.g.

// functions/my_api.dart

Future<void> _initializeSentry({ required String dsn }) async {
  // initialize sentry provider
}

Future<void> tracedFunction({
  // params...
  @Env.sentryDsn required String sentryDsn,
}) async {
  await _initializeSentry(dsn: dsn);
}

It would be nice if there was a way to consolidate this logic even more so functions can either move this logic to a middleware or have a pre-initialized service provider injected into the function like env variables are currently injected.