add typescript
This commit is contained in:
52
src/api.ts
Normal file
52
src/api.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user