🤖 Assistant
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # Trae 使用文档
> 本文档基于 [Trae 官方文档](https://docs.trae.com.cn/docs/what-is-trae?_lang=zh) 整理
## 什么是 Trae ?
(描述 Trae 的核心功能,例如:轻量级 HTTP 客户端、特性亮点等)
## 主要特性
- 🚀 简洁易用的 API 设计 - 🔧 请求/响应拦截器 - ⚡ 自动转换 JSON 数据 - 🔒 支持 TypeScript 类型推断 - 🌐 浏览器与 Node.js 双环境支持
## 安装方式
### NPM ```bash npm install trae
|
CDN
1
| <script src="https://unpkg.com/trae/dist/trae.min.js"></script>
|
快速入门
创建实例
1 2 3 4 5
| import trae from 'trae';
const api = trae.create({ baseUrl: 'https://api.example.com/v1' });
|
发起请求
1 2 3 4 5 6 7 8 9 10
| api.get('/users') .then(response => console.log(response.data)) .catch(error => console.error(error));
api.post('/users', { name: 'Alice', age: 28 });
|
API 参考
请求方法
1 2 3 4
| trae.get(url[, config]) trae.post(url, data[, config]) trae.put(url, data[, config]) trae.delete(url[, config])
|
配置项
参数 |
类型 |
默认值 |
说明 |
baseUrl |
string |
'' |
基础请求路径 |
headers |
object |
{} |
自定义请求头 |
timeout |
number |
0 |
请求超时时间 |
拦截器
请求拦截
1 2 3 4
| api.before((request) => { request.headers.Authorization = 'Bearer token'; return request; });
|
响应拦截
1 2 3 4
| api.after((response) => { console.log('Received response:', response); return response; });
|
错误处理
1 2 3 4 5 6 7 8
| api.get('/error-endpoint') .catch(error => { if (error.response) { console.log('服务器响应错误:', error.status); } else { console.log('请求未发出:', error.message); } });
|
常见问题
如何取消请求?
(解决方案说明)
版本日志
---
如需提供更准确的内容,请分享原始文档文本内容,我将帮助进行结构化转换。