FetchBackend
Concrete HttpBackend implementation bundled with @opra/client. Uses the browser/Node.js fetch API as the transport.
OpraHttpClient and HttpFetchClient construct a FetchBackend automatically. You only interact with it directly when you want to configure defaults, register interceptors, or override its protected methods.
Class
class FetchBackend extends HttpBackend
Constructor
new FetchBackend(serviceUrl: string, options?: FetchBackend.Options)
Properties
| Property | Type | Description |
|---|---|---|
serviceUrl | string | Normalized base URL |
interceptors | FetchBackend.Interceptor[] | Active interceptors (duplicates removed) |
defaults | FetchBackend.RequestDefaults | Default headers and query params merged into every request |
Protected methods
Override these to customize behavior without reimplementing the full transport.
| Method | Description |
|---|---|
send(request) | Calls fetch(request) — override to inject a custom fetch or a test double |
prepareRequest(init) | Applies defaults, normalizes the URL, sets Content-Type |
createResponse(init) | Constructs the HttpResponse instance |
parseBody(response) | Parses the response body by Content-Type |
Body serialization
prepareRequest sets Content-Type automatically based on the body value:
| Body type | Content-Type |
|---|---|
string / number / boolean | text/plain; charset="UTF-8" |
ReadableStream | application/octet-stream |
Buffer | application/octet-stream |
Blob | blob.type or application/octet-stream |
FormData | (browser-managed multipart boundary) |
| Any other object | application/json;charset="UTF-8" (JSON-serialized) |
Interfaces
FetchBackend.Options
interface Options extends HttpBackend.Options {
interceptors?: FetchBackend.Interceptor[];
defaults?: RequestDefaults;
document?: ApiDocument;
}
FetchBackend.RequestDefaults
Applied as baseline values to every request. Per-request values take precedence.
type RequestDefaults = {
headers: Headers;
params: URLSearchParams;
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
mode?: RequestMode;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
reportProgress?: boolean;
}
FetchBackend.RequestInit
interface RequestInit extends Omit<HttpBackend.RequestInit, 'body'>, globalThis.RequestInit {
duplex?: string;
reportProgress?: boolean;
}
FetchBackend.RequestOptions
Subset of RequestInit exposed as per-request options via .options() on HttpRequestObservable.
interface RequestOptions {
cache?: RequestCache;
credentials?: RequestCredentials;
integrity?: string;
keepalive?: boolean;
mode?: RequestMode;
redirect?: RequestRedirect;
referrer?: string;
referrerPolicy?: ReferrerPolicy;
reportProgress?: boolean;
}
See also
HttpBackend— abstract base; extend to build custom transportsHttpInterceptor— interceptors registered viaFetchBackend.Options