iboot-http-client
v1.1.3
Published
iBoot.xin 相关应用的客户端Http封装
Maintainers
Readme
iboot-http-client
中文简介:
iboot-http-client 是一个轻量的 HTTP 客户端库,旨在帮助开发者快速接入并使用 iCMS 系统的 API。它封装了常见的请求/响应处理、鉴权与类型声明,适用于 TypeScript 在浏览器或 Node.js 环境中的调用场景。
English Summary:
iboot-http-client is a lightweight HTTP client library designed to help developers quickly integrate with the iCMS system API. It wraps common request/response handling, authentication and TypeScript typings for both browser and Node.js usage.
主要目标 / Goals
- 简化调用 iCMS API 的流程 / Simplify calling iCMS APIs
- 提供 TypeScript 类型支持 / Provide TypeScript typings
- 支持全局配置、拦截器与鉴权扩展 / Support global config, interceptors and auth
特性 / Features
- 基于 Fetch 封装,兼容浏览器与 Node(可按需替换实现) / Built on Fetch, compatible with browser and Node (swapable)
- 全局
baseURL、默认headers、自动注入token/ GlobalbaseURL, defaultheaders, automatictokeninjection - 请求/响应拦截器支持 / Request/response interceptor support
- 类型声明文件位于
index.d.ts/ Type declarations available inindex.d.ts
安装 / Install
使用 npm:
npm install iboot-http-client或使用 pnpm / yarn:
pnpm add iboot-http-client
yarn add iboot-http-client快速开始 / Quick Start
示例(TypeScript):
import { HttpClient } from 'iboot-http-client'
const client = new HttpClient({
baseURL: 'https://api.your-icms.com',
headers: { 'Content-Type': 'application/json' },
token: 'your-access-token',
})
// GET example
const list = await client.get('/articles', { params: { page: 1, pageSize: 10 } })
// POST example
const created = await client.post('/articles', { title: 'Example', body: 'Content' })
console.log(list, created)更多方法签名与配置请参阅 src/HttpClient.ts 与 index.d.ts。
配置项 / Options
baseURL:iCMS API 根地址 / iCMS API base URLheaders:默认请求头 / Default request headerstoken:认证令牌(会注入到请求头)/ Auth token (automatically injected)timeout(可选):请求超时时间 / Optional request timeout
错误处理 / Error Handling
客户端在网络错误或非 2xx 响应时会抛出异常。请使用 try/catch 处理:
try {
await client.get('/protected')
} catch (err) {
// handle auth failure, network error or business errors
}开发与贡献 / Contributing
- 欢迎提交 issue 或 PR。请在 issue 中描述重现步骤与期望行为。
- 新特性建议请先在 issue 中讨论。
本项目遵循仓库根目录的 LICENSE 文件。
如需我将 README 中的中文或英文内容扩展为更详细的使用示例(例如:鉴权流程、拦截器实现、错误码映射),或把 README 同步到包的 package.json 的 homepage、repository 字段,请告诉我。
