Registry / http-networking / nestjs-http-promise

nestjs-http-promise

JSON →
library4.0.0jsnpmunverified

nestjs-http-promise is a NestJS module that extends the framework's official HTTP capabilities by providing a promise-based API for Axios-powered requests. This library, currently at version 4.0.0, aims to simplify HTTP client interactions by eliminating the need for explicit `.toPromise()` calls on RxJS Observables, which are typically returned by NestJS's default HttpModule. A key differentiator is its out-of-the-box integration of automatic request retries using `axios-retry` and enhanced Axios stack traces for improved debugging. The package generally follows major NestJS and Axios version updates, ensuring compatibility with the latest ecosystem features. It offers both static and asynchronous configuration options, allowing for flexible setup within various NestJS architectural patterns. Its primary function is to provide a more imperative, promise-centric approach to HTTP requests within a declarative NestJS module structure, contrasting with the Observable-first approach of the base NestJS HTTP module.

npm install nestjs-http-promise
INSTALL
IMPORT
SIG · NESTJS-HTTP-PROMIS
N
nestjs-http-promise
http-networkingjavascriptv4.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HttpModule
import { HttpModule } from 'nestjs-http-promise'
const { HttpModule } = require('nestjs-http-promise')
Use named import for the module. CommonJS `require` is generally not used in modern NestJS TypeScript projects and may lead to module resolution issues.
HttpService
import { HttpService } from 'nestjs-http-promise'
import HttpService from 'nestjs-http-promise'
HttpService is a named export. Attempting a default import will result in 'Module ''nestjs-http-promise'' has no default export' or undefined.
HttpModuleOptionsFactory
import { HttpModuleOptionsFactory } from 'nestjs-http-promise'
import { IHttpModuleOptionsFactory } from 'nestjs-http-promise'
This interface is crucial for implementing asynchronous module configuration via `registerAsync` and must be imported correctly.

This quickstart demonstrates how to install, configure (asynchronously), inject, and use `nestjs-http-promise` to perform promise-based HTTP requests with built-in retries, fetching and creating a test post.

