Skip to main content

HttpRequestObservable

HttpRequestObservable is the fluent request builder returned by all HttpClientBase methods. It extends RxJS Observable — the request is lazy and only executes when you subscribe or call .getBody() / .getResponse().

const body = await client.get<Order[]>('orders')
.param('filter', 'status = "active"')
.header('X-Tenant-Id', tenantId)
.getBody();

Class

class HttpRequestObservable<T, TBody, TRequestOptions, TResponseExt>
extends Observable<T>

Methods

header(name, value?)

Sets or removes a single request header.

header(name: string, value?: string | number | boolean | null): this

Passing null or omitting value removes the header. Also accepts a HeadersInit object to set multiple headers at once:

header(headers: HeadersInit): this
client.get('orders')
.header('Authorization', `Bearer ${token}`)
.header('X-Request-Id', requestId);

param(name, value?)

Sets or removes a URL query parameter. Passing null removes it.

param(name: string, value: any): this
param(params: URLSearchParamsInit | Record<string, string | number | boolean | Date>): this

Object and array values are JSON-serialized automatically.

client.get('orders')
.param('filter', 'status = "active"')
.param('limit', 20);

observe(type)

Changes what the observable emits. Returns a new observable — does not mutate the original.

observe(observe: HttpObserveType.Body): HttpRequestObservable<TBody, ...>
observe(observe: HttpObserveType.Response): HttpRequestObservable<HttpResponse<TBody>, ...>
observe(observe: HttpObserveType.ResponseHeader): HttpRequestObservable<HttpResponse<void>, ...>
observe(observe: HttpObserveType.Events): HttpRequestObservable<HttpEvent<TBody>, ...>
ModeEmits
Body (default)Parsed response body only
ResponseFull HttpResponse<TBody> after the body is received
ResponseHeaderHttpResponse<void> as soon as headers arrive
EventsAll HttpEvent items (Sent, UploadProgress, DownloadProgress, ResponseHeader, Response)

options(opts)

Merges backend-specific options (e.g. cache, credentials, reportProgress) into the request context.

options(opts: TRequestOptions): this
client.get('report/export')
.options({ reportProgress: true, credentials: 'include' });

getBody()

Convenience shorthand for lastValueFrom(observable.observe(Body)). Returns a Promise<TBody>.

getBody(): Promise<TBody>

getResponse()

Returns a Promise<HttpResponse<TBody> & TResponseExt> with the full response including status, headers, and body.

getResponse(): Promise<HttpResponse<TBody> & TResponseExt>

HttpObserveType enum

enum HttpObserveType {
Body = 'body',
Response = 'response',
ResponseHeader = 'response-header',
Events = 'events',
}

Examples

Read body as Promise

const orders = await client.get<Order[]>('orders').getBody();

Read full response (status + headers + body)

const res = await client.post<Order>('orders', payload).getResponse();
if (res.ok) {
console.log(res.status, res.body);
}

Track upload progress via Observable

import { filter } from 'rxjs';
import { HttpEventType } from '@opra/client';

client.post('upload', file)
.options({ reportProgress: true })
.observe(HttpObserveType.Events)
.pipe(filter(e => e.type === HttpEventType.UploadProgress))
.subscribe(e => console.log(`${e.loaded} / ${e.total}`));