Skip to content

Commit

Permalink
refactor: move from service to provider
Browse files Browse the repository at this point in the history
  • Loading branch information
SocketSomeone committed Dec 6, 2021
1 parent cecacf5 commit 7c1cd36
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<img align="right" width="95" height="148" title="NestJS logotype" src="https://nestjs.com/img/logo-small.svg" alt='Nest.JS logo'/>

Hot-shots Module for Nest.js Framework. A Node.js client for Etsy's StatsD server, Datadog's DogStatsD server, and InfluxDB's Telegraf
Hot-shots Module for Nest.js Framework. A Node.js client for [Etsy](http://etsy.com)'s [StatsD](https://github.com/statsd/statsd) server, Datadog's [DogStatsD](https://docs.datadoghq.com/developers/dogstatsd/?tab=hostagent) server, and [InfluxDB's](https://github.com/influxdata/telegraf) Telegraf
StatsD server.

**Feature**
Expand Down Expand Up @@ -47,15 +47,15 @@ export class AppModule {
}
```

Then inject `HotShotsService` for use `hot-shots`:
Then inject `StatsD` provider for use `hot-shots`:

```typescript
import { Injectable } from '@nestjs/common';
import { HotShotsService } from 'nestjs-hot-shots';
import { StatsD } from 'hot-shots';

@Injectable()
export class AppMetrics {
public constructor(private readonly metrics: HotShotsService) {
public constructor(private readonly metrics: StatsD) {
}

public metricStuff() {
Expand Down
2 changes: 1 addition & 1 deletion src/hot-shots.constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const MODULE_OPTIONS = 'hot-shots:module-options';
export const HOT_SHOTS_MODULE_OPTIONS = 'hot-shots:module-options';
35 changes: 23 additions & 12 deletions src/hot-shots.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,42 @@ import {
HotShotsModuleOptions,
HotShotsOptionsFactory
} from './hot-shots-options.interface';
import { MODULE_OPTIONS } from './hot-shots.constants';
import { HotShotsService } from './hot-shots.service';
import { HOT_SHOTS_MODULE_OPTIONS } from './hot-shots.constants';
import { StatsD } from 'hot-shots';

@Module({
providers: [HotShotsService],
exports: [HotShotsService]
})
@Module({})
export class HotShotsModule {
public static forRoot(options: HotShotsModuleOptions): DynamicModule {
const StatsDProvider: Provider<StatsD> = {
provide: StatsD,
useValue: new StatsD(options)
};

return {
module: HotShotsModule,
providers: [
{
provide: MODULE_OPTIONS,
provide: HOT_SHOTS_MODULE_OPTIONS,
useValue: options
}
]
},
StatsDProvider
],
exports: [StatsDProvider]
};
}

public static forRootAsync(options: HotShotsModuleAsyncOptions): DynamicModule {
const StatsDFactoryProvider: Provider<StatsD> = {
provide: StatsD,
useFactory: (options: HotShotsModuleOptions) => new StatsD(options),
inject: [HOT_SHOTS_MODULE_OPTIONS]
};

return {
module: HotShotsModule,
imports: options.imports,
providers: this.createAsyncProviders(options)
providers: this.createAsyncProviders(options).concat(StatsDFactoryProvider),
exports: [StatsDFactoryProvider]
};
}

Expand All @@ -49,14 +60,14 @@ export class HotShotsModule {
private static createAsyncOptionsProvider(options: HotShotsModuleAsyncOptions): Provider {
if (options.useFactory) {
return {
provide: MODULE_OPTIONS,
provide: HOT_SHOTS_MODULE_OPTIONS,
useFactory: async (...args: any[]) => await options.useFactory(...args),
inject: options.inject || []
};
}

return {
provide: MODULE_OPTIONS,
provide: HOT_SHOTS_MODULE_OPTIONS,
useFactory: async (optionsFactory: HotShotsOptionsFactory) =>
await optionsFactory.createHotShotsOptions(),
inject: [options.useExisting || options.useClass]
Expand Down
14 changes: 0 additions & 14 deletions src/hot-shots.service.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './hot-shots.module';
export * from './hot-shots.service';

0 comments on commit 7c1cd36

Please sign in to comment.