Refactor common functions

This commit is contained in:
2024-10-01 22:32:40 +02:00
parent e7fbdd9a8d
commit 61051ebdce
6 changed files with 79 additions and 145 deletions

View File

@@ -1,131 +1,81 @@
const API_BASE_URL = window.API_BASE_URL;
export const fetchFileList = async () => {
const apiCall = async (url, options = {}) => {
try {
const response = await fetch(`${API_BASE_URL}/files`);
if (!response.ok) {
throw new Error('Failed to fetch file list');
}
return await response.json();
} catch (error) {
console.error('Error fetching file list:', error);
throw error;
}
};
export const fetchFileContent = async (filePath) => {
try {
const response = await fetch(`${API_BASE_URL}/files/${filePath}`);
if (!response.ok) {
throw new Error('Failed to fetch file content');
}
return await response.text();
} catch (error) {
console.error('Error fetching file content:', error);
throw error;
}
};
export const saveFileContent = async (filePath, content) => {
const response = await fetch(`${API_BASE_URL}/files/${filePath}`, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: content,
});
if (!response.ok) {
throw new Error('Failed to save file');
}
return await response.text();
};
export const deleteFile = async (filePath) => {
try {
const response = await fetch(`${API_BASE_URL}/files/${filePath}`, {
method: 'DELETE',
});
if (!response.ok) {
throw new Error('Failed to delete file');
}
return await response.text();
} catch (error) {
console.error('Error deleting file:', error);
throw error;
}
};
export const fetchUserSettings = async (userId) => {
try {
const response = await fetch(`${API_BASE_URL}/settings?userId=${userId}`);
if (!response.ok) {
throw new Error('Failed to fetch user settings');
}
return await response.json();
} catch (error) {
console.error('Error fetching user settings:', error);
throw error;
}
};
export const saveUserSettings = async (settings) => {
try {
const response = await fetch(`${API_BASE_URL}/settings`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(settings),
});
const response = await fetch(url, options);
if (!response.ok) {
const errorData = await response.json().catch(() => null);
throw new Error(
errorData?.message || `HTTP error! status: ${response.status}`
);
}
return await response.json();
return response;
} catch (error) {
console.error('Error saving user settings:', error);
console.error(`API call failed: ${error.message}`);
throw error;
}
};
export const fetchFileList = async () => {
const response = await apiCall(`${API_BASE_URL}/files`);
return response.json();
};
export const fetchFileContent = async (filePath) => {
const response = await apiCall(`${API_BASE_URL}/files/${filePath}`);
return response.text();
};
export const saveFileContent = async (filePath, content) => {
const response = await apiCall(`${API_BASE_URL}/files/${filePath}`, {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: content,
});
return response.text();
};
export const deleteFile = async (filePath) => {
const response = await apiCall(`${API_BASE_URL}/files/${filePath}`, {
method: 'DELETE',
});
return response.text();
};
export const fetchUserSettings = async (userId) => {
const response = await apiCall(`${API_BASE_URL}/settings?userId=${userId}`);
return response.json();
};
export const saveUserSettings = async (settings) => {
const response = await apiCall(`${API_BASE_URL}/settings`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(settings),
});
return response.json();
};
export const pullChanges = async () => {
try {
const response = await fetch(`${API_BASE_URL}/git/pull`, {
method: 'POST',
});
if (!response.ok) {
throw new Error('Failed to pull changes');
}
return await response.json();
} catch (error) {
console.error('Error pulling changes:', error);
throw error;
}
const response = await apiCall(`${API_BASE_URL}/git/pull`, {
method: 'POST',
});
return response.json();
};
export const commitAndPush = async (message) => {
try {
const response = await fetch(`${API_BASE_URL}/git/commit`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message }),
});
if (!response.ok) {
throw new Error('Failed to commit and push changes');
}
return await response.json();
} catch (error) {
console.error('Error committing and pushing changes:', error);
throw error;
}
const response = await apiCall(`${API_BASE_URL}/git/commit`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message }),
});
return response.json();
};
export const getFileUrl = (filePath) => {
@@ -133,17 +83,9 @@ export const getFileUrl = (filePath) => {
};
export const lookupFileByName = async (filename) => {
try {
const response = await fetch(
`${API_BASE_URL}/files/lookup?filename=${encodeURIComponent(filename)}`
);
if (!response.ok) {
throw new Error('File not found');
}
const data = await response.json();
return data.paths;
} catch (error) {
console.error('Error looking up file:', error);
throw error;
}
const response = await apiCall(
`${API_BASE_URL}/files/lookup?filename=${encodeURIComponent(filename)}`
);
const data = await response.json();
return data.paths;
};