Interface FetcherRequestInfo

Overview

Represents the request information required to make an API request using a Fetcher.

Implementers should ensure that all request properties are correctly handled when making a request. Failing to do so may result in errors or unexpected behavior.

Key points of note
  • The URL is already formatted, including query parameters.
  • The body (if present) is already JSON.stringify-ed.
  • The appropriate headers (e.g., content-type, user-agent) are set but may be overridden.
  • The correct HTTP method must be used.
  • The timeout must be respected.
  • The request may need to force HTTP/1.1 instead of HTTP/2.

See

  • Fetcher
  • FetcherResponseInfo
interface FetcherRequestInfo {
    body: undefined | string;
    forceHttp1: boolean;
    headers: Record<string, string>;
    method: "GET" | "POST" | "DELETE";
    mkTimeoutError: (() => Error);
    timeout: number;
    url: string;
}

Properties

body: undefined | string
Overview

The JSON.stringify-ed body of the request, if applicable.

The content-type header is already present in FetcherResponseInfo.headers, so it's not necessary to set it yourself.

forceHttp1: boolean
Overview

Whether to force HTTP/1[.1] for the request.

Why this is important

The DevOps API does not support HTTP/2, so such requests must only use HTTP/1 or HTTP/1.1.

headers: Record<string, string>
Overview

The base headers to include in the request.

  • These headers are preconfigured but may be modified or overridden as necessary.
  • Ensure that required headers are correctly forwarded.
Included headers

Already included headers include:

method: "GET" | "POST" | "DELETE"
Overview

The HTTP method to use for the request.

Currently used methods:

  • GET
  • POST
  • DELETE

Future updates may require additional methods.

mkTimeoutError: (() => Error)
Overview

Creates a standardized timeout error for the request.

  • If the request times out, you should catch the timeout error first and throw the result of this method.
  • This ensures a consistent error format across different fetch implementations.
Example

As an example, this is how the FetchNative implementation handles timeouts:

Type declaration

    • (): Error
    • Returns Error

Example

public async fetch(info: FetcherRequestInfo): Promise<FetcherResponseInfo> {
try {
const resp = await fetch(info.url, {
signal: AbortSignal.timeout(info.timeout),
...,
});
} catch (e) {
if (e instanceof Error && e.name === 'TimeoutError') {
throw info.mkTimeoutError();
}
throw e;
}
}

Of course, some http clients may offer a more straightforward way to handle timeouts, such as axios's timeout option, and may have different ways to express timeouts occurring.

Whatever the case, they should be appropriately set & handled to ensure the timeout is respected.

timeout: number
Overview

The timeout duration for the request, in milliseconds.

Important
  • You must respect this timeout using AbortSignal or another valid timeout mechanism.
  • If the request exceeds this timeout, throw the error generated by mkTimeoutError().

See FetcherRequestInfo.mkTimeoutError for more information on how to handle timeouts.

url: string
Overview

The full URL to which the request should be made.

This URL is preformatted, and already includes any necessary query parameters.