Skip to main content

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

PropertyTypeDescription
serviceUrlstringNormalized base URL
interceptorsFetchBackend.Interceptor[]Active interceptors (duplicates removed)
defaultsFetchBackend.RequestDefaultsDefault headers and query params merged into every request

Protected methods

Override these to customize behavior without reimplementing the full transport.

MethodDescription
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 typeContent-Type
string / number / booleantext/plain; charset="UTF-8"
ReadableStreamapplication/octet-stream
Bufferapplication/octet-stream
Blobblob.type or application/octet-stream
FormData(browser-managed multipart boundary)
Any other objectapplication/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 transports
  • HttpInterceptor — interceptors registered via FetchBackend.Options