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:
endpoint(name, parametersopt) → {string}
- Source:
Generates an endpoint URL.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
name |
string | The name of the endpoint on the |
||
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
|
||||||||
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:
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:
head(url, optionsopt) → {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:
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:
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:
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:
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:
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 |