1.0.6 • Published 1 year ago

react-ajax-manager v1.0.6

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

AJAX-MANAGER

目前仍在测试完善中; (DEMO)https://github.com/hunterlord/ajax-manager-demo/tree/main/react

react-ajax-manager使用axiosreduximmutable 来快速创建ajax过程常用的状态,并自动处理。 且提供了处理接口,意在减少 ajax 相关开发工作量。

当前库依赖axiosreduximmutable

基本使用方法

  • 初始化

创建文件utils/axios.js

import AjaxManager from 'react-ajax-manager';

const { API_BASE_URL, AJAX_TIMEOUT } = process.env;

const axiosConfig = {
  baseURL: process.env.API_BASE_URL,
  timeout: AJAX_TIMEOUT || 60000,
};

const ajaxManager = new AjaxManager(axiosConfig);

export { ajaxManager };
  • 创建 API

redux/api/login.js

当前文件会exportactions, reducer, types共 3 个变量以便使用

import { ajaxManager } from 'utils/axios';

export default ajaxManager.create({
  name: 'user login',
  path: 'api.user.login', // 必须设置,代表redux状态树所处位置
  axiosConfig: {
    method: 'post',
    url: '/user/login',
  },
});
  • 在组件中使用
import apiUserLogin from '@/redux/api/login';
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';

export default function LoginPage {
  const distpach = useDispatch();

  useEffect(() => {
    dispatch(apiUserLogin.actions.reset());
  }, []);

  const onSubmit = () => {
    distpach(apiUserLogin.actions.request({
      username: 'username',
      password: 'password',
    }))
  }

  return (
    <div onClick={onSubmit}>test</div>
  );
}
  • 动态创建状态

api/user/remove.js

import { ajaxManager } from '@/utils/axios';

export const path = 'api.user.remove';
export default (id, store) =>
  ajaxManager.dynamic(
    id,
    store
  )({
    name: 'user remove',
    path,
    axiosConfig: {
      method: 'delete',
      url: '/user/remove',
    },
  });

组件中使用:

import apiUserRemove, { path as userRemovePath } from '@/redux/api/user/remove';
import { useDispatch, useStore, useSelector } from 'react-redux';
import { createSelector } from 'reselect';

const selectApiUserRemoveDynamic = createSelector(
  (state) => state,
  (state) => state[userRemovePath] || {}
);

const removeId = 1;

export default function UserRemove() {
  const distpach = useDispatch();
  const store = useStore();

  const ar = useSelector(selectApiUserRemoveDynamic);

  const handleRemove = () => {
    dispatch(apiUserRemove(removeId, store).actions.request({ id: removeId }));
  };

  return <div onClick={handleRemove}>test remove {ar[removeId]?.get('onFetch')}</div>;
}

基础状态

  return {
    onFetch: false,
    error: false,
    message: '',
    data,
    headers: {},
    status: null,
    canceled: false,
    tokenSource: null,
    loaded: false,
    params: {},
  };
参数名称类型描述
onFetchbooleanajax 请求状态
errorbooleanajax 请求结果
messageanyajax 返回的错误或成功信息
dataanyresponse 返回值
headersobjectajax 返回的头部信息
statusintajax 返回的 http status code
canceledboolean是否被取消执行
tokenSourceobject该值仅内部自动处理
loadedbooleanajax 请求结束且内部状态事件等处理结束后将转为true
paramsanyajax 请求参数

create

参数名称类型描述
createConfigobject状态及 AJAX 配置
createConfig
参数名称类型描述
axiosConfigobjectaxios 配置
namestring唯一标志,名称
defaultDataany存储于状态的初始 data 值,该值默认为 reponse 返回值
transformobject在请求执行前,动态修改设置了的 axiosConfig 的值
checkobject检测 response 返回值,决定了该次请求 error 值
processobject在更新状态之前,修改 response 的返回值
resetOnRquestboolean新的请求重制状态
eventsobject事件
createConfig.transform
参数名称类型描述
urlfunctionfunc 会返回 3 个参数,依次为 axiosConfig.url, params, states

params 为 action 的参数,即 ajax 的请求参数 states 为当前定义的所有状态

createConfig.check
参数名称类型描述
failurefunctionfunc 会返回 1 个参数,该值为 response

当 failure 返回为 true 时,状态 error 会变为 true

createConfig.process
参数名称类型描述
failurefunctionfunc 会返回 2 个参数,依次为 response、
successfunctionfunc 会返回 2 个参数,依次为 response

该返回仅改变 response 的值、对于既成事实的状态值不造成影响。

createConfig.events
参数名称类型描述
onFinalfunction当 ajax 请求结束后,不论成功与否,该事件都会执行
1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago