APIClient

shared/apiClient. APIClient

An API client with configurable endpoints.

Constructor

new APIClient(url, endpoints, fetchClient, defaultHeadersopt)

Source:
Tutorials:
Parameters:
Name Type Attributes Default Description
url string

The API entry point.

endpoints APIClientEndpoints

A dictionary of named endpoints relative to the API entry point.

fetchClient APIClientFetchClient

The fetch function that makes the requests.

defaultHeaders APIClientParametersDictionary <optional>
{}

A dictionary of default headers to include on every request.

Members

authorizationToken :string

Source:

An authorization token to include on the requests.

Type:

defaultHeaders :APIClientParametersDictionary

Source:

A dictionary of default headers to include on every request.

Type:

endpoints :Object.<string, (string|APIClientEndpoint)>

Source:

A dictionary of named endpoints relative to the API entry point.

Type:

fetchClient :APIClientFetchClient

Source:

The fetch function that makes the requests.

Type:

url :string

Source:

The API entry point.

Type:

Methods

delete(url, body, optionsopt) → {Promise.<Response>}

Source:

Makes a DELETE request.

Parameters:
Name Type Attributes Default Description
url string

The request URL.

body Object

The request body.

options APIClientFetchOptions <optional>
{}

The request options.

Returns:
Type
Promise.<Response>

endpoint(name, parametersopt) → {string}

Source:

Generates an endpoint URL.

Parameters:
Name Type Attributes Default Description
name string

The name of the endpoint on the endpoints property.

parameters APIClientParametersDictionary <optional>
{}

A dictionary of values that will replace placeholders on the endpoint definition.

Throws:

If the endpoint doesn't exist on the endpoints property.

Type
Error
Returns:
Type
string

error(response, status) → {Error}

Source:

Formats an error response into a proper Error object. This method should proabably be overwritten to accomodate the error messages for the API it's being used for.

Parameters:
Name Type Description
response Object

A received response from a request.

Properties
Name Type Attributes Description
error string <nullable>

An error message received on the response.

status number

The HTTP status of the response.

Returns:
Type
Error

fetch(options) → {Promise.<Response>}

Source:

Makes a request.

Parameters:
Name Type Description
options APIClientRequestOptions

The request options.

Returns:
Type
Promise.<Response>

get(url, optionsopt) → {Promise.<Response>}

Source:

Makes a GET request.

Parameters:
Name Type Attributes Default Description
url string

The request URL.

options APIClientFetchOptions <optional>
{}

The request options.

Returns:
Type
Promise.<Response>
Source:

Makes a HEAD request.

Parameters:
Name Type Attributes Default Description
url string

The request URL.

options APIClientFetchOptions <optional>
{}

The request options.

Returns:
Type
Promise.<Response>

headers(overwritesopt) → {Object.<string, (string|number)>}

Source:

Generates a dictionary of headers using the service defaultHeaders property as base. If a token was set using setAuthorizationToken, the method will add an Authorization header for the bearer token.

Parameters:
Name Type Attributes Default Description
overwrites Object.<string, (string|number)> <optional>
{}

Extra headers to add.

Returns:
Type
Object.<string, (string|number)>

patch(url, body, optionsopt) → {Promise.<Response>}

Source:

Makes a PATCH request.

Parameters:
Name Type Attributes Default Description
url string

The request URL.

body Object

The request body.

options APIClientFetchOptions <optional>
{}

The request options.

Returns:
Type
Promise.<Response>

post(url, body, optionsopt) → {Promise.<Response>}

Source:

Makes a POST request.

Parameters:
Name Type Attributes Default Description
url string

The request URL.

body Object

The request body.

options APIClientFetchOptions <optional>
{}

The request options.

Returns:
Type
Promise.<Response>

put(url, body, optionsopt) → {Promise.<Response>}

Source:

Makes a PUT request.

Parameters:
Name Type Attributes Default Description
url string

The request URL.

body Object

The request body.

options APIClientFetchOptions <optional>
{}

The request options.

Returns:
Type
Promise.<Response>

setAuthorizationToken(tokenopt)

Source:

Sets a bearer token for all the requests.

Parameters:
Name Type Attributes Default Description
token string <optional>
''

The new authorization token. If the value is empty, it will remove any token previously saved.

setDefaultHeaders(headersopt, overwriteopt)

Source:

Sets the default headers for the requests.

Parameters:
Name Type Attributes Default Description
headers APIClientParametersDictionary <optional>
{}

The new default headers.

overwrite boolean <optional>
true

If false, it will merge the new default headers with the current ones.