@ackee/antonio-core
Version:
A HTTP client built on fetch API with axios-like API.
89 lines (58 loc) • 2.85 kB
Markdown

# [](https://github.com/AckeeCZ/antonio/blob/master/LICENSE) [](https://travis-ci.com/AckeeCZ/antonio) [](https://reactjs.org/docs/how-to-contribute.html#your-first-pull-request) [](https://david-dm.org/AckeeCZ/antonio) [](https://bundlephobia.com/result?p=@ackee/antonio-core) [](https://bundlephobia.com/result?p=@ackee/antonio-core) 
# `@ackee/antonio-core`
HTTP client built on Fetch API.
## Table of contents
- [Install](#install)
- [Usage](#usage)
- [API (TypeDoc)](./docs)
## <a name="install"></a>Install
```bash
yarn add @ackee/antonio-core -S
```
## <a name="usage"></a>Usage
```js
import { Antonio } from '@ackee/antonio-core';
const api = new Antonio({
baseURL: 'https://jsonplaceholder.typicode.com/',
});
function* fetchTodos() {
// Since api.get returns generator function, `yield*` is required.
const { data, request, response } = yield* api.get('/todos', {
params: {
page: 1,
limit: 20,
},
});
}
```
## <a name="api"></a>API
### <a name="api-create"></a>`new Antonio(requestConfig?: RequestConfig, generalConfig?: GeneralConfig)`
Creates a new instance of `Antonio` with custom request config and general config:
```ts
import { Antonio } from '@ackee/antonio-core';
const api = new Antonio({
baseURL: 'https://some-domain.com/api/',
});
```
#### Instance methods
```ts
api.get(url: string, requestConfig?: RequestConfig): Generator<any, RequestResult>
api.delete(url: string, requestConfig?: RequestConfig): Generator<any, RequestResult>
api.head(url: string, requestConfig?: RequestConfig): Generator<any, RequestResult>
api.options(url: string, requestConfig?: RequestConfig): Generator<any, RequestResult>
api.post(url: string, data: RequestBody, requestConfig?: RequestConfig): Generator<any, RequestResult>
api.put(url: string, data: RequestBody, requestConfig?: RequestConfig): Generator<any, RequestResult>
api.patch(url: string, data: RequestBody, requestConfig?: RequestConfig): Generator<any, RequestResult>
```
## <a name="api-general-config"></a>`generalConfig: GeneralConfig`
Optional `@ackee/antonio-core` configuration:
```ts
{
// Default is [`loglevel`](https://www.npmjs.com/package/loglevel)
logger: loglevel,
}
```