Fix getActive API to use standardized response format
- Update /api/getActive to return { success: true, data: {...} } format - Add proper error handling with standardized error responses - Update main page to handle new response format for active sources - Remove unused variables and clean up code - Add trim() to file reads to handle whitespace properly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
8c2de2c66b
commit
5dd9707f13
2 changed files with 24 additions and 35 deletions
|
@ -1,7 +1,7 @@
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
// import config from '../../../config';
|
import { createSuccessResponse, createErrorResponse, withErrorHandling } from '../../../lib/apiHelpers';
|
||||||
|
|
||||||
const FILE_DIRECTORY = path.resolve(process.env.FILE_DIRECTORY || './files')
|
const FILE_DIRECTORY = path.resolve(process.env.FILE_DIRECTORY || './files')
|
||||||
// Ensure directory exists
|
// Ensure directory exists
|
||||||
|
@ -10,7 +10,7 @@ if (!fs.existsSync(FILE_DIRECTORY)) {
|
||||||
}
|
}
|
||||||
console.log('using', FILE_DIRECTORY)
|
console.log('using', FILE_DIRECTORY)
|
||||||
|
|
||||||
export async function GET() {
|
async function getActiveHandler() {
|
||||||
try {
|
try {
|
||||||
const largePath = path.join(FILE_DIRECTORY, 'large.txt');
|
const largePath = path.join(FILE_DIRECTORY, 'large.txt');
|
||||||
const leftPath = path.join(FILE_DIRECTORY, 'left.txt');
|
const leftPath = path.join(FILE_DIRECTORY, 'left.txt');
|
||||||
|
@ -20,38 +20,27 @@ export async function GET() {
|
||||||
const bottomLeftPath = path.join(FILE_DIRECTORY, 'bottomLeft.txt');
|
const bottomLeftPath = path.join(FILE_DIRECTORY, 'bottomLeft.txt');
|
||||||
const bottomRightPath = path.join(FILE_DIRECTORY, 'bottomRight.txt');
|
const bottomRightPath = path.join(FILE_DIRECTORY, 'bottomRight.txt');
|
||||||
|
|
||||||
const tankPath = path.join(FILE_DIRECTORY, 'tank.txt');
|
const large = fs.existsSync(largePath) ? fs.readFileSync(largePath, 'utf-8').trim() : null;
|
||||||
const treePath = path.join(FILE_DIRECTORY, 'tree.txt');
|
const left = fs.existsSync(leftPath) ? fs.readFileSync(leftPath, 'utf-8').trim() : null;
|
||||||
const kittyPath = path.join(FILE_DIRECTORY, 'kitty.txt');
|
const right = fs.existsSync(rightPath) ? fs.readFileSync(rightPath, 'utf-8').trim() : null;
|
||||||
const chickenPath = path.join(FILE_DIRECTORY, 'chicken.txt');
|
const topLeft = fs.existsSync(topLeftPath) ? fs.readFileSync(topLeftPath, 'utf-8').trim() : null;
|
||||||
|
const topRight = fs.existsSync(topRightPath) ? fs.readFileSync(topRightPath, 'utf-8').trim() : null;
|
||||||
|
const bottomLeft = fs.existsSync(bottomLeftPath) ? fs.readFileSync(bottomLeftPath, 'utf-8').trim() : null;
|
||||||
|
const bottomRight = fs.existsSync(bottomRightPath) ? fs.readFileSync(bottomRightPath, 'utf-8').trim() : null;
|
||||||
|
|
||||||
|
return createSuccessResponse({
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
large,
|
||||||
const large = fs.existsSync(largePath) ? fs.readFileSync(largePath, 'utf-8') : null;
|
left,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
right,
|
||||||
const left = fs.existsSync(leftPath) ? fs.readFileSync(leftPath, 'utf-8') : null;
|
topLeft,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
topRight,
|
||||||
const right = fs.existsSync(rightPath) ? fs.readFileSync(rightPath, 'utf-8') : null;
|
bottomLeft,
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
bottomRight
|
||||||
const topLeft = fs.existsSync(topLeftPath) ? fs.readFileSync(topLeftPath, 'utf-8') : null;
|
});
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
} catch (error) {
|
||||||
const topRight = fs.existsSync(topRightPath) ? fs.readFileSync(topRightPath, 'utf-8') : null;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
const bottomLeft = fs.existsSync(bottomLeftPath) ? fs.readFileSync(bottomLeftPath, 'utf-8') : null;
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
||||||
const bottomRight = fs.existsSync(bottomRightPath) ? fs.readFileSync(bottomRightPath, 'utf-8') : null;
|
|
||||||
|
|
||||||
const tank = fs.existsSync(tankPath) ? fs.readFileSync(tankPath, 'utf-8') : null;
|
|
||||||
const tree = fs.existsSync(treePath) ? fs.readFileSync(treePath, 'utf-8') : null;
|
|
||||||
const kitty = fs.existsSync(kittyPath) ? fs.readFileSync(kittyPath, 'utf-8') : null;
|
|
||||||
const chicken = fs.existsSync(chickenPath) ? fs.readFileSync(chickenPath, 'utf-8') : null;
|
|
||||||
|
|
||||||
// For SaT
|
|
||||||
return NextResponse.json({ large, left, right, topLeft, topRight, bottomLeft, bottomRight }, {status: 201})
|
|
||||||
// return NextResponse.json({ tank, tree, kitty, chicken }, {status: 201})
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error reading active sources:', error);
|
console.error('Error reading active sources:', error);
|
||||||
return NextResponse.json({ error: 'Failed to read active sources' }, {status: 500});
|
return createErrorResponse('Failed to read active sources', 500, 'Could not read source files', error);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const GET = withErrorHandling(getActiveHandler);
|
|
@ -81,7 +81,7 @@ export default function Home() {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
setStreams(streamsData.data);
|
setStreams(streamsData.data);
|
||||||
setActiveSources(activeData);
|
setActiveSources(activeData.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching data:', error);
|
console.error('Error fetching data:', error);
|
||||||
showError('Failed to Load Data', 'Could not fetch streams. Please refresh the page.');
|
showError('Failed to Load Data', 'Could not fetch streams. Please refresh the page.');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue