Urgent.News

What's breaking now, across thousands of outlets.

Tech

Stop repeating HttpClient boilerplate in Angular

Set your API base URL and headers once with provideApi(), then inject(ApiService) anywhere. A tiny typed wrapper for Angular 17+. Every Angular app that talks to a REST API ends up with the same few lines in every service: read the base URL from the environment, build the headers, glue the URL together, call HttpClient . Then the app grows, a second backend shows up, and those lines multiply. I…

In modern Angular apps that communicate with a REST API, developers often encounter the same few lines of code in every service. These lines involve reading the base URL from the environment, building the headers, constructing the URL, and calling HttpClient. As applications expand and additional backends are introduced, these lines of code tend to multiply, leading to tedious repetition.

To address this issue, a small wrapper called @arxis/api was developed and released in version 3.0, specifically tailored for Angular 17 and above.

The primary problem with the current approach is the presence of repetitive plumbing code in each service method. For instance, in a UserService, the majority of the method is dedicated to users, while the rest consists of boilerplate code. This boilerplate includes configuring the API, setting headers, and constructing URLs.

To resolve this issue, developers can install the @arxis/api package and configure the base URL and headers once using the provideApi() function. By doing so, they can then inject the ApiService anywhere within their Angular app. This tiny wrapper provides a typed interface for Angular services, streamlining the process of making HTTP requests.

The @arxis/api package offers a set of typed methods for common HTTP operations such as get, post, put, patch, and delete. These methods accept query parameters as a plain object and provide overloads similar to HttpClient, including observe: response and observe: events. This allows developers to maintain the same dispatch of data and events as with HttpClient, while enjoying a more concise and maintainable codebase.

Furthermore, @arxis/api is built on top of Angular's HttpClient, meaning that existing functionalities like interceptors, withFetch(), server-side rendering, and HttpTestingController remain fully functional. For example, an auth token that may change during app runtime can be managed through an interceptor, ensuring that it applies to every ApiService request.

In summary, the @arxis/api library provides a solution to the repetitive boilerplate code issue in Angular services that interact with REST APIs. By configuring the API once and injecting the ApiService, developers can focus on defining their endpoints, resulting in cleaner, more maintainable code while preserving the functionality of Angular's HttpClient.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Rootless Docker & Advanced Security: Which Root Are We Actually Talking About?

You run a container as root. That sounds dangerous. But here's a more interesting question: Which root? Root inside the container? Root on the host? Or the root user running the Docker daemon?

  • Root inside container differs from root on host
  • Docker daemon runs with root privileges by default
  • Rootless Docker reduces attack surface by limiting privileges

DAY 3 - SAGA Design Pattern

To manage distributed transactions across multiple microservices. It divides a large transaction into a series of smaller local transactions. Executed independently.

  • SAGA pattern manages distributed transactions across microservices.
  • Breaks large transaction into smaller local transactions executed independently.
  • Compensating actions undo previously completed operations in case of failure.

More from Thursday 24 September →