wendux / fly

:rocket: Supporting request forwarding and Promise based HTTP client for all JavaScript runtimes.
https://wendux.github.io/dist/#/language
MIT License
3.9k stars 639 forks source link

feat: 拓展ts类型提示 #283

Open AntzyMo opened 2 years ago

AntzyMo commented 2 years ago

request,get,post的单一any泛型去掉,拓展了更友好的ts提示,可以定义请求data的类型和返回类型

type wxLoginRes = 'code' | 'encryptedData' | 'iv' | 'rawData' | 'signature'
type loginToken = Record<'encryptedData' | 'expirationTimeSeconds' | 'signature', string>
interface loginRes {
  loginToken: loginToken
}

fly.post<Record<wxLoginRes, string>, loginRes>('xxx', {
  code,
  encryptedData,
  iv,
  signature,
  rawData
}).then(res=>{
  res.data.loginToken
})

还可以解构response,只需加上你想要解构的字段就可以,主要用到了UntieResponse类型

fly.post<Record<wxLoginRes, string>, loginRes>('xxx', {
  code,
  encryptedData,
  iv,
  signature,
  rawData
},'data').then(res=>{
  res.loginToken
})

我觉得这样会更好

AntzyMo commented 2 years ago

请问还在维护吗

AntzyMo commented 1 year ago

如果可以的话能放点权限让我去维护这库吗?