A Mapbox API request.
Note that creating a MapiRequest
does not send the request automatically.
Use the request's send
method to send it off and get a Promise
.
The emitter
property is an EventEmitter
that emits the following events:
'response'
- Listeners will be called with aMapiResponse
.'error'
- Listeners will be called with aMapiError
.'downloadProgress'
- Listeners will be called withProgressEvents
.'uploadProgress'
- Listeners will be called withProgressEvents
. Upload events are only available when the request includes a file.
emitter
EventEmitter An event emitter. See above.client
MapiClient This request'sMapiClient
.response
(MapiResponse | null) If this request has been sent and received a response, the response is available on this property.error
(MapiError | Error | null) If this request has been sent and received an error in response, the error is available on this property.aborted
boolean If the request has been aborted (viaabort
), this property will betrue
.sent
boolean If the request has been sent, this property will betrue
. You cannot send the same request twice, so if you need to create a new request that is the equivalent of an existing one, useclone
.path
string The request's path, including colon-prefixed route parameters.origin
string The request's origin.method
string The request's HTTP method.query
Object A query object, which will be transformed into a URL query string.params
Object A route parameters object, whose values will be interpolated the path.headers
Object The request's headers.body
(Object | string | null) Data to send with the request. If the request has a body, it will also be sent with the header'Content-Type: application/json'
.file
(Blob | ArrayBuffer | string | ReadStream) A file to send with the request. The browser client accepts Blobs and ArrayBuffers; the Node client accepts strings (filepaths) and ReadStreams.encoding
string The encoding of the response.sendFileAs
string The method to send thefile
. Options aredata
(x-www-form-urlencoded) orform
(multipart/form-data).
Get the URL of the request.
accessToken
string? By default, the access token of the request's client is used.
Returns string
Send the request. Returns a Promise that resolves with a MapiResponse
.
You probably want to use response.body
.
send
only retrieves the first page of paginated results. You can get
the next page by using the MapiResponse
's nextPage
function, or iterate through all pages using eachPage
instead of send
.
Returns Promise<MapiResponse>
Abort the request.
Any pending Promise
returned by send
will be rejected with
an error with type: 'RequestAbortedError'
. If you've created a request
that might be aborted, you need to catch and handle such errors.
This method will also abort any requests created while fetching subsequent
pages via eachPage
.
If the request has not been sent or has already been aborted, nothing will happen.
Invoke a callback for each page of a paginated API response.
The callback should have the following signature:
(
error: MapiError,
response: MapiResponse,
next: () => void
) => void
The next page will not be fetched until you've invoked the
next
callback, indicating that you're ready for it.
callback
Function
Clone this request.
Each request can only be sent once. So if you'd like to send the same request again, clone it and send away.
Returns MapiRequest A new MapiRequest
configured just like this one.
A Mapbox API response.
body
Object The response body, parsed as JSON.rawBody
string The raw response body.statusCode
number The response's status code.headers
Object The parsed response headers.links
Object The parsed response links.request
MapiRequest The response's originatingMapiRequest
.
Check if there is a next page that you can fetch.
Returns boolean
Create a request for the next page, if there is one.
If there is no next page, returns null
.
Returns (MapiRequest | null)
A Mapbox API error.
If there's an error during the API transaction,
the Promise returned by MapiRequest
's send
method should reject with a MapiError
.
request
MapiRequest The errored request.type
string The type of error. Usually this is'HttpError'
. If the request was aborted, so the error was not sent from the server, the type will be'RequestAbortedError'
.statusCode
number? The numeric status code of the HTTP response.body
(Object | string)? If the server sent a response body, this property exposes that response, parsed as JSON if possible.message
string? Whatever message could be derived from the call site and HTTP response.
A low-level Mapbox API client. Use it to create service clients that share the same configuration.
Services and MapiRequest
s use the underlying MapiClient
to
determine how to create, send, and abort requests in a way
that is appropriate to the configuration and environment
(Node or the browser).
accessToken
string The Mapbox access token assigned to this client.origin
string? The origin to use for API requests. Defaults to https://api.mapbox.com.