@ackee/antonio
Advanced tools
Comparing version 2.2.9 to 2.2.10
{ | ||
"name": "@ackee/antonio", | ||
"version": "2.2.9", | ||
"version": "2.2.10", | ||
"description": "A HTTP client built on axios. An access token is injected to authorization header by @ackee/petrus.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -5,3 +5,3 @@ ![ackee|Antonio](https://img.ack.ee/ackee/image/github/js) | ||
The HTTP client uses [axios](https://github.com/axios/axios) for making all HTTP requests and [@ackee/petrus](https://www.npmjs.com/package/@ackee/petrus) for adding an access token to HTTP Authorization header. | ||
A HTTP client that uses [axios](https://github.com/axios/axios) for making all HTTP requests and [@ackee/petrus](https://www.npmjs.com/package/@ackee/petrus) for adding an access token to the Authorization header. | ||
@@ -36,28 +36,25 @@ ## Table of contents | ||
Initialization is a simple 2 steps process. | ||
### 1. Create new instance | ||
By creating a new instance of `HttpClient`, you will get `api`, `authApi` objects and `saga` function. Then you connect the saga among your other sagas. That's all. | ||
### 1. Create `httpClient` instance | ||
Create one `httpClient` instance object per project. | ||
```js | ||
import * as Antonio from '@ackee/antonio'; | ||
const { api, authApi, saga } = Antonio.create({ | ||
baseURL: 'https://base-url.com/api/', | ||
}); | ||
const defaultRequestConfig = { | ||
baseURL: 'https://base-url.com/api', | ||
}; | ||
const { api, authApi, saga } = Antonio.create(defaultRequestConfig); | ||
export { api, authApi, saga }; | ||
``` | ||
### 2. Launch HttpClient saga | ||
### 2. Connect the saga | ||
Initializes the saga handlers generator. This should be passed along with your other sagas. | ||
```js | ||
import { saga as httpClient } from 'Config/antonio'; | ||
import { saga as antonio } from 'Config/antonio'; | ||
export default function*() { | ||
// httpClient saga must come before redux-token-auth saga | ||
yield all([httpClient()]); | ||
// antonio's saga must come before @ackee/petrus saga | ||
yield all([antonio()]); | ||
} | ||
@@ -75,4 +72,4 @@ ``` | ||
async function fetchTodo(todoId) { | ||
const response = await api.get('/todos/:todoId', { | ||
function* fetchTodo(todoId) { | ||
const response = yield api.get('/todos/:todoId', { | ||
// overwrite the default baseURL | ||
@@ -91,5 +88,5 @@ baseURL: 'https://jsonplaceholder.typicode.com/', | ||
By using methods under `authApi` object, it's guaranteed that each HTTP request is going to have access token in its `Authorization` header. | ||
By using methods under `authApi` object, it's guaranteed that each HTTP request is going to have an access token in its `Authorization` header. | ||
If the access token isn't available at the moment, the request is paused by `take(ACCESS_TOKEN_AVAILABLE)` effect, and timeout, if enabled, is set. See [`accessTokenUnavailableTimeout` at create method](#api-create-customConfig) for more details. | ||
If the access token isn't available at the moment, the request is paused by `take(ACCESS_TOKEN_AVAILABLE)` effect, and timeout, if enabled, is set. See the [`accessTokenUnavailableTimeout`](#api-create-customConfig) for more details. | ||
@@ -101,4 +98,4 @@ See [available properties](#api-create-http-client) of the `authApi` object. | ||
async function fetchPost(postId) { | ||
const response = await authApi.get(`/posts/${postId}`); | ||
function* fetchPost(postId) { | ||
const response = yield authApi.get(`/posts/${postId}`); | ||
@@ -117,9 +114,21 @@ return response.data; | ||
### <a name="api-create"></a>`create(axiosRequestConfig: Object, customConfig: Object) => httpClient:Object` | ||
### <a name="api-create"></a>`create(defaultRequestConfig: Object, customConfig: Object) => Object` | ||
This method receives two objects as arguments. | ||
- `axiosRequestConfig: Object` | ||
- `defaultRequestConfig: Object` | ||
The `axiosRequestConfig` is reserved for axios default request configuration, see [available options](https://github.com/axios/axios#request-config). | ||
The `defaultRequestConfig` object is passed to axios as default request configuration. | ||
__Available properties__: | ||
- [axios request config](https://github.com/axios/axios#request-config) | ||
- additional props: | ||
```js | ||
// `uriParams` - Key-value object containing request uri params. Params that are found in url are replaced, rest is ignored. | ||
uriParams: { | ||
// ':todoId' will be replaced with '1' | ||
// '/todos/:todoId' -> '/todos/1' | ||
todoId: '1', | ||
}, | ||
``` | ||
@@ -169,7 +178,7 @@ - <a name="api-create-customConfig"></a>`customConfig: Object` | ||
- <a name="api-create-http-client"></a>`httpClient: Object` | ||
- <a name="api-create-http-client"></a>`Object` | ||
#### `api`, `authApi` | ||
The `httpClient` object contains two axios instances: `api` and `authApi` with the same properties: | ||
`api` and `authApi` have the same following properties: | ||
@@ -188,23 +197,4 @@ - `api.request(config)` | ||
##### `config` | ||
- `uriParams: Object` - Key-value object containing request uri params. Params that are found in url are replaced, rest is ignored. | ||
```js | ||
yield api.get('/todos/:todoId', { | ||
baseURL: 'https://jsonplaceholder.typicode.com', | ||
uriParams: { | ||
// ':todoId' will be replaced with '1' | ||
todoId: '1', | ||
// 'foo' will be ignored and won't be added as a query parameter | ||
foo: '2', | ||
}, | ||
}); | ||
``` | ||
See rest of available options - [axios/request-config](https://github.com/axios/axios#request-config) | ||
#### `saga` | ||
Internal saga primarily for communication with `ackee-redux-token-auth`. | ||
Internal saga, primarily for communication with [`@ackee/petrus`](https://github.com/AckeeCZ/petrus). | ||
@@ -211,0 +201,0 @@ #### Example |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52379
225