electric helpers

This commit is contained in:
dal 2025-07-21 01:37:41 -06:00
parent 5ca40d032b
commit fdb46bba29
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
2 changed files with 20 additions and 14 deletions

View File

@ -2,21 +2,31 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { extractParamFromWhere, getElectricShapeUrl } from '.';
describe('getElectricShapeUrl', () => {
process.env.ELECTRIC_PROXY_URL = 'http://localhost:3000';
const originalElectricUrl = process.env.ELECTRIC_PROXY_URL;
let originalElectricUrl: string | undefined;
let originalSourceId: string | undefined;
beforeEach(() => {
// Clean up environment variable before each test
// Save original environment variables
originalElectricUrl = process.env.ELECTRIC_PROXY_URL;
originalSourceId = process.env.ELECTRIC_SOURCE_ID;
// Set default test values
process.env.ELECTRIC_PROXY_URL = 'http://localhost:3000';
process.env.ELECTRIC_SOURCE_ID = '';
});
afterEach(() => {
// Restore original environment variable after each test
// Restore original environment variables
if (originalElectricUrl !== undefined) {
process.env.ELECTRIC_PROXY_URL = originalElectricUrl;
} else {
process.env.ELECTRIC_PROXY_URL = '';
delete process.env.ELECTRIC_PROXY_URL;
}
if (originalSourceId !== undefined) {
process.env.ELECTRIC_SOURCE_ID = originalSourceId;
} else {
delete process.env.ELECTRIC_SOURCE_ID;
}
});

View File

@ -1,15 +1,11 @@
if (!process.env.ELECTRIC_PROXY_URL) {
throw new Error('ELECTRIC_PROXY_URL is not set');
}
if (process.env.NODE_ENV === 'production' && !process.env.ELECTRIC_SOURCE_ID) {
console.warn('ELECTRIC_SOURCE_ID is not set');
}
export const getElectricShapeUrl = (requestUrl: string) => {
const url = new URL(requestUrl);
const baseUrl = process.env.ELECTRIC_PROXY_URL || '';
const baseUrl = process.env.ELECTRIC_PROXY_URL;
if (!baseUrl) {
throw new Error('ELECTRIC_PROXY_URL is not set');
}
// Parse the base URL and replace the path with /v1/shape
const baseUrlObj = new URL(baseUrl);