Skip to content

Commit

Permalink
docs: add ctx.http docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 8, 2022
1 parent ec47f10 commit 1d63f2b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
36 changes: 36 additions & 0 deletions docs/guide/service/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>`

### http.axios(url, config)

- **url:** `string` 请求地址
- **config:** `AxiosRequestConfig` 配置项
- 返回值: `Promise<AxiosResponse<any>>`

### http.head(url, config)
### http.get(url, config)
### http.delete(url, config)

- **url:** `string` 请求地址
- **config:** `AxiosRequestConfig` 配置项
- 返回值: `Promise<any>`

### http.post(url, data, config)
### http.put(url, data, config)
### http.patch(url, data, config)

- **url:** `string` 请求地址
- **data:** `any` 请求数据
- **config:** `AxiosRequestConfig` 配置项
- 返回值: `Promise<any>`
10 changes: 9 additions & 1 deletion docs/guide/service/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 1d63f2b

Please sign in to comment.