refactor: repoList index

This commit is contained in:
Ravinou 2025-03-28 18:04:13 +01:00
commit fb68c4331b
No known key found for this signature in database
GPG key ID: EEEE670C40F6A4D7
2 changed files with 44 additions and 68 deletions

View file

@ -1,68 +0,0 @@
import fs from 'fs';
import path from 'path';
import { authOptions } from '../../../pages/api/auth/[...nextauth]';
import { getServerSession } from 'next-auth/next';
import tokenController from '../../../helpers/functions/tokenController';
export default async function handler(req, res) {
if (req.method === 'GET') {
// AUTHENTICATION
const FROM_IP = req.headers['x-forwarded-for'] || 'unknown';
const session = await getServerSession(req, res, authOptions);
const { authorization } = req.headers;
if (!session && !authorization) {
res.status(401).end();
return;
}
try {
if (!session && authorization) {
const API_KEY = authorization.split(' ')[1];
const permissions = await tokenController(API_KEY, FROM_IP);
if (!permissions) {
res.status(401).json({ message: 'Invalid API key' });
return;
}
if (!permissions.read) {
res.status(403).json({ message: 'Insufficient permissions' });
return;
}
}
} catch (error) {
res.status(500).json({ message: 'Internal Server Error' });
return;
}
// GET REPO LIST
try {
//Find the absolute path of the json directory
const jsonDirectory = path.join(process.cwd(), '/config');
//Check if the repo.json file exists and initialize it if not.
if (!fs.existsSync(jsonDirectory + '/repo.json')) {
fs.writeFileSync(jsonDirectory + '/repo.json', JSON.stringify([]));
}
//Read the file repo.json
let repoList = await fs.promises.readFile(jsonDirectory + '/repo.json', 'utf8');
repoList = JSON.parse(repoList);
res.status(200).json({ repoList });
} catch (error) {
console.log(error);
if (error.code == 'ENOENT') {
res.status(500).json({
message: 'No such file or directory',
});
} else {
res.status(500).json({
message: 'API error, contact the administrator !',
});
}
return;
}
} else {
res.status(405).json({
message: 'Method Not Allowed ',
});
return;
}
}

44
pages/api/repo/index.ts Normal file
View file

@ -0,0 +1,44 @@
import { authOptions } from '../auth/[...nextauth]';
import { getServerSession } from 'next-auth/next';
import { NextApiRequest, NextApiResponse } from 'next';
import { BorgWarehouseApiResponse } from '~/types/api/error.types';
import ApiResponse from '~/helpers/functions/apiResponse';
import { getRepoList, tokenController } from '~/helpers/functions';
import { Repository } from '~/types/domain/config.types';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<BorgWarehouseApiResponse | { repoList: Repository[] }>
) {
if (req.method !== 'GET') {
return ApiResponse.methodNotAllowed(res);
}
const session = await getServerSession(req, res, authOptions);
const { authorization } = req.headers;
if (!session && !authorization) {
return ApiResponse.unauthorized(res);
}
try {
if (!session && authorization) {
const permissions = await tokenController(req.headers);
if (!permissions) {
return ApiResponse.unauthorized(res, 'Invalid API key');
}
if (!permissions.read) {
return ApiResponse.forbidden(res, 'Insufficient permissions');
}
}
} catch (error) {
return ApiResponse.serverError(res);
}
try {
const repoList = await getRepoList();
return res.status(200).json({ repoList });
} catch (error) {
return ApiResponse.serverError(res);
}
}