Yaltopia-Tickets-App/lib/api.ts

98 lines
2.7 KiB
TypeScript

import {
createApi,
createLoggerMiddleware,
createTransformerMiddleware,
} from "@simple-api/core";
import { authMiddleware, refreshMiddleware } from "./api-middlewares";
// Trailing slash is essential for relative path resolution
export const BASE_URL = "https://api.yaltopiaticket.com/api/v1/";
/**
* Central API client using simple-api
*/
export const api = createApi({
baseUrl: BASE_URL,
middleware: [
createLoggerMiddleware(),
createTransformerMiddleware(),
refreshMiddleware,
],
services: {
news: {
middleware: [authMiddleware],
endpoints: {
getAll: { method: "GET", path: "news" },
getLatest: { method: "GET", path: "news/latest" },
},
},
auth: {
middleware: [authMiddleware],
endpoints: {
login: { method: "POST", path: "auth/login" },
register: { method: "POST", path: "auth/register-owner" },
refresh: { method: "POST", path: "auth/refresh" },
logout: { method: "POST", path: "auth/logout" },
profile: { method: "GET", path: "auth/profile" },
google: { method: "GET", path: "auth/google" },
callback: { method: "GET", path: "auth/google/callback" },
},
},
invoices: {
middleware: [authMiddleware],
endpoints: {
stats: { method: "GET", path: "invoices/stats" },
getAll: { method: "GET", path: "invoices" },
getById: { method: "GET", path: "invoices/:id" },
},
},
users: {
middleware: [authMiddleware],
endpoints: {
me: { method: "GET", path: "users/me" },
getAll: { method: "GET", path: "users" },
updateProfile: { method: "PUT", path: "users/me" },
create: { method: "POST", path: "auth/register" },
},
},
company: {
middleware: [authMiddleware],
endpoints: {
get: { method: "GET", path: "company" },
},
},
scan: {
middleware: [authMiddleware],
endpoints: {
invoice: { method: "POST", path: "scan/invoice" },
},
},
payments: {
middleware: [authMiddleware],
endpoints: {
getAll: { method: "GET", path: "payments" },
},
},
proforma: {
middleware: [authMiddleware],
endpoints: {
getAll: { method: "GET", path: "proforma" },
getById: { method: "GET", path: "proforma/:id" },
create: { method: "POST", path: "proforma" },
},
},
},
});
export interface AuthResponse {
accessToken: string;
refreshToken: string;
user: any;
}
// Explicit exports for convenience and to avoid undefined access
export const authApi = api.auth;
export const newsApi = api.news;
export const invoicesApi = api.invoices;
export const proformaApi = api.proforma;