tuax / tua-api Goto Github PK
View Code? Open in Web Editor NEW🏗 一款可配置的通用 api 请求函数生成工具(A common tool helps converting configs to api functions)
Home Page: https://tuax.github.io/tua-api/
License: MIT License
🏗 一款可配置的通用 api 请求函数生成工具(A common tool helps converting configs to api functions)
Home Page: https://tuax.github.io/tua-api/
License: MIT License
你的功能请求是否与某些问题相关?请描述
现在各种配置只能在文件里静态地配置,在使用上还是不够灵活。
例如 axios 的配置参数,headers 等,需要在调用接口时,一起传递。
描述您想要的解决方案
希望在发起请求的第二个参数传递配置。
fooApi.bar(
{ a: 1 }, // 第一个参数传的还是接口参数
{ axiosOptions: { ... } } // 第二个参数传配置
)
描述你考虑过的备选方案
虽然目前可以借助中间件实现,但在实际使用上还是有点儿繁琐。
.d.ts
声明文件其实可以这样判断是不是微信小程序的环境
export const isWx = () =>
typeof wx !== 'undefined' &&
typeof wx.request === 'function'
这样一来小程序端初始化时就不用手动填写 reqType: 'wx'
了。
版本
Version v1.3.2
描述一下 bug
简洁清晰地描述一下 bug。如果方便的话,添加一些截图描述你的问题。
当配置接口参数为 required: false
时,合并默认参数有问题。
复现 bug
复现的步骤:
export default {
...
pathList: [
{
path: 'foo',
params: {
bar: { required: false },
},
},
],
}
?bar=%5Bobject%20Object%5D
预期行为
简洁清晰地描述一下预期行为。
当填写 { required: false }
或其他对象类型时,当做空字符串处理。
版本
Version 1.1.0
描述一下 bug
简洁清晰地描述一下 bug。如果方便的话,添加一些截图描述你的问题。
在这个版本中删除了 ctx.req.fullPath
导致一些上报中间件报错
预期行为
加回来...
你的功能请求是否与某些问题相关?请描述
在 Node 端时希望根据 url query 中的字段,动态改变 host
描述您想要的解决方案
添加一个中间件修改 ctx.req.host
即可
const hostMiddleware = async (ctx, next) => {
ctx.req.host = 'new host'
await next()
}
但是目前使用 host 的中间件运行在自定义中间件之前,因此以上写法无法生效。
所以打算拆分 formatReqParamsMiddleware
中间件,将使用 host 的代码移到 setFullUrlMiddleware
中间件中。
你的功能请求是否与某些问题相关?请描述
当某个api的参数过多时,如果只有一个参数是需要对象控制的,那要把所有的参数都改写成对象,这样有点不友好,改动成本有点高
描述您想要的解决方案
能不能两者都支持,类似这样:
[
'userId',
'date',
{
'status': '0',
},
{
'foo': 1,
},
{
'bar': { required: true },
}
]
当数组中的对象类型的参数中有多个 key 时,直接抛出错误。
[
'userId',
{
'foo': 1,
'bar': { required: true },
}
]
描述你考虑过的备选方案
简洁清晰地描述一下你考虑过的其他备选方案,可能会有什么问题。
数组中的对象类型的参数有多个 key,这种方案灵活度太高,容易出问题。
附加上下文
添加一些问题的相关上下文。
当前需要手动导入配置,然后调用 tuaApi.getApi()
生成 apiMap,最后再起个名字导出...
这实在太折腾了...
需要更新 @tua-mp/cli 中相应模板
版本
Version [e.g. 0.1.0]
描述一下 bug
就是脚手架生成的项目,利用api生成一个user, 引入之后,编译没有问题,但是在小程序开发工具上报错
复现 bug
复现的步骤:
如果方便的话:
预期行为
简洁清晰地描述一下预期行为。
附加上下文
添加一些问题的相关上下文。
现在引入 web 版本是 import TuaApi from 'tua-api'
而小程序版本是 import TuaApi from 'tua-api/dist/mp'
考虑是否要将小程序版本也合进去...
区分小程序版本使用以下的方式
import TuaApi from 'tua-api'
const tuaApi = new TuaApi({ reqType: 'wx' })
这样以后添加其他平台小程序的接口也更方便...
你的功能请求是否与某些问题相关?请描述
目前仅支持字符串类型的接口请求,如果需要通过 axios 发送 FormData 数据需要这么写
const formData = new FormData()
imgUploadApi.userUpload(null, {
reqFnParams: { reqParams: formData },
axiosOptions: {
transformRequest: null,
headers: { 'Content-Type': 'multipart/form-data' },
},
})
描述您想要的解决方案
假如 api 接收的数据是 FormData 类型,则按照以下方式调用即可
imgUploadApi.userUpload(formData)
小程序里使用 setTimeout 异步 hide loading 报错...
而且在开发者工具里不复现,在手机端复现。
把异步 hide loading 改成同步的就好了...
当前中间件可以使用以下方法添加:
但是现在没法解决【个别接口不需要调用全局中间件】这种情况。
因此打算添加一个 useGlobalMiddleware
的配置来解决这个问题,可以配置在一级或二级,默认为 true。
你的功能请求是否与某些问题相关?请描述
当前虽然已经有了 @tua-mp/cli,但还是不妥。
描述您想要的解决方案
tua-api 自身支持 cli
配置文件定为 tua.config.js
。在没有配置文件时,使用内置模板。可配置的选项有 apiTemplates
,即 api 模板的路径,默认值为 .templates
。
module.exports = {
apiTemplates: 'some/path',
}
你的功能请求是否与某些问题相关?请描述
语言表达太抽象,Talk is cheap, show me the code!
描述您想要的解决方案
借助 codesandbox 搞一个在线 example。
描述你考虑过的备选方案
codepen 和 jsfiddle 也可以搞个
版本
Version 1.2.0
描述一下 bug
简洁清晰地描述一下 bug。如果方便的话,添加一些截图描述你的问题。
使用 axios 发送 get 请求时,若在 apiConfig 中填写的 params 为对象形式,由于会被透传给 axios 所以最后参数会有冗余字段。
复现 bug
复现的步骤:
export default {
type: 'get',
reqType: 'axios',
pathList: [
{
path: 'foo',
params: { a: { required: true } },
},
],
}
exampleApi.foo({ a: '123' })
?a=123&a=%7Brequired%3Atrue%7D
预期行为
没有多余参数
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.