Files
workshop-js/src/api.ts

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2025-11-12 15:09:04 -03:00
import axios from 'axios'
export const API_BASE = import.meta.env.VITE_API_BASE ?? 'http://localhost:5000'
const TOKEN = import.meta.env.VITE_TOKEN ?? ''
// ajuste o path caso sua API seja /api/workshops
const WORKSHOPS_PATH = '/workshops'
// src/api.ts
export type Workshop = {
id: string;
title: string;
startAt: string; // ISO string
endAt: string; // ISO string
isOnline: boolean;
location?: string | null;
capacity?: number;
};
export type WorkshopList = Workshop[];
export type NewWorkshop = {
title: string;
startAt: string; // ISO
description: string;
endAt: string; // ISO
isOnline: boolean;
location?: string | null;
capacity?: number; // >= 1 (opcional)
};
const getHeaders = () => {
console.log(TOKEN)
return {
Authorization: `Bearer ${TOKEN}`
}
}
export async function searchWorkshops(q: string): Promise<WorkshopList> {
const { data } = await axios.get<WorkshopList>(
`${API_BASE}${WORKSHOPS_PATH}`,
{ params: { q }, headers: getHeaders() }
);
return data;
}
export async function createWorkshop(payload: NewWorkshop): Promise<Workshop> {
// POST de criação; backend retorna o Workshop criado (com id)
const { data } = await axios.post<Workshop>(
`${API_BASE}${WORKSHOPS_PATH}`,
payload, { headers: getHeaders() }
);
return data;
}