diff --git a/docs/guide/service/http.md b/docs/guide/service/http.md index 8f6b33ecd6..143be1e891 100644 --- a/docs/guide/service/http.md +++ b/docs/guide/service/http.md @@ -3,3 +3,39 @@ sidebarDepth: 2 --- # 发送网络请求 + +`ctx.http` 是 Koishi 的内置服务,其上封装了一套基于 [axios](https://github.com/axios/axios) 的网络请求 API。 + +你可能会有这样的疑问:为什么不直接使用 axios,而是使用 `ctx.http`?这是因为许多插件都需要发起网络请求,而诸如代理、超时等配置又通常是插件无关的。因此我们为这些通用需求提供了 [全局的配置项](../../api/core/app.html#options-request-proxyagent),各个插件则只需要调用 `ctx.http` 即可。 + +## 实例方法 + +### http(method, url, config) + +- **method:** `string` 请求方法 +- **url:** `string` 请求地址 +- **config:** `AxiosRequestConfig` 配置项 +- 返回值: `Promise` + +### http.axios(url, config) + +- **url:** `string` 请求地址 +- **config:** `AxiosRequestConfig` 配置项 +- 返回值: `Promise>` + +### http.head(url, config) +### http.get(url, config) +### http.delete(url, config) + +- **url:** `string` 请求地址 +- **config:** `AxiosRequestConfig` 配置项 +- 返回值: `Promise` + +### http.post(url, data, config) +### http.put(url, data, config) +### http.patch(url, data, config) + +- **url:** `string` 请求地址 +- **data:** `any` 请求数据 +- **config:** `AxiosRequestConfig` 配置项 +- 返回值: `Promise` diff --git a/docs/guide/service/router.md b/docs/guide/service/router.md index 5de5027ab0..863d50a934 100644 --- a/docs/guide/service/router.md +++ b/docs/guide/service/router.md @@ -10,7 +10,15 @@ Koishi 默认情况下并不会监听任何端口,如要启用网络服务请 Router API 提供了一个基于 [Koa Router](https://github.com/koajs/router) 的简单路由系统,用于管理 Koishi 应用收到的网络请求。除了 Koa Router 所支持的方法外,Router API 还提供了一些额外的功能,例如支持接受 WebSocket 连接等。 -## 扩展方法 +## 实例方法 + +### router[method](path, middleware) + +- **method:** 可以是 `GET`, `POST`, `PUT`, `DELETE`, `PATCH` 或 `ALL` +- **path:** `string | RegExp | (string | RegExp)[]` 路径 +- **middleware:** `Function` Koa 中间件 + +处理特定路径上的网络请求。具体请参见 [这里](https://github.com/koajs/router/blob/master/API.md)。 ### router.ws(path, handler)