Aprende a conectar tu frontend con cualquier backend usando el BFF de Humalight
Los ejemplos funcionan con Strapi, APIs REST custom y más
Los ejemplos a continuación son ilustrativos. Adapta las rutas y estructuras de datos según tu backend. Si usas Strapi, reemplaza tu-content-type con el nombre de tus content types reales.
// Llamar al BFF desde tu aplicación cliente
const response = await fetch('/api/strapi/tu-content-type');
const data = await response.json();
console.log(data.data);import { httpClient } from '@/lib/http';
async function getData() {
const response = await httpClient.get('/api/tu-endpoint');
return response.data;
}const response = await httpClient.get( '/api/tu-content-type?populate=*' );
const response = await fetch('/api/strapi/tu-content-type', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
data: {
title: 'Nuevo elemento',
description: 'Descripción del elemento'
}
})
});
const data = await response.json();const response = await fetch('/api/strapi/tu-content-type/1', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
data: {
title: 'Título actualizado'
}
})
});
const data = await response.json();const response = await fetch('/api/strapi/tu-content-type/1', {
method: 'DELETE'
});
const data = await response.json();const response = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
identifier: 'user@example.com', // o username
password: 'password123'
})
});
const { jwt, user } = await response.json();
// Guardar el token para usar en peticiones futuras
localStorage.setItem('token', jwt);const response = await fetch('/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
username: 'johndoe',
email: 'john@example.com',
password: 'password123'
})
});
const { jwt, user } = await response.json();
localStorage.setItem('token', jwt);// Obtener datos del usuario actual
const token = localStorage.getItem('token');
const response = await fetch('/api/user/me', {
headers: {
'Authorization': `Bearer ${token}`
}
});
const { data: userData } = await response.json();
console.log(userData.username, userData.email);await fetch('/api/auth/forgot-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email: 'user@example.com'
})
});
// El usuario recibirá un email con el códigoawait fetch('/api/auth/reset-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
code: 'codigo_del_email',
password: 'nuevaPassword123',
passwordConfirmation: 'nuevaPassword123'
})
});/api/auth/login/api/auth/register/api/auth/verify/api/auth/forgot-password/api/auth/reset-password/api/auth/change-password/api/user/me/api/user/update/api/strapi/[tu-content-type]/api/strapi/[tu-content-type]/api/strapi/[tu-content-type]/[id]/api/strapi/[tu-content-type]/[id].env.local