import { writable, derived } from 'svelte/store';
export const endpoints = writable([]);
export const showBasicAuthFields = writable(false);
export const showTokenAuthFields = writable(false);
export const path = writable('');
export const methods = writable({
GET: true,
POST: false,
PUT: false,
DELETE: false
});
export const status_code = writable();
export const delay = writable();
export const rate_limit = writable('');
export const authType = writable('none');
export const username = writable('');
export const password = writable('');
export const tokenData = writable('');
export const response_file = writable(null);
export const isGraphQL = writable(false);
export const grpcRPC = writable('');
export const grpcService = writable('');
export const showPathField = derived(isGraphQL, $isGraphQL => !$isGraphQL);
export const disableHTTPMethods = derived(isGraphQL, $isGraphQL => $isGraphQL);
let endpointsLoaderRef;
export function setEndpointsLoader(ref) {
endpointsLoaderRef = ref;
}
export async function loadEndpoints() {
showLoader(true);
try {
const response = await fetch('/list');
const data = await response.json();
endpoints.set(Object.entries(data).map(([path, config]) => ({
path,
...config
})));
} catch (error) {
showNotification('Failed to load endpoints', 'error');
} finally {
showLoader(false);
}
}
export function extractMethods(methodsSelected) {
const selectedMethods = [];
Object.entries(methodsSelected).forEach(([method, selected]) => {
if (selected) {
selectedMethods.push(method);
}
});
if (selectedMethods.length === 0) {
showNotification('Please select at least one HTTP method', 'error');
return;
}
return selectedMethods.join(",");
}
export function handleAuthentication(authTypeValue, usernameValue, passwordValue, tokenDataValue) {
let authenticationValue = null;
if (authTypeValue === 'basic') {
authenticationValue = {
username: usernameValue,
password: passwordValue
};
} else if (authTypeValue === 'token') {
try {
authenticationValue = JSON.parse(tokenDataValue);
} catch (e) {
showNotification('Invalid token data format.', 'error');
return;
}
}
return JSON.stringify(authenticationValue);
}
export async function registerEndpoint(formData) {
showLoader(true);
try {
const response = await fetch('/register', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error('Failed to register endpoint');
}
await response.json();
showNotification('Endpoint registered successfully!', 'success');
resetForm();
await loadEndpoints();
} catch (error) {
showNotification(error.message, 'error');
} finally {
showLoader(false);
}
}
export async function deleteEndpoint(pathToDelete) {
if (confirm(`Are you sure you want to delete the endpoint "${pathToDelete}"?`)) {
showLoader(true);
try {
const response = await fetch(`/delete/${encodeURIComponent(pathToDelete)}`, {
method: 'DELETE'
});
if (!response.ok) {
throw new Error('Failed to delete endpoint');
}
showNotification('Endpoint deleted successfully!', 'success');
await loadEndpoints();
} catch (error) {
showNotification(error.message, 'error');
} finally {
showLoader(false);
}
}
}
export function resetForm() {
path.set('');
methods.set({ GET: true, POST: false, PUT: false, DELETE: false });
status_code.set(null);
delay.set(null);
rate_limit.set('');
authType.set('none');
username.set('');
password.set('');
tokenData.set('');
response_file.set(null);
isGraphQL.set(false);
grpcRPC.set('')
const fileInput = document.getElementById('response-file');
if (fileInput) fileInput.value = '';
}
export function showNotification(message, type) {
const notification = document.createElement('div');
notification.className = `notification ${type}`;
notification.textContent = message;
document.querySelector('.container').prepend(notification);
setTimeout(() => {
notification.remove();
}, 5000);
}
export function showLoader(show) {
if (endpointsLoaderRef) {
endpointsLoaderRef.style.display = show ? 'block' : 'none';
}
}
export function isAuthenticated(endpoint) {
return endpoint.authentication !== null && endpoint.authentication !== undefined;
}
export function handleSubmit(event, pathValue, methodsValue, status_codeValue, delayValue,
rate_limitValue, authTypeValue, usernameValue, passwordValue,
tokenDataValue, response_fileValue, isGraphQLValue, grpcService, grpcRPC) {
event.preventDefault();
const formData = new FormData();
if (isGraphQLValue) {
formData.append("path", "/api/graphql");
} else {
formData.append("path", pathValue);
}
formData.append("methods", extractMethods(methodsValue));
formData.append("status_code", status_codeValue);
formData.append("delay", delayValue);
formData.append("rate_limit", rate_limitValue);
formData.append("authentication", handleAuthentication(authTypeValue, usernameValue, passwordValue, tokenDataValue));
formData.append("isGraphQL", isGraphQLValue.toString());
formData.append("grpcService", grpcService);
formData.append("grpcRPC", grpcRPC);
if (response_fileValue) {
formData.append('file', response_fileValue);
} else {
showNotification('Please select a JSON file', 'error');
return;
}
registerEndpoint(formData);
}
export function handleFileInput(event) {
const files = event.target.files;
if (files.length > 0) {
response_file.set(files[0]);
}
}
export function updateAuthFields(newAuthType) {
showBasicAuthFields.set(newAuthType === 'basic');
showTokenAuthFields.set(newAuthType === 'token');
}
export function handleGraphQLToggle(isEnabled) {
if (isEnabled) {
methods.update(_ => ({
GET: false,
POST: true,
PUT: false,
DELETE: false
}));
path.set('/api/graphql');
} else {
methods.update(_ => ({
GET: true,
POST: false,
PUT: false,
DELETE: false
}));
path.set('');
}
}