3.0.6 • Published 8 months ago

@nodeboot/starter-http v3.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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

OptionTypeDescription
baseURLstringBase URL for HTTP requests
timeoutnumberRequest timeout in milliseconds
headersobjectDefault headers for all requests
paramsobjectDefault query parameters
httpLoggingbooleanEnables 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.

3.0.6

8 months ago

3.0.5

8 months ago

3.0.4

9 months ago

3.0.3

9 months ago

3.0.2

10 months ago

3.0.1

10 months ago

3.0.0

10 months ago

2.2.0

10 months ago

2.1.0

11 months ago

2.0.5

11 months ago

2.0.4

11 months ago

2.0.3

11 months ago

2.0.2

11 months ago

2.0.1

11 months ago

2.0.0

11 months ago