Constructor
new Api(settings)
Constructor for an Api object
Parameters:
Name | Type | Description |
---|---|---|
settings |
ApiSettings
|
Settings to create api instance |
Methods
_debug(…args)
Console log all arguments if verbose setting is true
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
args |
*
|
<repeatable> |
All args passed to console.log |
authorize(args)
Authorize this API. If token is provided, it will be used for authorization.
If username/password are provided, settings.authorization
must be set to 'Basic',
and a token will be generated using Api#base64encode
with input: ${username}:${password}
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
args |
object
|
|
Throws:
-
- Arguments MUST provide headers, token OR username & password
- Type
-
Error
base64decode(b64, formatopt) → {string}
Decode given base64 string
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
b64 |
string
|
Base64 string to be decoded |
||
format |
string
|
<optional> |
'utf8' |
Target format for decoded string |
Returns:
- Type:
-
string
- Returns base64 decoded string
Example
Example usage of base64decode.
api.base64encode('hello world'); // returns 'aGVsbG8gd29ybGQ='
base64encode(str) → {string}
Encode given string as base64
Parameters:
Name | Type | Description |
---|---|---|
str |
string
|
Input string to be encoded |
Returns:
- Type:
-
string
- Returns base64 encoded string
Example
Example usage of base64encode.
api.base64encode('aGVsbG8gd29ybGQ='); // returns 'hello world'
baseUrl() → {string}
Construct the base URL for this API (using other member variables)
Returns:
- Type:
-
string
- Returns the constructed BaseUrl (root + stage + prefix + version)
buildUrl(path) → {string}
Build a request URL for a specific path
Parameters:
Name | Type | Description |
---|---|---|
path |
string
|
Build full request url for path |
Returns:
- Type:
-
string
- Fully constructed request url
(async) callRoute(args) → {Promise.<object>}
Generic request execution method. For a GET request, only the "path" parameter is required
Parameters:
Name | Type | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
args |
object
|
Arguments to call api route
|
Returns:
- Type:
-
Promise.<object>
- Returns promise with response data
(async) delete(path, optionsopt) → {Promise.<object>}
Query API route for path
using the DELETE method with a body
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
path |
string
|
Request path |
|
options |
object
|
<optional> |
Additional fetch options |
Returns:
- Type:
-
Promise.<object>
- Returns promise with response data
Example
Example usage of delete.
var api = new Api({ root: 'api.example.com' })
api.delete('todos/1')
.then(resp => {
// resp = response from DELETE https://api.example.com/todos/1
})
.catch(err => {
// Something went wrong
})
(async) get(optionsopt) → {Promise.<object>}
Helper for simple GET requests (only need to call "api.get('something')")
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
object
|
<optional> |
Additional fetch options |
Returns:
- Type:
-
Promise.<object>
- Returns promise with response data
Example
Example usage of get.
var api = new Api({ root: 'api.example.com' })
api.get('todos')
.then(resp => {
// resp = response from GET https://api.example.com/todos
})
.catch(err => {
// Something went wrong
})
handleEvents(response)
Process event emitters for response
Parameters:
Name | Type | Description |
---|---|---|
response |
object
|
Fetch response to trigger events |
handleMiddlewares(response)
Process middleware chain
Parameters:
Name | Type | Description |
---|---|---|
response |
object
|
Fetch response to be passed through middleware chain |
hasValidToken() → {boolean}
Check if this API instance has a valid token
Returns:
- Type:
-
boolean
- Whether or not this API instance has a valid token
(async) head(path, optionsopt) → {Promise.<object>}
Query API route for path
using the HEAD method (no response body)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
path |
string
|
Request path |
|
options |
object
|
<optional> |
Additional fetch options |
Returns:
- Type:
-
Promise.<object>
- Returns promise with response data
Example
Example usage of head.
var api = new Api({ root: 'api.example.com' })
api.head('todos', payload)
.then(resp => {
// response from HEAD https://api.example.com/todos (no response body)
// resp = {
// url: 'https://api.example.com/todos',
// status: 200,
// statusText: 'OK',
// headers: { ... }
// }
})
.catch(err => {
// Something went wrong
})
init()
Initialize API instance
Throws:
-
Unable to load fetch library via NodeJS or window.fetch
- Type
-
Error
isAuthorized() → {boolean}
Check if this API instance is authorized
Returns:
- Type:
-
boolean
- If this API instance has
settings.authorization
set and a valid token
(async) patch(path, bodyopt, optionsopt) → {Promise.<object>}
Query API route for path
using the PATCH method with a body
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
path |
string
|
Request path |
|
body |
object
|
<optional> |
Request payload |
options |
object
|
<optional> |
Additional fetch options |
Returns:
- Type:
-
Promise.<object>
- Returns promise with response data
Example
Example usage of patch.
var api = new Api({ root: 'api.example.com' })
var payload = {
todo: {
name: 'new name (patched)'
}
}
api.patch('todos/1', payload)
.then(resp => {
// resp = response from PATCH https://api.example.com/todos/1
})
.catch(err => {
// Something went wrong
})
(async) post(path, bodyopt, optionsopt) → {Promise.<object>}
Query API route for path
using the POST method with a body
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
path |
string
|
Request path |
|
body |
object
|
<optional> |
Request payload |
options |
object
|
<optional> |
Additional fetch options |
Returns:
- Type:
-
Promise.<object>
- Returns promise with response data
Example
Example usage of post.
var api = new Api({ root: 'api.example.com' })
var payload = {
todo: {
name: 'foobar'
}
}
api.post('todos', payload)
.then(resp => {
// resp = response from POST https://api.example.com/todos
})
.catch(err => {
// Something went wrong
})
(async) put(path, bodyopt, optionsopt) → {Promise.<object>}
Query API route for path
using the PUT method with a body
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
path |
string
|
Request path |
|
body |
object
|
<optional> |
Request payload |
options |
object
|
<optional> |
Additional fetch options |
Returns:
- Type:
-
Promise.<object>
- Returns promise with response data
Example
Example usage of put.
var api = new Api({ root: 'api.example.com' })
var payload = {
todo: {
name: 'new name'
}
}
api.put('todos/1', payload)
.then(resp => {
// resp = response from PUT https://api.example.com/todos/1
})
.catch(err => {
// Something went wrong
})
sanitize()
Sanitize all API variables for this API instance
Throws:
-
API root must be HTTPS if secureOnly is true in the
ApiSettings
- Type
-
Error
use(fn)
Register middleware
Parameters:
Name | Type | Description |
---|---|---|
fn |
function
|
Middleware function (must resolve with response) |
validate()
Validate this API instance
Throws:
-
- If required schema is invalid
- Type
-
Error