Blocker: How to load environment variables on broswer environment using webpack.config.js

itskios09

Fredrick Kioko

Posted on April 30, 2024

Blocker: How to load environment variables on broswer environment using webpack.config.js

payment.resource.ts

`import useSWR from 'swr';
import { openmrsFetch, useConfig } from '@openmrs/esm-framework';
import { BillingConfig } from '../../config-schema';
import { Buffer } from 'buffer';
import axios from 'axios';

type PaymentMethod = {
uuid: string;
description: string;
name: string;
retired: boolean;
};

const swrOption = {
errorRetryCount: 2,
};

export const usePaymentModes = () => {
const { excludedPaymentMode } = useConfig();
const url = /ws/rest/v1/cashier/paymentMode;
const { data, isLoading, error, mutate } = useSWR<{ data: { results: Array } }>(
url,
openmrsFetch,
swrOption,
);
const allowedPaymentModes =
excludedPaymentMode?.length > 0
? data?.data?.results.filter((mode) => !excludedPaymentMode.some((excluded) => excluded.uuid === mode.uuid)) ?? []
: data?.data?.results ?? [];
return {
paymentModes: allowedPaymentModes,
isLoading,
mutate,
error,
};
};

export const generateStkAccessToken = async () => {
try {
const consumerKey = '';
const consumerSecret = '';
const authorizationUrl = 'https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials';
const auth = Buffer.from(${consumerKey}:${consumerSecret}).toString('base64');
const headers = {
'Content-Type': 'application/json',
Authorization: Basic ${auth},
};
const response = await axios(authorizationUrl, { method: 'GET', headers });
const { access_token } = await response.data;
return access_token;
} catch (error) {
console.error(error);
}
};

export const initiateStkPush = async (payload) => {
try {
const access_token = await generateStkAccessToken();
const initiateUrl = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
const response = await axios.post(initiateUrl, payload, {
headers: {
'Content-Type': 'application/json',
Authorization: Bearer ${access_token},
},
});
return response.data;
} catch (error) {
console.error(error);
throw error;
}
};`

webpack.config.js
module.exports = require('openmrs/default-webpack-config');

I want to using dotenv on the payment.resource.ts

💖 💪 🙅 🚩
itskios09
Fredrick Kioko

Posted on April 30, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024

Modern C++ for LeetCode 🧑‍💻🚀
leetcode Modern C++ for LeetCode 🧑‍💻🚀

November 29, 2024