HttpClient
Owns the base URL, the optional bearer token and a dataField toggle that controls whether responses are unwrapped from a data envelope. HttpClient follows a singleton pattern: register one instance at app boot and every RequestBuilder constructed without an explicit client falls back to it.
Importing
import { HttpClient } from '@basmilius/http-client';Constructor
new HttpClient(authToken: string | null, baseUrl: string, dataField?: boolean)| Argument | Type | Description |
|---|---|---|
authToken | string | null | Initial bearer token. May be replaced via the authToken setter. |
baseUrl | string | Base URL prepended to every request path. |
dataField | boolean | When true, RequestBuilder unwraps { data: ... } envelopes from JSON responses. Defaults to false. |
Instance properties
authToken—string \| null. Read/write. Used byRequestBuilder.bearerTokenwhen no explicit token is passed.baseUrl—string. Read-only.dataField—boolean. Read-only. Whentrue, the safe runners onRequestBuilderunwrapdata.datafrom JSON envelopes.
Static members
instance
Returns the registered global instance. Throws when no client has been registered.
static get instance(): HttpClient;const client = HttpClient.instance;The error message reads:
There is currently no HttpClient instance registered. Register one using the HttpClient.register() function.
register
Registers an HttpClient as the global default. Subsequent HttpClient.instance reads return the registered client; a RequestBuilder constructed without an explicit client argument falls back to it.
static register(client: HttpClient): void;Example
import { HttpClient } from '@basmilius/http-client';
const client = new HttpClient(null, 'https://api.example.com');
HttpClient.register(client);
// later, after sign-in
client.authToken = 'eyJhbGciOi...';Replacing the global
Calling HttpClient.register again replaces the global. Mixing two clients in the same runtime is safe as long as services pass an explicit HttpClient to request(path, client).
const adminClient = new HttpClient(adminToken, 'https://admin.api.example.com');
class AdminService extends BaseService {
async settings() {
return await this
.request('/settings', adminClient)
.method('get')
.bearerToken()
.run();
}
}The dataField envelope
Some backends wrap every JSON response inside a data field:
{ "data": { "id": "user-1", "email": "a@example.com" } }Set dataField to true so that the safe runners on RequestBuilder unwrap it automatically:
const client = new HttpClient(null, 'https://api.example.com', true);
HttpClient.register(client);When dataField is false (the default), the response body is passed through verbatim.
Notes
HttpClientdoes not perform any requests itself — it is consumed byRequestBuilder.- Call
HttpClient.register()exactly once at app boot. Services have no constructor and rely onHttpClient.instancebeing available.