Skip to content

Commit

Permalink
patch: check environment variable existence (#1427)
Browse files Browse the repository at this point in the history
* patch: check evironment variable existence

* patch: environment variable existence checks

* remove empty line

Co-authored-by: Thoralf-M <[email protected]>

* patch: remove unnecessary checks

* Update bindings/nodejs/examples/client/06-simple-block.ts

* patch: fix lint errors

---------

Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
raprocks and Thoralf-M authored Nov 21, 2023
1 parent 8fa1e3b commit 14a86f3
Show file tree
Hide file tree
Showing 56 changed files with 342 additions and 174 deletions.
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/04-get-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will get output from a known outputId.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
localPow: true,
});
try {
Expand Down
13 changes: 6 additions & 7 deletions bindings/nodejs/examples/client/05-get-address-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ require('dotenv').config({ path: '.env' });
// conditions and sum the amounts and native tokens.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL', 'MNEMONIC']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

try {
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
}
const secretManager = new SecretManager({
mnemonic: process.env.MNEMONIC,
mnemonic: process.env.MNEMONIC as string,
});

// Generate the first address
Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/06-simple-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will send a block without a payload.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL', 'EXPLORER_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
localPow: true,
});

Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/07-get-block-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will send a block and get the data and metadata for it.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

try {
Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/08-data-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will send a block with a tagged data payload.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL', 'EXPLORER_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

const options = {
Expand Down
14 changes: 6 additions & 8 deletions bindings/nodejs/examples/client/09-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,23 @@ require('dotenv').config({ path: '.env' });
// In this example we will send a transaction.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL', 'MNEMONIC', 'EXPLORER_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
localPow: true,
});

try {
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
}

// Configure your own mnemonic in ".env". Since the output amount cannot be zero, the mnemonic must contain non-zero
// balance
const secretManager = {
mnemonic: process.env.MNEMONIC,
mnemonic: process.env.MNEMONIC as string,
};

// We generate an address from our own mnemonic so that we send the funds to ourselves
Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/10-mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ require('dotenv').config({ path: '.env' });
// In this example we will listen to MQTT topics and print the block and milestone payloads.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

// Connecting to a MQTT broker using raw ip doesn't work with TCP. This is a limitation of rustls.
const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

// Array of topics to subscribe to
Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/11-build-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will build a basic output with various options.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

try {
Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/12-get-raw-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will get the raw bytes of a block.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

try {
Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/13-build-alias-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will build an alias output.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

try {
Expand Down
12 changes: 5 additions & 7 deletions bindings/nodejs/examples/client/14-build-foundry-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@ require('dotenv').config({ path: '.env' });
// In this example we will build a foundry output.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL', 'MNEMONIC']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

try {
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
}

const aliasId =
'0xff311f59790ccb85343a36fbac2f06d233734794404142b308c13f2c616935b5';

Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/15-build-nft-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will build an NFT output.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
});

try {
Expand Down
8 changes: 5 additions & 3 deletions bindings/nodejs/examples/client/16-custom-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ require('dotenv').config({ path: '.env' });
// In this example we will get output from a known nft by calling the node endpoint using a "custom plugin" call.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
localPow: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ const ADDRESS_FILE_NAME = 'offline-signing-address.json';
// In this example we will generate an address offline which will be used later to find inputs.
async function run() {
initLogger();

try {
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
for (const envVar of ['MNEMONIC']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

try {
const secretManager = new SecretManager({
mnemonic: process.env.MNEMONIC,
mnemonic: process.env.MNEMONIC as string,
});

// Generates an address offline.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ const PREPARED_TRANSACTION_FILE_NAME =
// In this example we will get inputs and prepare a transaction
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const onlineClient = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
localPow: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ const SIGNED_TRANSACTION_FILE_NAME = 'offline-signing-signed-transaction.json';
// In this example we will sign the prepared transaction.
async function run() {
initLogger();
for (const envVar of ['MNEMONIC']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}

const offlineClient = new Client({});

try {
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
}

const secretManager = {
mnemonic: process.env.MNEMONIC,
mnemonic: process.env.MNEMONIC as string,
};

// Read in prepared transaction from example 2_transaction_preparation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ const SIGNED_TRANSACTION_FILE_NAME = 'offline-signing-signed-transaction.json';
// In this example we will send the signed transaction in a block.
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
for (const envVar of ['NODE_URL', 'EXPLORER_URL']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}
const onlineClient = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
nodes: [process.env.NODE_URL as string],
localPow: true,
});

Expand Down
10 changes: 6 additions & 4 deletions bindings/nodejs/examples/evm/send-evm-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ const TX_OPTIONS = {

async function run(): Promise<void> {
const provider = new Web3(RPC_ENDPOINT);
try {
if (!process.env.MNEMONIC) {
throw new Error('.env MNEMONIC is undefined, see .env.example');
for (const envVar of ['MNEMONIC']) {
if (!(envVar in process.env)) {
throw new Error(`.env ${envVar} is undefined, see .env.example`);
}
}
try {
const mnemonicSecretManager = {
mnemonic: process.env.MNEMONIC,
mnemonic: process.env.MNEMONIC as string,
};

const secretManager = new SecretManager(mnemonicSecretManager);
Expand Down
10 changes: 6 additions & 4 deletions bindings/nodejs/examples/exchange/3-check-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ require('dotenv').config({ path: '.env' });

async function run() {
try {
if (!process.env.WALLET_DB_PATH) {
throw new Error(
'.env WALLET_DB_PATH is undefined, see .env.example',
);
for (const envVar of ['WALLET_DB_PATH']) {
if (!(envVar in process.env)) {
throw new Error(
`.env ${envVar} is undefined, see .env.example`,
);
}
}

const wallet = new Wallet({
Expand Down
Loading

0 comments on commit 14a86f3

Please sign in to comment.