Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

javascript - How to refactor this json reader?

Currently I have the following function

export const getUrl = (
  env = "pre",
  server = "host",
  resource: any = {},
  data: any = {},
) => {
  try {
    let relativeUrl = resource.level2
      ? resource.level2 === "login"
        ? parametrizeUrl(
          serversInfo[server][resource.level1].base + "/login",
          data
        )
        : parametrizeUrl(
          serversInfo[server][resource.level1][resource.level2],
          data
        )
      : resource.level1
        ? parametrizeUrl(serversInfo[server][resource.level1].base, data)
        : "";
    if (server === "old") {
      if (relativeUrl.indexOf("?") === -1) {
        relativeUrl = relativeUrl + "?";
      }
      relativeUrl = relativeUrl + getParametersEncoded(getOldParams());
    }
    return `${getBaseUrl(server, env)}/${relativeUrl}`;
  } catch (err) {
    console.log("Error obtaining url: ", err);
  }
};

TLDR: This url reads an object, and composes a url based on the values of the following json (Example extract)

const serversInfo = {
cdn: {
    dev: "http://devserver.com",
    pre: "https://preserver.com",
    prod: "https://prodsever.com",
    politics: {
      base: "assets/politics"
    }
//.......
}

It is called as following

getUrl(process.env.REACT_APP_ENV /*dev-pre-prod*/, "cdn", {
        level1: "politics"
      })

yielding: parametrizeUrl(serversInfo[server][resource.level1].base, data)

Problem I need to modify getUrl to be like

getUrl(process.env.REACT_APP_CDN_URL , {
        level1: "politics"
      })

Being process.env.REACT_APP_CDN_URL == http://devserver.com || https://preserver.com || https://prodsever.com

And I dont understand how to readapt the function to make it work that way.

question from:https://stackoverflow.com/questions/65881932/how-to-refactor-this-json-reader

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...