@ackee/antonio-core 中文文档教程
@ackee/antonio-core
基于 Fetch API 构建的 HTTP 客户端。
Table of contents
Install
yarn add @ackee/antonio-core -S
Usage
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,
},
});
}
API
new Antonio(requestConfig?: RequestConfig, generalConfig?: GeneralConfig)
使用自定义请求配置和常规配置创建 Antonio
的新实例:
import { Antonio } from '@ackee/antonio-core';
const api = new Antonio({
baseURL: 'https://some-domain.com/api/',
});
Instance methods
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>
generalConfig: GeneralConfig
可选的 @ackee/antonio-core
配置:
{
// Default is [`loglevel`](https://www.npmjs.com/package/loglevel)
logger: loglevel,
}
@ackee/antonio-core
HTTP client built on Fetch API.
Table of contents
Install
yarn add @ackee/antonio-core -S
Usage
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,
},
});
}
API
new Antonio(requestConfig?: RequestConfig, generalConfig?: GeneralConfig)
Creates a new instance of Antonio
with custom request config and general config:
import { Antonio } from '@ackee/antonio-core';
const api = new Antonio({
baseURL: 'https://some-domain.com/api/',
});
Instance methods
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>
generalConfig: GeneralConfig
Optional @ackee/antonio-core
configuration:
{
// Default is [`loglevel`](https://www.npmjs.com/package/loglevel)
logger: loglevel,
}
更多