Skip to content

Latest commit

 

History

History
255 lines (174 loc) · 7.58 KB

classes.md

File metadata and controls

255 lines (174 loc) · 7.58 KB

Table of Contents

MapiRequest

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 a MapiResponse.
  • 'error' - Listeners will be called with a MapiError.
  • 'downloadProgress' - Listeners will be called with ProgressEvents.
  • 'uploadProgress' - Listeners will be called with ProgressEvents. Upload events are only available when the request includes a file.

Properties

  • emitter EventEmitter An event emitter. See above.
  • client MapiClient This request's MapiClient.
  • 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 (via abort), this property will be true.
  • sent boolean If the request has been sent, this property will be true. You cannot send the same request twice, so if you need to create a new request that is the equivalent of an existing one, use clone.
  • 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 the file. Options are data (x-www-form-urlencoded) or form (multipart/form-data).

url

Get the URL of the request.

Parameters

  • accessToken string? By default, the access token of the request's client is used.

Returns string

send

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

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.

eachPage

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.

Parameters

clone

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.

MapiResponse

A Mapbox API response.

Properties

  • 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 originating MapiRequest.

hasNextPage

Check if there is a next page that you can fetch.

Returns boolean

nextPage

Create a request for the next page, if there is one. If there is no next page, returns null.

Returns (MapiRequest | null)

MapiError

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.

Properties

  • 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.

MapiClient

A low-level Mapbox API client. Use it to create service clients that share the same configuration.

Services and MapiRequests 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).

Properties