0.0.12 • Published 5 months ago

axios.andbridge v0.0.12

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

对axios的一个请求封装

1.提供axios最原始的配置。 2.提供默认错误处理。 3.提供自定义错误处理。 4.目前仅支持服务端数据格式:

{
    code: httpCode,
    data:any,
    message: string
}

5.持续更新中。

For Example

最基本的请求示例 以Post为例Get同理。

import axios from "axios.andbridge"
axios.defaults.baseURL = "http://192.168.20.29";
[model层]:
export type User = {
  username: string;
  password: string;
};

export type UserInfo = {
  name: string;
  age: number;
  sex: string;
};


[api(network)层]:
import { Post } from "axios.andbridge";
import type {UserForm,UserInfo} from "@/model/user";
export const Test = async (params: UserForm) => Post<UserInfo>({url: "/test",params});




[视图层]:
import { Test } from "./test";
import { UserInfo } from "@/model/user";
let userInfo = ref<UserInfo>();
const useTest = async () => {
  userInfo.value = await Test({
    username: "test",
    password: "test",
  });
};
useTest();

取消默认错误处理 实现自定义错误捕获 视图层捕获

import type { ResponseError} from "axios.andbridge";
try{
 userInfo.value = await Test({
    username: "test",
    password: "test",
  });
}catch (error) {
  // 取消默认错误弹窗
  (error as ResponseError).hiddenDefaultErrorHandler();
  // 获得error对象 进行定制化错误处理
  throw error;
}

文件下载示例

[api(network)层]:
const download = async (params:null) => Post<any>({
  url:"/download",
  params,
  others: {
      responseType: "blob",
  },
});


[视图层]:
const useDownload = async () => {
  try {
      const data = await download(null);
      const url = window.URL.createObjectURL(new Blob([data]))
      const a = document.createElement('a')
      a.style.display = 'none'
      a.href = url
      a.setAttribute('download','excel.xls')
      document.body.appendChild(a)
      a.click() //执行下载
      window.URL.revokeObjectURL(a.href) //释放url
      document.body.removeChild(a) //释放标签
  }catch (error) {
    throw error;
  }
}
0.0.10

5 months ago

0.0.11

5 months ago

0.0.12

5 months ago

0.0.9

5 months ago

0.0.8

5 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.3

5 months ago