first commit
This commit is contained in:
359
src/services/api-client.ts
Normal file
359
src/services/api-client.ts
Normal file
@@ -0,0 +1,359 @@
|
||||
|
||||
import ky from 'ky';
|
||||
|
||||
|
||||
const API_KEY = process.env.PANCAKE_API_KEY;
|
||||
const API_URL = process.env.PANCAKE_API_URL;
|
||||
|
||||
const api = ky.create({ prefixUrl: API_URL, headers: { "Authorization": `Bearer ${API_KEY}` } });
|
||||
|
||||
type PaginationParams = { limit?: number, start?: number, sort_by?: string, sort_dir?: 'asc' | 'desc' }
|
||||
// Clients
|
||||
async function getAllClients(params: PaginationParams) {
|
||||
const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
|
||||
const url = `${BASE_URL}/clients?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function getOneClient(id: string) {
|
||||
const url = `${BASE_URL}/clients/show?id=${id}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function createNewClient(data) {
|
||||
const url = `${BASE_URL}/clients/new`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function updateClient(data) {
|
||||
const url = `${BASE_URL}/clients/edit`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function deleteClient(id: string) {
|
||||
const url = `${BASE_URL}/clients/delete`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Projects
|
||||
async function getOneProject(id: string) {
|
||||
const url = `${BASE_URL}/projects/show?id=${id}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function getAllProjects(params: PaginationParams) {
|
||||
const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
|
||||
const url = `${BASE_URL}/projects?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`;
|
||||
const response = await fetch(url);
|
||||
console.log("🚀 ~ getAllProjects ~ response:", response)
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
|
||||
// Projects (continued)
|
||||
async function createNewProject(data) {
|
||||
const url = `${BASE_URL}/projects/new`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function updateProject(data) {
|
||||
const url = `${BASE_URL}/projects/edit`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Tasks
|
||||
async function getTasksByProject(projectId: string) {
|
||||
const url = `${BASE_URL}/projects/tasks?id=${projectId}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function createTask(data) {
|
||||
const url = `${BASE_URL}/projects/tasks/new`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function updateTask(data) {
|
||||
const url = `${BASE_URL}/projects/tasks/update`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function deleteTask(taskId: string) {
|
||||
const url = `${BASE_URL}/projects/tasks/show`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: taskId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function logTimeOnTask(data) {
|
||||
const url = `${BASE_URL}/projects/tasks/log_time`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function completeTask(taskId: string) {
|
||||
const url = `${BASE_URL}/projects/tasks/compete`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: taskId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function reopenTask(taskId: string) {
|
||||
const url = `${BASE_URL}/projects/tasks/reopen`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: taskId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Invoices
|
||||
async function getAllInvoices(params: { client_id: string } & PaginationParams) {
|
||||
const { client_id, limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
|
||||
const queryParams = new URLSearchParams({ client_id, limit: limit.toFixed(0), start: start.toFixed(0), sort_by, sort_dir });
|
||||
const url = `${BASE_URL}/invoices?${queryParams.toString()}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function getOneInvoice(id: string) {
|
||||
const url = `${BASE_URL}/invoices/show?id=${id}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function createNewInvoice(data) {
|
||||
const url = `${BASE_URL}/invoices/new`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function updateInvoice(data) {
|
||||
const url = `${BASE_URL}/invoices/edit`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function deleteInvoice(invoiceId: string) {
|
||||
const url = `${BASE_URL}/invoices/delete`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: invoiceId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function openInvoice(invoiceId: string) {
|
||||
const url = `${BASE_URL}/invoices/open`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: invoiceId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function closeInvoice(invoiceId: string) {
|
||||
const url = `${BASE_URL}/invoices/close`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: invoiceId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function markInvoicePaid(invoiceId: string) {
|
||||
const url = `${BASE_URL}/invoices/paid`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: invoiceId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function sendInvoice(invoiceId: string) {
|
||||
const url = `${BASE_URL}/invoices/send`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: invoiceId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// Users
|
||||
async function getOneUser(id: string) {
|
||||
const url = `${BASE_URL}/users/show?id=${id}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function getAllUsers(params: PaginationParams) {
|
||||
const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
|
||||
const queryParams = new URLSearchParams({ limit: limit.toFixed(0), start: start.toFixed(0), sort_by, sort_dir });
|
||||
const url = `${BASE_URL}/users?${queryParams.toString()}`;
|
||||
const response = await fetch(url);
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function updateUser(data) {
|
||||
const url = `${BASE_URL}/users/edit`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function deleteUser(userId: string) {
|
||||
const url = `${BASE_URL}/users/delete`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ id: userId })
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export {
|
||||
// Clients
|
||||
getAllClients,
|
||||
getOneClient,
|
||||
createNewClient,
|
||||
updateClient,
|
||||
deleteClient,
|
||||
|
||||
// Projects
|
||||
getOneProject,
|
||||
getAllProjects,
|
||||
createNewProject,
|
||||
updateProject,
|
||||
|
||||
// Tasks
|
||||
getTasksByProject,
|
||||
createTask,
|
||||
updateTask,
|
||||
deleteTask,
|
||||
logTimeOnTask,
|
||||
completeTask,
|
||||
reopenTask,
|
||||
|
||||
// Invoices
|
||||
getAllInvoices,
|
||||
getOneInvoice,
|
||||
createNewInvoice,
|
||||
updateInvoice,
|
||||
deleteInvoice,
|
||||
openInvoice,
|
||||
closeInvoice,
|
||||
markInvoicePaid,
|
||||
sendInvoice,
|
||||
|
||||
// Users
|
||||
getOneUser,
|
||||
getAllUsers,
|
||||
updateUser,
|
||||
deleteUser
|
||||
};
|
||||
Reference in New Issue
Block a user