Node.js Typescript扩展HTTP响应接口
因此,我使用Node.js Typescript扩展HTTP响应接口,node.js,typescript,Node.js,Typescript,因此,我使用axios库编写了以下代码: const BTrustURLResponse: Response = await axios.get(`${process.env.BTRUST_URL}/flow/${process.env.BTRUST_FLOWID}/link?callback_url=${callback_url}`, { headers: { 'Authorization': `Bearer ${process.env.BTRUST_API_KEY}`
axios
库编写了以下代码:
const BTrustURLResponse: Response = await axios.get(`${process.env.BTRUST_URL}/flow/${process.env.BTRUST_FLOWID}/link?callback_url=${callback_url}`, {
headers: {
'Authorization': `Bearer ${process.env.BTRUST_API_KEY}`,
},
});
我确信(console.log(Object.keys(BTrustURLResponse))
)返回的对象具有属性数据
。但是默认情况下,Response
接口不包括数据
属性
我怎样才能修好它
我尝试了以下方法:
@types
目录,带有响应
目录,然后: declare global {
export interface Response {
data?: string,
}
}
tsconfig.json
中做了以下操作:类型根“:[“@types”,“/node\u modules/@types”]
.data
和响应从文档中看,似乎数据:{}
是一个对象,但您使用字符串作为数据对象的类型<代码>数据?:字符串
,这可能是个问题。
如果您不知道响应的结构,那么首先可以使用数据?:any
,如果这样做有效,那么您可以提供适当类型的数据对象。您不必创建单独的@types文件来创建响应
接口。Axios有一个名为AxiosResponse
的通用接口,它从get(url:string,config?:AxiosRequestConfig):Promise获取T
通用变量代码>。。然后,T
generic var用于AxiosResponse的data
属性
见此:
interface ResponseData{
_id?: string
//... or any "property" you wanna add
}
const BTrustURLResponse = await axios.get<RespnseData>(`${process.env.BTRUST_URL}/flow/${process.env.BTRUST_FLOWID}/link?callback_url=${callback_url}`, {
headers: {
'Authorization': `Bearer ${process.env.BTRUST_API_KEY}`,
},
});
接口响应数据{
_id?:字符串
//…或任何你想添加的“财产”
}
const BTrustURLResponse=wait axios.get(`process.env.BTRUST\u URL}/flow/${process.env.BTRUST\u FLOWID}/link?callback\u URL=${callback\u URL}`{
标题:{
'Authorization':`Bearer${process.env.BTRUST\u API\u KEY}`,
},
});
希望它能起作用……这能回答你的问题吗?