thx / rapper Goto Github PK
View Code? Open in Web Editor NEW一个自带类型的请求库
Home Page: https://www.yuque.com/rap/rapper/readme
一个自带类型的请求库
Home Page: https://www.yuque.com/rap/rapper/readme
不管是overrideFetch还是createFetch,把headers设置成 application/x-www-form-urlencoded 都不起作用,只有写在fetch的第二个参数中有用,且content-type属性必需是小驼峰contentType才行
以下代码设置后是不起作用的:
export const fetch = createFetch(
{
/** 'prefix' 前缀,统一设置 url 前缀,默认是 '' */
// prefix: 'https://rap2api.taobao.org/app/mock/3402',
fetchOption: {
/** 全局配置请求 content-type,默认是 'application/json' */
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
credentials: 'same-origin',
},
},
{ fetchType: FetchType.AUTO },
);
以下代码设置是可以的:
fetch['POST/api/detail'](
{ uid: 1 },
{
contentType: 'application/x-www-form-urlencoded',
}
)
当根结点是数组时,会生成_root_层级
如在rap2中导入[{a: 1}, {a: 2}]
时
rapper生成代码的interface 会变成
Res: {
_root_: {
a: number;
}[]
}
这样是无效的
request["POST/data-api/v1/nodes/{node_id}/products"]({node_id: '123' ...rest})
helper!!!
无法区分query和body的参数
无论在rap2上设置query或者body他都作为params进入
目前生成的代码大概是这样:
export interface IModels {
'GET/welcome': {
Req: {},
Res: {},
}
}
export const createFetch() {
return {
'GET/welcome': (input) => doFetch('GET', '/welcome', input),
// ...
}
}
export const fetch = createFetch()
这样会生成大量的没有意义的 api 实例函数(createFetch
函数内返回的对象在编译成 js 后依旧存在), 建议改成这样:
export interface IModels {
'GET/welcome': {
Req: {},
Res: {},
}
}
export function fetch<K extends keyof IModels>(key: K, input: IModels[K]['Req']): Promise<IModels[K]['Res']> {
const pos = key.indexOf('/')
const method = key.substr(0, pos)
const url = key.substr(pos)
return doFetch(method, url, input)
}
这样生成的 js 代码就只有一个 fetch
实例了, 会简单很多, 并且 IDE 在提示的时候, 对函数参数的支持会比 ['GET/welcome']
这种形式更友好.
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.