All files / src/lib/api base.ts

100% Statements 55/55
95.91% Branches 47/49
100% Functions 6/6
100% Lines 54/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202      156x     103x 91x   12x 103x 103x         156x                                                           47x   47x     47x 47x 47x           156x       14x 1x   13x             156x   156x                   156x 50x     156x     112x 112x 2x 2x           2x               110x 110x   1x       1x             109x 3x               106x         112x 111x 17x     94x   112x 3x 3x     91x                                 9x 9x 9x     9x 3x 5x       9x               9x 9x   1x   8x 2x               6x    
// Base API client utilities
 
// Cached API base URL - resolved lazily for testability
let _apiBaseUrl: string | null = null;
 
function getApiBaseUrl(): string {
	if (_apiBaseUrl !== null) {
		return _apiBaseUrl;
	}
	const url = import.meta.env.VITE_API_URL || "http://localhost:7001";
	_apiBaseUrl = url;
	return url;
}
 
// For backwards compatibility - use getApiBaseUrl() for new code
export const API_BASE_URL =
	import.meta.env.VITE_API_URL || "http://localhost:7001";
 
export type RequestOptions = {
	method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
	body?: unknown;
	headers?: Record<string, string>;
	signal?: AbortSignal;
};
 
export type ApiResponse<T> = {
	success: boolean;
	data?: T;
	error?: {
		code: string;
		message: string;
		fieldErrors?: Record<string, string>;
	};
	meta?: {
		total?: number;
		page?: number;
		limit?: number;
		totalPages?: number;
		hasNext?: boolean;
		hasPrev?: boolean;
	};
};
 
export class ApiError extends Error {
	public fieldErrors?: Record<string, string>;
	constructor(
		public code: string,
		message: string,
		public status: number,
		fieldErrors?: Record<string, string>,
	) {
		super(message);
		this.name = "ApiError";
		this.fieldErrors = fieldErrors;
	}
}
 
/** User-friendly fallback message for unexpected errors */
export const GENERIC_ERROR_MESSAGE =
	"We're sorry, this could not be completed. Please write to us at hi@interioring.com and we'll get back to you.";
 
/** Extract a user-friendly error message from any error */
export function getErrorMessage(err: unknown): string {
	if (err instanceof ApiError) {
		return err.message;
	}
	return GENERIC_ERROR_MESSAGE;
}
 
export async function request<T>(
	endpoint: string,
	options: RequestOptions = {},
): Promise<ApiResponse<T>> {
	const { method = "GET", body, headers = {}, signal } = options;
 
	const config: RequestInit = {
		method,
		headers: {
			"Content-Type": "application/json",
			...headers,
		},
		credentials: "include", // Include cookies for session
		signal,
	};
 
	if (body) {
		config.body = JSON.stringify(body);
	}
 
	const response = await fetch(`${API_BASE_URL}${endpoint}`, config);
 
	// Check if response is actually JSON before parsing
	const contentType = response.headers.get("content-type");
	if (!contentType?.includes("application/json")) {
		const textResponse = await response.text();
		console.error("Non-JSON response from API:", {
			endpoint,
			status: response.status,
			contentType,
			responsePreview: textResponse.substring(0, 200),
		});
		throw new ApiError(
			"INVALID_RESPONSE",
			GENERIC_ERROR_MESSAGE,
			response.status,
		);
	}
 
	let data: ApiResponse<T>;
	try {
		data = await response.json();
	} catch (jsonError) {
		console.error("Failed to parse JSON response:", {
			endpoint,
			error: jsonError instanceof Error ? jsonError.message : String(jsonError),
		});
		throw new ApiError(
			"PARSE_ERROR",
			GENERIC_ERROR_MESSAGE,
			response.status,
		);
	}
 
	if (!response.ok || !data.success) {
		throw new ApiError(
			data.error?.code || "UNKNOWN_ERROR",
			data.error?.message || "An error occurred",
			response.status,
			data.error?.fieldErrors,
		);
	}
 
	return data;
}
 
// Helper to get full image URL from relative path
export function getImageUrl(path: string): string {
	if (!path) return "";
	if (path.startsWith("http://") || path.startsWith("https://")) {
		return path;
	}
	const imageBaseUrl =
		import.meta.env.VITE_IMAGE_URL || `${getApiBaseUrl()}/api/images`;
	// If path starts with /api/images, strip the prefix and use the image base URL
	if (path.startsWith("/api/images/")) {
		const storageKey = path.replace("/api/images/", "");
		return `${imageBaseUrl}/${storageKey}`;
	}
	// Otherwise assume it's a relative path and construct the full URL
	return `${imageBaseUrl}/${path}`;
}
 
/**
 * Upload a file to the API, or send form fields without a file when file is null.
 *
 * @param endpoint - API endpoint path (e.g., "/api/pro/123/projects/456/upload-photo")
 * @param file - File to upload, or null to send only additionalFields
 * @param additionalFields - Optional additional form fields to include
 * @returns API response with the uploaded resource
 */
export async function uploadFile<T>(
	endpoint: string,
	file: File | null,
	additionalFields?: Record<string, string>,
	options?: { signal?: AbortSignal; method?: "POST" | "PUT" | "PATCH" },
): Promise<ApiResponse<T>> {
	const formData = new FormData();
	Eif (file !== null) {
		formData.append("file", file);
	}
 
	if (additionalFields) {
		for (const [key, value] of Object.entries(additionalFields)) {
			formData.append(key, value);
		}
	}
 
	const response = await fetch(`${getApiBaseUrl()}${endpoint}`, {
		method: options?.method ?? "POST",
		credentials: "include",
		body: formData,
		signal: options?.signal,
	});
 
	let data: ApiResponse<T>;
	try {
		data = await response.json();
	} catch {
		throw new ApiError("PARSE_ERROR", GENERIC_ERROR_MESSAGE, response.status);
	}
	if (!response.ok || !data.success) {
		throw new ApiError(
			data.error?.code || "UPLOAD_ERROR",
			data.error?.message || "Failed to upload file",
			response.status,
			data.error?.fieldErrors,
		);
	}
 
	return data as ApiResponse<T>;
}