@luxury-presence/nestjs-liveby
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@luxurypresence/nestjs-liveby

LiveBy module for for Nest based on the LiveBy's api.

Installation

$ npm i --save @luxury-presence/nestjs-liveby @nestjs/axios rxjs

Usage

Import LiveByModule:

@Module({
  imports: [LiveByModule.register({
    apiKey: 'LIVEBY_API_KEY',
    clientId: 'LIVEBY_CLIENT_ID',
    baseApi: 'LIVEBY_BASE_API',
  })],
  providers: [...],
})
export class SearchModule {}

Inject LiveByService:

@Injectable()
export class SearchService {
  constructor(private readonly liveByService: LiveByService) {}

  async findWalkScores(lat: number, lon: number): Promise<WalkScores> {
    return this.liveByService.findWalkScores(lat, lon);
  }

  async findAreaOfInterest(
    lat: number,
    lon: number
  ): Promise<AreaOfInterest[]> {
    return this.liveByService.findAreasOfInterest(lat, lon);
  }
}

Async options

Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use registerAsync() method, that provides a couple of various ways to deal with async data.

1. Use factory

LiveByModule.registerAsync({
  useFactory: () => ({
    apiKey: 'LIVEBY_API_KEY',
    clientId: 'LIVEBY_CLIENT_ID',
    baseApi: 'LIVEBY_BASE_API',
  }),
});

Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).

LiveByModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    apiKey: configService.get('LIVEBY_API_KEY'),
    clientId: configService.get('LIVEBY_CLIENT_ID'),
    baseApi: configService.get('LIVEBY_BASE_API'),
  }),
  inject: [ConfigService],
}),

2. Use class

LiveByModule.registerAsync({
  useClass: LiveByConfigService,
});

Above construction will instantiate LiveByConfigService inside LiveByModule and will leverage it to create options object.

class LiveByConfigService implements LiveByModuleOptionsFactory {
  createLiveByModuleOptions(): LiveByModuleOptions {
    return {
      apiKey: 'LIVEBY_API_KEY',
      clientId: 'LIVEBY_CLIENT_ID',
      baseApi: 'LIVEBY_BASE_API',
    };
  }
}

3. Use existing

LiveByModule.registerAsync({
  imports: [ConfigModule],
  useExisting: ConfigService,
}),

It works the same as useClass with one critical difference - LiveByModule will lookup imported modules to reuse already created ConfigService, instead of instantiating it on its own.

License

MIT

Package Sidebar

Install

npm i @luxury-presence/nestjs-liveby

Weekly Downloads

156

Version

1.1.0

License

MIT

Unpacked Size

32.7 kB

Total Files

41

Last publish

Collaborators

  • dallinlp
  • gauntc
  • nscarafia
  • spencermmelo
  • elisabethgross568
  • ryanresurreccion
  • rfonseca.luxurypresence
  • antopisciolp
  • mflores17
  • vinay-lp
  • bcanepa-luxurypresence
  • acarlislelp
  • galgordo
  • ghardt
  • kelios
  • aandraslp
  • luxmedina
  • janlp
  • bradfordh
  • trobertslp
  • dougngo
  • wheitor
  • erikchau
  • viktorqa
  • xueli-lp
  • augustoalegon
  • mikebsvitla
  • taraslp
  • nazariy
  • nkashtan
  • paulchamberlain
  • jwolterslp
  • freire71
  • ojizero
  • ceallaighbrian
  • nfeigenlp
  • antopiscio
  • joebalanciolp
  • nwolfflp
  • dimasc_io
  • alex_bychkov
  • minkathalp
  • dsaldanolp
  • paulomatosj
  • qwrobins
  • carcaz
  • gabycruz
  • leocorrealp
  • danielfrank
  • rifflock
  • ulysses-odysseus
  • michaellauritsch
  • dhutton
  • jacksonwolff
  • kyletnguyen
  • sergey-at-lp
  • edwardyeh
  • timoteo
  • thenickot2
  • luxurypresenceci
  • emersonlopxs
  • kevinvoduy
  • polosergio
  • azorkin
  • pedro-surf
  • andersoonluan