diff --git a/src/components/listProjects.ts b/src/components/listProjects.ts index 278037e..ef56b39 100644 --- a/src/components/listProjects.ts +++ b/src/components/listProjects.ts @@ -1,10 +1,10 @@ import { Button, Flex } from "wretched"; import { getAllProjects } from "../services/api-client.js"; -const projects = await getAllProjects({ limit: 100 }); +const projectsResp = await getAllProjects({ limit: 100, sort_by: "date_updated", sort_dir: "desc" }); export const projectView = Flex.down({ - children: projects.map(x => new Button({ + children: projectsResp.projects.map(x => new Button({ text: x.name }) ) diff --git a/src/index.ts b/src/index.ts index f2e2ab1..0736f81 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -import "dotenv" +import 'dotenv/config' import { Screen, Box, Flow, Text, Button, interceptConsoleLog, ConsoleLog, iTerm2, Window, Flex } from 'wretched' import * as utility from "wretched/dist/components/utility"; import { projectView } from "./components/listProjects.js"; diff --git a/src/services/api-client.ts b/src/services/api-client.ts index 5179207..2fff533 100644 --- a/src/services/api-client.ts +++ b/src/services/api-client.ts @@ -4,72 +4,67 @@ 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: { "Authorization": `Bearer ${API_KEY}` } }); +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 = `${BASE_URL}/clients?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`; - const response = await fetch(url); + 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 = `${BASE_URL}/clients/show?id=${id}`; - const response = await fetch(url); + const url = `clients/show?id=${id}`; + const response = await api.get(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) + const url = `clients/new`; + const response = await api.post(url, { + json: 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) + const url = `clients/edit`; + const response = await api.post(url, { + json: 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 }) + const url = `clients/delete`; + const response = await api.post(url, { + json: { id } }); return response.json(); } - +type ListResponse = { + status: boolean, + message: string, + count: number, +} & Record> +type ProjectResponse = { + name: string +} // Projects -async function getOneProject(id: string) { - const url = `${BASE_URL}/projects/show?id=${id}`; - const response = await fetch(url); +async function getOneProject(id: string): Promise { + const url = `projects/show?id=${id}`; + const response = await api.get(url); return response.json(); } -async function getAllProjects(params: PaginationParams) { +async function getAllProjects(params: PaginationParams): Promise> { 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) + const url = `projects?limit=${limit}&start=${start}&sort_by=${sort_by}&sort_dir=${sort_dir}`; + const response = await api.get(url); return response.json(); } @@ -77,62 +72,46 @@ async function getAllProjects(params: PaginationParams) { // 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) + const url = `projects/new`; + const response = await api.post(url, { + json: 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) + const url = `projects/edit`; + const response = await api.post(url, { + json: data }); return response.json(); } // Tasks async function getTasksByProject(projectId: string) { - const url = `${BASE_URL}/projects/tasks?id=${projectId}`; - const response = await fetch(url); + const url = `projects/tasks?id=${projectId}`; + const response = await api.get(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) + const url = `projects/tasks/new`; + const response = await api.post(url, { + json: 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) + const url = `projects/tasks/update`; + const response = await api.post(url, { + json: data }); return response.json(); } async function deleteTask(taskId: string) { - const url = `${BASE_URL}/projects/tasks/show`; + const url = `projects/tasks/show`; const response = await fetch(url, { method: 'POST', headers: { @@ -144,37 +123,25 @@ async function deleteTask(taskId: string) { } 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) + const url = `projects/tasks/log_time`; + const response = await api.post(url, { + json: 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 }) + const url = `projects/tasks/compete`; + const response = await api.post(url, { + json: { 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 }) + const url = `projects/tasks/reopen`; + const response = await api.post(url, { + json: { id: taskId } }); return response.json(); } @@ -183,136 +150,100 @@ async function reopenTask(taskId: string) { 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); + const url = `invoices?${queryParams.toString()}`; + const response = await api.get(url); return response.json(); } async function getOneInvoice(id: string) { - const url = `${BASE_URL}/invoices/show?id=${id}`; - const response = await fetch(url); + const url = `invoices/show?id=${id}`; + const response = await api.get(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) + const url = `invoices/new`; + const response = await api.post(url, { + json: 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) + const url = `invoices/edit`; + const response = await api.post(url, { + json: 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 }) + const url = `invoices/delete`; + const response = await api.post(url, { + json: { 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 }) + const url = `invoices/open`; + const response = await api.post(url, { + json: { 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 }) + const url = `invoices/close`; + const response = await api.post(url, { + json: { 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 }) + const url = `invoices/paid`; + const response = await api.post(url, { + json: { 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 }) + const url = `invoices/send`; + const response = await api.post(url, { + json: { id: invoiceId } }); return response.json(); } // Users async function getOneUser(id: string) { - const url = `${BASE_URL}/users/show?id=${id}`; - const response = await fetch(url); + 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 = `${BASE_URL}/users?${queryParams.toString()}`; - const response = await fetch(url); + const url = `users?${queryParams.toString()}`; + const response = await api.get(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) + const url = `users/edit`; + const response = await api.post(url, { + json: 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 }) + const url = `users/delete`; + const response = await api.post(url, { + json: { id: userId } }); return response.json(); }