admin-panel-front/src/config/env.ts
2026-08-01 15:50:27 +03:30

34 lines
1 KiB
TypeScript

const DEFAULT_AUTH_BASE_URL = 'https://accounts-staging.gooyal.ir';
const DEFAULT_API_BASE_URL = 'http://localhost:3000';
const readEnv = (key: string, fallback?: string) => {
const value = import.meta.env[key as keyof ImportMetaEnv] as string | boolean | undefined;
if (typeof value === 'string' && value.trim()) {
return value.trim();
}
if (fallback !== undefined) {
return fallback;
}
throw new Error('Authentication client credentials are missing.');
};
const authClientId = readEnv('VITE_AUTH_CLIENT_ID');
const authClientSecret = readEnv('VITE_AUTH_CLIENT_SECRET');
export const env = {
authClientId,
authClientSecret,
authBaseUrl: readEnv('VITE_AUTH_BASE_URL', DEFAULT_AUTH_BASE_URL),
apiBaseUrl: readEnv('VITE_API_BASE_URL', DEFAULT_API_BASE_URL),
};
export const createBasicAuthHeader = (clientId = env.authClientId, clientSecret = env.authClientSecret) => {
if (!clientId || !clientSecret) {
throw new Error('Authentication client credentials are missing.');
}
return `Basic ${btoa(`${clientId}:${clientSecret}`)}`;
};