1.5.0 • Published 7 months ago

@shencom/request v1.5.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
7 months ago

@shencom/request

封装 axios 请求,兼容了 uni-appuni.requestuni.uploadFile 的适配器;并且对 GET 请求和 POST 请求进行简单改写,添加了 upload 文件上传方法

官网连接: https://www.axios-http.cn/docs/intro

Install

pnpm add @shencom/request

uniapp

📢 注意:目前只测试使用 vite 创建的项目。

import { adapter, http, version } from '@shencom/request';
// or
import { http, version } from '@shencom/request/uniapp';

使用 @shencom/request/uniapp 会自定加载 adapter 适配器

使用 @shencom/request 需要自行配置

http.defaults.adapter = adapter;

browser

📢 注意:vue/cli 的项目中,开发环境中会出现 uni 未定义的情况,打包正常会进行 tree-shaking 处理。

import { http, version } from '@shencom/request';

Default Config

添加和修改一些配置,其他配置同 axios

NameDescriptionDefault
timeout请求时间20000
isFilterEmpty是否过滤空字符串的参数true
errcode接口业务状态码0000
// 接口.isFilterEmpty > defaults.isFilterEmpty
http.defaults.isFilterEmpty = false; // 默认 true
http.post('xx', {}, { isFilterEmpty: false }); // 默认 true

// 接口.errcode > defaults.errcode
http.defaults.errcode = '1111'; // 默认 '0000'
http.post('xx', {}, { errcode: '1111' }); // 默认 true

Headers

NameDescriptionTypeDefaultOptional
scid租户 idString--
AuthorizationtokenString--
miniProgram平台环境--weixin / ali / h5 / app

Set Headers

具体设置参考官网文档

// 配置示例,具体可自行根据业务进行配置
axios.defaults.headers.common.scid = scid;
axios.defaults.headers.common.miniProgram = miniProgram;
axios.defaults.headers.common.timeout = 20000;

Method

type ScRequest = <R = Dictionary, D = Dictionary>(
  url: string,
  data?: D,
  config?: Config<D>,
) => Promise<ScResponse<R>>;

get

post

upload

Example

GET and POST

对这个 2 个请求进行了一个响应拦截处理,res.data?.errcode === '0000', 遇见需要改变的请求状态码,可以在 http.defaults.errcode 和接口参数中进行设置, 如果不需要 errcode 可以使用 http.request 进行自定义请求

http.post('url', { a: 1 }).then((res) => {
  console.info(res.data);
});

// 不使用 token,和修改 scid
http.post('url', { a: 1 }, { headers: { Authorization: null, scid: 'xxx' } }).then((res) => {
  console.info(res.data);
});

// 修改 errcode
http.post('url', {}, { headers: { errcode: '1111' } }).then((res) => {
  console.info(res.data);
});

// 不对参数进行空字符串过滤
http.post('url', { a: '' }, { headers: { isFilterEmpty: false } }).then((res) => {
  console.info(res.data);
});

http.post<{ val: number }>('url').then((res) => {
  console.info(res.data.val); // val:number
});

// 类型错误
http.post<{ val: number }, { code: number }>('url', {}).then((res) => {
  console.info(res.data.val); // val:number
});

// 类型错误: 不能将类型“string”分配给类型“number”。
http.post<{ val: number }, { code: number }>('url', { code: '' }).then((res) => {
  console.info(res.data.val); // val:number
});

http.post<{ val: number }, { code: number }>('url', { code: 1 }).then((res) => {
  console.info(res.data.val); // val:number
});

UPLOAD

upload API 使用说明

一般上传使用在 服务器OSS 上传的情况
OSS 直传情况下,响应 data 返回的是空字符串,并且需要再 Header 中获取 ETag 数据 所以响应的数据没有做过滤

小程序

const data = { target: 0, open: 1, name: 'file', file };
http.upload('url', data);

web 和 uniapp web

const data = new FormData();
data.append('target', '0');
data.append('open', '1');
data.append('name', 'file');
data.append('file', file);

http.upload('url', data);

提示: 不推荐直接使用 upload 方法进行上传数据,推荐使用 @shencom/api 中的两个方法进行上传文件 BaseFileUploadBaseFileOssUpload 上传文件

Interceptors

const Token = () => {
  const Authorization = UserInfo.getToken();
  return Authorization ? `bearer ${Authorization}` : null;
};

http.interceptors.request.use(
  (res) => {
    // token 处理
    const { Authorization } = res.headers!;
    if (Authorization === null || JSON.stringify(Authorization) === '{}') {
      delete res.headers!.Authorization;
    } else {
      const token = Token();
      if (token) res.headers!.Authorization = token;
      else delete res.headers!.Authorization;
    }

    // 业务处理
    console.info('请求数据 :', res.url, '\n', res);

    return res;
  },
  (error) => {
    // 业务处理
    return Promise.reject(error);
  },
);

http.interceptors.response.use(
  (res) => {
    // 业务处理
    console.info('响应数据 :', res.config.url, '\n', res);

    return res;
  },
  (error) => {
    // 业务处理
    return Promise.reject(error);
  },
);
1.4.5

9 months ago

1.4.4

9 months ago

1.4.3

10 months ago

1.4.2

10 months ago

1.5.0

7 months ago

1.4.2-alpha.0

10 months ago

1.4.1

11 months ago

1.4.6-alpha.0

7 months ago

1.4.0

11 months ago

1.3.0

1 year ago

1.2.10-alpha.0

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.9

1 year ago

1.2.0

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.1.3

2 years ago

1.2.5-alpha.0

1 year ago

1.2.1

2 years ago

1.1.2

2 years ago

1.1.3-beta.1

2 years ago

1.1.3-beta.0

2 years ago

1.1.3-beta.6

2 years ago

1.1.3-beta.5

2 years ago

1.1.3-beta.4

2 years ago

1.1.3-beta.2

2 years ago

1.0.2

2 years ago