longurl-js
Version:
LongURL - Programmable URL management framework with entity-driven design and production-ready infrastructure
102 lines (101 loc) • 3.74 kB
JavaScript
;
/**
* Environment and Database Configuration
*
* This module handles environment loading and Supabase configuration.
* Only imported when database operations are needed.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadEnvironment = loadEnvironment;
exports.getSupabaseConfig = getSupabaseConfig;
exports.createDatabaseConfig = createDatabaseConfig;
exports.initializeDatabaseEnvironment = initializeDatabaseEnvironment;
const dotenv = __importStar(require("dotenv"));
const path = __importStar(require("path"));
const fs_1 = require("fs");
const types_1 = require("../types");
/**
* Load environment variables from various possible locations
*/
function loadEnvironment() {
// Try different possible locations for .env file
const envPaths = [
'.env',
'.env.local',
path.resolve(process.cwd(), '../../.env'),
path.resolve(process.cwd(), '../../.env.local')
];
for (const envPath of envPaths) {
if ((0, fs_1.existsSync)(envPath)) {
console.log(`Loading environment from ${envPath}`);
dotenv.config({ path: envPath });
break;
}
}
}
/**
* Get Supabase configuration from environment variables
* Throws error if not properly configured
*/
function getSupabaseConfig() {
const supabaseUrl = process.env.SUPABASE_URL || process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_ANON_KEY || process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseKey) {
throw new Error('Supabase URL or key not configured.\n' +
'Please set SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY environment variables.');
}
return { url: supabaseUrl, key: supabaseKey };
}
/**
* Create database configuration for CLI operations
*/
function createDatabaseConfig() {
const { url, key } = getSupabaseConfig();
return {
strategy: types_1.StorageStrategy.LOOKUP_TABLE,
connection: { url, key },
lookupTable: process.env.LONGURL_TABLE_NAME || 'short_urls',
urlIdColumn: 'url_id'
};
}
/**
* Initialize environment and return database config
* Call this only when database operations are needed
*/
function initializeDatabaseEnvironment() {
loadEnvironment();
return createDatabaseConfig();
}