Adapter
The main goal of Adapter is make some magic and return function that execute query according to passed url and Request object or enchant another Adapter.
In most cases - adapter is using to adapt api function for used approaches or libraries (fetch, axios, rxjs, local storage) and execute query.
Also adapter is using for enchanting other adapters - add additional delays for testing, log api calls, save request response as blob, filter params etc. For example there are delayAdapter, saveBlobAdater, localStorage adapter are enchanters.
# Adapter list
# Under the hood
api function does not guarantee that XHR method will be called. It just call adapter with generated config. What magic will be done depends on used adapter. In case of localStorageAdapter - it will save and get data from localStorage.
import fetchAdapter from 'bivrost-fetch-adapter';
const fetch = fetchAdapter(options);
Options depends on the adapter. But in most cases options are:
- headers - requests headers. NOTE that headers could be also set with interceptors
- interceptors - adapter interceptors - request / response /error
- Axios Adapter Options
- Fetch Adapter Options
Example:
import bivrostApi from 'bivrost/http/api'
import fetchAdapter from 'bivrost-fetch-adapter';
const api = bivrostApi({
protocol: 'http:'
host: 'localhost:3001',
adapter: fetchAdapter()
});
const createUser = api('POST /user?:hash&:version');
createUser({
name: 'john_doe',
hash: 'eecab3',
version: 'v1.2.0'
});
As the result api function will call adapter with generated config and returns its response.
{
"method": "POST",
"path": "/user?hash=eecab3&version=v1.2.0",
"body": {
"name": "John Doe"
}
}
Next two example will produce same result:
- Using api function:
import bivrostApi from 'bivrost/http/api'
import fetchAdapter from 'bivrost-fetch-adapter';
const api = bivrostApi({
protocol: 'http:'
host: 'localhost:3001',
adapter: fetchAdapter()
});
const createUser = api('POST /user?:hash&:version');
return createUser({
name: 'john_doe',
hash: 'eecab3',
version: 'v1.2.0'
});
- Using adapter directly:
import fetchAdapter from 'bivrost-fetch-adapter';
const fetch = fetchAdapter();
return fetch({
method: 'POST',
path: 'http://localhost:3001/user?hash=eecab3&version=v1.2.0',
body: {
name: 'John Doe',
},
});