@nodeboot/starter-http v3.0.6
NodeBoot HTTP Client Starter
The NodeBoot HTTP Client Starter provides a seamless way to integrate HTTP clients into your NodeBoot application. It allows you to declare HTTP clients, manage their lifecycle, and inject them into services using dependency injection (DI).
š¦ Installation
npm install @nodeboot/starter-httpš Features
- Declarative HTTP clients using
@HttpClient() - Fully configurable using Axios-based options
- Integrated with the NodeBoot application lifecycle
- Dependency injection support
- Optional HTTP request/response logging
š Usage Guide
1ļøā£ Enabling HTTP Clients in Your Application
To use HTTP clients in a NodeBoot application, enable the feature using @EnableHttpClients():
import {Container} from "typedi";
import {NodeBoot, NodeBootApp, NodeBootApplication, NodeBootAppView} from "@nodeboot/core";
import {ExpressServer} from "@nodeboot/express-server";
import {EnableDI} from "@nodeboot/di";
import {EnableComponentScan} from "@nodeboot/scan";
import {EnableHttpClients} from "@nodeboot/starter-http";
@EnableDI(Container)
@EnableHttpClients()
@EnableComponentScan()
@NodeBootApplication()
export class SampleBackendApp implements NodeBootApp {
start(): Promise<NodeBootAppView> {
return NodeBoot.run(ExpressServer);
}
}2ļøā£ Defining an HTTP Client
Use the @HttpClient() decorator to define an HTTP client. The client configuration extends Axios request settings.
import {HttpClient, HttpClientStub} from "@nodeboot/starter-http";
@HttpClient({
baseURL: "https://jsonplaceholder.typicode.com",
timeout: 5000,
httpLogging: true,
})
export class MicroserviceHttpClient extends HttpClientStub {}Alternatively, the client can be configured using configuration properties right from the app-config.yaml file. In this case,
you should provide the placeholder for the config path and let Node-Boot autoconfigure it.
import {HttpClient, HttpClientStub} from "@nodeboot/starter-http";
@HttpClient("${integrations.http.sampleapi}")
export class MicroserviceHttpClient extends HttpClientStub {}# app-config.yaml
integrations:
http:
sampleapi:
baseURL: "https://jsonplaceholder.typicode.com"
timeout: 5000
httpLogging: true
headers:
X-API-KEY: ${API_KEY}Configuration Options
| Option | Type | Description |
|---|---|---|
baseURL | string | Base URL for HTTP requests |
timeout | number | Request timeout in milliseconds |
headers | object | Default headers for all requests |
params | object | Default query parameters |
httpLogging | boolean | Enables logging for HTTP requests/responses |
3ļøā£ Injecting the HTTP Client into a Service
Once defined, the HTTP client can be injected into a service for making API calls:
import {Service} from "typedi";
import {Logger} from "@nodeboot/logger";
import {MicroserviceHttpClient} from "./MicroserviceHttpClient";
@Service()
export class UserService {
constructor(private readonly logger: Logger, private readonly httpClient: MicroserviceHttpClient) {}
public async findExternalUsers(): Promise<User[]> {
this.logger.info("Fetching users from external service...");
const result = await this.httpClient.get("/users");
this.logger.info(`Fetched ${result.data.length} users.`);
return result.data;
}
}šÆ Summary
ā
Enable HTTP clients using @EnableHttpClients()
ā
Define HTTP clients using @HttpClient()
ā
Inject them into services for easy API integration
ā
Automatic logging for debugging API calls
š License
This package is licensed under the MIT License.