• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript axios.CancelToken类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中axios.CancelToken的典型用法代码示例。如果您正苦于以下问题:TypeScript CancelToken类的具体用法?TypeScript CancelToken怎么用?TypeScript CancelToken使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了CancelToken类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: reduceRequestConfig

export const CancellableRequest = <T>(
  ...fns: Function[]
): CancellableRequest<T> => {
  const config = reduceRequestConfig(...fns);
  const source = Axios.CancelToken.source();

  if (config.validationErrors) {
    return {
      cancel: () => null,
      request: () =>
        Promise.reject({
          config: omit(['validationErrors'], config),
          response: { data: { errors: config.validationErrors } }
        })
    };
  }

  return {
    cancel: source.cancel,
    request: () =>
      Axios({ ...config, cancelToken: source.token }).then(
        response => response.data
      )
  };
};
开发者ID:linode,项目名称:manager,代码行数:25,代码来源:index.ts


示例2: constructor

  constructor(config?: AxiosRequestConfig) {
    // initalize config if does not supply
    this.cancelTokenSource = Axios.CancelToken.source();
    const defualtConfig = { cancelToken: this.cancelTokenSource.token };
    const axiosConfig = config ? Object.assign({}, config, defualtConfig) : defualtConfig;

    this._config = axiosConfig;
    this._axios = Axios.create(this._config);
    this.interceptors = {
      request: this._axios.interceptors.request,
      response: this._axios.interceptors.response
    }
  }
开发者ID:JiarongGu,项目名称:ReactCoreTemplate,代码行数:13,代码来源:HttpClient.ts


示例3: clearTimeouts

    store.subscribe((): void => {
        const state = store.getState(); // The state is immutable Map.
        const example = state.get("example");
        if (!example || example === prevState) {
            if (prevState && !example) {
                prevState = "";
                clearTimeouts(_timerRef);
            }
            return void 0;
        }
        if (example && example !== prevState) {
            prevState = example;
            let value = "";
            let filter = "";

            if (example === "random") {
                const _arr = new Uint32Array(120);
                (window || global).crypto.getRandomValues(_arr);
                _arr.forEach((item) => value += item.toString(16));
                filter = "b32B32H16H16f2x128a32 bin Bin Hex hex float ascii";
                _exampleReplayer(value, filter, store);
                return void 0;
            }

            if (examples.indexOf(example) > -1) {
                store.dispatch(actions.startLoading());
                // Cancel inflight request (if any)
                if (_cancelTokens.length) {
                    cancelRequests();
                }
                const cancelSource = axios.CancelToken.source();
                _cancelTokens.push(cancelSource);
                fetchExample(example, cancelSource, store);
                return void 0;
            }
        }
    });
开发者ID:shuntksh,项目名称:binaryscanr,代码行数:37,代码来源:exampleHandler.ts


示例4: next

export const simpleApiCall = ({ dispatch, getState }) => next => (action) => {
    if (Array.isArray(action)) {
        // batch action: pass it on
        return next(action);
    }

    const {
        types,
        auto = false,
        getCancelSource = () => undefined,
        shouldCallApi = () => true,
        requiresAuth = true,
        onSuccess = () => ({}),
        batchSuccess = () => undefined,
        onError = error => ({ error: error.response.data }),
        payload = {},
        url,
        method,
        params,
        data,
        headers = {},
        cancellable = false,
    } = action;

    if (!types) {
        // normal action: pass it on
        return next(action);
    }

    if (!Array.isArray(types) || types.length !== 3 || !types.every(type => typeof type === 'string')) {
        throw new Error('Expected an array of three string types.');
    }

    const state = getState();

    if (requiresAuth) {
        if (!state.user.data) {
            throw new Error('Authentication is required for this action');
        }
    }

    if (!shouldCallApi(state)) {
        return;
    }

    const source = getCancelSource(state);
    if (!auto && source) {
        source.cancel('Request is no longer valid, cancelling.');
    }

    const cancelSource = cancellable ? axios.CancelToken.source() : undefined;
    const token = cancelSource ? cancelSource.token : undefined;

    const [requestType, successType, failureType] = types;

    dispatch({ ...payload, cancelSource, type: requestType });

    const csrftoken = cookie.load('csrftoken');

    return axios({
        url,
        method,
        params,
        data,
        headers: { 'X-CSRFToken': csrftoken, ...headers },
        cancelToken: token,
    }).then((response) => {
        const batched = batchSuccess(response, state);
        if (batched) {
            dispatch([
                { ...payload, type: successType, ...onSuccess(response) },
                ...batched,
            ]);
        } else {
            dispatch({ ...payload, type: successType, ...onSuccess(response) });
        }
    }).catch(error => {
        if (axios.isCancel(error)) {
            console.warn(error.message);
        } else {
            dispatch({ ...payload, type: failureType, ...onError(error) });
        }
    });
};
开发者ID:terranodo,项目名称:eventkit-cloud,代码行数:84,代码来源:middlewares.ts



注:本文中的axios.CancelToken类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript axios-mock-adapter.onGet函数代码示例发布时间:2022-05-25
下一篇:
TypeScript axios.AxiosPromise类代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap