Giter Site home page Giter Site logo

Comments (5)

wendux avatar wendux commented on August 11, 2024
  1. 动态配置公共头是支持的,您可以在代码中任何地方动态设置公共请求头, 如下:
fly.config.headers["xx"]="xyz"

注意:设置公共请求头会对该fly实例发起的所有请求生效,如果你是想实现某次请求使用特定的请求头的话,不应该去动态改公共的请求头,而是应该设置本次请求的头,如:

fly.post(url,data,{
   headers:{xx:"xyz"}
})
  1. 如果您想添加公共body, 可以通过请求拦截器去添加公共body,请求拦截器对该fly实例发起的所有请求有效,如:
fly.interceptors.request.use((config)=>{
 var wrapBody={
  common:{...},  //公共body
  body:config.body  //本次请求body
 }
 config.body=wrapBody
})

from fly.

vsona avatar vsona commented on August 11, 2024

我们是这种 token作为公共参数放到header里面,但是又不能写死 只能动态获取,因为token随时会刷新或者踢票。如果每个接口都单独加上 又显得麻烦。

  context.params = {
    ...{
      version,
      subVersion,
      channelType,
      token: accountService.getToken()
    },
    ...context.params
  }

这样的话 目前就只能在公共的request拦截里面 注入token到headers里面了。
fly.config.headers的设置可以考虑这种:

export const promoteValue = (target = {}, funcArgs = []) => {
  if (typeof target === 'object') {
    return target
  }
  if (typeof target === 'function') {
    return target(...funcArgs)
  }
}

from fly.

vsona avatar vsona commented on August 11, 2024

@wendux 还有个疑问想了很久不明白。

            var rqiPromise;
            if (rqi.handler) {
                rqiPromise = rqi.handler(options, Promise) || options;
            }

这里 如果在handler里面调用 promise.resolve('haha')

在请求的then里面会接收到 haha

我看handler传参是Promise, Promise.resolve()应该是返回一个新的Promise对象呀?

怎么会改变外层promise的状态导致直接resolve呢?

应该是继续往下走了,options的值是haha

            rqiPromise.then((options) => {
                // Normalize the request url
                url = utils.trim(options.url);
                var baseUrl = utils.trim(options.baseURL || "");
                if (!url && isBrowser && !baseUrl) url = location.href;

from fly.

wendux avatar wendux commented on August 11, 2024
  1. 如果你要动态在header中设置token, 可以:
   fly.config.headers.token="xxx"

这样不需要在拦截器里面设置啊,不是很明白你的意思。
2. 在handler中返回 promise.resolve('haha')后,是应该直接终止本次请求,将'haha'作为本次请求返回的的数据,这块有问题,已修复,请将fly升级到>=0.3.6.

from fly.

vsona avatar vsona commented on August 11, 2024
   fly.config.headers.token="xxx"

这样设置token 每次我token改变都要主动重新同步一下fly里面保存的token。

比如我清除了本地token,那就要执行

delete fly.config.headers.token

如果又重新登录了 那还要在登录成功的代码里加上

fly.config.headers.token = getToken()

数据流不应该这样。应该是每个请求在组装的时候都主动获取一次token。
比如这样:

fly.config.headers = () => {
    token: getToken(),
    version: 1.0.2
}
  1. 原来是代码没更新。我看了好久。新版代码就对了:joy:

from fly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.