跳到主要内容

axios

axios 是一个从 HTTP 服务器获取数据的api,实现了axios的部分功能。

警告

这里的axiosapi的返回值跟js的axios不一样,这里的axiosapi只返回了data数据,如果需要返回的headers相关的数据,需要使用axiosFull

参数定义

名称类型必填说明
configAxiosRequestConfigAxios请求的配置信息

函数签名

import type { AxiosRequestConfig } from "axios";
// responseData是请求的服务返回的数据
export type ResponseData = any;
export declare const axios: (config: AxiosRequestConfig) => Promise<ResponseData>;

export interface AxiosError {
response: {
data: any;
}
}

export interface AxiosFullResponse {
data: ResponseData;
headers: Record<string, any>;
status: number;
statusText: string;
}

export declare const axiosFull: (config: AxiosConfig) => Promise<AxiosFullResponse>

参考示例

import { axios, axiosFull } from "@giteeteam/apps-api";

export const run = async () => {
try {
const data = await axios({
url,
method: "post",
data: {},
headers: {},
});
} catch(err: AxiosError) {
// You can get the returned data from err.response.data
throw new Error(err.response?.data?.message)
}

try {
const { data, status, headers } = await axiosFull({
url,
method: "post",
data: {},
headers: {},
});
} catch(err: AxiosError) {
// You can get the returned data from err.response.data
throw new Error(err.response?.data?.message)
}
};