Centralize API config and audit repository coverage
This commit is contained in:
16
src/config/api.js
Normal file
16
src/config/api.js
Normal file
@@ -0,0 +1,16 @@
|
||||
const SUPABASE_URL = import.meta.env.VITE_SUPABASE_URL || 'https://yuanqfswhberkoevtmfr.supabase.co'
|
||||
const SUPABASE_ANON_KEY = import.meta.env.VITE_SUPABASE_ANON_KEY || 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ'
|
||||
|
||||
export const apiConfig = {
|
||||
supabaseUrl: SUPABASE_URL,
|
||||
restUrl: import.meta.env.VITE_SUPABASE_REST_URL || `${SUPABASE_URL}/rest/v1`,
|
||||
functionsUrl: import.meta.env.VITE_SUPABASE_FUNCTIONS_URL || `${SUPABASE_URL}/functions/v1`,
|
||||
storageUrl: import.meta.env.VITE_SUPABASE_STORAGE_URL || `${SUPABASE_URL}/storage/v1`,
|
||||
anonKey: SUPABASE_ANON_KEY,
|
||||
}
|
||||
|
||||
export const apiHeaders = {
|
||||
apikey: apiConfig.anonKey,
|
||||
Authorization: `Bearer ${apiConfig.anonKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
@@ -1,17 +1,9 @@
|
||||
const BASE_URL = 'https://yuanqfswhberkoevtmfr.supabase.co/rest/v1'
|
||||
const FUNCTIONS_URL = 'https://yuanqfswhberkoevtmfr.supabase.co/functions/v1'
|
||||
const API_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ'
|
||||
|
||||
const headers = {
|
||||
'apikey': API_KEY,
|
||||
'Authorization': `Bearer ${API_KEY}`,
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
import { apiConfig, apiHeaders } from '../config/api.js'
|
||||
|
||||
export const patientRepository = {
|
||||
// 1. Listar pacientes
|
||||
async getAll() {
|
||||
const response = await fetch(`${BASE_URL}/patients?select=*`, { headers })
|
||||
const response = await fetch(`${apiConfig.restUrl}/patients?select=*`, { headers: apiHeaders })
|
||||
if (!response.ok) throw new Error('Erro ao buscar pacientes')
|
||||
return response.json()
|
||||
},
|
||||
@@ -39,30 +31,30 @@ export const patientRepository = {
|
||||
},
|
||||
|
||||
// 2. Criar paciente (direto)
|
||||
async create(data) {
|
||||
const body = {
|
||||
full_name: data.name,
|
||||
cpf: data.cpf,
|
||||
email: data.email,
|
||||
phone_mobile: data.phone,
|
||||
birth_date: data.birthDate || null,
|
||||
created_by: data.createdBy || '00000000-0000-0000-0000-000000000000',
|
||||
}
|
||||
async create(data) {
|
||||
const body = {
|
||||
full_name: data.name,
|
||||
cpf: data.cpf,
|
||||
email: data.email,
|
||||
phone_mobile: data.phone,
|
||||
birth_date: data.birthDate || null,
|
||||
created_by: data.createdBy || '00000000-0000-0000-0000-000000000000',
|
||||
}
|
||||
|
||||
const response = await fetch(`${BASE_URL}/patients`, {
|
||||
method: 'POST',
|
||||
headers: { ...headers, 'Prefer': 'return=representation' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
const response = await fetch(`${apiConfig.restUrl}/patients`, {
|
||||
method: 'POST',
|
||||
headers: { ...apiHeaders, Prefer: 'return=representation' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
console.error('Erro da API ao criar paciente:', error)
|
||||
throw new Error(error.message || error.hint || JSON.stringify(error))
|
||||
}
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
console.error('Erro da API ao criar paciente:', error)
|
||||
throw new Error(error.message || error.hint || JSON.stringify(error))
|
||||
}
|
||||
|
||||
return response.json()
|
||||
},
|
||||
return response.json()
|
||||
},
|
||||
|
||||
// 3. Criar paciente com validação de CPF (Edge Function)
|
||||
async createWithValidation(data) {
|
||||
@@ -75,9 +67,9 @@ async create(data) {
|
||||
created_by: data.createdBy || '00000000-0000-0000-0000-000000000000',
|
||||
}
|
||||
|
||||
const response = await fetch(`${FUNCTIONS_URL}/create-patient`, {
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/create-patient`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
headers: apiHeaders,
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
@@ -99,9 +91,9 @@ async create(data) {
|
||||
birth_date: data.birthDate || null,
|
||||
}
|
||||
|
||||
const response = await fetch(`${BASE_URL}/patients?id=eq.${patientId}`, {
|
||||
const response = await fetch(`${apiConfig.restUrl}/patients?id=eq.${patientId}`, {
|
||||
method: 'PATCH',
|
||||
headers: { ...headers, 'Prefer': 'return=representation' },
|
||||
headers: { ...apiHeaders, Prefer: 'return=representation' },
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
@@ -111,12 +103,12 @@ async create(data) {
|
||||
|
||||
// 5. Deletar paciente
|
||||
async remove(patientId) {
|
||||
const response = await fetch(`${BASE_URL}/patients?id=eq.${patientId}`, {
|
||||
const response = await fetch(`${apiConfig.restUrl}/patients?id=eq.${patientId}`, {
|
||||
method: 'DELETE',
|
||||
headers,
|
||||
headers: apiHeaders,
|
||||
})
|
||||
|
||||
if (!response.ok) throw new Error('Erro ao deletar paciente')
|
||||
return true
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user