This module provides a class to work with server response data.
The class' API is pretty similar to native the Response class
but has some extra functionality. The class doesn't implement native Response static methods.
Because, the core/request module can use different engines, but not only fetch or XMLHttpRequest.
These engines can have different peculiarities, so we need to create a new abstraction.
There are a few differences between these classes:
The body property is not a stream object. Instead, it contains raw response data used to initialize an instance.
Each Response instance is an async iterable object. So you can use this feature to process the response as a stream.
Notice, not every request engine can stream response.
/** * A meta flag that indicates that the request is important: is usually used with decoders to indicate that * the request needs to be executed as soon as possible */ important?: boolean;
status?: StatusCodes; statusText?: string;
/** * A list of status codes (or a single code) that match successful operation. * Also, you can pass a range of codes. */ okStatuses?: OkStatuses;
/** * Type of the response data */ responseType?: ResponseType;
/** * Set of response headers */ headers?: RawHeaders;
/** * A function or sequence of functions to decode a response body */ decoder?: WrappedDecoder | WrappedDecoders;
/** * A function or sequence of functions to decode a response chunk when you are parsing the response in a stream form */ streamDecoder?: WrappedStreamDecoder | WrappedStreamDecoders;
/** * Reviver function for `JSON.parse` * @default`convertIfDate` */ jsonReviver?: JSONCb | false; }
Parses the response body and returns a promise with the result.
The operation result is memoized, and you can't parse the response as a stream after invoking this method.
A way to parse data is based on the response Content-Type header or a passed responseType constructor option.
Also, a sequence of decoders is applied to the parsed result if they are passed with a decodersconstructor option.
Parses the response body as a stream and yields chunks via an async iterator.
You can't parse the response as a whole data after invoking this method.
A way to parse data chunks is based on the response Content-Type header or a passed responseTypeconstructor option.
Also, a sequence of stream decoders is applied to the parsed chunk if they are passed with a streamDecoders constructor option.
core/request/response
This module provides a class to work with server response data. The class' API is pretty similar to native the Response class but has some extra functionality. The class doesn't implement native Response static methods.
Why not just use the native Response class?
Because, the
core/request
module can use different engines, but not onlyfetch
orXMLHttpRequest
. These engines can have different peculiarities, so we need to create a new abstraction.Difference between native and V4 Response classes
There are a few differences between these classes:
body
property is not a stream object. Instead, it contains raw response data used to initialize an instance.Response
instance is an async iterable object. So you can use this feature to process the response as a stream. Notice, not every request engine can stream response.Extra API
Events
bodyUsed
decode
streamUsed
Constructor options
With creation of a Response instance you can pass a bunch of options.
Properties
streamUsed
True, if the response body is already read as a stream.
okStatuses
A list of status codes (or a single code) that match successful operation. Also, you can pass a range of codes.
decoders
A list of response decoders.
streamDecoders
A list of response decoders to apply for chunks when you are parsing response in a stream form.
jsonReviver
A reviver function for
JSON.parse
.emitter
Event emitter to broadcast response events.
Methods
decode
Parses the response body and returns a promise with the result. The operation result is memoized, and you can't parse the response as a stream after invoking this method.
A way to parse data is based on the response
Content-Type
header or a passedresponseType
constructor option. Also, a sequence of decoders is applied to the parsed result if they are passed with adecoders
constructor option.decodeStream
Parses the response body as a stream and yields chunks via an async iterator. You can't parse the response as a whole data after invoking this method.
A way to parse data chunks is based on the response
Content-Type
header or a passedresponseType
constructor option. Also, a sequence of stream decoders is applied to the parsed chunk if they are passed with astreamDecoders
constructor option.jsonStream
Parses the response data stream as a JSON tokens and yields them via an async iterator.
textStream
Parses the response data stream as a text chunks and yields them via an async iterator.
stream
Parses the response data stream as an ArrayBuffer chunks and yields them via an async iterator.
[Symbol.asyncIterator]
Returns an iterator by the response body. Mind, when you parse response via iterator, you won't be able to use other parse methods, like
json
ortext
.