@hohp/http-client v1.0.1
Description
Http client module for Nest based on the axios package.
Installation
$ npm i --save axiosUsage
Import HttpClientModule:
@Module({
imports: [HttpClientModule.register({ baseUrl: 'http://localhost:3000' })],
providers: []
})
export class CatModule {}Inject HttpClientService:
@Injectable()
export class CatService {
constructor(private readonly httpClientService: HttpClientService) {}
}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
HttpClientModule.registerAsync({
useFactory: () => ({
baseUrl: 'http://localhost:3000'
})
});Obviously, our factory behaves like every other one (might be async and is able to inject dependencies through inject).
HttpClientModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
baseUrl: configService.get<string>('BASE_URL'),
}),
inject: [ConfigService],
})2. Use class
HttpClientModule.registerAsync({
useClass: HttpClientConfigService
});Above construction will instantiate HttpClientConfigService inside HttpClientModule and will leverage it to create options object.
class HttpClientConfigService implements HttpClientModuleOptionsFactory {
createHttpClientOptions(): HttpClientModuleOptions {
return {
baseUrl: 'http://localhost:3000'
};
}
}3. Use existing
HttpClientModule.registerAsync({
imports: [ConfigModule],
useExisting: ConfigService,
})It works the same as useClass with one critical difference - HttpClientModule will look up imported modules to
reuse already created ConfigService, instead of instantiating it on its own.
API Spec
The HttpClientService uses axios underneath.