Tauri 实战总结 系列一(接口请求)

在这里插入图片描述

前言:2022年下半年本人开发了一款基于 Tauri 的应用程序,从 Tauri 小白到项目上线一路收获颇丰。现将开发过程中踩过的坑做一下总结,本系列不定期更新,示例源码地址:tauri-study-examples

接口请求处理

项目中没有使用 axios 等前端 HTTP 请求库,使用的是 Tauri 内置的 fetch 方法,但该方法使用比较简单,没有请求拦截器或响应拦截器相关配置,所以我们有必要在此基础上做下二次封装。

1. 配置安全域名

tauri.conf.json 里添加配置

"allowlist": {
  "http": {
    "scope": [
      "https://xxx/*"
    ]
  }
}

2. 封装 http 请求

新建 utils/http.ts 文件

import { fetch, ResponseType } from '@tauri-apps/api/http';
import qs from 'qs';

const isAbsoluteURL = (url: string): boolean => {
  return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
};

const combineURLs = (baseURL: string, relativeURL: string): string => {
  return relativeURL
    ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
    : baseURL;
};

const buildFullPath = (baseURL: string, requestedURL: string) => {
  if (baseURL && !isAbsoluteURL(requestedURL)) {
    return combineURLs(baseURL, requestedURL);
  }
  return requestedURL;
};

const buildURL = (url: string, params: any): string => {
  if (!params) {
    return url;
  }
  const serializedParams = qs.stringify(params);
  if (serializedParams) {
    url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
  }
  return url;
};

const server = '';
const baseURL = `${server}/api/v1`;

const BODY_TYPE = {
  Form: 'Form',
  Json: 'Json',
  Text: 'Text',
  Bytes: 'Bytes',
};

const commonOptions = {
  timeout: 60,
  responseType: ResponseType.JSON,
};

const http = (url: string, options: any = {}) => {
  const params = { ...options.params };
  if (!options.headers) options.headers = {};
  // todo 可以往 headers 中添加 token 或 cookie 等信息

  if (options?.body) {
    if (options.body.type === BODY_TYPE.Form) {
      options.headers['Content-Type'] = 'multipart/form-data';
    }
  }

  options = { ...commonOptions, ...options };

  return fetch(buildURL(buildFullPath(baseURL, url), params), options)
    .then(({ status, data }) => {
      if (status >= 200 && status < 400) {
        return { data };
      }
      return Promise.reject({ status, data });
    })
    .catch((err) => {
      console.error(err);
      return Promise.reject(err);
    });
};

export default http;

3. 简单使用

GET 请求
import http from '@/utils/http';

const params = {};

http('https://www.baidu.com/get', {
  method: 'get',
  params,
});
POST 请求
import { Body } from '@tauri-apps/api/http';
import http from '@/utils/http';

const body = Body.json({
  test: '1',
});

http('https://www.baidu.com/post', {
  method: 'post',
  body,
});
POST 上传文件
import { Body } from '@tauri-apps/api/http';
import http from '@/utils/http';

const generateFileInfo = (
  arrayBuffer: any,
  mime: string,
  fileName: string,
) => {
  return {
    file: new Uint8Array(arrayBuffer),
    mime,
    fileName,
  };
};

const arrayBuffer = await file.arrayBuffer();
const body = Body.form({
  file: generateFileInfo(arrayBuffer, file.type, file.name),
  // todo 其他参数
});

http('https://www.baidu.com/upload', {
  method: 'post',
  body,
});
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑ii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值