Files
SweetHome/app/utils/api.js
Claw AI fd3dcf4cc3
All checks were successful
Build frontend / build (push) Successful in 38s
Update mappers for flat API response + enrich with property info
- api.js: getRentProperties/getSaleProperties now fetch PropertyInformation
  for each property's propInfoId (when Properties/Get endpoint is fixed)
- Updated all 3 mapApiProperty functions to handle flat response format
  (no nested propertyInformation) - uses defaults for missing fields
- Status/type mapping checks both flat and nested fields
2026-03-26 22:59:08 +00:00

163 lines
4.5 KiB
JavaScript

const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'http://45.93.137.91/api';
async function apiFetch(endpoint, options = {}) {
const token = typeof window !== 'undefined' ? localStorage.getItem('token') : null;
const headers = {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
...options.headers,
};
const res = await fetch(`${API_BASE}${endpoint}`, {
...options,
headers,
});
if (!res.ok) {
const text = await res.text().catch(() => '');
throw new Error(`API ${res.status}: ${text || res.statusText}`);
}
const text = await res.text();
if (!text) return null;
try {
const json = JSON.parse(text);
// API wraps responses in { data, errors, isSuccess, isFailure, statusCode }
if (json && typeof json === 'object' && 'data' in json) {
return json.data;
}
return json;
} catch {
return text;
}
}
// ─── Rent Properties ───
export async function getRentProperties() {
const rentList = await apiFetch('/RentProperties/GetRentProperties');
if (!Array.isArray(rentList) || rentList.length === 0) return rentList;
// Fetch property info for each rent property's propInfoId
const enriched = await Promise.all(
rentList.map(async (item) => {
try {
const propInfo = await apiFetch(`/Properties/Get/${item.propInfoId}`);
return { ...item, propertyInformation: propInfo };
} catch {
return item;
}
})
);
return enriched;
}
export async function getRentProperty(id) {
const item = await apiFetch(`/RentProperties/GetRentProperties?id=${id}`);
if (!item) return item;
// If it's an array (all results), pick the matching one
const property = Array.isArray(item) ? item.find(p => p.id == id) || item[0] : item;
if (property?.propInfoId) {
try {
const propInfo = await apiFetch(`/Properties/Get/${property.propInfoId}`);
return { ...property, propertyInformation: propInfo };
} catch {
// ignore
}
}
return property;
}
export async function getRentPropertyLocations(params = {}) {
const qs = new URLSearchParams();
if (params.maxOffset != null) qs.set('maxOffset', params.maxOffset);
if (params.minOffset != null) qs.set('minOffset', params.minOffset);
const query = qs.toString();
return apiFetch(`/RentProperties/GetRentPropertiesLocations${query ? `?${query}` : ''}`);
}
// ─── Sale Properties ───
export async function getSaleProperties() {
const saleList = await apiFetch('/SaleProperties/GetSaleProperties');
if (!Array.isArray(saleList) || saleList.length === 0) return saleList;
const enriched = await Promise.all(
saleList.map(async (item) => {
try {
const propInfo = await apiFetch(`/Properties/Get/${item.propInfoId}`);
return { ...item, propertyInformation: propInfo };
} catch {
return item;
}
})
);
return enriched;
}
export async function getSaleProperty(id) {
const item = await apiFetch(`/SaleProperties/GetSaleProperties?id=${id}`);
const property = Array.isArray(item) ? item.find(p => p.id == id) || item[0] : item;
if (property?.propInfoId) {
try {
const propInfo = await apiFetch(`/Properties/Get/${property.propInfoId}`);
return { ...property, propertyInformation: propInfo };
} catch {
// ignore
}
}
return property;
}
// ─── Properties (generic) ───
export async function getProperty(id) {
return apiFetch(`/Properties/Get/${id}`);
}
// ─── Recommendations ───
export async function getRecommendations() {
return apiFetch('/Recommendations/GetRecommendations');
}
export async function getTopRecommendations(count = 10) {
return apiFetch(`/Recommendations/GetTopRecommendations?count=${count}`);
}
// ─── Reservations ───
export async function getReservations() {
return apiFetch('/Reservations/GetReservations');
}
export async function getReservation(id) {
return apiFetch(`/Reservations/GetReservation?id=${id}`);
}
export async function checkAvailability(propertyId, fromDate = null, toDate = null) {
const qs = new URLSearchParams();
if (fromDate) qs.set('fromDate', fromDate);
if (toDate) qs.set('toDate', toDate);
const query = qs.toString();
return apiFetch(`/Reservations/GetAvailable/${propertyId}${query ? `?${query}` : ''}`);
}
export async function bookReservation(data) {
return apiFetch('/Reservations/Book', {
method: 'POST',
body: JSON.stringify(data),
});
}
// ─── Terms ───
export async function getTerms() {
return apiFetch('/Terms/GetTerms');
}