question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

My Libs need to access the environments variables inside the Apps

See original GitHub issue

Hi guys. My repository has three projects on the apps folder. Each project has a self environment file. I have a service folder on the libs folder with some services. These services need to use the environment variables of each project in your methods. Bellow, I will show the code…

Code

Env file on lp project:

API_URL = https://URL/
AUTHORIZATION = CODE
ORIGIN_CODE = CODE

Env file on modal project:

API_URL = https://ANOTHER_URL/
AUTHORIZATION = ANOTHER_CODE
ORIGIN_CODE = ANOTHER_CODE

An example of service:

export const fetchData = async (params: IData) => {
  try {
    const url = `${API_URL}path/`;
    const data = (await axios.post(url, params)).data;

    return data;
  } catch (err) {
    throw err;
  }
}

The problem is, all projects will have to use the services, so, I need to pass on services the env variables. How can I do this?

I try this:

export const fetchData = async (env: IEnvironments, params: IData) => {
  try {
    const url = `${env.API_URL}path/`;
    let data = (await axios.post(url, params)).data;
  } catch (err) {
    throw err;
  }
}

The problem is, I need to pass the env variable in all methods calls. It’s ok? The NX has a better choice?

Regards.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
danielfyhrcommented, Jul 17, 2020

Here’s one way to do it. In my opinion your libraries should not be aware of environments. In the code below the “fetchData” is created somewhere in the application lp, and shared within lp. All usages in lp will point to this one function which in turn uses the library.

It is not really a question of NX here, but more of programming practices.

// in fetch-data library
export const createDataFetcher = (env: IEnvironments) => async (params: IData) => {
    const url = `${env.API_URL}path/`;
    const data = (await axios.post(url, params)).data;
    return data;
};

// export somewhere in lp, usable in lp:
export const fetchData = createDataFetcher({ API_URL: process.env.API_URL });

// somewhere else in lp
const fetchDataAndDoSomething = async () => {
  const params: IData = { message: 'hello' };
  const data = await fetchData(params); // using the fetchData created above
  // do something with data...
  return data;
};

// export somewhere in modal, usable in modal:
// export const fetchData = createDataFetcher({ API_URL: process.env.API_URL });
0reactions
dulcetticommented, Jul 25, 2020

Forget my last message, I solved the question 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

best practices to share environment variables with lib modules
I have 3 Angular apps in an Nx project that share some common environment variables. A public site, a private portal, and an...
Read more >
Use environment variables in solutions - Power Apps
On the command bar, select New > More, and then select Environment variable. On the right pane, complete the following columns, and then...
Read more >
Environment Variables: What They Are and How To Use Them
User environment variables are those that are local to a user profile in Windows systems. These variables are used to store user-specific ...
Read more >
Manage Shared Libraries with Environment Variables
You can call directly to the toolA.exe , the required environment variables to locate both the executable and the shared libraries are automatically...
Read more >
Passing environment variables to angular library
I have 3 Angular apps in an Nx project that share some common environment variables. A public site, a private portal, and an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found