
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
npm i api-plus -S
yarn add api-plus -S
pnpm i api-plus -S
你可以借助 script 标签直接通过 CDN 来使用 api-plus:
<script src="https://unpkg.com/api-plus/lib/api-plus.global.js"></script>
这里我们使用了 unpkg,但你也可以使用任何提供 npm 包服务的 CDN,例如 jsdelivr 或 cdnjs。当然,你也可以下载此文件并自行提供服务。
上面的例子使用了全局构建版本的 api-plus
,该版本的所有顶层 API 都以属性的形式暴露在了全局的 apiPlus
对象上。这里有一个使用全局构建版本的例子:
<script src="https://unpkg.com/api-plus/lib/api-plus.global.js"></script>
<script>
const { transform } = new apiPlus()
const userApi = transform({
userInfo: '[get]userInfo'
})
const result = userApi.userInfo() // Promise
</script>
在本文档的其余部分我们使用的主要是 ES 模块语法。现代浏览器大多都已原生支持 ES 模块。因此我们可以像这样通过 CDN 以及原生 ES 模块使用 Vue:
<script type="module">
import apiPlus from 'https://unpkg.com/api-plus/lib/api-plus.esm-browser.js'
const { transform } = new apiPlus()
const userApi = transform({
userInfo: '[get]userInfo'
})
const result = userApi.userInfo() // Promise
</script>
注意我们使用了 <script type="module">
,且导入的 CDN URL 指向的是 api-plus
的 ES 模块构建版本。
在上面的示例中,我们使用了完整的 CDN URL 来导入,但在文档的其余部分中,你将看到如下代码:
import apiPlus from 'api-plus'
我们可以使用导入映射表 (Import Maps) 来告诉浏览器如何定位到导入的 apiPlus:
<script type="importmap">
{
"imports": {
"api-plus": "https://unpkg.com/api-plus/lib/api-plus.esm-browser.js"
}
}
</script>
<script type="module">
import apiPlus from 'api-plus'
const { transform } = new apiPlus()
const userApi = transform({
userInfo: '[get]userInfo'
})
const result = userApi.userInfo() // Promise
</script>
目前只有基于 Chromium 的浏览器支持导入映射表,所以我们推荐你在学习过程中使用 Chrome 或 Edge。 如果你使用的是 Firefox 浏览器,则该功能仅在 102+ 版本中受支持,且目前需要启用
about:config
中的dom.importMaps.enabled
选项。 如果你更喜欢那些还不支持导入映射表的浏览器,你可以使用 es-module-shims 来进行 polyfill。
import apiPlus from 'api-plus'
const { transform } = new apiPlus()
const userApi = transform({
getUserList: '[post]/user/list/' //请求地址转换成 => http://当前域名/user/list/
})
const payload = { // 请求参数
username: 'tangxiaomi',
userId: 123
}
const result = userApi.userInfo(payload) // Promise
import apiPlus from 'api-plus'
const { transform } = new apiPlus({
handleError?: handleError
handleResponse?: handleResponse
requestMethods?: Object
domain?: Domain,
axiosConfig?: AxiosRequestConfig
})
请求错误处理
const { transform } = new apiPlus({
handleError: (err) => {
console.log('err: ', err);
}
})
请求成功处理
const { transform } = new apiPlus({
handleResponse: (result) => {
console.log('result: ', result);
}
})
自定义请求方法
const { transform } = new apiPlus({
requestMethods: {
supotGet: () => { // '[supotGet]/user/list/'
}
}
})
请求域名, 默认当前域名
object | string | function
const { transform } = new apiPlus({
domain: 'http:http://www.example.com'
})
[get]<domain>/user/list/:username/userId\?
─┬─ ─┬─ ─────┬──── ───┬──── ───┬───
│ │ │ │ │
│ │ │ │ 可选参数(类似vue-router匹配规则)
│ │ │ 接口参数
│ │ │
│ │ 接口地址
│ 请求域名
│
请求方法(get | post | delete...)
/user/list 转换成 => http://www.example.com/user/list 发送get请求
[get]user/list => 转换成 => http://www.example.com/user/list 发送get请求
[post]user/list => 转换成 => http://www.example.com/user/list 发送post请求
[post]user/list/:username => 转换成 => http://www.example.com/user/list/tangxiaomi 发送post请求 需要传入参数 username
import apiPlus from 'api-plus'
const { transform } = new apiPlus()
const userApi = transform({
getUserList: '[post]user/list/:username'
})
const result = userApi.getUserList({
username: 'tangxiaomi'
})
FAQs
api-plus | 快速构建前端应该接口请求
We found that api-plus demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.