Do not understand this functional programming, do not understand

const TOKEN = "123"
const ROOT = "//www.baidu.com"

const root = createAPI => (...args) => {
  const result = createAPI(...args)
  return {...result, url: `${ROOT}${result.url}`}
}

const auth = createAPI => (...args) => {
  const result = createAPI(...args)
  return {
    ...result,
    headers: {...result.headers, authorization: `Bearer ${TOKEN}`},
  }
}

const projects = team => {
  return {
    url: `/api/0/teams/${team}/projects/`,
  }
}

root(auth(projects))("fe")
// 
{
   headers: {authorization: "Bearer 123"}
   url: "//www.baidu.com/api/0/teams/fe/projects/"
}

,

-sharp-sharp-sharp 
Apr.09,2021

  • function root accepts a function A and returns a function B . The function body of the function B is:

`<br>const root = function(createAPI){

return function (...args){
    const result = createAPI(...args);
    return {
        ...result,
        url: `${ROOT}${result.url}`;
    }
}

}

`
root accepts one function An and returns another function B. the return result of the
function B is an object, which combines the result of function A
auth.

the final parameter is passed to projects and then the returned objects are combined

Menu