首页 > 其他分享 >ts简易网络请求封装

ts简易网络请求封装

时间:2023-04-26 21:45:13浏览次数:59  
标签:status axios 封装 string ts 简易 import message type

  1. utils/axios/type/axios.ts
    export type ErrorMessageMode = "none" | "modal" | "message" | undefined;
    export interface RequestOptions {
      // Splicing request parameters to url
      joinParamsToUrl?: boolean;
      // Format request parameter time
      formatDate?: boolean;
      // Whether to process the request result
      isTransformResponse?: boolean;
      // Whether to return native response headers
      // For example: use this attribute when you need to get the response headers
      isReturnNativeResponse?: boolean;
      // Whether to join url
      joinPrefix?: boolean;
      // Interface address, use the default apiUrl if you leave it blank
      apiUrl?: string;
      // 请求拼接路径
      urlPrefix?: string;
      // Error message prompt type
      errorMessageMode?: ErrorMessageMode;
      // Whether to add a timestamp
      joinTime?: boolean;
      ignoreCancelToken?: boolean;
      // Whether to send token in header
      withToken?: boolean;
    }
    export interface Result<T = unknown> {
      code: number;
      type: "success" | "error" | "warning";
      message: string;
      result: T;
    }
    // multipart/form-data: upload file
    export interface UploadFileParams {
      // Other parameters
      data?: any;
      // File parameter interface field name
      name?: string;
      // file name
      file: File | Blob;
      // file name
      filename?: string;
      [key: string]: any;
    }
    

      

  2. utils/axios/config.ts
    import type { AxiosRequestConfig } from "axios";
    import type { RequestOptions } from "./types/axios";
    export interface CreateAxiosOptions extends AxiosRequestConfig {
      requestOptions?: RequestOptions;
    }
    export function DefaultAxiosConfig(): CreateAxiosOptions {
      return {
        baseURL: "/api",
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
          "Access-Control-Allow-Origin-Type": "*",
        },
        timeout: 30000,
        withCredentials: true,
        validateStatus() {
          return true;
        },
        requestOptions: {
          isTransformResponse: true,
        },
      };
    }
    

     

  3. utils/axios/index.ts

    import { FrogAxios } from "./frog-axios";
    import { DefaultAxiosConfig } from "./config";
    function createFrogAxios() {
      return new FrogAxios(DefaultAxiosConfig());
    }
    export const defHttp = createFrogAxios();
    

      

  4. utils/axios/frog-axios.ts

    import axios, { type InternalAxiosRequestConfig } from "axios";
    import type {
      AxiosInstance,
      AxiosRequestConfig,
      AxiosResponse,
      AxiosError,
    } from "axios";
    import type { CreateAxiosOptions } from "./config";
    import type { RequestOptions, Result } from "./types/axios";
    import { errorHandle } from "./errorHandle";
    export class FrogAxios {
      private axiosInstance: AxiosInstance;
      private readonly options: CreateAxiosOptions;
      constructor(options: CreateAxiosOptions) {
        this.options = options;
        this.axiosInstance = axios.create(options);
        this.setupInterceptors();
      }
      private setupInterceptors() {
        /**
         * 请求拦截器
         * 每次请求前,如果存在token则在请求头中携带token
         */
        this.axiosInstance.interceptors.request.use(
          (config:InternalAxiosRequestConfig<any>) => {
            config.data = JSON.stringify(config.data);
            return config;
          },
          (error) => {
            return Promise.reject(error);
          }
        );
        this.axiosInstance.interceptors.response.use(
          (response:AxiosResponse<any, any>) => {
            if (response.status === 200) {
              return response;
            } else {
              errorHandle(response.status);
              console.log(response);
              return response;
            }
          },
          (error) => {
            if (!navigator.onLine) {
              console.log("网络不可用,请链接网络");
              // message.error('网络不可用,请链接网络');
            }
          }
        );
      }
      post<T = unknown>(
        config: AxiosRequestConfig,
        options?: RequestOptions
      ): Promise<T> {
        return this.request({ ...config, method: "POST" }, options);
      }
      get<T = unknown>(
        config: AxiosRequestConfig,
        options?: RequestOptions
      ): Promise<T> {
        return this.request({ ...config, method: "GET" }, options);
      }
      request<T = unknown>(
        config: AxiosRequestConfig,
        options?: RequestOptions
      ): Promise<T> {
        const conf: CreateAxiosOptions = config;
        const { requestOptions } = this.options;
        const opt: RequestOptions = Object.assign({}, requestOptions, options);
        conf.requestOptions = opt;
        return new Promise((resolve, reject) => {
          this.axiosInstance
            .request<unknown, AxiosResponse<Result>>(conf)
            .then((res: AxiosResponse<Result>) => {
              resolve(res as unknown as Promise<T>);
            })
            .catch((e: Error | AxiosError) => {
              reject(e);
            });
        });
      }
    }
    

     

  5. utils/axios/errorHandle.ts 

    // import { message } from 'antd';
    export const errorHandle = (status: number) => {
      // 状态码判断
      switch (status) {
        case 400:
          console.log(status);
          // message.warning('发出的请求有错误,服务器没有进行新建或修改数据的操作==>' + status);
          break;
        case 401: // 重定向
          console.log(status);
          // message.warning('token:登录失效==>' + status);
          break;
        case 404:
          console.log(status);
          // message.warning('网络请求不存在==>' + status);
          break;
        case 410:
          console.log(status);
          // message.warning('请求的资源被永久删除,且不会再得到的==>' + status);
          break;
        case 500:
          console.log("500");
          // message.error('服务器发生错误,请检查服务器==>' + status);
          break;
        case 502:
          console.log(status);
          // message.error('网关错误==>' + status);
          break;
        case 503:
          console.log(status);
          // message.error('服务不可用,服务器暂时过载或维护==>' + status);
          break;
        case 504:
          console.log(status);
          // message.error('网关超时==>' + status);
          break;
        default:
          console.log(status);
        // message.error('其他错误错误==>' + status);
      }
    };
    

     

  6. services/index.ts

    import { defHttp } from "@/utils/axios";
    import type { RootObject } from "./type";
    export async function handleList() {
      return await defHttp.get<RootObject>({ url: "/v2/index_entry" });
    }
    

      

  7. services/type.ts

    /*
     * @Description: 
     * @Date: 2023-04-25 22:54:12
     * @Author: grab
     * @Copyright: (c) 2018-2022 宁波哥爱帮科技有限公司
     */
    export interface RootObject {
        data: Datum[];
        status: number;
        statusText: string;
        headers: Headers;
        config: Config;
        request: Env;
      }
      
      interface Config {
        transitional: Transitional;
        adapter: string[];
        transformRequest: null[];
        transformResponse: null[];
        timeout: number;
        xsrfCookieName: string;
        xsrfHeaderName: string;
        maxContentLength: number;
        maxBodyLength: number;
        env: Env;
        headers: Headers2;
        baseURL: string;
        withCredentials: boolean;
        method: string;
        url: string;
      }
      
      interface Headers2 {
        Accept: string;
        'Access-Control-Allow-Origin-Type': string;
      }
      
      interface Env {
      }
      
      interface Transitional {
        silentJSONParsing: boolean;
        forcedJSONParsing: boolean;
        clarifyTimeoutError: boolean;
      }
      
      interface Headers {
        'access-control-allow-credentials': string;
        'access-control-allow-headers': string;
        'access-control-allow-methods': string;
        'access-control-allow-origin': string;
        'content-encoding': string;
        'content-type': string;
        date: string;
        etag: string;
        server: string;
        vary: string;
        'x-powered-by': string;
      }
      
      interface Datum {
        id: number;
        is_in_serving: boolean;
        description: string;
        title: string;
        link: string;
        image_url: string;
        icon_url: string;
        title_color: string;
        __v: number;
      }
    

      

  8. 使用

    <template>
      <view class="content">
        <view @click="get()">网络请求点击事件测试</view>
      </view>
    </template>
      
    <script lang="ts">
    import type { RootObject } from "@/services/type";
    import { defineComponent } from "vue";
    import { handleList} from "../../services/index";
    export default defineComponent({
      setup() {
        function get() {
          handleList().then((res:RootObject) => {
            console.log(res.data);
          });
        }
        return {
          get,
        };
      },
    });
    </script>
    
      
      <style>
    </style>
    
    function get() {
      throw new Error('Function not implemented.');
    }
      
    

      

     

