Skip to content

fugle-dev/fugle-marketdata-nest

Repository files navigation

@fugle/marketdata-nest

NPM version

A Nest module wrapper for @fugle/marketdata

Installation

To begin using it, we first install the required dependencies.

$ npm install --save @fugle/marketdata-nest @fugle/marketdata

Getting started

Once the installation is complete, to use the RestClient or WebSocketClient, first import FugleMarketDataModule and pass the options with apiKey to the forRoot() method.

import { Module } from '@nestjs/common';
import { FugleMarketDataModule } from '@fugle/marketdata-nest';

@Module({
  imports: [
    FugleMarketDataModule.forRoot({
      apiKey: 'YOUR_API_KEY',
    }),
  ],
})
export class AppModule {}

Next, inject the RestClient instance using the @InjectRestClient() decorator.

constructor(@InjectRestClient() private readonly client: RestClient) {}

The @InjectWebSocketClient() decorator is used for the WebSocketClient instance injection.

constructor(@InjectWebSocketClient() private readonly client: WebSocketClient) {}

Async configuration

When you need to pass module options asynchronously instead of statically, use the forRootAsync() method. As with most dynamic modules, Nest provides several techniques to deal with async configuration.

One technique is to use a factory function:

FugleMarketDataModule.forRootAsync({
  useFactory: () => ({
    apiKey: 'YOUR_API_KEY',
  }),
});

Like other factory providers, our factory function can be async and can inject dependencies through inject.

FugleMarketDataModule.forRootAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    apiKey: configService.get('FUGLE_MARKETDATA_API_KEY'),
  }),
  inject: [ConfigService],
});

Alternatively, you can configure the FugleMarketDataModule using a class instead of a factory, as shown below.

FugleMarketDataModule.forRootAsync({
  useClass: FugleMarketDataConfigService,
});

The construction above instantiates FugleMarketDataConfigService inside FugleMarketDataModule, using it to create an options object. Note that in this example, the FugleMarketDataConfigService has to implement FugleMarketDataModuleOptionsFactory interface as shown below. The FugleMarketDataModule will call the createFugleMarketDataOptions() method on the instantiated object of the supplied class.

@Injectable()
class FugleMarketDataConfigService implements FugleMarketDataModuleOptionsFactory {
  createFugleMarketDataOptions(): FugleMarketDataModuleOptions {
    return {
      apiKey: 'YOUR_API_KEY',
    };
  }
}

If you want to reuse an existing options provider instead of creating a private copy inside the FugleMarketDataModule, use the useExisting syntax.

FugleMarketDataModule.forRootAsync({
  imports: [ConfigModule],
  useExisting: FugleMarketDataConfigService,
});

Reference

License

MIT