291 lines
6.7 KiB
TypeScript
291 lines
6.7 KiB
TypeScript
|
|
import ky from 'ky';
|
|
|
|
|
|
const API_KEY = process.env.PANCAKE_API_KEY;
|
|
const API_URL = process.env.PANCAKE_API_URL;
|
|
console.log("🚀 ~ API_URL:", API_URL)
|
|
|
|
const api = ky.create({ prefixUrl: API_URL, headers: { "x-api-key": `${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 = `clients?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`;
|
|
const response = api.get(url);
|
|
return response.json();
|
|
}
|
|
|
|
async function getOneClient(id: string) {
|
|
const url = `clients/show?id=${id}`;
|
|
const response = await api.get(url);
|
|
return response.json();
|
|
}
|
|
|
|
async function createNewClient(data) {
|
|
const url = `clients/new`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function updateClient(data) {
|
|
const url = `clients/edit`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function deleteClient(id: string) {
|
|
const url = `clients/delete`;
|
|
const response = await api.post(url, {
|
|
json: { id }
|
|
});
|
|
return response.json();
|
|
}
|
|
type ListResponse<A extends string, T> = {
|
|
status: boolean,
|
|
message: string,
|
|
count: number,
|
|
} & Record<A, Array<T>>
|
|
type ProjectResponse = {
|
|
name: string
|
|
}
|
|
// Projects
|
|
async function getOneProject(id: string): Promise<ProjectResponse> {
|
|
const url = `projects/show?id=${id}`;
|
|
const response = await api.get(url);
|
|
return response.json();
|
|
}
|
|
|
|
async function getAllProjects(params: PaginationParams): Promise<ListResponse<"projects", ProjectResponse>> {
|
|
const { limit = 5, start = 0, sort_by = 'id', sort_dir = 'asc' } = params;
|
|
const url = `projects?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`;
|
|
const response = await api.get(url);
|
|
|
|
return response.json();
|
|
}
|
|
|
|
|
|
// Projects (continued)
|
|
async function createNewProject(data) {
|
|
const url = `projects/new`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function updateProject(data) {
|
|
const url = `projects/edit`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
// Tasks
|
|
async function getTasksByProject(projectId: string) {
|
|
const url = `projects/tasks?id=${projectId}`;
|
|
const response = await api.get(url);
|
|
return response.json();
|
|
}
|
|
|
|
async function createTask(data) {
|
|
const url = `projects/tasks/new`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function updateTask(data) {
|
|
const url = `projects/tasks/update`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function deleteTask(taskId: string) {
|
|
const 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 = `projects/tasks/log_time`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function completeTask(taskId: string) {
|
|
const url = `projects/tasks/compete`;
|
|
const response = await api.post(url, {
|
|
json: { id: taskId }
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function reopenTask(taskId: string) {
|
|
const url = `projects/tasks/reopen`;
|
|
const response = await api.post(url, {
|
|
json: { 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 = `invoices?${queryParams.toString()}`;
|
|
const response = await api.get(url);
|
|
return response.json();
|
|
}
|
|
|
|
async function getOneInvoice(id: string) {
|
|
const url = `invoices/show?id=${id}`;
|
|
const response = await api.get(url);
|
|
return response.json();
|
|
}
|
|
|
|
async function createNewInvoice(data) {
|
|
const url = `invoices/new`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function updateInvoice(data) {
|
|
const url = `invoices/edit`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function deleteInvoice(invoiceId: string) {
|
|
const url = `invoices/delete`;
|
|
const response = await api.post(url, {
|
|
json: { id: invoiceId }
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function openInvoice(invoiceId: string) {
|
|
const url = `invoices/open`;
|
|
const response = await api.post(url, {
|
|
json: { id: invoiceId }
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function closeInvoice(invoiceId: string) {
|
|
const url = `invoices/close`;
|
|
const response = await api.post(url, {
|
|
json: { id: invoiceId }
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function markInvoicePaid(invoiceId: string) {
|
|
const url = `invoices/paid`;
|
|
const response = await api.post(url, {
|
|
json: { id: invoiceId }
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function sendInvoice(invoiceId: string) {
|
|
const url = `invoices/send`;
|
|
const response = await api.post(url, {
|
|
json: { id: invoiceId }
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
// Users
|
|
async function getOneUser(id: string) {
|
|
const url = `users/show?id=${id}`;
|
|
const response = await api.get(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 = `users?${queryParams.toString()}`;
|
|
const response = await api.get(url);
|
|
return response.json();
|
|
}
|
|
|
|
async function updateUser(data) {
|
|
const url = `users/edit`;
|
|
const response = await api.post(url, {
|
|
json: data
|
|
});
|
|
return response.json();
|
|
}
|
|
|
|
async function deleteUser(userId: string) {
|
|
const url = `users/delete`;
|
|
const response = await api.post(url, {
|
|
json: { 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
|
|
};
|