标签:status,axios,封装,string,ts,简易,import,message,type
From: https://www.cnblogs.com/zjxzhj/p/17357451.html

相关文章

  • As a restaurant owner, write a professional email to the supplier to get these p
    Asarestaurantowner,writeaprofessionalemailtothesuppliertogettheseproductseveryweek:Wine(x10)Eggs(x24)Bread(x12)DearSupplier,Ihopethismessagefindsyouwell.Mynameis[YourName],andIamwritingonbehalfofmyrestaurant......
  • struts2 登录(无验证)
    版本:struts-2.3.16.1开发工具:javaee版本的eclipse服务器:tomcat6新建工程,如图:要加的jar包如图:web.xml:<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/n......
  • oracle 高级分组 GROUPING SETS
    用SCOTT/TIGER登录。groupingsets就是对参数中的每个参数做group,也就是有几个参数做几次group。SQL:SELECTJOB,DEPTNO,SUM(SAL)FROMEMPGROUPBYGROUPINGSETS(JOB,DEPTNO);结果:......
  • struts2 验证
    工程如图:Java代码:packagecom.struts2.action;importcom.opensymphony.xwork2.ActionSupport;publicclassLoginActionextendsActionSupport{ privatestaticfinallongserialVersionUID=1L; privateStringusername; privateStringpassword; private......
  • struts1 PlugIn接口
    Struts1提供PlugIn接口,可以动态增减ActionServlet的功能。如果需要在应用启动或关闭时完成某些操作,可以创建自己的PlugIn类。Java代码:packagecom.struts1.plugin;importjavax.servlet.ServletException;importorg.apache.struts.action.ActionServlet;importorg.apache.st......
  • struts1 上传文件
    java代码:packagecom.struts1.form;importorg.apache.struts.action.ActionForm;importorg.apache.struts.upload.FormFile;publicclassUploadFileFormextendsActionForm{ privatestaticfinallongserialVersionUID=1L; privateFormFileformFile; pu......
  • struts1 标签
    JSP引入标签前提必须把struts-taglib包及其依赖包加到WEB-INF/lib目录。<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%><%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%><%@tagliburi=......
  • struts1 重复提交
    Struts的Token机制能够很好的解决表单重复提交的问题,基本原理是:服务器端在处理到达的请求之前,会将请求中包含的令牌值与保存在当前用户会话中的令牌值进行比较,看是否匹配。在处理完该请求后,且在答复发送给客户端之前,将会产生一个新的令牌,该令牌除传给客户端以外,也会将用户会话中......
  • pycharm中python测试一直‘Instantiating tests...’转圈
    问题描述:defget_formatted_name(first,last):"""生成简洁的姓名"""full_name=first+""+lastreturnfull_name.title()importunittestfromname_functionimportget_formatted_nameclassNamesTestCase(unit......
  • Ubuntu 22.04.1 LTS 安装 nacos
    参考官方文档:Nacos快速开始前提条件:64bitJDK1.8+Maven3.2.x+ 通过克隆的方法安装1、克隆代码gitclonehttps://github.com/alibaba/nacos.git2、安装注意:Nacos的运行需要以至少2C4g60g*3的机器配置下运行。因此,低于此配置,在安装时就会发生内存溢出的错误。cd......