import { Module, Injectable, INestApplication } from '@nestjs/common'; import { HttpModule, HttpService, HttpModuleOptionsFactory, HttpModuleOptions } from 'nestjs-http-promise'; import { NestFactory } from '@nestjs/core'; // Example of an async configuration service @Injectable() class HttpConfigService implements HttpModuleOptionsFactory { async createHttpOptions(): Promise<HttpModuleOptions> { // Simulate fetching configuration data asynchronously, e.g., from a config service or environment variables const configurationData = await Promise.resolve({ baseURL: 'https://jsonplaceholder.typicode.com', timeout: 5000, maxRetries: 3, isBetterStackTraceEnabled: true // Enable improved stack traces by default }); return { baseURL: configurationData.baseURL, timeout: configurationData.timeout, retries: configurationData.maxRetries, isBetterStackTraceEnabled: configurationData.isBetterStackTraceEnabled, }; } } @Injectable() class MyApiService { constructor(private readonly httpService: HttpService) {} /** * Fetches a post by ID. This call benefits from the module's configured retries. */ async fetchPost(id: number): Promise<any> { console.log(`Attempting to fetch post ${id}...`); // The HttpService methods return Promises directly const response = await this.httpService.get(`/posts/${id}`); return response.data; } /** * Creates a new post. Retries can be overridden per request. */ async createPost(title: string, body: string): Promise<any> { console.log('Attempting to create a new post...'); const response = await this.httpService.post('/posts', { title, body, userId: 1 }, { retries: 0 // No retries for create operations for this specific request }); return response.data; } } @Module({ imports: [ // Asynchronously configure HttpModule using a factory class HttpModule.registerAsync({ useClass: HttpConfigService, }), ], providers: [MyApiService, HttpConfigService], // HttpConfigService must be provided if used with useClass exports: [MyApiService], // Export the service if it's used by other modules }) class ApiIntegrationModule {} async function bootstrap() { const app: INestApplication = await NestFactory.create(ApiIntegrationModule); await app.listen(3000, () => { console.log('NestJS Application listening on port 3000'); }); const apiService = app.get(MyApiService); try { const post = await apiService.fetchPost(1); console.log('\nFetched Post 1:', post); const newPost = await apiService.createPost('Hello Registry', 'This is a test post from the registry quickstart.'); console.log('\nCreated New Post:', newPost); } catch (error: any) { console.error('\nAn error occurred during API calls:'); console.error('Error message:', error.message); if (error.response) { console.error('Response status:', error.response.status); console.error('Response data:', error.response.data); } else if (error.code === 'ECONNABORTED') { console.error('Request timed out or cancelled.'); } } finally { await app.close(); console.log('\nApplication closed.'); } } // To run this quickstart, save it as a .ts file in a NestJS project and execute with `ts-node` or compile. // In a real application, `bootstrap()` would be called from `main.ts`. bootstrap();
Debug
Known issues
breakingVersion 3.0.0 introduced significant dependency upgrades, bumping Axios from v0.x.x to v1.x.x and NestJS peer dependency to v10.x.x. This may require manual updates to your project's `axios` and `@nestjs/*` dependencies to maintain compatibility.
fix
Ensure your project's `axios` version is `^1.x.x` and your NestJS core packages (`@nestjs/common`, etc.) are compatible with `^10.x.x` (or `>=7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0 || ^11.0.0` as per peer deps).
affects: >=3.0.0
gotchaThe `isBetterStackTraceEnabled` feature, which adds data to Axios stack traces, is enabled by default. While generally helpful, it can be explicitly disabled by setting `isBetterStackTraceEnabled: false` in the module configuration if it causes unexpected behavior or performance overhead in specific environments.
fix
Pass `{ isBetterStackTraceEnabled: false }` in `HttpModule.register()` or `createHttpOptions()` if you need to disable the enhanced stack traces.
affects: >=1.2.1
gotchaWhen using `HttpModule.registerAsync()` with `useClass`, the implementing class (e.g., `HttpConfigService`) must implement the `HttpModuleOptionsFactory` interface and must also be provided in the `providers` array of the importing module. Failure to provide the class will result in a NestJS DI error.
fix
Ensure the class passed to `useClass` is added to the `providers` array of the module importing `HttpModule.registerAsync()`.
affects: >=1.0.0
Errors
Common errors & fixes
Nest can't resolve dependencies of the HttpService (?). Please make sure that the argument at index [0] is available in the ApiIntegrationModule context.
HttpModule was not properly imported or configured in the module where HttpService is being injected.
fix
Verify that `HttpModule` (either `HttpModule.register()` or `HttpModule.registerAsync()`) is correctly listed in the `imports` array of the module that declares the component injecting `HttpService`.
Error: Cannot find module 'nestjs-http-promise'
The package was not installed or an incorrect import path was used.
fix
Run `npm install nestjs-http-promise` or `yarn add nestjs-http-promise`. Check that the import statement matches `import { ... } from 'nestjs-http-promise'`.
TypeError: Cannot read properties of undefined (reading 'get') (or similar for post, put, etc.) on HttpService instance.
HttpService was injected but its dependencies (Axios) might not have been properly initialized, or the module was not set up correctly leading to an undefined HttpService.
fix
Ensure `HttpModule` is correctly imported in the module that provides `HttpService`. For `registerAsync`, double-check the `useFactory` or `useClass` implementation to ensure valid `HttpModuleOptions` are returned.
Upgrade
Version history
4.0.0latest on npm
Audit
Dependencies
@nestjs/commonrequiredCore NestJS framework dependency, required for module and service integration.
reflect-metadatarequiredRequired by NestJS for dependency injection and decorator functionality.
axiosrequiredUnderlying HTTP client library used for making requests.
Agent activity
4 hits · last 30 days
node
4
Resources
nestjs-http-promise — npm install nestjs-http-promise · libregistry