import type { Form, Submission, LoginCredentials } from './types'; const API_BASE_URL = 'http://127.0.0.1:8080'; /** * Create a new form. * @param name The name of the form. * @param fields The fields of the form in JSON format. * @returns The ID of the created form. */ export async function createForm(name: string, fields: unknown): Promise { const response = await fetch(`${API_BASE_URL}/forms`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, fields }) }); if (!response.ok) { throw new Error(`Error creating form: ${response.statusText}`); } return await response.json(); } /** * Get all forms. * @returns An array of forms. */ export async function getForms(): Promise { const response = await fetch(`${API_BASE_URL}/forms`, { method: 'GET', headers: { Accept: 'application/json' } }); if (!response.ok) { throw new Error(`Error fetching forms: ${response.statusText}`); } return await response.json(); } /** * Submit a form. * @param formId The ID of the form to submit. * @param data The submission data in JSON format. * @returns The ID of the created submission. */ export async function submitForm(formId: string, data: unknown): Promise { const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); if (!response.ok) { throw new Error(`Error submitting form: ${response.statusText}`); } return await response.json(); } /** * Get all submissions for a specific form. * @param formId The ID of the form. * @returns An array of submissions for the form. */ export async function getSubmissions(formId: string): Promise { const response = await fetch(`${API_BASE_URL}/forms/${formId}/submissions`, { method: 'GET', headers: { Accept: 'application/json' } }); if (!response.ok) { throw new Error(`Error fetching submissions: ${response.statusText}`); } return await response.json(); } /** * Admin login to get a token. * @param credentials The login credentials (username and password). * @returns The generated JWT token if successful. */ export async function adminLogin(credentials: LoginCredentials): Promise { const response = await fetch(`${API_BASE_URL}/admin/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(credentials) }); if (!response.ok) { throw new Error(`Error during admin login: ${response.statusText}`); } const data = await response.json(); return data.token; // Assuming the response contains the token } /** * Create a new admin user. * @param user The login credentials for the admin user. * @returns A success message upon creation. */ export async function createAdmin(user: LoginCredentials): Promise { const response = await fetch(`${API_BASE_URL}/admin/create`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(user) }); if (!response.ok) { throw new Error(`Error creating admin user: ${response.statusText}`); } const data = await response.json(); return data.message; // Assuming the response contains a success message }