Response Object
Once that rest client was defined, you can proceed to execute your REST calls, the response object obtained aims to cover every details needed by a complex application.
The return type of every REST method is basically a Promise, wrapping IResponse<TResponse, TError>, where:
TResponse, will infer theresponse.datatypeTError(optional), will infer theresponse.error.datatype
To start the inference just use Typescript Generics on the REST method:
const response = await client.get<MyObject, MyError>("/path")...then you will have type inference on the following scenarios:
response.datatyped asMyObjecton success, orundefinedon failureresponse.error.datatyped asMyErroron failure, orundefinedon success
Keep reading to better understand every prop provided by the response object.
fetchResponse
Instance of type Response, the native response object from the Fetch API.
request
interface IRequest {
options: Partial<Options>;
url: URL
method: HttpMethod
body?: any
}The request object used to perform the request, containing:
- The
Optionsobject used - The URL instance evaluated using host, basePath and the request path.
- The HTTP method used to perform the request.
- The (optional) request body, typically used when
HttpMethodis PUT or POST.
error
An instance of type RestError if present, or undefined on successful requests.
INFO
Keep in mind that this is considering the throwExcluding logics.
status
type StatusCode = numberThe HTTP Status code of the response.
headers
An instance of type Headers class, representing the response headers of the request.
data
The parsed response if present.
throwFilter
When a provided throw filter (via throwExcluding) matches, this property will expose it.
repeat
() => Promise<IResponse>A shortcut to repeat the request sent with the same options.
const first = await get<any>("/action");
const second = await first.repeat();You can even override request options on a local repeat call.
const response = await second.repeat({ responseType: "text" });