Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Adds dependency injection system #1461

Merged
merged 12 commits into from
Nov 21, 2022
3 changes: 3 additions & 0 deletions common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export type DeepPartial<T> = PartialDeep<T>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Constructor<T = AnyObject, ARGS extends unknown[] = any[]> = new (...args: ARGS) => T;

// eslint-disable-next-line @typescript-eslint/ban-types
export type Abstract<T> = Function & { prototype: T };

// Construct object from properties of T that extend U.
export type PickWhere<T, U> = Pick<
T,
Expand Down
39 changes: 38 additions & 1 deletion docs/app-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The app configuration in `app.ts` is the place where you can add plugins, compon
The app configuration files in the `src` folder are the main entry point of your Jovo apps. They usually include the following elements:
- [Components](./components.md) can be registered
- [Plugins](./plugins.md) and [Hooks](./hooks.md) can be added to extend the framework functionality
- [Service providers](./service-providers-dependency-injection.md) can be added for dependency injection
- Framework configurations, like logging behavior, can be modified

Here is an example [`app.ts` file](https://github.com/jovotech/jovo-v4-template/blob/master/src/app.ts):
Expand Down Expand Up @@ -125,7 +126,7 @@ app.use(

## Configuration Elements

The configuration object that can be passed to both the constructor and the `configure()` method contains [components](#components), [plugins](#plugins), [logging](#logging), and [routing](#routing).
The configuration object that can be passed to both the constructor and the `configure()` method contains [components](#components), [plugins](#plugins), [providers](#providers), [logging](#logging), and [routing](#routing).

```typescript
{
Expand All @@ -135,6 +136,9 @@ The configuration object that can be passed to both the constructor and the `con
plugins: [
// ...
],
providers: [
// ...
],
logging: {
// ...
},
Expand Down Expand Up @@ -209,6 +213,39 @@ app.plugins.SomePlugin

This can be helpful if you want to add additional configurations to the default plugin config outside `app.ts`. See [staging](#staging) for more information.

### Providers

You can add service providers for [dependency injection](./services-providers-dependency-injection.md) like this:

```typescript
import { OrderService } from './services/OrderService';
// ...

{
// ...

providers: [
OrderService,
// ...
],
}
```

It is also possible to use the `provide` option to specify a token, for which you want to inject a dependency, separately from the injected value or type. [Learn more about the different provider types here](./service-providers-dependency-injection.md#types-of-providers).

```typescript
{
providers: [
{
provide: OrderService,
useClass: MockOrderService,
},
// ...
]
}
```


### Logging

[Logging](./logging.md) is enabled by adding the following to the app config:
Expand Down
Loading