Skip to content

Commit

Permalink
Adding example of using env vars in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
poplexity committed Jul 30, 2023
1 parent c2fa7b5 commit b78a709
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
5 changes: 3 additions & 2 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ The environment variables ares set based on the single variable `MAINNET=true`
`quasar.config.js`.
*/

// eslint-disable-next-line no-undef

const sharedEnv = {
NETWORK_PROTOCOL: 'https',
NETWORK_PORT: 443,
Expand Down Expand Up @@ -44,6 +46,5 @@ const MAINNET = {
TELOS_ESCROW_CONTRACT_ADDRESS: '0x95F5713A1422Aa3FBD3DCB8D553945C128ee3855',
};

const env = process.env.NETWORK === 'mainnet' ? MAINNET : TESTNET;

module.exports = env;
module.exports = processOrContext => processOrContext === 'mainnet' ? MAINNET : TESTNET;
5 changes: 4 additions & 1 deletion functions/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// - add the functions directory to the tsconfig so it gets compiled
// - change the CI workflow so it copies only the .js files


export function onRequest(context) {
const env = require('../env')(context);
console.dir(context);
return new Response(`Hello, ${context.env.NETWORK} world!!!!!!!!`);
const apiEndpoint = env.NETWORK_EVM_RPC;
return new Response(`Hello, ${context.env.NETWORK} world!!!!!!!!\n\nNetwork RPC is: ${apiEndpoint}`);
}
2 changes: 1 addition & 1 deletion quasar.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/* eslint-env node */

require('dotenv').config();
const env = require('./env');
const env = require('./env')(process);
const ESLintPlugin = require('eslint-webpack-plugin');
const nodePolyfillWebpackPlugin = require('node-polyfill-webpack-plugin');

Expand Down
4 changes: 4 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "wrangler-local"

[vars]
NETWORK="mainnet"

0 comments on commit b78a709

Please sign in to comment.